fix(iframe): reuse initial document for about blank variants

This commit is contained in:
ldm0
2026-11-19 00:25:52 +08:00
parent e18450b42f
commit 8b32ef9227
7 changed files with 114 additions and 21 deletions
@@ -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', '*');
});
}
</script>
</body>
@@ -1,9 +1,10 @@
<!doctype html>
<html>
<body>
<iframe id="child" src="about:blank"></iframe>
<script>
const iframe = document.getElementById("child");
const iframe = document.createElement("iframe");
iframe.id = "child";
iframe.src = "about:blank";
let loadCount = 0;
let initialWindow = null;
let initialDocument = null;
@@ -137,6 +138,7 @@
iframe.contentDocument.body.getAttribute("data-script-ran") ?? "null"
);
});
document.body.insertBefore(iframe, document.currentScript);
</script>
</body>
</html>
@@ -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()
@@ -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())
}
@@ -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 { .. } => {
@@ -454,8 +454,13 @@ impl PageScriptHost {
};
let initial_empty_document_init: Option<ChildInitialEmptyDocumentInit> = 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(
@@ -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",""]]"##
);
}