fix(dom): initialize document modes and blank popup URLs

Reset child documents to no-quirks before opening a parser, while allowing
completed doctype tokens or EOF to select the subsequent document mode.

Create initial and navigated blank popup documents without a doctype, and
synchronize the committed Location before binding the replacement Document.
Cover chunked input, native no-quirks state, opener isolation, and blank
popup navigation.

Validation: cargo fmt --all; workspace all-target/all-feature Clippy with
-D warnings; 19,832 nextest tests passed, 13 skipped (32 jobs and threads).
Eight Chromium behavior probes match; 24 official WPT pages have no
regressions and three additional document.open subtests pass.
This commit is contained in:
ldm0
2026-11-22 02:01:53 +08:00
parent 361c55d856
commit f5ccc84f38
3 changed files with 133 additions and 99 deletions
@@ -1145,7 +1145,7 @@ impl PageScriptHost {
crate::dom_parser::parse_browsing_context_document_projection_from_source(
scope,
initial_url.clone(),
"<!doctype html><html><head></head><body></body></html>",
"<html><head></head><body></body></html>",
Some("text/html"),
None,
crate::parser::HtmlParser::with_scripting_enabled(
@@ -2640,6 +2640,9 @@ impl PageScriptHost {
) {
clear_lightweight_popup_window_document_event_state(scope, window);
self.clear_lightweight_popup_document_projection(popup_id);
// Blank navigations bypass the fetched-response path. Publish their
// committed Location before linking the new Document to that object.
sync_window_location_runtime_state(scope, window, document_url.as_str());
let base_url = self
.lightweight_popup_base_url(scope, popup_id)
.unwrap_or_else(|| document_url.clone());
@@ -2654,7 +2657,7 @@ impl PageScriptHost {
crate::dom_parser::parse_browsing_context_document_projection_from_source(
scope,
document_url,
"<!doctype html><html><head></head><body></body></html>",
"<html><head></head><body></body></html>",
Some("text/html"),
None,
crate::parser::HtmlParser::with_scripting_enabled(
@@ -9,6 +9,7 @@ fn popup_live_document_open_replaces_the_tree_and_close_finishes_the_parser() {
(() => {
const w = open(), d = w.document, root = d.documentElement, old = [...d.childNodes];
try {
const initial = [d.compatMode, d.doctype, old.map(n => n.nodeName)];
const observer = new MutationObserver(() => {});
observer.observe(d, {childList:true});
const same = d.open() === d;
@@ -18,7 +19,7 @@ fn popup_live_document_open_replaces_the_tree_and_close_finishes_the_parser() {
removals.length, removals[0].removedNodes.length === old.length];
d.close();
const closed = [d.readyState, d.body.localName, d.body.textContent];
return JSON.stringify({opened, closed});
return JSON.stringify({initial, opened, closed});
} finally { w.close(); }
})()
"#,
@@ -26,10 +27,40 @@ fn popup_live_document_open_replaces_the_tree_and_close_finishes_the_parser() {
.unwrap();
assert_eq!(
result,
r#"{"opened":[true,true,0,null,null,null,false,"loading","CSS1Compat",1,true],"closed":["complete","body",""]}"#
r#"{"initial":["BackCompat",null,["HTML"]],"opened":[true,true,0,null,null,null,false,"loading","CSS1Compat",1,true],"closed":["complete","body",""]}"#
);
}
#[test]
fn popup_blank_navigation_restores_quirks_mode_without_a_doctype() {
let mut vm = new_storage_test_vm("https://popup-blank-mode.test/");
assert_eq!(
vm.eval(
r#"
document.open();
document.write('<!doctype html><body>standards opener');
document.close();
globalThis.modePopup = open('about:blank', 'mode-popup');
globalThis.oldModeDocument = modePopup.document;
oldModeDocument.open();
oldModeDocument.write('<!doctype html><body>standards popup');
oldModeDocument.close();
oldModeDocument.compatMode;
"#,
)
.unwrap(),
"CSS1Compat"
);
vm.eval("open('about:blank?replacement', 'mode-popup')")
.unwrap();
assert_eq!(
vm.eval("JSON.stringify([modePopup.document !== oldModeDocument, modePopup.document.URL, modePopup.document.compatMode, modePopup.document.doctype, Array.from(modePopup.document.childNodes, n => n.nodeName), modePopup.document.body.textContent, document.compatMode])")
.unwrap(),
r#"[true,"about:blank?replacement","BackCompat",null,["HTML"],"","CSS1Compat"]"#
);
vm.eval("modePopup.close()").unwrap();
}
#[test]
fn popup_live_document_write_preserves_split_tokens_and_native_node_identity() {
let mut vm = new_storage_test_vm("https://popup-live-token.test/");
@@ -394,6 +394,101 @@ fn document_open_resets_compat_mode_and_parser_updates_it_incrementally() {
);
}
#[test]
fn child_document_open_resets_compat_mode_before_doctype_or_eof() {
let mut vm = new_parsed_test_vm(
"https://child-document-open-quirks.test/",
"<!doctype html><body>parent",
);
let result = vm
.eval(
r#"
(() => {
const f = document.body.appendChild(document.createElement('iframe'));
const d = f.contentDocument;
d.open();
d.write('<body>initial quirks document');
d.close();
const rows = [];
for (const chunks of [
[],
['<!doctype html public', ' "-//IETF//DTD HTML 3//"', '>'],
['<!doctype html', '>'],
['<!--comment-->', '<body>quirks'],
['<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">']
]) {
const modes = [d.compatMode];
d.open();
modes.push(d.compatMode);
for (const chunk of chunks) {
d.write(chunk);
modes.push(d.compatMode);
}
d.close();
modes.push(d.compatMode);
rows.push(modes.join('|'));
}
f.remove();
return rows.join('\n');
})()
"#,
)
.unwrap();
assert_eq!(
result,
[
"BackCompat|CSS1Compat|BackCompat",
"BackCompat|CSS1Compat|CSS1Compat|CSS1Compat|BackCompat|BackCompat",
"BackCompat|CSS1Compat|CSS1Compat|CSS1Compat|CSS1Compat",
"CSS1Compat|CSS1Compat|CSS1Compat|BackCompat|BackCompat",
"BackCompat|CSS1Compat|CSS1Compat|CSS1Compat",
]
.join("\n")
);
assert_eq!(vm.eval("document.compatMode").unwrap(), "CSS1Compat");
}
#[test]
fn child_document_open_initializes_native_no_quirks_mode() {
for open in ["d.open()", "d.write('<!doctype html')"] {
let mut vm = new_parsed_test_vm(
"https://child-document-open-native-mode.test/",
"<body>quirks parent",
);
vm.eval(&format!(
r#"
const f = document.body.appendChild(document.createElement('iframe'));
f.id = 'mode-frame';
const d = f.contentDocument;
{open};
const html = d.appendChild(d.createElement('html'));
html.appendChild(d.createElement('body'));
d.body.innerHTML = '<p class="UPPER">child</p>';
"#
))
.unwrap();
assert_eq!(
vm.eval("JSON.stringify([d.compatMode, d.querySelectorAll('.upper').length, document.compatMode])")
.unwrap(),
r#"["CSS1Compat",0,"BackCompat"]"#,
"{open}"
);
let host = vm._context_host.borrow();
let frame = host.dom_host().element_handle_by_id("mode-frame").unwrap();
let document = host.child_browsing_context_document_handle(frame).unwrap();
assert_eq!(
host.dom_host()
.node(document)
.unwrap()
.as_document()
.unwrap()
.quirks_mode(),
selectors::matching::QuirksMode::NoQuirks,
"{open}: native mode must not be limited-quirks"
);
}
}
// Ported from WPT opening-the-input-stream/custom-element.window.js. The
// dynamic-markup counter is a parser construction guard, not a blanket custom
// element construction guard.
@@ -645,98 +740,3 @@ fn inline_style_has_not_any_link_invalidates_on_plain_child_insertion() {
assert_eq!(result, "rgb(0, 0, 255)|rgb(128, 128, 128)|rgb(0, 0, 255)");
}
#[test]
fn child_document_open_resets_compat_mode_before_doctype_or_eof() {
let mut vm = new_parsed_test_vm(
"https://child-document-open-quirks.test/",
"<!doctype html><body>parent",
);
let result = vm
.eval(
r#"
(() => {
const f = document.body.appendChild(document.createElement('iframe'));
const d = f.contentDocument;
d.open();
d.write('<body>initial quirks document');
d.close();
const rows = [];
for (const chunks of [
[],
['<!doctype html public', ' "-//IETF//DTD HTML 3//"', '>'],
['<!doctype html', '>'],
['<!--comment-->', '<body>quirks'],
['<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">']
]) {
const modes = [d.compatMode];
d.open();
modes.push(d.compatMode);
for (const chunk of chunks) {
d.write(chunk);
modes.push(d.compatMode);
}
d.close();
modes.push(d.compatMode);
rows.push(modes.join('|'));
}
f.remove();
return rows.join('\n');
})()
"#,
)
.unwrap();
assert_eq!(
result,
[
"BackCompat|CSS1Compat|BackCompat",
"BackCompat|CSS1Compat|CSS1Compat|CSS1Compat|BackCompat|BackCompat",
"BackCompat|CSS1Compat|CSS1Compat|CSS1Compat|CSS1Compat",
"CSS1Compat|CSS1Compat|CSS1Compat|BackCompat|BackCompat",
"BackCompat|CSS1Compat|CSS1Compat|CSS1Compat",
]
.join("\n")
);
assert_eq!(vm.eval("document.compatMode").unwrap(), "CSS1Compat");
}
#[test]
fn child_document_open_initializes_native_no_quirks_mode() {
for open in ["d.open()", "d.write('<!doctype html')"] {
let mut vm = new_parsed_test_vm(
"https://child-document-open-native-mode.test/",
"<body>quirks parent",
);
vm.eval(&format!(
r#"
const f = document.body.appendChild(document.createElement('iframe'));
f.id = 'mode-frame';
const d = f.contentDocument;
{open};
const html = d.appendChild(d.createElement('html'));
html.appendChild(d.createElement('body'));
d.body.innerHTML = '<p class="UPPER">child</p>';
"#
))
.unwrap();
assert_eq!(
vm.eval("JSON.stringify([d.compatMode, d.querySelectorAll('.upper').length, document.compatMode])")
.unwrap(),
r#"["CSS1Compat",0,"BackCompat"]"#,
"{open}"
);
let host = vm._context_host.borrow();
let frame = host.dom_host().element_handle_by_id("mode-frame").unwrap();
let document = host.child_browsing_context_document_handle(frame).unwrap();
assert_eq!(
host.dom_host()
.node(document)
.unwrap()
.as_document()
.unwrap()
.quirks_mode(),
selectors::matching::QuirksMode::NoQuirks,
"{open}: native mode must not be limited-quirks"
);
}
}