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(dom): share host-including ancestry checks
ldm0981f9b15ddf6
diff --git a/moli-dom/src/native/document.rs b/moli-dom/src/native/document.rsindex c4e90222b..b228dc0c3 100644--- a/moli-dom/src/native/document.rs+++ b/moli-dom/src/native/document.rs@@ -292,4 +292,20 @@ impl DocumentType {}#[derive(Debug, Clone, Default)]-pub struct DocumentFragment;+pub struct DocumentFragment {+ host: Option<NativeNodeId>,+}++impl DocumentFragment {+ pub fn new(host: Option<NativeNodeId>) -> Self {+ Self { host }+ }++ pub fn host(&self) -> Option<NativeNodeId> {+ self.host+ }++ pub(crate) fn set_host(&mut self, host: NativeNodeId) {+ self.host = Some(host);+ }+}diff --git a/moli-dom/src/native/host/clone.rs b/moli-dom/src/native/host/clone.rsindex 4430f1f3a..f21c7c7d7 100644--- a/moli-dom/src/native/host/clone.rs+++ b/moli-dom/src/native/host/clone.rs@@ -252,6 +252,7 @@ impl DomHost {available_to_element_internals,} => {self.dom.register_stylesheet_candidate_tree_scope(clone);+ self.dom.set_document_fragment_host(clone, host);self.shadow_roots_by_host.borrow_mut().insert(host,ShadowRootState {@@ -399,6 +400,7 @@ impl DomHost {available_to_element_internals,} => {self.dom.register_stylesheet_candidate_tree_scope(clone);+ self.dom.set_document_fragment_host(clone, host);self.shadow_roots_by_host.borrow_mut().insert(host,ShadowRootState {diff --git a/moli-dom/src/native/host/document.rs b/moli-dom/src/native/host/document.rsindex 26deabe90..d99e541e3 100644--- a/moli-dom/src/native/host/document.rs+++ b/moli-dom/src/native/host/document.rs@@ -1366,7 +1366,7 @@ impl DomHost {if is_template {let template_contents = self.dom- .create_template_contents_fragment_for_document(document_handle);+ .create_template_contents_fragment_for_document(document_handle, Some(node_id));if let Some(element) = self.node_mut(node_id).and_then(|node| node.data_mut().as_element_mut())@@ -1677,6 +1677,7 @@ impl DomHost {return None;}let root = self.create_document_fragment();+ self.dom.set_document_fragment_host(root, host);let owner_document = self.node(host).and_then(Node::owner_document);let connected = self.is_connected(host);self.dom.register_stylesheet_candidate_tree_scope(root);@@ -2244,6 +2245,7 @@ impl DomHost {continue;}self.dom.register_stylesheet_candidate_tree_scope(root);+ self.dom.set_document_fragment_host(root, host);self.shadow_roots_by_host.borrow_mut().insert(host,ShadowRootState {diff --git a/moli-dom/src/native/host/mutation/owner_lifecycle.rs b/moli-dom/src/native/host/mutation/owner_lifecycle.rsindex eade2cc41..5f6ef0b08 100644--- a/moli-dom/src/native/host/mutation/owner_lifecycle.rs+++ b/moli-dom/src/native/host/mutation/owner_lifecycle.rs@@ -72,7 +72,9 @@ impl NativeDom {mut reference_child: Option<NativeNodeId>,commit_candidate_registration: bool,) -> Option<OwnerLifecycleChanges> {- if !self.can_have_children(parent) || parent == child || self.is_ancestor(child, parent) {+ if !self.can_have_children(parent)+ || self.is_host_including_inclusive_ancestor(child, parent)+ {return None;}let parent_type = self.node(parent)?.node_type();@@ -97,8 +99,7 @@ impl NativeDom {{let fragment_children = self.child_ids(child).collect::<Vec<_>>();let all_children_are_insertable = fragment_children.iter().all(|fragment_child| {- parent != *fragment_child- && !self.is_ancestor(*fragment_child, parent)+ !self.is_host_including_inclusive_ancestor(*fragment_child, parent)&& self.node(*fragment_child).is_some_and(|node| {!node.data().is_document_fragment()&& Self::can_insert_child_type(parent_type, node.node_type())diff --git a/moli-dom/src/native/mod.rs b/moli-dom/src/native/mod.rsindex 64b68acf6..da7de0d71 100644--- a/moli-dom/src/native/mod.rs+++ b/moli-dom/src/native/mod.rs@@ -345,7 +345,7 @@ impl NativeDom {false,);if local_name.eq_ignore_ascii_case("template") {- let fragment = self.create_template_contents_fragment();+ let fragment = self.create_template_contents_fragment(handle);if let Some(element) = self.node_mut(handle).and_then(|node| node.data_mut().as_element_mut())@@ -384,7 +384,7 @@ impl NativeDom {false,);if local_name.eq_ignore_ascii_case("template") {- let fragment = self.create_template_contents_fragment();+ let fragment = self.create_template_contents_fragment(handle);if let Some(element) = self.node_mut(handle).and_then(|node| node.data_mut().as_element_mut())@@ -488,20 +488,21 @@ impl NativeDom {owner_document: NativeNodeId,) -> NativeNodeId {self.create_node(- NodeData::DocumentFragment(DocumentFragment),+ NodeData::DocumentFragment(DocumentFragment::default()),Some(owner_document),false,false,)}- pub fn create_template_contents_fragment(&mut self) -> NativeNodeId {- self.create_template_contents_fragment_for_document(self.document_node_id)+ pub fn create_template_contents_fragment(&mut self, host: NativeNodeId) -> NativeNodeId {+ self.create_template_contents_fragment_for_document(self.document_node_id, Some(host))}pub fn create_template_contents_fragment_for_document(&mut self,document_handle: NativeNodeId,+ host: Option<NativeNodeId>,) -> NativeNodeId {let url = self.node(document_handle)@@ -514,7 +515,30 @@ impl NativeDom {false,false,);- self.create_document_fragment_for_document(owner_document)+ self.create_node(+ NodeData::DocumentFragment(DocumentFragment::new(host)),+ Some(owner_document),+ false,+ false,+ )+ }++ pub fn set_document_fragment_host(+ &mut self,+ fragment: NativeNodeId,+ host: NativeNodeId,+ ) -> bool {+ let Some(fragment) = self+ .node_mut(fragment)+ .and_then(|node| node.data_mut().as_document_fragment_mut())+ else {+ return false;+ };+ if fragment.host() == Some(host) {+ return false;+ }+ fragment.set_host(host);+ true}pub fn create_processing_instruction(&mut self, target: &str, data: &str) -> NativeNodeId {@@ -558,6 +582,28 @@ impl NativeDom {false}+ pub fn is_host_including_inclusive_ancestor(+ &self,+ candidate_ancestor: NativeNodeId,+ node_id: NativeNodeId,+ ) -> bool {+ let mut current = Some(node_id);+ while let Some(handle) = current {+ if handle == candidate_ancestor {+ return true;+ }+ let Some(node) = self.node(handle) else {+ return false;+ };+ current = node.parent_node().or_else(|| {+ node.data()+ .as_document_fragment()+ .and_then(DocumentFragment::host)+ });+ }+ false+ }+pub fn detach_from_parent(&mut self, child: NativeNodeId) {let _ = self.detach_from_parent_with_stylesheet_candidate_changes(child);}@@ -2305,6 +2351,46 @@ mod tests {);}+ #[test]+ fn template_content_host_participates_in_hierarchy_checks() {+ let mut dom = NativeDom::new_html(test_url());+ let document = dom.document_node_id();+ let parent = dom.create_element("div");+ let template = dom.create_element("template");+ let content = dom+ .node(template)+ .and_then(Node::as_element)+ .and_then(Element::template_contents)+ .expect("template content");+ let span = dom.create_element("span");++ assert!(dom.append_child(document, parent));+ assert!(dom.append_child(parent, template));+ assert!(dom.append_child(content, span));+ assert_eq!(+ dom.node(content)+ .map(Node::data)+ .and_then(NodeData::as_document_fragment)+ .and_then(DocumentFragment::host),+ Some(template)+ );+ assert!(dom.is_host_including_inclusive_ancestor(template, span));+ assert!(dom.is_host_including_inclusive_ancestor(parent, span));++ for (insertion_parent, child) in [+ (content, parent),+ (content, template),+ (span, parent),+ (span, template),+ ] {+ assert!(!dom.append_child(insertion_parent, child));+ }++ assert_eq!(dom.parent_node(parent), Some(document));+ assert_eq!(dom.parent_node(template), Some(parent));+ assert_eq!(dom.parent_node(span), Some(content));+ }+#[test]fn cloned_template_content_keeps_template_owner_document() {let mut host = DomHost::from_dom(NativeDom::new_html(test_url()));@@ -2332,6 +2418,17 @@ mod tests {.and_then(Node::first_child).expect("cloned template content child");+ assert_eq!(+ host.node(cloned_content)+ .map(Node::data)+ .and_then(NodeData::as_document_fragment)+ .and_then(DocumentFragment::host),+ Some(clone)+ );+ assert!(host.is_host_including_inclusive_ancestor(clone, cloned_child));+ assert!(!host.is_host_including_inclusive_ancestor(template, cloned_child));+ assert!(!host.append_child(cloned_child, clone));+assert_ne!(cloned_content_owner, host.document_handle());assert_eq!(host.node(cloned_child).and_then(Node::owner_document),diff --git a/moli-dom/src/native/node/data.rs b/moli-dom/src/native/node/data.rsindex b840ae5b5..66bd1b841 100644--- a/moli-dom/src/native/node/data.rs+++ b/moli-dom/src/native/node/data.rs@@ -146,4 +146,11 @@ impl NodeData {_ => None,}}++ pub fn as_document_fragment_mut(&mut self) -> Option<&mut DocumentFragment> {+ match self {+ Self::DocumentFragment(fragment) => Some(fragment),+ _ => None,+ }+ }}diff --git a/moli-renderer-v8/src/native_bridge/node/mutation/core.rs b/moli-renderer-v8/src/native_bridge/node/mutation/core.rsindex 11d028b3b..938621a7c 100644--- a/moli-renderer-v8/src/native_bridge/node/mutation/core.rs+++ b/moli-renderer-v8/src/native_bridge/node/mutation/core.rs@@ -272,7 +272,7 @@ fn validate_pre_insert_parent_and_ancestor(child: DomHandle,) -> bool {if !node_can_contain_children(runtime, parent)- || node_move_before_shadow_including_contains(runtime, child, parent)+ || node_is_host_including_inclusive_ancestor(runtime, child, parent){throw_dom_exception(scope, "HierarchyRequestError", 3, "Hierarchy Error");return false;@@ -415,7 +415,7 @@ fn node_move_before_after_argument_validation(let runtime = unsafe { &*runtime_ptr };if !node_move_before_is_valid_parent(runtime, parent)|| !node_move_before_is_valid_child(runtime, child)- || node_move_before_shadow_including_contains(runtime, child, parent)+ || node_is_host_including_inclusive_ancestor(runtime, child, parent)|| node_move_before_shadow_including_root(runtime, parent)!= node_move_before_shadow_including_root(runtime, child){@@ -486,29 +486,14 @@ fn node_move_before_is_valid_child(runtime: &JsContextHost, handle: DomHandle) -})}-pub(super) fn node_move_before_shadow_including_contains(+pub(super) fn node_is_host_including_inclusive_ancestor(runtime: &JsContextHost,ancestor: DomHandle,node: DomHandle,) -> bool {- let mut current = Some(node);- while let Some(handle) = current {- if handle == ancestor {- return true;- }- current = runtime- .dom_host()- .node(handle)- .and_then(Node::parent_node)- .or_else(|| {- runtime- .dom_host()- .is_shadow_root(handle)- .then(|| runtime.dom_host().shadow_root_host(handle))- .flatten()- });- }- false+ runtime+ .dom_host()+ .is_host_including_inclusive_ancestor(ancestor, node)}fn node_move_before_shadow_including_root(diff --git a/moli-renderer-v8/src/native_bridge/node/mutation/parent_node.rs b/moli-renderer-v8/src/native_bridge/node/mutation/parent_node.rsindex 0d34e773a..c4ee0fa28 100644--- a/moli-renderer-v8/src/native_bridge/node/mutation/parent_node.rs+++ b/moli-renderer-v8/src/native_bridge/node/mutation/parent_node.rs@@ -1,5 +1,5 @@use super::core::{- node_can_contain_children, node_move_before_shadow_including_contains, node_type_is_insertable,+ node_can_contain_children, node_is_host_including_inclusive_ancestor, node_type_is_insertable,pre_insert_validation_handles,};use super::fragment::{@@ -148,7 +148,7 @@ fn validate_parent_node_insertion(return false;}for child in inserted {- if node_move_before_shadow_including_contains(runtime, *child, parent) {+ if node_is_host_including_inclusive_ancestor(runtime, *child, parent) {throw_dom_exception(scope, "HierarchyRequestError", 3, "Hierarchy Error");return false;}@@ -281,7 +281,7 @@ fn validate_parent_node_replace_children(return false;}for child in inserted {- if node_move_before_shadow_including_contains(runtime, *child, parent) {+ if node_is_host_including_inclusive_ancestor(runtime, *child, parent) {throw_dom_exception(scope, "HierarchyRequestError", 3, "Hierarchy Error");return false;}diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/detached_documents.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/detached_documents.rsindex 2b652467c..579856279 100644--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/detached_documents.rs+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/detached_documents.rs@@ -847,6 +847,66 @@ fn detached_html_template_content_uses_separate_owner_document() {);}+#[test]+fn template_content_host_rejects_insertion_cycles_after_adoption() {+ let mut vm = new_storage_test_vm("https://template-content-host.test/");++ let result = vm+ .eval(+ r#"+(() => {+ if (!document.documentElement) {+ document.appendChild(document.createElement('html'));+ }+ if (!document.body) {+ document.documentElement.appendChild(document.createElement('body'));+ }+ const parent = document.createElement('div');+ const template = document.createElement('template');+ template.innerHTML = '<span>content</span>';+ parent.appendChild(template);+ document.body.appendChild(parent);+ const content = template.content;+ const span = content.firstChild;+ const errorName = callback => {+ try {+ callback();+ return 'none';+ } catch (error) {+ return error.name;+ }+ };+ const attempts = () => [+ errorName(() => content.appendChild(parent)),+ errorName(() => content.appendChild(template)),+ errorName(() => span.appendChild(parent)),+ errorName(() => span.appendChild(template))+ ];+ const beforeAdoption = attempts();+ const newDocument = document.implementation.createHTMLDocument('');+ const adopted = newDocument.adoptNode(content);+ const afterAdoption = attempts();+ newDocument.body.appendChild(content);+ return [+ ...beforeAdoption,+ adopted === content,+ content.ownerDocument === newDocument,+ ...afterAdoption,+ content.firstChild === null,+ span.parentNode === newDocument.body,+ template.parentNode === parent+ ].join('|');+})()+"#,+ )+ .expect("template content host hierarchy probe should evaluate");++ assert_eq!(+ result,+ "HierarchyRequestError|HierarchyRequestError|HierarchyRequestError|HierarchyRequestError|true|true|HierarchyRequestError|HierarchyRequestError|HierarchyRequestError|HierarchyRequestError|true|true|true"+ );+}+#[test]fn detached_html_image_decode_uses_prototype_method() {let mut vm = new_storage_test_vm("https://detached-image-decode-surface.test/");