From 8b32ef9227da80e021a9359e00858bdb3726f8ce Mon Sep 17 00:00:00 2001 From: ldm0 Date: Fri, 4 Sep 2026 13:25:10 +0800 Subject: [PATCH] fix(iframe): reuse initial document for about blank variants --- ..._browsing_context_navigation_identity.html | 36 +++++----- ..._context_pending_navigation_coherence.html | 6 +- .../child_documents/initial_empty.rs | 9 ++- .../child_frame_navigation/history_seed.rs | 5 ++ .../context_host/child_frames/discovery.rs | 3 + .../context_host/child_frames/registry.rs | 9 +++ .../extracted/frame_navigation_and_history.rs | 67 +++++++++++++++++++ 7 files changed, 114 insertions(+), 21 deletions(-) diff --git a/moli-core/tests/fixtures/runtime/window_child_browsing_context_navigation_identity.html b/moli-core/tests/fixtures/runtime/window_child_browsing_context_navigation_identity.html index c07f2fe0d49..a711d316a55 100644 --- a/moli-core/tests/fixtures/runtime/window_child_browsing_context_navigation_identity.html +++ b/moli-core/tests/fixtures/runtime/window_child_browsing_context_navigation_identity.html @@ -10,31 +10,31 @@ const iframe = document.createElement('iframe'); iframe.id = 'child'; iframe.src = 'about:blank'; + let child = null; + let loadCount = 0; + iframe.addEventListener('load', () => { + loadCount += 1; + if (loadCount === 1) { + iframe.src = '/compat/window-child-browsing-context-target-name-a'; + return; + } + document.body.dataset.windowSame = String(child === iframe.contentWindow); + document.body.dataset.locationUpdated = String( + iframe.contentWindow?.location?.href ?? 'null' + ); + document.body.dataset.documentUpdated = String( + iframe.contentWindow?.document?.body?.textContent?.trim() ?? 'null' + ); + iframe.contentWindow?.postMessage('kept', '*'); + }); document.body.appendChild(iframe); - - const child = iframe.contentWindow; + child = iframe.contentWindow; if (!child) { document.body.dataset.windowSame = 'no-child'; } else { - let loadCount = 0; child.addEventListener('message', (event) => { document.body.dataset.listenerPreserved = String(event.data); }); - iframe.addEventListener('load', () => { - loadCount += 1; - if (loadCount === 1) { - iframe.src = '/compat/window-child-browsing-context-target-name-a'; - return; - } - document.body.dataset.windowSame = String(child === iframe.contentWindow); - document.body.dataset.locationUpdated = String( - iframe.contentWindow?.location?.href ?? 'null' - ); - document.body.dataset.documentUpdated = String( - iframe.contentWindow?.document?.body?.textContent?.trim() ?? 'null' - ); - iframe.contentWindow?.postMessage('kept', '*'); - }); } diff --git a/moli-core/tests/fixtures/runtime/window_child_browsing_context_pending_navigation_coherence.html b/moli-core/tests/fixtures/runtime/window_child_browsing_context_pending_navigation_coherence.html index 532550ac0f3..7f609198196 100644 --- a/moli-core/tests/fixtures/runtime/window_child_browsing_context_pending_navigation_coherence.html +++ b/moli-core/tests/fixtures/runtime/window_child_browsing_context_pending_navigation_coherence.html @@ -1,9 +1,10 @@ - diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_documents/initial_empty.rs b/moli-renderer-v8/src/native_bridge/context_host/child_documents/initial_empty.rs index 0339451dce9..c2c3b4bbeed 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_documents/initial_empty.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_documents/initial_empty.rs @@ -12,6 +12,7 @@ use url::Url; /// has no response body, parser input, or navigation completion to restore. #[derive(Clone, Debug)] pub(in crate::native_bridge::context_host) struct ChildInitialEmptyDocumentInit { + document_url: Url, creator_base_url: Url, inherited_origin: String, policy_container: DocumentPolicyContainer, @@ -49,8 +50,13 @@ impl PageScriptHost { pub(in crate::native_bridge::context_host) fn capture_child_initial_empty_document_init( &self, handle: DomHandle, + document_url: Url, policy_container: DocumentPolicyContainer, ) -> ChildInitialEmptyDocumentInit { + debug_assert!( + moli_url::is_about_blank(&document_url), + "an initial-empty child Document must match about:blank" + ); let resource_authority = self .parent_document_resource_loader_for_child_context(handle) .expect("initial-empty child Document requires its exact parent authority") @@ -61,6 +67,7 @@ impl PageScriptHost { self.child_browsing_context_creator_origin(handle) }; ChildInitialEmptyDocumentInit { + document_url, creator_base_url: self.document_base_url_for_child_context(handle), inherited_origin, policy_container, @@ -87,7 +94,7 @@ impl PageScriptHost { "initial-empty Document initialization requires a vacant child owner slot" ); let loader_id = self.allocate_child_document_loader_id(); - let document_url = Url::parse("about:blank").expect("static about:blank must parse"); + let document_url = init.document_url.clone(); let document_handle = self.create_empty_live_child_html_document(document_url.clone(), Some("text/html")); self.dom_host_mut() diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frame_navigation/history_seed.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frame_navigation/history_seed.rs index f99be90b8a0..ee7fac56a79 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_frame_navigation/history_seed.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_frame_navigation/history_seed.rs @@ -34,6 +34,11 @@ impl PageScriptHost { bootstrap: &ChildBrowsingContextBootstrap, ) -> NavigationHistoryEntrySeed { let url = Self::child_browsing_context_navigation_entry_url(bootstrap); + if let Some(url) = url.as_ref().filter(|url| moli_url::is_about_blank(url)) { + let mut seed = page_child_browsing_context_single_entry_seed(None); + seed.entries[0].url = url.as_str().to_owned(); + return seed; + } page_child_browsing_context_single_entry_seed(url.as_ref()) } diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frames/discovery.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frames/discovery.rs index 175857c9df3..b658765082d 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_frames/discovery.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_frames/discovery.rs @@ -34,6 +34,9 @@ impl PageScriptHost { attribute_bootstrap: &ChildBrowsingContextBootstrap, ) -> ChildBrowsingContextBootstrap { match attribute_bootstrap { + ChildBrowsingContextBootstrap::Url(url) if moli_url::is_about_blank(url) => { + attribute_bootstrap.clone() + } ChildBrowsingContextBootstrap::Url(_) | ChildBrowsingContextBootstrap::Request(_) | ChildBrowsingContextBootstrap::Srcdoc { .. } => { diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frames/registry.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frames/registry.rs index aef56735da7..2b868cb1823 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_frames/registry.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_frames/registry.rs @@ -454,8 +454,13 @@ impl PageScriptHost { }; let initial_empty_document_init: Option = is_new .then(|| { + let document_url = + Self::child_browsing_context_bootstrap_url(&live_bootstrap) + .filter(moli_url::is_about_blank) + .expect("an initial live child bootstrap must match about:blank"); self.capture_child_initial_empty_document_init( handle, + document_url, document_policy_container.clone(), ) }); @@ -1038,6 +1043,10 @@ fn child_browsing_context_bootstrap_is_initial_about_blank( bootstrap: &ChildBrowsingContextBootstrap, ) -> bool { matches!(bootstrap, ChildBrowsingContextBootstrap::AboutBlank) + || matches!( + bootstrap, + ChildBrowsingContextBootstrap::Url(url) if moli_url::is_about_blank(url) + ) } fn child_browsing_context_bootstrap_uses_initial_empty_load( diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/frame_navigation_and_history.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/frame_navigation_and_history.rs index 89df29a7ae6..fa7cda8d8c3 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/frame_navigation_and_history.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/frame_navigation_and_history.rs @@ -4230,3 +4230,70 @@ fn reattaching_iframe_does_not_resurrect_retired_window_relations() { r#"{"before":{"grandchildLoaded":true,"childParentIsTop":true,"childTopIsTop":true,"grandchildParentIsChild":true,"grandchildTopIsTop":true},"detached":{"childParentIsNull":true,"childTopIsNull":true,"grandchildParentIsNull":true,"grandchildTopIsNull":true},"reattached":{"newChildWindow":true,"oldChildParentIsNull":true,"oldChildTopIsNull":true,"oldGrandchildParentIsNull":true,"oldGrandchildTopIsNull":true,"oldGrandchildRemoved":true}}"# ); } + +#[test] +fn explicit_about_blank_iframe_variants_reuse_initial_empty_document_synchronously() { + let mut vm = new_storage_test_vm("https://iframe-explicit-initial-blank.test/page.html"); + + let result = vm + .eval( + r##" +(() => { + const root = document.body || document.documentElement || document; + const variants = ["about:blank", "about:blank#foo", "about:blank?foo"]; + globalThis.__explicitInitialBlankFrames = []; + const observed = variants.map(src => { + const frame = document.createElement("iframe"); + const frameEvents = []; + frame.addEventListener("load", () => frameEvents.push("load")); + frame.src = src; + root.appendChild(frame); + const child = frame.contentWindow; + const windowEvents = []; + child.addEventListener("load", () => windowEvents.push("load")); + child.addEventListener("pageshow", () => windowEvents.push("pageshow")); + child.document.body.textContent = src; + globalThis.__explicitInitialBlankFrames.push({frame, child, windowEvents}); + return [ + frameEvents.join(","), + child.location.href, + child.document.URL, + child.document.body.textContent, + child.navigation.entries().map(entry => entry.url).join(","), + child.navigation.activation === null, + ].join("|"); + }); + return JSON.stringify(observed); +})() +"##, + ) + .expect("explicit initial about:blank variants should evaluate"); + + assert_eq!( + result, + r##"["load|about:blank|about:blank|about:blank|about:blank|true","load|about:blank#foo|about:blank#foo|about:blank#foo|about:blank#foo|true","load|about:blank?foo|about:blank?foo|about:blank?foo|about:blank?foo|true"]"## + ); + assert!( + !vm.has_pending_child_navigation_commit_for_test(), + "matching about:blank iframe URLs must not schedule a replacement NavigationCommit" + ); + assert!( + !vm.has_ready_child_frame_semantic_turn_for_test(ChildFrameSemanticTurnKind::HostLoad), + "synchronous matching about:blank loads must not leave HostLoad work" + ); + + vm.drain_pending_child_frame_work_for_test(); + assert_eq!( + vm.eval( + r##" +JSON.stringify(__explicitInitialBlankFrames.map(({child, windowEvents}) => [ + child.location.href, + child.document.body.textContent, + windowEvents.join(","), +])) +"##, + ) + .expect("explicit initial about:blank state should remain stable"), + r##"[["about:blank","about:blank",""],["about:blank#foo","about:blank#foo",""],["about:blank?foo","about:blank?foo",""]]"## + ); +}