Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(history): use document lifecycle for initial blank replacement

ldm03f8ce8a9401a
diff --git a/moli-core/tests/fixtures/runtime/window_child_browsing_context_location_navigation.html b/moli-core/tests/fixtures/runtime/window_child_browsing_context_location_navigation.html
index 7b8fd710a..1e75d4bfb 100644
--- a/moli-core/tests/fixtures/runtime/window_child_browsing_context_location_navigation.html
+++ b/moli-core/tests/fixtures/runtime/window_child_browsing_context_location_navigation.html
@@ -83,8 +83,8 @@
String(window.history.length === topHistoryBefore)
);
document.body.setAttribute(
- 'data-child-history-advanced',
- String(childWindow.history.length === childHistoryBefore + 1)
+ 'data-child-history-unchanged',
+ String(childWindow.history.length === childHistoryBefore)
);
document.body.setAttribute(
'data-child-current-entry-index',
diff --git a/moli-core/tests/history_child.rs b/moli-core/tests/history_child.rs
index 6c1b6875b..db0fd964f 100644
--- a/moli-core/tests/history_child.rs
+++ b/moli-core/tests/history_child.rs
@@ -302,11 +302,13 @@ async fn assert_child_location_navigation_stays_window_local(
"{}",
page.serialize_html_async().await.unwrap()
);
+ // The iframe's src navigation has not committed when the fixture navigates
+ // its Window. Replacing the initial empty Document keeps joint history stable.
assert!(
page.serialize_html_async()
.await
.unwrap()
- .contains("data-top-history-unchanged=\"false\""),
+ .contains("data-top-history-unchanged=\"true\""),
"{}",
page.serialize_html_async().await.unwrap()
);
@@ -314,13 +316,13 @@ async fn assert_child_location_navigation_stays_window_local(
page.serialize_html_async()
.await
.unwrap()
- .contains("data-child-history-advanced=\"true\"")
+ .contains("data-child-history-unchanged=\"true\"")
);
assert!(
page.serialize_html_async()
.await
.unwrap()
- .contains("data-child-current-entry-index=\"1\""),
+ .contains("data-child-current-entry-index=\"0\""),
"{}",
page.serialize_html_async().await.unwrap()
);
diff --git a/moli-renderer-v8/src/context_bootstrap/location_navigation.rs b/moli-renderer-v8/src/context_bootstrap/location_navigation.rs
index 8f5e461be..f999070e0 100644
--- a/moli-renderer-v8/src/context_bootstrap/location_navigation.rs
+++ b/moli-renderer-v8/src/context_bootstrap/location_navigation.rs
@@ -305,7 +305,7 @@ fn navigate_location_object_with_source_element_and_child_navigate_event<'s>(
};
let replaces_initial_about_blank = child_handle.is_some_and(|handle| {
context_host_ptr_for_navigation_owner(scope, owner).is_some_and(|host_ptr| {
- unsafe { &*host_ptr }.child_browsing_context_is_on_initial_about_blank_entry(handle)
+ unsafe { &*host_ptr }.child_current_document_is_initial_empty(handle)
})
});
let effective_kind = match kind {
@@ -574,7 +574,7 @@ fn navigate_location_object_with_source_element_and_child_navigate_event<'s>(
let replaces_initial_empty_document = !is_javascript_url
&& matches!(kind, LocationNavigationKind::Assign)
&& host_ptr.is_some_and(|host_ptr| {
- unsafe { &*host_ptr }.child_browsing_context_is_on_initial_about_blank_entry(handle)
+ unsafe { &*host_ptr }.child_current_document_is_initial_empty(handle)
});
let kind = if replaces_initial_empty_document {
LocationNavigationKind::Replace
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frame_navigation/commands.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frame_navigation/commands.rs
index a64331550..edba37d62 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_frame_navigation/commands.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frame_navigation/commands.rs
@@ -22,8 +22,7 @@ impl JsContextHost {
handle,
"The navigation was canceled.".to_owned(),
);
- let replace_initial_empty_document =
- self.child_browsing_context_is_on_initial_about_blank_entry(handle);
+ let replace_initial_empty_document = self.child_current_document_is_initial_empty(handle);
if let Some(entry) = self.child_browsing_contexts.get_mut(&handle) {
if replace_initial_empty_document {
entry.replace_navigation_in_entry_seed(&url);
@@ -44,8 +43,7 @@ impl JsContextHost {
if !self.child_browsing_contexts.contains_key(&handle) {
return false;
}
- let replace_initial_empty_document =
- self.child_browsing_context_is_on_initial_about_blank_entry(handle);
+ let replace_initial_empty_document = self.child_current_document_is_initial_empty(handle);
if let Some(entry) = self.child_browsing_contexts.get_mut(&handle) {
if replace_initial_empty_document {
entry.replace_navigation_in_entry_seed(&request.url);
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/window.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/window.rs
index fb8a4ea71..47f3bc1c6 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/window.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/window.rs
@@ -846,6 +846,9 @@ impl JsContextHost {
Some(request)
}
+ // This is a property of the committed Document, independent of its URL or
+ // history entries. Same-document URL updates preserve the creation kind;
+ // a later navigation to about:blank installs a non-initial Document.
pub(crate) fn child_current_document_is_initial_empty(&self, handle: DomHandle) -> bool {
self.frame_owner_store
.current_child_document_creation_kind(handle)
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frames/lookup.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frames/lookup.rs
index 2e4ac8739..f0db7e5a2 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_frames/lookup.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frames/lookup.rs
@@ -18,20 +18,6 @@ impl JsContextHost {
.len()
}
- pub(crate) fn child_browsing_context_is_on_initial_about_blank_entry(
- &self,
- handle: DomHandle,
- ) -> bool {
- let seed_is_initial_about_blank = self
- .child_browsing_contexts
- .get(&handle)
- .is_some_and(ChildBrowsingContextEntry::navigation_seed_is_initial_about_blank_commit);
- seed_is_initial_about_blank
- && self
- .child_browsing_context_current_url(handle)
- .is_some_and(|url| moli_url::is_about_blank(&url))
- }
-
pub(crate) fn child_browsing_context_handles_in_document_order(&self) -> Vec<DomHandle> {
let mut handles = Vec::new();
self.collect_child_browsing_context_handles_in_document_order_from_document(
diff --git a/moli-renderer-v8/src/native_bridge/element/url_attributes/iframe.rs b/moli-renderer-v8/src/native_bridge/element/url_attributes/iframe.rs
index 4d06707c7..bde955ffa 100644
--- a/moli-renderer-v8/src/native_bridge/element/url_attributes/iframe.rs
+++ b/moli-renderer-v8/src/native_bridge/element/url_attributes/iframe.rs
@@ -38,8 +38,7 @@ pub(in crate::native_bridge) fn update_iframe_snapshot_navigation(
handle,
previous_seed_snapshot,
);
- let replace_current =
- runtime.child_browsing_context_is_on_initial_about_blank_entry(handle);
+ let replace_current = runtime.child_current_document_is_initial_empty(handle);
if runtime.queue_child_browsing_context_navigation_from_existing_seed(
handle,
&navigation_target,
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
index f8b1dd90a..c310cc9db 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
@@ -12791,6 +12791,156 @@ child.location.href = "about:blank#2";
assert_eq!(result, "about:blank#2|1|about:blank|");
}
+
+#[test]
+fn initial_about_blank_iframe_assign_replaces_history_after_same_document_update() {
+ for update_fragment in [false, true] {
+ let mut vm = new_storage_test_vm("https://initial-blank-history.test/");
+ vm.exec(
+ r#"
+const frame = document.createElement('iframe');
+(document.body || document.documentElement || document).appendChild(frame);
+const initialDocument = frame.contentDocument;
+"#,
+ None,
+ )
+ .expect("initial about:blank frame should be created");
+ if update_fragment {
+ vm.exec("frame.contentWindow.location.hash = 'updated';", None)
+ .expect("initial document fragment should update");
+ }
+ assert_eq!(
+ vm.eval(
+ r#"[
+ frame.contentDocument === initialDocument,
+ frame.contentDocument.readyState,
+ history.length,
+ frame.contentWindow.history.length
+].join('|')"#,
+ )
+ .expect("initial document state should evaluate"),
+ "true|complete|1|1"
+ );
+
+ vm.exec(
+ "frame.contentWindow.location.assign('about:blank?next');",
+ None,
+ )
+ .expect("initial document navigation should queue");
+ vm.drain_pending_child_frame_work_for_test();
+
+ assert_eq!(
+ vm.eval(
+ r#"[
+ frame.contentWindow.location.href,
+ frame.contentDocument.readyState,
+ history.length,
+ frame.contentWindow.history.length,
+ frame.contentDocument === initialDocument
+].join('|')"#,
+ )
+ .expect("initial document replacement should evaluate"),
+ "about:blank?next|complete|1|1|false",
+ "same-document fragment update: {update_fragment}"
+ );
+ }
+}
+
+#[test]
+fn non_initial_about_blank_iframe_assign_appends_history_after_replace() {
+ for initial_srcdoc in [false, true] {
+ let mut vm = new_storage_test_vm("https://non-initial-blank-history.test/");
+ vm.exec(
+ r#"
+const frame = document.createElement('iframe');
+const historySnapshot = () => [
+ frame.contentWindow.location.href,
+ frame.contentDocument.readyState,
+ history.length,
+ frame.contentWindow.history.length
+].join('|');
+"#,
+ None,
+ )
+ .expect("frame should be created");
+ if initial_srcdoc {
+ vm.exec("frame.srcdoc = '<!doctype html><body>loaded';", None)
+ .expect("srcdoc should be set before connecting the frame");
+ }
+ vm.exec(
+ "(document.body || document.documentElement || document).appendChild(frame);",
+ None,
+ )
+ .expect("frame should connect");
+ vm.drain_pending_child_frame_work_for_test();
+ assert_eq!(
+ vm.eval("historySnapshot()")
+ .expect("loaded frame state should evaluate"),
+ if initial_srcdoc {
+ "about:srcdoc|complete|1|1"
+ } else {
+ "about:blank|complete|1|1"
+ }
+ );
+
+ vm.exec(
+ r#"
+const documentBeforeReplace = frame.contentDocument;
+frame.contentWindow.location.replace('about:blank');
+"#,
+ None,
+ )
+ .expect("document replacement should queue");
+ vm.drain_pending_child_frame_work_for_test();
+ assert_eq!(
+ vm.eval(
+ "[historySnapshot(), frame.contentDocument === documentBeforeReplace].join('|')"
+ )
+ .expect("loaded non-initial about:blank state should evaluate"),
+ "about:blank|complete|1|1|false"
+ );
+
+ let (handle, previous_entry_id) = {
+ let host = vm._context_host.borrow();
+ let handle = host.child_browsing_context_handles_in_document_order()[0];
+ let seed = host
+ .child_browsing_context_navigation_seed_snapshot(handle)
+ .expect("loaded child should have a navigation seed")
+ .committed_navigation_entry_seed;
+ let current_entry = seed
+ .entries
+ .iter()
+ .find(|entry| entry.history_index == seed.current_index)
+ .expect("loaded child should have a current history entry");
+ (handle, current_entry.id.clone())
+ };
+ vm.exec(
+ "frame.contentWindow.location.assign('about:blank?next');",
+ None,
+ )
+ .expect("non-initial document navigation should queue");
+ vm.drain_pending_child_frame_work_for_test();
+ assert_eq!(
+ vm.eval("historySnapshot()")
+ .expect("non-initial document history should evaluate"),
+ "about:blank?next|complete|2|2",
+ "initial srcdoc: {initial_srcdoc}"
+ );
+ let seed = vm
+ ._context_host
+ .borrow()
+ .child_browsing_context_navigation_seed_snapshot(handle)
+ .expect("navigated child should have a navigation seed")
+ .committed_navigation_entry_seed;
+ assert!(
+ seed.entries.iter().any(|entry| {
+ entry.id == previous_entry_id && entry.history_index < seed.current_index
+ }),
+ "assign must retain the previous history step; initial srcdoc: {initial_srcdoc}"
+ );
+ }
+}
+
#[test]
fn contextual_fragment_scripts_run_when_inserted() {
let mut vm = new_storage_test_vm("https://contextual-fragment-scripts.test/");