Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(navigation): normalize fragments in Location.hash setter

ldm07d13c042620b
diff --git a/moli-renderer-v8/src/context_bootstrap/location_navigation.rs b/moli-renderer-v8/src/context_bootstrap/location_navigation.rs
index a9aaedb6b..58013f776 100644
--- a/moli-renderer-v8/src/context_bootstrap/location_navigation.rs
+++ b/moli-renderer-v8/src/context_bootstrap/location_navigation.rs
@@ -1,6 +1,6 @@
use super::location_runtime::{
is_same_document_fragment_navigation, location_href_slot, resolve_location_navigation_target,
- sync_location_object, urls_equal_ignoring_fragment,
+ sync_location_object,
};
use super::navigation_activation::install_navigation_transition;
use super::navigation_callbacks::cancel_active_intercepted_same_document_navigation;
@@ -48,13 +48,6 @@ pub(crate) enum LocationNavigationKind {
Reload,
}
-#[derive(Default)]
-struct LocationNavigationOptions {
- dispatch_child_navigate_event_for_all_kinds: bool,
- force_exact_same_document_navigation: bool,
- hash_setter: bool,
-}
-
pub(crate) fn resolve_cross_window_location_target(
scope: &mut v8::PinScope<'_, '_>,
raw: &str,
@@ -91,30 +84,7 @@ pub(crate) fn navigate_location_object<'s>(
raw_target: Option<String>,
) {
navigate_location_object_with_source_element_and_child_navigate_event(
- scope,
- location,
- kind,
- raw_target,
- None,
- LocationNavigationOptions::default(),
- );
-}
-
-pub(in crate::context_bootstrap) fn navigate_location_hash<'s>(
- scope: &mut v8::PinScope<'s, '_>,
- location: v8::Local<'s, v8::Object>,
- target: String,
-) {
- navigate_location_object_with_source_element_and_child_navigate_event(
- scope,
- location,
- LocationNavigationKind::Assign,
- Some(target),
- None,
- LocationNavigationOptions {
- hash_setter: true,
- ..Default::default()
- },
+ scope, location, kind, raw_target, None, false, false,
);
}
@@ -161,10 +131,8 @@ pub(crate) fn navigate_top_level_same_document_from_browser(
kind,
Some(target),
None,
- LocationNavigationOptions {
- force_exact_same_document_navigation: true,
- ..Default::default()
- },
+ false,
+ true,
);
true
}
@@ -218,10 +186,8 @@ pub(crate) fn navigate_top_level_meta_refresh(
kind,
Some(target.to_string()),
None,
- LocationNavigationOptions {
- force_exact_same_document_navigation: same_document_fragment,
- ..Default::default()
- },
+ false,
+ same_document_fragment,
);
context_host_ptr_from_global_bridge(scope)
.is_some_and(|host_ptr| unsafe { &*host_ptr }.has_pending_location_navigation())
@@ -242,7 +208,8 @@ pub(crate) fn navigate_location_object_with_source_element<'s>(
kind,
raw_target,
source_element,
- LocationNavigationOptions::default(),
+ false,
+ false,
);
}
@@ -253,15 +220,7 @@ pub(crate) fn navigate_location_object_with_child_navigate_event<'s>(
raw_target: Option<String>,
) {
navigate_location_object_with_source_element_and_child_navigate_event(
- scope,
- location,
- kind,
- raw_target,
- None,
- LocationNavigationOptions {
- dispatch_child_navigate_event_for_all_kinds: true,
- ..Default::default()
- },
+ scope, location, kind, raw_target, None, true, false,
);
}
@@ -271,13 +230,9 @@ fn navigate_location_object_with_source_element_and_child_navigate_event<'s>(
kind: LocationNavigationKind,
raw_target: Option<String>,
source_element: Option<v8::Local<'s, v8::Object>>,
- options: LocationNavigationOptions,
+ dispatch_child_navigate_event_for_all_kinds: bool,
+ force_exact_same_document_navigation: bool,
) {
- let LocationNavigationOptions {
- dispatch_child_navigate_event_for_all_kinds,
- force_exact_same_document_navigation,
- hash_setter,
- } = options;
let owner = runtime_window_owner(scope, location);
if let Some(context) = location.get_creation_context(scope)
&& let Some(host_ptr) = crate::util::context_host_ptr_from_context_slot(context)
@@ -345,7 +300,7 @@ fn navigate_location_object_with_source_element_and_child_navigate_event<'s>(
if !matches!(kind, LocationNavigationKind::Reload)
&& exact_same_href
&& source_element.is_none()
- && (raw_target_is_fragment_only || hash_setter)
+ && raw_target_is_fragment_only
&& !force_exact_same_document_navigation
{
return;
@@ -382,18 +337,9 @@ fn navigate_location_object_with_source_element_and_child_navigate_event<'s>(
return;
}
- // A hash setter can remove the fragment entirely. Full URL assignments
- // without a target fragment still take the cross-document route.
- let same_document_fragment = if hash_setter {
- current_url
- .as_ref()
- .is_some_and(|current| urls_equal_ignoring_fragment(current, &resolved))
- } else {
- is_same_document_fragment_navigation(current_url.as_ref(), &resolved)
- };
if !matches!(kind, LocationNavigationKind::Reload)
&& (!exact_same_href || force_exact_same_document_navigation)
- && same_document_fragment
+ && is_same_document_fragment_navigation(current_url.as_ref(), &resolved)
{
let opaque_origin = navigation_document_has_opaque_origin(scope, owner);
if !opaque_origin
diff --git a/moli-renderer-v8/src/context_bootstrap/location_runtime.rs b/moli-renderer-v8/src/context_bootstrap/location_runtime.rs
index d9ae4ec5e..c4a801dd6 100644
--- a/moli-renderer-v8/src/context_bootstrap/location_runtime.rs
+++ b/moli-renderer-v8/src/context_bootstrap/location_runtime.rs
@@ -1,5 +1,5 @@
use super::location_navigation::{
- LocationNavigationKind, navigate_location_hash, navigate_location_object,
+ LocationNavigationKind, navigate_location_object,
navigate_location_object_with_child_navigate_event,
};
use super::navigation_callbacks::{document_location_getter, document_location_setter};
diff --git a/moli-renderer-v8/src/context_bootstrap/location_runtime/install.rs b/moli-renderer-v8/src/context_bootstrap/location_runtime/install.rs
index fca5ac676..5db4085aa 100644
--- a/moli-renderer-v8/src/context_bootstrap/location_runtime/install.rs
+++ b/moli-renderer-v8/src/context_bootstrap/location_runtime/install.rs
@@ -378,24 +378,13 @@ fn location_writable_attribute_setter_callback<'s>(
navigate_location_object(scope, holder, LocationNavigationKind::Assign, Some(value));
}
LocationAttribute::Hash => {
- let hash = if value.is_empty() {
- String::new()
- } else if value.starts_with('#') {
- value
- } else {
- format!("#{value}")
- };
- let current = location_href_slot(scope, holder).unwrap_or_default();
- let base = current
- .find('#')
- .map(|index| current[..index].to_owned())
- .unwrap_or_else(|| current.clone());
- let target = format!("{base}{hash}");
- if target == current {
- rv.set_undefined();
- return;
- }
- navigate_location_hash(scope, holder, target);
+ navigate_modified_location_url(scope, holder, |current| {
+ let previous = current.fragment().unwrap_or_default().to_owned();
+ // Location.hash keeps an empty fragment, and compares the
+ // encoded result with an absent fragment treated as empty.
+ current.set_fragment(Some(value.strip_prefix('#').unwrap_or(&value)));
+ current.fragment().unwrap_or_default() != previous
+ });
}
LocationAttribute::Search => {
let query = value.strip_prefix('?').unwrap_or(&value).to_owned();
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/history_api.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/history_api.rs
index d293f6364..206f58dbe 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/history_api.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/history_api.rs
@@ -420,7 +420,7 @@ fn location_hash_clear_stays_in_the_current_document() {
assert_eq!(
serde_json::from_str::<serde_json::Value>(&result).unwrap(),
serde_json::json!({
- "href": "https://location-hash-clear.test/index.html",
+ "href": "https://location-hash-clear.test/index.html#",
"hash": "",
"sameDocument": true,
"events": [[true, true]]
@@ -451,6 +451,47 @@ fn location_hash_reassignment_compares_the_encoded_fragment() {
assert!(vm.take_pending_location_navigation_with_seed().is_none());
}
+#[test]
+fn location_hash_empty_and_absent_fragments_are_equal_for_assignment() {
+ for (initial, value) in [("", ""), ("", "#"), ("#", ""), ("#", "#")] {
+ let href = format!("https://location-hash-noop.test/index.html{initial}");
+ let mut vm = new_storage_test_vm(&href);
+ let value = serde_json::to_string(value).unwrap();
+ let result = vm
+ .eval(&format!(
+ r#"
+ (() => {{
+ const before = document;
+ const length = history.length;
+ let events = 0;
+ navigation.onnavigate = () => ++events;
+ location.hash = {value};
+ return JSON.stringify({{
+ href: location.href,
+ hash: location.hash,
+ sameDocument: document === before,
+ sameHistory: history.length === length,
+ events
+ }});
+ }})()
+ "#,
+ ))
+ .expect("assigning an equivalent empty hash should be a no-op");
+ assert_eq!(
+ serde_json::from_str::<serde_json::Value>(&result).unwrap(),
+ serde_json::json!({
+ "href": href,
+ "hash": "",
+ "sameDocument": true,
+ "sameHistory": true,
+ "events": 0
+ }),
+ "initial fragment {initial:?}, assigned hash {value}"
+ );
+ assert!(vm.take_pending_location_navigation_with_seed().is_none());
+ }
+}
+
#[tokio::test]
async fn clearing_a_srcdoc_hash_preserves_the_child_document_and_origin() {
let mut vm = new_storage_test_vm("https://srcdoc-hash-clear.test/");
@@ -479,10 +520,15 @@ async fn clearing_a_srcdoc_hash_preserves_the_child_document_and_origin() {
hashChild.navigation.onnavigate = event => events.push([
event.destination.sameDocument, event.hashChange
]);
+ hashChild.location.hash = '#';
+ const emptyHashNoOpHref = hashChild.location.href;
hashChild.location.hash = 'start';
hashChild.location.hash = '';
+ hashChild.location.hash = '';
+ hashChild.location.hash = '#';
return JSON.stringify({
initialHref,
+ emptyHashNoOpHref,
href: hashChild.location.href,
sameDocument: hashChild.document === before,
sameOrigin: hashChild.origin === window.origin,
@@ -496,7 +542,8 @@ async fn clearing_a_srcdoc_hash_preserves_the_child_document_and_origin() {
serde_json::from_str::<serde_json::Value>(&result).unwrap(),
serde_json::json!({
"initialHref": "about:srcdoc",
- "href": "about:srcdoc",
+ "emptyHashNoOpHref": "about:srcdoc",
+ "href": "about:srcdoc#",
"sameDocument": true,
"sameOrigin": true,
"events": [[true, true], [true, true]]