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): queue insertion upgrades in tree order
ldm0dd650eb96b58
diff --git a/moli-renderer-v8/src/custom_elements.rs b/moli-renderer-v8/src/custom_elements.rsindex 6fc1ebaf3..4372d1565 100644--- a/moli-renderer-v8/src/custom_elements.rs+++ b/moli-renderer-v8/src/custom_elements.rs@@ -152,8 +152,9 @@ pub(crate) use registry_retarget::{};mod subtree_upgrade;pub(crate) use subtree_upgrade::{- enqueue_upgrade_reactions_for_subtree, upgrade_late_defined_connected_tree_after_parser_sync,- upgrade_subtree_if_defined, upgrade_subtree_if_defined_for_registry,+ enqueue_upgrade_reaction_if_defined, enqueue_upgrade_reactions_for_subtree,+ upgrade_late_defined_connected_tree_after_parser_sync, upgrade_subtree_if_defined,+ upgrade_subtree_if_defined_for_registry,};mod traversal;mod upgrade_eligibility;diff --git a/moli-renderer-v8/src/custom_elements/subtree_upgrade.rs b/moli-renderer-v8/src/custom_elements/subtree_upgrade.rsindex f47a3db96..bc7509e33 100644--- a/moli-renderer-v8/src/custom_elements/subtree_upgrade.rs+++ b/moli-renderer-v8/src/custom_elements/subtree_upgrade.rs@@ -52,6 +52,18 @@ pub(crate) fn upgrade_subtree_if_defined_for_registry(true}+pub(crate) fn enqueue_upgrade_reaction_if_defined(+ scope: &mut v8::PinScope<'_, '_>,+ host_ptr: *mut JsContextHost,+ handle: DomHandle,+) -> bool {+ if !can_upgrade_handle(host_ptr, handle) {+ return false;+ }+ enqueue_custom_element_reaction(scope, host_ptr, handle, CustomElementReaction::Upgrade);+ true+}+pub(crate) fn enqueue_upgrade_reactions_for_subtree(scope: &mut v8::PinScope<'_, '_>,host_ptr: *mut JsContextHost,@@ -61,15 +73,7 @@ pub(crate) fn enqueue_upgrade_reactions_for_subtree(collect_shadow_including_subtree_handles(host_ptr, root, &mut handles);let mut enqueued = false;for handle in handles {- if can_upgrade_handle(host_ptr, handle) {- enqueue_custom_element_reaction(- scope,- host_ptr,- handle,- CustomElementReaction::Upgrade,- );- enqueued = true;- }+ enqueued |= enqueue_upgrade_reaction_if_defined(scope, host_ptr, handle);}enqueued}diff --git a/moli-renderer-v8/src/document_runtime/mutation_commands/tree/custom_element_lifecycle.rs b/moli-renderer-v8/src/document_runtime/mutation_commands/tree/custom_element_lifecycle.rsindex c1570a2ee..234424128 100644--- a/moli-renderer-v8/src/document_runtime/mutation_commands/tree/custom_element_lifecycle.rs+++ b/moli-renderer-v8/src/document_runtime/mutation_commands/tree/custom_element_lifecycle.rs@@ -92,7 +92,7 @@ impl DocumentRuntime {host_ptr: *mut JsContextHost,roots: &[DomHandle],was_connected: bool,- sync_upgrade_subtrees: bool,+ upgrade_subtrees: bool,) {if was_connected|| !roots@@ -105,27 +105,34 @@ impl DocumentRuntime {// been defined and none have been upgraded yet — the typical state for// pages that don't use the Custom Elements API. See// CustomElementStore::is_subtree_lifecycle_quiescent.- let host = unsafe { &*host_ptr };- if host.custom_elements_subtree_lifecycle_quiescent() {+ if unsafe { &*host_ptr }.custom_elements_subtree_lifecycle_quiescent() {return;}- if sync_upgrade_subtrees {- for &root in roots {- if custom_elements::is_shadow_including_rooted_in_browsing_context_document(- host, root,- ) {- let _ = custom_elements::upgrade_subtree_if_defined(scope, host_ptr, root);+ for &root in roots {+ if !self.is_custom_element_lifecycle_connected(root) {+ continue;+ }+ let can_upgrade = upgrade_subtrees+ && custom_elements::is_shadow_including_rooted_in_browsing_context_document(+ unsafe { &*host_ptr },+ root,+ );+ let mut handles = Vec::new();+ self.collect_subtree_handles_preorder(root, &mut handles);+ for handle in handles {+ // Insertion queues either a connection for an existing custom+ // element or an upgrade. The upgrade owns its initial connection+ // callback, and both kinds of reaction retain subtree order.+ let is_custom = unsafe { &*host_ptr }+ .custom_elements_for_node_handle(handle)+ .is_some_and(|store| store.is_upgraded_handle(handle));+ if is_custom {+ custom_elements::enqueue_connected_callback(scope, host_ptr, handle);+ } else if can_upgrade {+ custom_elements::enqueue_upgrade_reaction_if_defined(scope, host_ptr, handle);}}}- for &root in roots {- self.enqueue_custom_element_lifecycle_in_subtree(- scope,- host_ptr,- root,- "connectedCallback",- );- }}pub(super) fn enqueue_custom_element_form_association_callbacks_in_subtrees(diff --git a/moli-renderer-v8/src/document_runtime/mutation_commands/tree/insertion_followups.rs b/moli-renderer-v8/src/document_runtime/mutation_commands/tree/insertion_followups.rsindex 6f1afacf5..41916a2b7 100644--- a/moli-renderer-v8/src/document_runtime/mutation_commands/tree/insertion_followups.rs+++ b/moli-renderer-v8/src/document_runtime/mutation_commands/tree/insertion_followups.rs@@ -129,7 +129,7 @@ impl DocumentRuntime {scope,host_ptr,insertion_plan,- profile.sync_upgrade_connected_subtrees,+ profile.upgrade_connected_subtrees,);});}@@ -138,7 +138,7 @@ impl DocumentRuntime {scope,host_ptr,insertion_plan,- profile.sync_upgrade_connected_subtrees,+ profile.upgrade_connected_subtrees,);}}@@ -149,7 +149,7 @@ impl DocumentRuntime {scope: &mut v8::PinScope<'_, '_>,host_ptr: *mut JsContextHost,insertion_plan: &TreeInsertionPlan<'_>,- sync_upgrade_connected_subtrees: bool,+ upgrade_connected_subtrees: bool,) {let was_connected = insertion_plan.was_lifecycle_connected_before_insert();let adopted_across_documents = insertion_plan.adoption.crosses_documents();@@ -181,7 +181,7 @@ impl DocumentRuntime {host_ptr,insertion_plan.insertion_roots,was_connected && !adopted_across_documents,- sync_upgrade_connected_subtrees,+ upgrade_connected_subtrees,);let is_lifecycle_connected = insertion_plan.insertion_rootsdiff --git a/moli-renderer-v8/src/document_runtime/mutation_commands/tree/policy.rs b/moli-renderer-v8/src/document_runtime/mutation_commands/tree/policy.rsindex bb1460a9d..a73b908ee 100644--- a/moli-renderer-v8/src/document_runtime/mutation_commands/tree/policy.rs+++ b/moli-renderer-v8/src/document_runtime/mutation_commands/tree/policy.rs@@ -21,7 +21,7 @@ pub(super) struct TreeMutationSourceProfile {pub(super) source: TreeMutationSideEffectSource,pub(super) reaction_policy: TreeReactionDispatchPolicy,pub(super) nonce_policy: TreeNoncePolicy,- pub(super) sync_upgrade_connected_subtrees: bool,+ pub(super) upgrade_connected_subtrees: bool,pub(super) queue_parser_details_toggle_events: bool,}@@ -53,7 +53,7 @@ impl TreeMutationSourceProfile {source: TreeMutationSideEffectSource::JsDomApi,reaction_policy,nonce_policy,- sync_upgrade_connected_subtrees: true,+ upgrade_connected_subtrees: true,queue_parser_details_toggle_events: false,}}@@ -70,7 +70,7 @@ impl TreeMutationSourceProfile {source: TreeMutationSideEffectSource::ParserTreeSink,reaction_policy: TreeReactionDispatchPolicy::AppendToCurrentQueue,nonce_policy: TreeNoncePolicy::HideInsertedContentAttributes,- sync_upgrade_connected_subtrees: false,+ upgrade_connected_subtrees: false,queue_parser_details_toggle_events: true,}}@@ -94,7 +94,7 @@ impl TreeMutationSourceProfile {source: TreeMutationSideEffectSource::JsDomApi,reaction_policy: TreeReactionDispatchPolicy::AppendToCurrentQueue,nonce_policy: TreeNoncePolicy::HideInsertedContentAttributes,- sync_upgrade_connected_subtrees: false,+ upgrade_connected_subtrees: false,queue_parser_details_toggle_events: true,}}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 8a6ca1a4b..313298375 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,7 @@use super::*;use crate::custom_elements::CustomElementRegistryKey;+mod insertion;mod parser;#[tokio::test]diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements/insertion.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements/insertion.rsnew file mode 100644index 000000000..b604d820d--- /dev/null+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements/insertion.rs@@ -0,0 +1,96 @@+use super::*;++#[test]+fn insertion_upgrade_enqueues_connected_callback_once() {+ let mut vm = new_storage_test_vm("https://custom-element-insertion.test/");+ let result = vm.eval(r#"+ (() => {+ const results = [];+ for (const parentTag of ['option', 'div']) {+ for (const initiallyCustom of [false, true]) {+ const frame = document.createElement('iframe');+ (document.body || document.documentElement || document).appendChild(frame);+ const w = frame.contentWindow, d = w.document;+ const log = [];+ class Inserted extends w.HTMLElement {+ constructor() { super(); log.push('constructed'); }+ connectedCallback() { log.push('connected'); }+ disconnectedCallback() { log.push('disconnected'); }+ }+ w.customElements.define('inserted-element', Inserted);+ const parent = d.createElement(parentTag);+ d.body.appendChild(parent);+ const element = (initiallyCustom ? d : document).createElement('inserted-element');+ const created = log.splice(0);+ parent.appendChild(element);+ const inserted = log.splice(0);+ if (parentTag === 'option') parent.text = 'replacement';+ else parent.textContent = 'replacement';+ const removed = log.splice(0);+ parent.appendChild(element);+ const reconnected = log.splice(0);+ results.push({created, inserted, removed, reconnected, custom:element instanceof Inserted});+ frame.remove();+ }+ }+ return JSON.stringify(results);+ })()+ "#).expect("insertion upgrades should own their initial connected callback");+ let results: Vec<serde_json::Value> = serde_json::from_str(&result).unwrap();+ assert_eq!(results.len(), 4);+ for (index, result) in results.iter().enumerate() {+ let (created, inserted) = if index % 2 == 0 {+ (+ serde_json::json!([]),+ serde_json::json!(["constructed", "connected"]),+ )+ } else {+ (+ serde_json::json!(["constructed"]),+ serde_json::json!(["connected"]),+ )+ };+ assert_eq!(+ result,+ &serde_json::json!({+ "created":created, "inserted":inserted, "removed":["disconnected"],+ "reconnected":["connected"], "custom":true+ }),+ "variant: {index}"+ );+ }+}++#[test]+fn insertion_upgrade_preserves_reaction_order_with_already_custom_siblings() {+ let mut vm = new_storage_test_vm("https://custom-element-insertion-order.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;+ const log = [];+ class Inserted extends w.HTMLElement {+ constructor() { super(); log.push('construct:' + this.id); }+ connectedCallback() { log.push('connected:' + this.id); }+ }+ w.customElements.define('inserted-element', Inserted);+ const parent = d.createElement('div');+ const a = d.createElement('inserted-element'); a.id = 'a';+ const b = document.createElement('inserted-element'); b.id = 'b';+ const c = d.createElement('inserted-element'); c.id = 'c';+ parent.append(a, b, c);+ log.length = 0;+ d.body.appendChild(parent);+ return JSON.stringify(log);+ })()+ "#,+ )+ .expect("insertion reactions should follow subtree order regardless of upgrade state");+ assert_eq!(+ result,+ r#"["connected:a","construct:b","connected:b","connected:c"]"#+ );+}