Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(document): reset child quirks mode when opening a stream

ldm07d753485f6a4
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_documents/live_parser.rs b/moli-renderer-v8/src/native_bridge/context_host/child_documents/live_parser.rs
index c644e32ed..86e96d1a8 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_documents/live_parser.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_documents/live_parser.rs
@@ -1194,6 +1194,10 @@ impl JsContextHost {
.current_child_document_task_owner(child_handle)
.expect("committed child document-open parser must have a task owner");
assert_eq!(task_owner.document_owner(), owner);
+ // open() resets the mode before any input is consumed. The new parser
+ // can select quirks mode later, after a complete doctype token or EOF.
+ self.dom_host_mut()
+ .set_html_quirks_mode_for_parser_document(document_handle, QuirksMode::NoQuirks);
let parser = DocumentParserSession::start_open_live_document(
document_url,
document_handle,
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document/document_write.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document/document_write.rs
index 7c6ab68b6..0068b6f10 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document/document_write.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document/document_write.rs
@@ -704,3 +704,98 @@ fn document_visibility_and_default_view_follow_receiver_across_realms() {
assert_eq!(result, "[]");
}
+
+#[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"
+ );
+ }
+}