fix(history): preserve joint length for initial child documents

This commit is contained in:
ldm0
2026-11-24 14:28:15 +08:00
parent 61fe27e110
commit dafcd59c79
2 changed files with 20 additions and 8 deletions
@@ -65,6 +65,9 @@ async fn history_traversal_retirement_reentry_preserves_reattached_frame_and_sib
const container = document.body || document.documentElement || document;
container.appendChild(frame); container.appendChild(sibling);
globalThis.oldChild = frame.contentWindow;
// Initial about:blank forces pushState to replace; use a real document
// so both Windows participate in the traversal being retired.
oldChild.document.open(); oldChild.document.write('<body>child'); oldChild.document.close();
history.replaceState(0, ''); oldChild.history.replaceState(0, '');
history.pushState(1, ''); oldChild.history.pushState(1, '');
globalThis.log = [];
@@ -137,6 +140,7 @@ async fn history_traversal_retirement_error_can_start_successor_admission() {
r#"
globalThis.frame = document.createElement('iframe');
(document.body || document.documentElement || document).appendChild(frame);
frame.contentDocument.open(); frame.contentDocument.write('<body>child'); frame.contentDocument.close();
history.replaceState(0, ''); frame.contentWindow.history.replaceState(0, '');
history.pushState(1, ''); frame.contentWindow.history.pushState(1, '');
globalThis.gates = []; globalThis.log = [];
@@ -306,6 +310,7 @@ async fn history_traversal_detach_releases_pending_admission_and_ignores_late_ca
globalThis.frame = document.createElement('iframe');
(document.body || document.documentElement || document).appendChild(frame);
globalThis.child = frame.contentWindow;
child.document.open(); child.document.write('<body>child'); child.document.close();
"#,
)
.unwrap();
@@ -4164,6 +4164,7 @@ fn explicit_about_blank_iframe_variants_reuse_initial_empty_document_synchronous
if (!document.documentElement) document.appendChild(document.createElement("html"));
const root = document.body || document.documentElement;
const variants = ["about:blank", "about:blank#foo", "about:blank?foo"];
const initialJointLength = history.length;
globalThis.__explicitInitialBlankFrames = [];
const observed = variants.map(src => {
const frame = document.createElement("iframe");
@@ -4184,9 +4185,11 @@ fn explicit_about_blank_iframe_variants_reuse_initial_empty_document_synchronous
child.document.body.textContent,
child.navigation.entries().map(entry => entry.url).join(","),
child.navigation.activation === null,
child.history.length,
history.length,
].join("|");
});
return JSON.stringify(observed);
return JSON.stringify({initialJointLength, observed});
})()
"##,
)
@@ -4194,7 +4197,7 @@ fn explicit_about_blank_iframe_variants_reuse_initial_empty_document_synchronous
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"]"##
r##"{"initialJointLength":1,"observed":["load|about:blank|about:blank|about:blank|about:blank|true|1|1","load|about:blank#foo|about:blank#foo|about:blank#foo|about:blank#foo|true|1|1","load|about:blank?foo|about:blank?foo|about:blank?foo|about:blank?foo|true|1|1"]}"##
);
assert!(
!vm.has_pending_child_navigation_commit_for_test(),
@@ -4209,14 +4212,18 @@ fn explicit_about_blank_iframe_variants_reuse_initial_empty_document_synchronous
assert_eq!(
vm.eval(
r##"
JSON.stringify(__explicitInitialBlankFrames.map(({child, windowEvents}) => [
child.location.href,
child.document.body.textContent,
windowEvents.join(","),
]))
JSON.stringify({
jointLength: history.length,
frames: __explicitInitialBlankFrames.map(({child, windowEvents}) => [
child.location.href,
child.document.body.textContent,
child.history.length,
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",""]]"##
r##"{"jointLength":1,"frames":[["about:blank","about:blank",1,""],["about:blank#foo","about:blank#foo",1,""],["about:blank?foo","about:blank?foo",1,""]]}"##
);
}