Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(custom-elements): integrate child document parser construction
ldm01a02d17bf634
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.rsindex 6fc592f8c..99acba783 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@@ -2,6 +2,7 @@ use super::super::{ChildParserClassicScriptCandidate, JsContextHost};use crate::{StylesheetBlockingReadView, StylesheetElementRead,content_security_policy::ContentSecurityPolicyScriptElementRequest,+ custom_elements,document_runtime::DomHandle,document_script_scheduler::FrameDocumentClassicScriptSchedulerWork,dom::native::{Attribute, DomMutationEffects, Node},@@ -72,13 +73,32 @@ impl<'a, 'scope, 'pin> ChildFrameLiveParserOwner<'a, 'scope, 'pin> {}}- fn sync_child_parser_side_effects(&mut self, effects: Option<&DomMutationEffects>) {- if effects.is_none_or(|effects| effects.did_change()) {- self.host- .sync_owner_style_sheet_texts_for_document_tree_scopes(self.child_document_handle);- self.host- .sync_child_browsing_context_subtree(self.scope, self.child_document_handle);+ fn apply_child_parser_mutation_effects(&mut self, effects: &DomMutationEffects) {+ if !effects.did_change() {+ return;+ }+ let host_ptr = self.host as *mut JsContextHost;+ crate::observer_runtime::queue_mutation_records(+ self.scope,+ host_ptr,+ self.host.dom_host(),+ effects,+ );+ // Parser insertions enqueue reactions without introducing a checkpoint.+ // document.write/close own their CEReactions scope; otherwise the backup+ // queue runs at the next script or custom-element construction boundary.+ for &root in effects.tree().disconnected_roots() {+ custom_elements::enqueue_disconnected_callbacks_for_subtree(self.scope, host_ptr, root);}+ custom_elements::enqueue_connected_and_form_callbacks_for_already_upgraded_subtrees(+ self.scope,+ host_ptr,+ effects.tree().connected_roots(),+ );+ self.host+ .sync_owner_style_sheet_texts_for_document_tree_scopes(self.child_document_handle);+ self.host+ .sync_child_browsing_context_subtree(self.scope, self.child_document_handle);}}@@ -134,7 +154,7 @@ impl StylesheetBlockingReadView for ChildFrameLiveParserOwner<'_, '_, '_> {impl ParserMutationEffectConsumer for ChildFrameLiveParserOwner<'_, '_, '_> {fn consume_parser_mutation_effects(&mut self, effects: DomMutationEffects) {- self.sync_child_parser_side_effects(Some(&effects));+ self.apply_child_parser_mutation_effects(&effects);}}@@ -258,7 +278,7 @@ impl ParserDomReadConsumer for ChildFrameLiveParserOwner<'_, '_, '_> {impl ParserDomMutationConsumer for ChildFrameLiveParserOwner<'_, '_, '_> {fn apply_parser_dom_mutation(&mut self, mutation: ParserDomMutation) {let effects = mutation.apply_to_dom_host(self.host.dom_host_mut());- self.sync_child_parser_side_effects(Some(&effects));+ self.apply_child_parser_mutation_effects(&effects);}fn create_parser_element_without_attributes(@@ -416,9 +436,42 @@ impl ParserDomMutationConsumer for ChildFrameLiveParserOwner<'_, '_, '_> {impl ParserElementCreationConsumer for ChildFrameLiveParserOwner<'_, '_, '_> {fn create_parser_element(&mut self,- _request: ParserElementCreationRequest<'_>,+ request: ParserElementCreationRequest<'_>,) -> Option<DomHandle> {- None+ let document_has_body = self+ .document_body_handle_for_document(request.document_handle)+ .is_some();+ let child_handle = self+ .host+ .child_browsing_context_host_for_document_handle(request.document_handle)?;+ // A child with a registered definition already has a realm. Keep the+ // initial parser lazy, and create wrappers in that realm once it exists.+ let context = self+ .host+ .child_browsing_context_relevant_context(self.scope, child_handle)?;+ let scope = &mut v8::ContextScope::new(self.scope, context);+ let host_ptr = self.host as *mut JsContextHost;+ custom_elements::create_and_construct_parser_custom_element_direct_for_document(+ scope,+ host_ptr,+ request.document_handle,+ document_has_body,+ request.local_name,+ request.namespace,+ request.prefix,+ request.attributes,+ request.intended_parent,+ |document_handle, local_name, namespace, prefix| {+ self.host+ .dom_host_mut()+ .create_parser_element_without_attributes_for_document(+ document_handle,+ local_name,+ namespace,+ prefix,+ )+ },+ )}}diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rsindex 170dd66af..589cc348b 100644--- a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs@@ -332,69 +332,74 @@ fn child_document_write_or_writeln_callback<'s>(mut rv: v8::ReturnValue<'_, v8::Value>,append_newline: bool,) {- let Some(handle) = child_document_handle_from_callback_data(scope, args.data()) else {+ let Some(host_ptr) = context_host_ptr_from_global_bridge(scope) else {rv.set_undefined();return;};- let document = args.this();- if child_document_has_throw_on_dynamic_markup_insertion_counter(scope, document) {- throw_dynamic_markup_invalid_state(scope);- return;- }- let mut chunk = String::new();- for index in 0..args.length() {- let Some(value) = args.get(index).to_string(scope) else {+ crate::custom_elements::with_custom_element_reaction_scope(scope, host_ptr, |scope| {+ let Some(handle) = child_document_handle_from_callback_data(scope, args.data()) else {+ rv.set_undefined();return;};- chunk.push_str(&value.to_rust_string_lossy(scope));- }- if append_newline {- chunk.push('\n');- }- let Some(host_ptr) = context_host_ptr_from_global_bridge(scope) else {- rv.set_undefined();- return;- };- let host = unsafe { &mut *host_ptr };- if host.child_document_stream_is_blocked_by_navigation(handle) {- rv.set_undefined();- return;- }- let script_context = if host.child_document_parser_is_active(handle) {- match unsafe { &mut *host_ptr }.ensure_prebootstrapped_child_default_context(scope, handle)- {- Ok(context) => context,- Err(error) => {- tracing::warn!(- %error,- child_handle = handle.index(),- "failed to enter the child LocalWindow context for document.write"- );- rv.set_undefined();+ let document = args.this();+ if child_document_has_throw_on_dynamic_markup_insertion_counter(scope, document) {+ throw_dynamic_markup_invalid_state(scope);+ return;+ }+ let mut chunk = String::new();+ for index in 0..args.length() {+ let Some(value) = args.get(index).to_string(scope) else {return;- }+ };+ chunk.push_str(&value.to_rust_string_lossy(scope));+ }+ if append_newline {+ chunk.push('\n');}- } else {- let Some(context) = begin_child_document_stream_replacement(scope, handle, document) else {+ let host = unsafe { &mut *host_ptr };+ if host.child_document_stream_is_blocked_by_navigation(handle) {rv.set_undefined();return;+ }+ let script_context = if host.child_document_parser_is_active(handle) {+ match unsafe { &mut *host_ptr }+ .ensure_prebootstrapped_child_default_context(scope, handle)+ {+ Ok(context) => context,+ Err(error) => {+ tracing::warn!(+ %error,+ child_handle = handle.index(),+ "failed to enter the child LocalWindow context for document.write"+ );+ rv.set_undefined();+ return;+ }+ }+ } else {+ let Some(context) = begin_child_document_stream_replacement(scope, handle, document)+ else {+ rv.set_undefined();+ return;+ };+ context};- context- };- let Some(document_handle) = child_document_native_handle_for_runtime(scope, host_ptr, document)- else {+ let Some(document_handle) =+ child_document_native_handle_for_runtime(scope, host_ptr, document)+ else {+ rv.set_undefined();+ return;+ };+ let _ = unsafe { &mut *host_ptr }.pump_child_document_write_parser(+ scope,+ script_context,+ handle,+ document_handle,+ Some(chunk),+ false,+ );rv.set_undefined();- return;- };- let _ = unsafe { &mut *host_ptr }.pump_child_document_write_parser(- scope,- script_context,- handle,- document_handle,- Some(chunk),- false,- );- rv.set_undefined();+ });}fn child_document_close_callback<'s>(@@ -402,60 +407,63 @@ fn child_document_close_callback<'s>(args: v8::FunctionCallbackArguments<'s>,mut rv: v8::ReturnValue<'_, v8::Value>,) {- let Some(handle) = child_document_handle_from_callback_data(scope, args.data()) else {- rv.set_undefined();- return;- };- let document = args.this();- if child_document_has_throw_on_dynamic_markup_insertion_counter(scope, document) {- throw_dynamic_markup_invalid_state(scope);- return;- }let Some(host_ptr) = context_host_ptr_from_global_bridge(scope) else {rv.set_undefined();return;};- let Some(document_handle) = child_document_native_handle_for_runtime(scope, host_ptr, document)- else {- rv.set_undefined();- return;- };- let host = unsafe { &mut *host_ptr };- if host.child_document_stream_is_blocked_by_navigation(handle) {- rv.set_undefined();- return;- }- if host.child_document_parser_is_active(handle)- && host- .child_current_script_handle_for_document(document_handle)- .is_some()- {- rv.set_undefined();- return;- }- let script_context = match unsafe { &mut *host_ptr }- .ensure_prebootstrapped_child_default_context(scope, handle)- {- Ok(context) => context,- Err(error) => {- tracing::warn!(- %error,- child_handle = handle.index(),- "failed to enter the child LocalWindow context for document.close"- );+ crate::custom_elements::with_custom_element_reaction_scope(scope, host_ptr, |scope| {+ let Some(handle) = child_document_handle_from_callback_data(scope, args.data()) else {rv.set_undefined();return;+ };+ let document = args.this();+ if child_document_has_throw_on_dynamic_markup_insertion_counter(scope, document) {+ throw_dynamic_markup_invalid_state(scope);+ return;}- };- let _ = unsafe { &mut *host_ptr }.pump_child_document_write_parser(- scope,- script_context,- handle,- document_handle,- None,- true,- );- rv.set_undefined();+ let Some(document_handle) =+ child_document_native_handle_for_runtime(scope, host_ptr, document)+ else {+ rv.set_undefined();+ return;+ };+ let host = unsafe { &mut *host_ptr };+ if host.child_document_stream_is_blocked_by_navigation(handle) {+ rv.set_undefined();+ return;+ }+ if host.child_document_parser_is_active(handle)+ && host+ .child_current_script_handle_for_document(document_handle)+ .is_some()+ {+ rv.set_undefined();+ return;+ }+ let script_context = match unsafe { &mut *host_ptr }+ .ensure_prebootstrapped_child_default_context(scope, handle)+ {+ Ok(context) => context,+ Err(error) => {+ tracing::warn!(+ %error,+ child_handle = handle.index(),+ "failed to enter the child LocalWindow context for document.close"+ );+ rv.set_undefined();+ return;+ }+ };+ let _ = unsafe { &mut *host_ptr }.pump_child_document_write_parser(+ scope,+ script_context,+ handle,+ document_handle,+ None,+ true,+ );+ rv.set_undefined();+ });}fn redirect_child_document_open_to_window_open<'s>(diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements.rsindex e5020f076..8a6ca1a4b 100644--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements.rs+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements.rs@@ -1,6 +1,8 @@use super::*;use crate::custom_elements::CustomElementRegistryKey;+mod parser;+#[tokio::test]async fn popup_classic_script_custom_element_microtasks_wait_for_outer_javascript() {assert_popup_custom_element_microtask_order(false).await;diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements/parser.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements/parser.rsnew file mode 100644index 000000000..dc403be2f--- /dev/null+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements/parser.rs@@ -0,0 +1,226 @@+use super::*;++#[test]+fn child_parser_write_constructs_elements_and_runs_reactions_synchronously() {+ let mut vm = new_storage_test_vm("https://child-parser-custom-elements.test/");+ let result = vm.eval(r#"+ (() => {+ const results = [];+ for (const method of ['write', 'writeln']) {+ for (const explicitOpen of [false, true]) {+ const frame = document.createElement('iframe');+ (document.body || document.documentElement || document).appendChild(frame);+ const w = frame.contentWindow, d = w.document;+ if (explicitOpen) d.open();+ const log = [];+ class Written extends w.HTMLElement {+ constructor() { super(); log.push('construct:' + (this.ownerDocument === d)); }+ static get observedAttributes() { return ['title']; }+ attributeChangedCallback(name, oldValue, value) { log.push(name + ':' + oldValue + ':' + value); }+ connectedCallback() { log.push('connected'); }+ }+ class WrittenButton extends w.HTMLButtonElement {+ constructor() { super(); log.push('button'); }+ connectedCallback() { log.push('button-connected'); }+ }+ w.customElements.define('child-written', Written);+ w.customElements.define('child-button', WrittenButton, {extends:'button'});+ d[method]('<!doctype html><body><child-written title="parsed"></child-written><button is="child-button"></button>');+ results.push({+ custom:d.querySelector('child-written') instanceof Written,+ button:d.querySelector('button') instanceof WrittenButton,+ log:log.slice()+ });+ d.close();+ frame.remove();+ }+ }+ return JSON.stringify(results);+ })()+ "#).expect("child parser write and writeln should construct custom elements");+ let expected = serde_json::json!({+ "custom": true,+ "button": true,+ "log": ["construct:true", "title:null:parsed", "button", "connected", "button-connected"]+ });+ assert_eq!(+ serde_json::from_str::<serde_json::Value>(&result).unwrap(),+ serde_json::Value::Array(vec![expected; 4])+ );+}++#[test]+fn child_parser_write_preserves_outer_javascript_microtask_boundary() {+ let mut vm = new_storage_test_vm("https://child-parser-microtasks.test/");+ let result = vm+ .eval(+ r#"+ globalThis.childParserLog = [];+ const frame = document.createElement('iframe');+ (document.body || document.documentElement || document).appendChild(frame);+ const w = frame.contentWindow, d = w.document;+ class Written extends w.HTMLElement {+ constructor() {+ super(); childParserLog.push('constructor');+ Promise.resolve().then(() => childParserLog.push('constructor-microtask'));+ }+ connectedCallback() { childParserLog.push('connected'); }+ }+ w.customElements.define('child-written', Written);+ Promise.resolve().then(() => childParserLog.push('earlier-microtask'));+ d.write('<!doctype html><body><child-written></child-written>');+ childParserLog.push('after-write');+ d.close();+ JSON.stringify(childParserLog);+ "#,+ )+ .expect("child document.write should keep outer JavaScript on the stack");+ assert_eq!(result, r#"["constructor","connected","after-write"]"#);+ assert_eq!(+ vm.eval("JSON.stringify(childParserLog)").unwrap(),+ r#"["constructor","connected","after-write","earlier-microtask","constructor-microtask"]"#+ );+}++#[test]+fn child_parser_write_queues_mutation_records_for_parser_insertions() {+ let mut vm = new_storage_test_vm("https://child-parser-observers.test/");+ let result = vm+ .eval(+ r#"+ (() => {+ const frame = document.createElement('iframe');+ (document.body || document.documentElement || document).appendChild(frame);+ const w = frame.contentWindow, d = w.document;+ d.open(); d.write('<!doctype html><body>');+ const observer = new w.MutationObserver(() => {});+ observer.observe(d.body, {childList:true, subtree:true});+ d.write('<b><i>hello</i></b>');+ const result = observer.takeRecords().map(r => [+ r.type, r.target.nodeName,+ Array.from(r.addedNodes, n => n.nodeName), r.removedNodes.length+ ]);+ observer.disconnect(); d.close(); frame.remove();+ return JSON.stringify(result);+ })()+ "#,+ )+ .expect("child parser mutations should be observable before write returns");+ assert_eq!(+ result,+ r##"[["childList","BODY",["B"],0],["childList","B",["I"],0],["childList","I",["#text"],0]]"##+ );+}++#[test]+fn child_parser_guards_dynamic_markup_during_construction_and_attribute_reactions() {+ let mut vm = new_storage_test_vm("https://child-parser-dynamic-markup.test/");+ let result = vm.eval(r#"+ (() => {+ const frame = document.createElement('iframe');+ const other = document.createElement('iframe');+ const root = document.body || document.documentElement || document;+ root.appendChild(frame); root.appendChild(other);+ const w = frame.contentWindow, d = w.document;+ const log = [];+ const probe = phase => {+ for (const method of ['open', 'close', 'write', 'writeln']) {+ try { d[method](''); log.push(phase + ':' + method + ':allowed'); }+ catch (e) { log.push(phase + ':' + method + ':' + e.name + ':' + (e instanceof w.DOMException)); }+ }+ other.contentDocument.write('<p>' + phase + '</p>');+ };+ class Written extends w.HTMLElement {+ constructor() { super(); probe('construct'); }+ static get observedAttributes() { return ['title']; }+ attributeChangedCallback() { probe('attribute'); }+ }+ w.customElements.define('child-written', Written);+ d.write('<!doctype html><body><child-written title="parsed"></child-written>');+ d.write('<p>after</p>');+ d.close(); other.contentDocument.close();+ return JSON.stringify({log, after:d.querySelector('p').textContent,+ other:Array.from(other.contentDocument.querySelectorAll('p'), p => p.textContent)});+ })()+ "#).expect("dynamic markup guards should cover construction and token attribute reactions");+ let mut log = Vec::new();+ for phase in ["construct", "attribute"] {+ for method in ["open", "close", "write", "writeln"] {+ log.push(format!("{phase}:{method}:InvalidStateError:true"));+ }+ }+ assert_eq!(+ serde_json::from_str::<serde_json::Value>(&result).unwrap(),+ serde_json::json!({"log": log, "after":"after", "other":["construct", "attribute"]})+ );+}++#[tokio::test]+async fn child_parser_srcdoc_checkpoints_mutations_before_custom_element_construction() {+ for (markup, before) in [+ (+ "<b><child-parsed></child-parsed></b>",+ serde_json::json!([["BODY", "B"]]),+ ),+ (+ "<b><i>hello</b><child-parsed></child-parsed>",+ serde_json::json!([["BODY", "B"], ["B", "I"], ["I", "#text"], ["BODY", "I"]]),+ ),+ ] {+ let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).unwrap();+ let mut vm = new_page_task_executor_test_vm_with_loader(+ "https://child-parser-checkpoint.test/",+ &loader,+ );+ let html = format!(+ r#"<!doctype html><body><script>+ window.batches = [];+ const describe = records => records.map(r => [r.target.nodeName,+ Array.from(r.addedNodes, n => n.nodeName).join(',')]);+ class Parsed extends HTMLElement {{+ constructor() {{ super(); window.beforeConstructor = batches.slice(); }}+ }}+ customElements.define('child-parsed', Parsed);+ new MutationObserver(records => batches.push(describe(records)))+ .observe(document.body, {{childList:true, subtree:true}});+ </script>{markup}</body>"#+ );+ vm.eval(&format!(+ r#"+ globalThis.childParserLoaded = false;+ globalThis.childParserFrame = document.createElement('iframe');+ childParserFrame.onload = () => {{ childParserLoaded = true; }};+ childParserFrame.srcdoc = {html:?};+ (document.body || document.documentElement || document).appendChild(childParserFrame);+ 'queued';+ "#+ ))+ .expect("srcdoc parser probe should queue");+ advance_page_task_executor_until_eval_equals(+ &mut vm,+ &loader,+ "String(childParserLoaded)",+ "true",+ "child parser load",+ )+ .await;+ let result = vm+ .eval(+ r#"JSON.stringify({before:childParserFrame.contentWindow.beforeConstructor,+ after:childParserFrame.contentWindow.batches})"#,+ )+ .unwrap();+ let result: serde_json::Value = serde_json::from_str(&result).unwrap();+ assert_eq!(+ result["before"],+ serde_json::json!([before.clone()]),+ "markup: {markup}"+ );+ let parent = if markup.contains("<i>") { "I" } else { "B" };+ assert_eq!(+ result["after"],+ serde_json::json!([before, [[parent, "CHILD-PARSED"]]]),+ "markup: {markup}"+ );+ }+}