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(style): invalidate documents when moving shadow trees
ldm02e01ea44082b
diff --git a/moli-dom/src/native/host/document.rs b/moli-dom/src/native/host/document.rsindex 4121aebde..697675260 100644--- a/moli-dom/src/native/host/document.rs+++ b/moli-dom/src/native/host/document.rs@@ -1544,8 +1544,13 @@ impl DomHost {&mut self,inserted_roots: &[DomHandle],shadow_hosts: &[DomHandle],+ departed_connected_document: Option<DomHandle>,) -> Vec<DomStylesheetOwnerChange> {- let mut membership_documents = HashSet::new();+ // `insertBefore` performs an implicit removal when its child already+ // has a parent. Preserve that pre-splice membership here so one update+ // invalidates both the old and new Document TreeScope universes.+ let mut membership_documents: HashSet<DomHandle> =+ departed_connected_document.into_iter().collect();let mut stylesheet_owners = Vec::new();for &root in inserted_roots {let Some(node) = self.node(root) else {diff --git a/moli-dom/src/native/host/mutation/tree.rs b/moli-dom/src/native/host/mutation/tree.rsindex 68ac1b37e..d3a73fb6c 100644--- a/moli-dom/src/native/host/mutation/tree.rs+++ b/moli-dom/src/native/host/mutation/tree.rs@@ -312,6 +312,16 @@ impl DomHost {&inserted_option_owner_snapshots,);let new_owner_document = self.owner_document_handle(parent);+ let removed_connected_shadow_tree = single_child_was_connected_before_insert+ && implicit_removal_slot_state+ .as_ref()+ .is_some_and(|(_, _, context, _)| !context.shadow_hosts.is_empty())+ && (!self.is_connected(child) || new_owner_document != old_owner_document);+ let departed_connected_shadow_tree_document = if removed_connected_shadow_tree {+ old_owner_document+ } else {+ None+ };let mut shadow_stylesheet_owner_changes = Vec::new();let inserted_shadow_hosts =self.record_inserted_subtree_candidates_in_subtrees(&inserted_roots);@@ -319,6 +329,7 @@ impl DomHost {self.sync_shadow_tree_scopes_for_inserted_subtrees(&inserted_roots,&inserted_shadow_hosts,+ departed_connected_shadow_tree_document,),);self.record_mutation(MutationScope::QueryState);diff --git a/moli-dom/src/native/mod.rs b/moli-dom/src/native/mod.rsindex 401a0b972..e1f44c828 100644--- a/moli-dom/src/native/mod.rs+++ b/moli-dom/src/native/mod.rs@@ -1862,6 +1862,100 @@ mod tests {);}+ #[test]+ fn implicit_move_to_detached_parent_invalidates_old_document_tree_scopes() {+ let mut host = DomHost::from_dom(NativeDom::new_html(test_url()));+ let document = host.document_node_id();+ let shadow_host = host.create_element("section");+ assert!(host.append_child(document, shadow_host));+ let shadow_root = host+ .attach_shadow_root(shadow_host, "open")+ .expect("connected shadow root");+ let version_before_move = host.document_tree_scope_version(document);+ assert_eq!(host.snapshot_connected_shadow_roots(), vec![shadow_root]);++ let detached_parent = host.create_element("main");+ assert!(host.append_child(detached_parent, shadow_host));++ assert_eq!(+ host.document_tree_scope_version(document),+ version_before_move + 1,+ "the implicit removal must invalidate the old Document TreeScope universe",+ );+ assert_eq!(host.connected_shadow_roots_cache_versions_for_test(), None);+ assert!(host.snapshot_connected_shadow_roots().is_empty());+ }++ #[test]+ fn implicit_move_of_nested_shadow_tree_invalidates_old_document_once() {+ let mut host = DomHost::from_dom(NativeDom::new_html(test_url()));+ let document = host.document_node_id();+ let outer_host = host.create_element("section");+ assert!(host.append_child(document, outer_host));+ let outer_root = host+ .attach_shadow_root(outer_host, "open")+ .expect("outer connected shadow root");+ let inner_host = host.create_element("article");+ assert!(host.append_child(outer_root, inner_host));+ let inner_root = host+ .attach_shadow_root(inner_host, "open")+ .expect("nested connected shadow root");+ let version_before_move = host.document_tree_scope_version(document);+ let roots = host.snapshot_connected_shadow_roots();+ assert!(roots.contains(&outer_root));+ assert!(roots.contains(&inner_root));++ let detached_parent = host.create_element("main");+ assert!(host.append_child(detached_parent, outer_host));++ assert_eq!(+ host.document_tree_scope_version(document),+ version_before_move + 1,+ "one subtree move must advance the old Document only once",+ );+ assert!(!host.is_connected(outer_root));+ assert!(!host.is_connected(inner_root));+ assert!(host.snapshot_connected_shadow_roots().is_empty());+ }++ #[test]+ fn implicit_cross_document_move_invalidates_both_tree_scope_universes() {+ let mut host = DomHost::from_dom(NativeDom::new_html(test_url()));+ let old_document = host.document_node_id();+ let new_document = host.create_detached_html_document();+ let new_parent = host.create_parser_element_without_attributes_for_document(+ new_document,+ "main".to_owned(),+ "http://www.w3.org/1999/xhtml".to_owned(),+ None,+ );+ assert!(host.append_child(new_document, new_parent));+ host.mark_subtree_connected_preserving_owner_document(new_document);++ let shadow_host = host.create_element("section");+ assert!(host.append_child(old_document, shadow_host));+ let shadow_root = host+ .attach_shadow_root(shadow_host, "open")+ .expect("old-document shadow root");+ let old_version_before_move = host.document_tree_scope_version(old_document);+ let new_version_before_move = host.document_tree_scope_version(new_document);++ assert!(host.append_child(new_parent, shadow_host));++ assert_eq!(host.owner_document_handle(shadow_host), Some(new_document));+ assert!(host.is_connected(shadow_root));+ assert_eq!(+ host.document_tree_scope_version(old_document),+ old_version_before_move + 1,+ "the implicit removal must invalidate the departed Document",+ );+ assert_eq!(+ host.document_tree_scope_version(new_document),+ new_version_before_move + 1,+ "the insertion must invalidate the destination Document",+ );+ }+#[test]fn connected_shadow_roots_snapshot_cache_uses_shadow_connection_version() {let mut host = DomHost::from_dom(NativeDom::new_html(test_url()));diff --git a/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs b/moli-renderer-v8/src/native_bridge/context_host/host_environment.rsindex d84683f3b..a605c042b 100644--- a/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs+++ b/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs@@ -1641,6 +1641,16 @@ impl JsContextHost {.retained_stylist_identity_for_document_for_test(document)}+ #[cfg(test)]+ pub(crate) fn retained_shadow_scope_flush_count_for_document_for_test(+ &self,+ document: DomHandle,+ root: DomHandle,+ ) -> Option<u64> {+ self.style_engine+ .retained_shadow_scope_flush_count_for_document_for_test(document, root)+ }+#[cfg(test)]pub(crate) fn pending_style_invalidation_work_item_count_for_document_for_test(&self,diff --git a/moli-renderer-v8/src/script_vm.rs b/moli-renderer-v8/src/script_vm.rsindex 9fce8e112..2fc4f748f 100644--- a/moli-renderer-v8/src/script_vm.rs+++ b/moli-renderer-v8/src/script_vm.rs@@ -2917,6 +2917,17 @@ impl ScriptVm {.retained_stylist_identity_for_document_for_test(document)}+ #[cfg(test)]+ pub(crate) fn retained_shadow_scope_flush_count_for_document_for_test(+ &self,+ document: DomHandle,+ root: DomHandle,+ ) -> Option<u64> {+ self._context_host+ .borrow()+ .retained_shadow_scope_flush_count_for_document_for_test(document, root)+ }+#[cfg(test)]pub(crate) fn shared_worker_client_count_for_test(&self) -> usize {self._context_hostdiff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rsindex c8441e734..a814ee4ad 100644--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs@@ -2178,6 +2178,149 @@ fn computed_style_reuses_retained_system_across_connected_shadow_roots() {);}+#[test]+fn implicit_move_to_detached_parent_removes_retained_shadow_scope() {+ let mut vm = new_storage_test_vm("https://implicit-shadow-detach-style-world.test/");+ let document = vm.document_handle_for_test();++ assert_eq!(+ vm.eval(+ r#"(() => {+ const root = document.documentElement ||+ document.appendChild(document.createElement('html'));+ const body = document.body || root.appendChild(document.createElement('body'));+ const host = document.createElement('section');+ host.id = 'movable-shadow-host';+ body.append(host);+ const shadow = host.attachShadow({ mode: 'open' });+ const target = document.createElement('span');+ target.id = 'target';+ shadow.append(target);+ const sheet = new CSSStyleSheet();+ sheet.replaceSync('#target { color: rgb(1, 2, 3); }');+ shadow.adoptedStyleSheets = [sheet];+ globalThis.__movableShadowHost = host;+ globalThis.__detachedShadowParent = document.createElement('div');+ return getComputedStyle(target).color;+ })()"#,+ )+ .expect("connected adopted stylesheet should resolve"),+ "rgb(1, 2, 3)",+ );+ let shadow_root = {+ let context = vm._context_host.borrow();+ let host = context.dom_host();+ let shadow_host = host+ .element_handle_by_id("movable-shadow-host")+ .expect("shadow host handle");+ host.shadow_root_handle(shadow_host)+ .expect("shadow root handle")+ };+ assert!(+ vm.retained_shadow_scope_flush_count_for_document_for_test(document, shadow_root)+ .is_some(),+ "the initial observation must install the adopted ShadowRoot scope",+ );+ let updates_before_move = vm.retained_style_system_update_count_for_document_for_test(document);++ vm.eval(+ r#"(() => {+ __detachedShadowParent.appendChild(__movableShadowHost);+ return getComputedStyle(document.body).display;+ })()"#,+ )+ .expect("the old Document should observe the implicit removal");++ assert_eq!(+ vm.retained_shadow_scope_flush_count_for_document_for_test(document, shadow_root),+ None,+ "the old Document must discard the departed adopted ShadowRoot scope",+ );+ assert_eq!(+ vm.retained_style_system_update_count_for_document_for_test(document),+ updates_before_move + 1,+ "the next old-Document observation must apply one TreeScope update",+ );+}++#[test]+fn implicit_cross_document_move_transfers_retained_shadow_scope() {+ let mut vm = new_storage_test_vm("https://implicit-shadow-cross-document.test/");+ let parent_document = vm.document_handle_for_test();+ assert_eq!(+ vm.eval(+ r#"(() => {+ const html = document.documentElement ||+ document.appendChild(document.createElement('html'));+ const body = document.body || html.appendChild(document.createElement('body'));+ const frame = document.createElement('iframe');+ frame.id = 'shadow-move-frame';+ body.append(frame);+ frame.contentDocument.open();+ frame.contentDocument.write('<!doctype html><body></body>');+ frame.contentDocument.close();++ const host = document.createElement('section');+ host.id = 'cross-document-shadow-host';+ body.append(host);+ const shadow = host.attachShadow({ mode: 'open' });+ shadow.innerHTML = `+ <style>#target { color: rgb(9, 8, 7); }</style>+ <span id="target"></span>`;+ globalThis.__crossDocumentShadowHost = host;+ globalThis.__crossDocumentShadowTarget = shadow.getElementById('target');+ return getComputedStyle(__crossDocumentShadowTarget).color;+ })()"#,+ )+ .expect("parent shadow style should initialize"),+ "rgb(9, 8, 7)",+ );+ let child_document = child_document_handle_for_frame_id(&vm, "shadow-move-frame");+ let shadow_root = {+ let host = vm.document_runtime.dom_host();+ let shadow_host = host+ .element_handle_by_id("cross-document-shadow-host")+ .expect("cross-document shadow host handle");+ host.shadow_root_handle(shadow_host)+ .expect("cross-document shadow root handle")+ };+ let parent_identity = vm.retained_stylist_identity_for_document_for_test(parent_document);+ assert!(+ vm.retained_shadow_scope_flush_count_for_document_for_test(parent_document, shadow_root)+ .is_some()+ );++ assert_eq!(+ vm.eval(+ r#"(() => {+ const frame = document.getElementById('shadow-move-frame');+ frame.contentDocument.body.appendChild(__crossDocumentShadowHost);+ const parentProbe = getComputedStyle(document.body).display;+ const childColor = frame.contentWindow+ .getComputedStyle(__crossDocumentShadowTarget).color;+ return [parentProbe, childColor].join('|');+ })()"#,+ )+ .expect("cross-document shadow move should evaluate"),+ "block|rgb(9, 8, 7)",+ );+ assert_eq!(+ vm.retained_stylist_identity_for_document_for_test(parent_document),+ parent_identity,+ "the old Document world must update in place",+ );+ assert_eq!(+ vm.retained_shadow_scope_flush_count_for_document_for_test(parent_document, shadow_root),+ None,+ "the old Document must release the moved ShadowRoot scope",+ );+ assert!(+ vm.retained_shadow_scope_flush_count_for_document_for_test(child_document, shadow_root)+ .is_some(),+ "the destination Document must install the moved ShadowRoot scope",+ );+}+#[test]fn child_layout_uses_complete_stable_tree_scope_universe() {let mut vm = new_storage_test_vm("https://child-tree-scope-universe.test/");