The headless browser for AI agents and web scraping
Back ShadowRoot with native tree scopes
SGavrlcc04e785b8fe
diff --git a/crates/obscura-dom/src/lib.rs b/crates/obscura-dom/src/lib.rsindex 6ad84be0..1ec230f7 100644--- a/crates/obscura-dom/src/lib.rs+++ b/crates/obscura-dom/src/lib.rs@@ -6,5 +6,8 @@ pub mod tree_sink;pub mod selector;pub mod serialize;-pub use tree::{Attribute, DomTree, Node, NodeData, NodeId};+pub use tree::{+ AttachShadowError, Attribute, DomTree, Node, NodeData, NodeId, ShadowRoot,+ ShadowRootMode,+};pub use tree_sink::{parse_fragment, parse_fragment_with_context, parse_html};diff --git a/crates/obscura-dom/src/tree.rs b/crates/obscura-dom/src/tree.rsindex f70958dd..288068b7 100644--- a/crates/obscura-dom/src/tree.rs+++ b/crates/obscura-dom/src/tree.rs@@ -1,6 +1,6 @@use html5ever::{LocalName, Namespace, Prefix, QualName};use std::cell::RefCell;-use std::collections::HashMap;+use std::collections::{HashMap, HashSet};use std::fmt;#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]@@ -26,6 +26,45 @@ impl fmt::Display for NodeId {}}+/// The encapsulation mode recorded by a native shadow root.+#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]+pub enum ShadowRootMode {+ Open,+ Closed,+}++/// Stable metadata for a shadow-root node in this tree.+///+/// A shadow root owns an ordinary child list, but is not an ordinary child of+/// its host. The separate host edge keeps `parentNode`-style walks scoped to+/// one tree while still allowing composed-tree operations to cross explicitly.+#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]+pub struct ShadowRoot {+ pub id: NodeId,+ pub host: NodeId,+ pub mode: ShadowRootMode,+}++#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub enum AttachShadowError {+ HostIsNotElement,+ HostAlreadyHasShadowRoot,+ InvalidShadowRoot,+}++impl fmt::Display for AttachShadowError {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ let message = match self {+ Self::HostIsNotElement => "shadow host is not an element",+ Self::HostAlreadyHasShadowRoot => "shadow host already has a shadow root",+ Self::InvalidShadowRoot => "shadow root is not a detached fragment node",+ };+ f.write_str(message)+ }+}++impl std::error::Error for AttachShadowError {}+#[derive(Clone, Debug, PartialEq, Eq)]pub struct Attribute {pub name: QualName,@@ -211,6 +250,11 @@ pub(crate) struct DomTreeInner {pub(crate) free_list: Vec<u32>,pub(crate) document: NodeId,pub(crate) id_index: HashMap<String, NodeId>,+ /// Shadow roots are arena nodes with their own child list. They are kept+ /// outside the ordinary parent links so light-tree traversal never crosses+ /// into a shadow tree by accident.+ shadow_roots: HashMap<NodeId, ShadowRoot>,+ shadow_roots_by_host: HashMap<NodeId, NodeId>,// Whether the document was parsed in (full) quirks mode. In quirks mode CSS// class and id selectors match ASCII-case-insensitively.pub(crate) quirks: bool,@@ -233,6 +277,8 @@ impl DomTree {free_list: Vec::new(),document: NodeId(0),id_index: HashMap::new(),+ shadow_roots: HashMap::new(),+ shadow_roots_by_host: HashMap::new(),quirks: false,}),}@@ -257,6 +303,188 @@ impl DomTree {self.inner.borrow()}+ /// Create and attach a dormant native shadow-root node to `host`.+ ///+ /// This only creates the tree-scope relationship. Parser attachment,+ /// selector scoping, slot assignment, style, layout, and paint remain+ /// separate opt-in layers.+ pub fn attach_shadow_root(+ &self,+ host: NodeId,+ mode: ShadowRootMode,+ ) -> Result<NodeId, AttachShadowError> {+ {+ let inner = self.inner.borrow();+ let host_is_element = inner+ .nodes+ .get(host.index())+ .and_then(|node| node.as_ref())+ .is_some_and(Node::is_element);+ if !host_is_element {+ return Err(AttachShadowError::HostIsNotElement);+ }+ if inner.shadow_roots_by_host.contains_key(&host) {+ return Err(AttachShadowError::HostAlreadyHasShadowRoot);+ }+ }++ let root = self.new_node(NodeData::Document);+ // A freshly allocated document-fragment backing node satisfies every+ // invariant below. If this ever fails, remove it so the arena does not+ // retain an unreachable allocation.+ if let Err(error) = self.attach_shadow_root_node(host, root, mode) {+ self.remove(root);+ return Err(error);+ }+ Ok(root)+ }++ /// Attach an existing detached fragment node as a shadow root. html5ever's+ /// declarative-shadow hook supplies the template-contents fragment through+ /// this path, but the hook remains disabled until style/layout integration+ /// is ready.+ pub(crate) fn attach_shadow_root_node(+ &self,+ host: NodeId,+ root: NodeId,+ mode: ShadowRootMode,+ ) -> Result<(), AttachShadowError> {+ let mut inner = self.inner.borrow_mut();+ let host_is_element = inner+ .nodes+ .get(host.index())+ .and_then(|node| node.as_ref())+ .is_some_and(Node::is_element);+ if !host_is_element {+ return Err(AttachShadowError::HostIsNotElement);+ }+ if inner.shadow_roots_by_host.contains_key(&host) {+ return Err(AttachShadowError::HostAlreadyHasShadowRoot);+ }++ let valid_root = root != inner.document+ && !inner.shadow_roots.contains_key(&root)+ && inner+ .nodes+ .get(root.index())+ .and_then(|node| node.as_ref())+ .is_some_and(|node| {+ matches!(node.data, NodeData::Document)+ && node.parent.is_none()+ && node.prev_sibling.is_none()+ && node.next_sibling.is_none()+ });+ if !valid_root {+ return Err(AttachShadowError::InvalidShadowRoot);+ }++ let info = ShadowRoot {+ id: root,+ host,+ mode,+ };+ inner.shadow_roots.insert(root, info);+ inner.shadow_roots_by_host.insert(host, root);+ Ok(())+ }++ /// Return the native root hosted by `host`, including closed roots. Web API+ /// visibility is intentionally left to the caller.+ pub fn shadow_root(&self, host: NodeId) -> Option<NodeId> {+ self.inner.borrow().shadow_roots_by_host.get(&host).copied()+ }++ pub fn shadow_root_info(&self, root: NodeId) -> Option<ShadowRoot> {+ self.inner.borrow().shadow_roots.get(&root).copied()+ }++ pub fn is_shadow_root(&self, node: NodeId) -> bool {+ self.inner.borrow().shadow_roots.contains_key(&node)+ }++ /// Return the root of `node`'s local tree scope. This follows ordinary+ /// parent links only, so a shadow descendant resolves to its ShadowRoot and+ /// a light descendant resolves to its document or detached subtree root.+ pub fn tree_scope_root(&self, node: NodeId) -> Option<NodeId> {+ let inner = self.inner.borrow();+ let mut current = node;+ for _ in 0..=inner.nodes.len() {+ let current_node = inner.nodes.get(current.index())?.as_ref()?;+ match current_node.parent {+ Some(parent) => current = parent,+ None => return Some(current),+ }+ }+ None+ }++ pub fn containing_shadow_root(&self, node: NodeId) -> Option<NodeId> {+ let root = self.tree_scope_root(node)?;+ self.is_shadow_root(root).then_some(root)+ }++ /// Return the topmost root after crossing ShadowRoot-to-host edges. This is+ /// the native counterpart of `getRootNode({ composed: true })`.+ pub fn shadow_including_root(&self, node: NodeId) -> Option<NodeId> {+ let inner = self.inner.borrow();+ let mut current = node;+ for _ in 0..=inner.nodes.len() {+ let current_node = inner.nodes.get(current.index())?.as_ref()?;+ if let Some(parent) = current_node.parent {+ current = parent;+ } else if let Some(root) = inner.shadow_roots.get(¤t) {+ current = root.host;+ } else {+ return Some(current);+ }+ }+ None+ }++ fn host_including_parent(inner: &DomTreeInner, node: NodeId) -> Option<NodeId> {+ inner+ .nodes+ .get(node.index())+ .and_then(|entry| entry.as_ref())+ .and_then(|entry| entry.parent)+ .or_else(|| inner.shadow_roots.get(&node).map(|root| root.host))+ }++ /// DOM insertion rejects a node when it is a host-including inclusive+ /// ancestor of the destination parent. Ordinary parent links are not enough+ /// for this check because a ShadowRoot's parent is intentionally null.+ fn would_create_host_including_cycle(+ inner: &DomTreeInner,+ parent: NodeId,+ child: NodeId,+ ) -> bool {+ let child_can_be_ancestor = inner+ .nodes+ .get(child.index())+ .and_then(|entry| entry.as_ref())+ .is_some_and(|entry| entry.first_child.is_some())+ || inner.shadow_roots_by_host.contains_key(&child);+ if !child_can_be_ancestor {+ return false;+ }++ let mut current = Some(parent);+ for _ in 0..=inner.nodes.len() {+ let node = match current {+ Some(node) => node,+ None => return false,+ };+ if node == child {+ return true;+ }+ current = Self::host_including_parent(inner, node);+ }++ // A valid host-including chain cannot be longer than the arena. Refuse+ // mutation if pre-existing corruption ever violates that invariant.+ true+ }+pub fn new_node(&self, data: NodeData) -> NodeId {let mut inner = self.inner.borrow_mut();let id = if let Some(slot) = inner.free_list.pop() {@@ -317,38 +545,25 @@ impl DomTree {if parent_id == child_id {return;}- // Appending an ancestor of the parent under that parent makes the- // parent/child graph cyclic, and every later descendants()/children()/- // textContent walk (none carry a visited set) would loop forever, pinning- // the thread in native Rust where neither tokio nor the V8 watchdog can- // interrupt it. Per the DOM spec this is a HierarchyRequestError; treat it- // as a no-op, like the self-append guard above. Only a node that already- // has children can be an ancestor, so a fresh/leaf child (the common- // append) skips the walk: O(1) hot path, O(depth) only when relocating a- // populated subtree.+ // A ShadowRoot is never itself an ordinary child, and moving a host+ // below its own root would create a cycle even though the root's parent+ // pointer is null. Follow both ordinary parents and root-to-host edges.{let inner = self.inner.borrow();- let child_has_children = inner.nodes.get(child_id.index())- .and_then(|n| n.as_ref())- .map(|n| n.first_child.is_some())- .unwrap_or(false);- if child_has_children {- let mut cur = inner.nodes.get(parent_id.index())- .and_then(|n| n.as_ref())- .and_then(|n| n.parent);- let mut steps = 0usize;- while let Some(p) = cur {- if p == child_id {- return;- }- steps += 1;- if steps > inner.nodes.len() {- return; // pre-existing corruption: refuse rather than risk a cycle- }- cur = inner.nodes.get(p.index())- .and_then(|n| n.as_ref())- .and_then(|n| n.parent);- }+ let parent_exists = inner+ .nodes+ .get(parent_id.index())+ .is_some_and(Option::is_some);+ let child_exists = inner+ .nodes+ .get(child_id.index())+ .is_some_and(Option::is_some);+ if !parent_exists+ || !child_exists+ || inner.shadow_roots.contains_key(&child_id)+ || Self::would_create_host_including_cycle(&inner, parent_id, child_id)+ {+ return;}}self.detach(child_id);@@ -397,32 +612,20 @@ impl DomTree {}};- // Inserting the parent itself, or any ancestor of the parent, as a child- // of that parent would create a cycle (same non-terminating-walk hang as- // append_child). Reject it, matching the self-insert guard above. Gate on- // the inserted node actually having children, so the common case (insert a- // fresh node) stays O(1).+ // Apply the same host-including cycle and root-node constraints as+ // append_child. A leaf host still needs this check because its hosted+ // root is not present in the ordinary child list.{let inner = self.inner.borrow();- let new_has_children = inner.nodes.get(new_sibling_id.index())- .and_then(|n| n.as_ref())- .map(|n| n.first_child.is_some())- .unwrap_or(false);- if new_has_children {- let mut cur = Some(parent_id);- let mut steps = 0usize;- while let Some(p) = cur {- if p == new_sibling_id {- return;- }- steps += 1;- if steps > inner.nodes.len() {- return;- }- cur = inner.nodes.get(p.index())- .and_then(|n| n.as_ref())- .and_then(|n| n.parent);- }+ let new_exists = inner+ .nodes+ .get(new_sibling_id.index())+ .is_some_and(Option::is_some);+ if !new_exists+ || inner.shadow_roots.contains_key(&new_sibling_id)+ || Self::would_create_host_including_cycle(&inner, parent_id, new_sibling_id)+ {+ return;}}@@ -530,42 +733,100 @@ impl DomTree {}pub fn remove(&self, node_id: NodeId) {+ let nodes_to_remove = self.inclusive_owned_subtrees(node_id);+ if nodes_to_remove.is_empty() {+ return;+ }self.detach(node_id);- let descendants = self.descendants(node_id);let mut inner = self.inner.borrow_mut();let mut ids_to_remove = Vec::new();- for &desc_id in &descendants {- if let Some(Some(node)) = inner.nodes.get(desc_id.index()) {+ for &id in &nodes_to_remove {+ if let Some(Some(node)) = inner.nodes.get(id.index()) {if let Some(id_val) = node.get_attribute("id") {ids_to_remove.push(id_val.to_string());}}}- if let Some(Some(node)) = inner.nodes.get(node_id.index()) {- if let Some(id_val) = node.get_attribute("id") {- ids_to_remove.push(id_val.to_string());- }- }for id_str in ids_to_remove {inner.id_index.remove(&id_str);}+ // Remove both directions before freeing any arena slot. Otherwise a+ // reused NodeId could inherit an old host/root relationship.+ for &id in &nodes_to_remove {+ if let Some(root) = inner.shadow_roots.remove(&id) {+ if inner.shadow_roots_by_host.get(&root.host) == Some(&id) {+ inner.shadow_roots_by_host.remove(&root.host);+ }+ }+ if let Some(root_id) = inner.shadow_roots_by_host.remove(&id) {+ inner.shadow_roots.remove(&root_id);+ }+ }+// Only free slots that are currently live. Freeing an out-of-range id// would panic on direct indexing, and freeing an already-freed slot// would push it onto the free list a second time — later handing the// same NodeId to two live nodes (aliasing).- for desc_id in descendants {- if matches!(inner.nodes.get(desc_id.index()), Some(Some(_))) {- inner.nodes[desc_id.index()] = None;- inner.free_list.push(desc_id.0);+ for id in nodes_to_remove {+ if matches!(inner.nodes.get(id.index()), Some(Some(_))) {+ inner.nodes[id.index()] = None;+ inner.free_list.push(id.0);}}- if matches!(inner.nodes.get(node_id.index()), Some(Some(_))) {- inner.nodes[node_id.index()] = None;- inner.free_list.push(node_id.0);+ }++ /// Collect an ordinary subtree plus every shadow tree owned by a host in+ /// that subtree. This is used only by the arena-freeing path; normal DOM+ /// traversal must remain tree-scoped and therefore never follows host edges.+ fn inclusive_owned_subtrees(&self, node_id: NodeId) -> Vec<NodeId> {+ let inner = self.inner.borrow();+ if !inner+ .nodes+ .get(node_id.index())+ .is_some_and(Option::is_some)+ {+ return Vec::new();+ }++ let mut result = Vec::new();+ let mut seen = HashSet::new();+ let mut stack = vec![node_id];+ while let Some(current) = stack.pop() {+ if !seen.insert(current) {+ continue;+ }+ let Some(node) = inner+ .nodes+ .get(current.index())+ .and_then(|entry| entry.as_ref())+ else {+ continue;+ };+ result.push(current);++ if let Some(root) = inner.shadow_roots_by_host.get(¤t) {+ stack.push(*root);+ }++ let mut children = Vec::new();+ let mut child = node.first_child;+ while let Some(child_id) = child {+ children.push(child_id);+ if children.len() > inner.nodes.len() {+ break;+ }+ child = inner+ .nodes+ .get(child_id.index())+ .and_then(|entry| entry.as_ref())+ .and_then(|entry| entry.next_sibling);+ }+ stack.extend(children.into_iter().rev());}+ result}pub fn children(&self, node_id: NodeId) -> Vec<NodeId> {@@ -583,6 +844,13 @@ impl DomTree {result}+ /// Snapshot the direct children of `host`'s shadow root. Ordinary+ /// `children(host)` continues to return only light children.+ pub fn shadow_children(&self, host: NodeId) -> Option<Vec<NodeId>> {+ let root = self.shadow_root(host)?;+ Some(self.children(root))+ }+pub fn descendants(&self, node_id: NodeId) -> Vec<NodeId> {let inner = self.inner.borrow();let mut result = Vec::new();@@ -644,6 +912,13 @@ impl DomTree {result}+ /// Snapshot the descendants of `host`'s shadow tree without including the+ /// ShadowRoot node itself.+ pub fn shadow_descendants(&self, host: NodeId) -> Option<Vec<NodeId>> {+ let root = self.shadow_root(host)?;+ Some(self.descendants(root))+ }+/// Returns the node after `current` in document order, without leaving the/// subtree rooted at `root`.///@@ -780,7 +1055,20 @@ impl DomTree {}pub fn get_element_by_id(&self, id: &str) -> Option<NodeId> {- self.inner.borrow().id_index.get(id).copied()+ let indexed = self.inner.borrow().id_index.get(id).copied();+ if indexed.is_some_and(|node| self.containing_shadow_root(node).is_none()) {+ return indexed;+ }++ // Creation happens before insertion, so the O(1) best-effort index can+ // point at a shadow descendant. Never expose that node through+ // document.getElementById; recover the first matching light-tree+ // element in document order instead. Detached and template-content+ // nodes retain the legacy best-effort lookup behavior used internally.+ self.descendants(self.document()).into_iter().find(|node_id| {+ self.with_node(*node_id, |node| node.get_attribute("id") == Some(id))+ .unwrap_or(false)+ })}pub fn text_content(&self, node_id: NodeId) -> String {@@ -881,6 +1169,12 @@ impl DomTree {/// element type and namespace. Template contents are stored in a separate/// document node and therefore need their own remapped clone.pub fn clone_node(&self, source_node_id: NodeId, deep: bool) -> Option<NodeId> {+ // DOM cloneNode is not defined for ShadowRoot nodes. A host clone keeps+ // its light subtree only; the separate registry means the shadow root+ // is naturally omitted from that traversal.+ if self.is_shadow_root(source_node_id) {+ return None;+ }let source_data = self.get_node(source_node_id)?.data;let cloned_root = self.new_node(source_data);let mut stack = Vec::new();@@ -1080,6 +1374,27 @@ impl Default for DomTree {mod tests {use super::*;+ fn element(tree: &DomTree, local: &str) -> NodeId {+ tree.new_node(NodeData::Element {+ name: QualName::new(None, ns!(html), LocalName::from(local)),+ attrs: vec![],+ template_contents: None,+ mathml_annotation_xml_integration_point: false,+ })+ }++ fn element_with_id(tree: &DomTree, local: &str, id: &str) -> NodeId {+ tree.new_node(NodeData::Element {+ name: QualName::new(None, ns!(html), LocalName::from(local)),+ attrs: vec![Attribute {+ name: QualName::new(None, Namespace::default(), LocalName::from("id")),+ value: id.into(),+ }],+ template_contents: None,+ mathml_annotation_xml_integration_point: false,+ })+ }+#[test]fn test_new_tree_has_document() {let tree = DomTree::new();@@ -1112,6 +1427,152 @@ mod tests {assert_ne!(x, y, "double-free aliased two live nodes onto the same slot");}+ #[test]+ fn native_shadow_root_keeps_light_and_shadow_tree_scopes_separate() {+ let tree = DomTree::new();+ let document = tree.document();+ let host = element(&tree, "x-card");+ let light = element(&tree, "span");+ tree.append_child(document, host);+ tree.append_child(host, light);++ let root = tree+ .attach_shadow_root(host, ShadowRootMode::Closed)+ .expect("element can host one shadow root");+ let shadow = element(&tree, "button");+ tree.append_child(root, shadow);++ assert_eq!(+ tree.shadow_root_info(root),+ Some(ShadowRoot {+ id: root,+ host,+ mode: ShadowRootMode::Closed,+ })+ );+ assert!(tree.is_shadow_root(root));+ assert_eq!(tree.shadow_root(host), Some(root));+ assert_eq!(tree.get_node(root).unwrap().parent, None);+ assert_eq!(tree.children(host), vec![light]);+ assert_eq!(tree.shadow_children(host), Some(vec![shadow]));+ assert_eq!(tree.shadow_descendants(host), Some(vec![shadow]));++ let document_nodes = tree.descendants(document);+ assert!(!document_nodes.contains(&root));+ assert!(!document_nodes.contains(&shadow));+ assert_eq!(tree.tree_scope_root(light), Some(document));+ assert_eq!(tree.tree_scope_root(root), Some(root));+ assert_eq!(tree.tree_scope_root(shadow), Some(root));+ assert_eq!(tree.containing_shadow_root(light), None);+ assert_eq!(tree.containing_shadow_root(shadow), Some(root));+ assert_eq!(tree.shadow_including_root(shadow), Some(document));+ assert_eq!(+ tree.attach_shadow_root(host, ShadowRootMode::Open),+ Err(AttachShadowError::HostAlreadyHasShadowRoot)+ );+ }++ #[test]+ fn document_id_lookup_never_exposes_a_shadow_descendant() {+ let tree = DomTree::new();+ let document = tree.document();+ let host = element(&tree, "x-card");+ tree.append_child(document, host);+ let root = tree+ .attach_shadow_root(host, ShadowRootMode::Open)+ .unwrap();++ // The shadow element is created first, so it owns the best-effort+ // global id-index entry. Public document lookup still has to recover+ // the light-tree match rather than leak across the tree scope.+ let shadow_match = element_with_id(&tree, "span", "shared");+ tree.append_child(root, shadow_match);+ let light_match = element_with_id(&tree, "span", "shared");+ tree.append_child(host, light_match);++ assert_eq!(tree.get_element_by_id("shared"), Some(light_match));+ assert_eq!(+ tree.query_selector_from(document, "#shared").unwrap(),+ Some(light_match)+ );+ assert_eq!(+ tree.query_selector_from(root, "#shared").unwrap(),+ Some(shadow_match)+ );+ }++ #[test]+ fn shadow_host_edges_participate_in_cycle_rejection() {+ let tree = DomTree::new();+ let document = tree.document();+ let host = element(&tree, "x-card");+ tree.append_child(document, host);+ let root = tree+ .attach_shadow_root(host, ShadowRootMode::Open)+ .unwrap();+ let shadow_child = element(&tree, "span");+ tree.append_child(root, shadow_child);++ // Root nodes cannot become ordinary children.+ tree.append_child(host, root);+ assert_eq!(tree.get_node(root).unwrap().parent, None);+ assert!(tree.children(host).is_empty());++ // A host is a host-including ancestor of every node in its shadow+ // tree, even when it has no light children.+ tree.append_child(root, host);+ tree.insert_before(shadow_child, host);+ assert_eq!(tree.get_node(host).unwrap().parent, Some(document));+ assert_eq!(tree.children(root), vec![shadow_child]);+ }++ #[test]+ fn freeing_a_host_reclaims_shadow_nodes_and_registry_entries() {+ let tree = DomTree::new();+ let host = element(&tree, "x-card");+ tree.append_child(tree.document(), host);+ let root = tree+ .attach_shadow_root(host, ShadowRootMode::Open)+ .unwrap();+ let shadow_host = element(&tree, "nested-card");+ tree.append_child(root, shadow_host);+ let nested_root = tree+ .attach_shadow_root(shadow_host, ShadowRootMode::Closed)+ .unwrap();+ let nested_child = element(&tree, "span");+ tree.append_child(nested_root, nested_child);++ assert_eq!(tree.len(), 6);+ tree.remove(host);+ assert_eq!(tree.len(), 1);+ for removed in [host, root, shadow_host, nested_root, nested_child] {+ assert!(tree.get_node(removed).is_none());+ assert!(!tree.is_shadow_root(removed));+ }++ // Reusing freed slots must not resurrect either registry direction.+ let replacement = element(&tree, "div");+ assert_eq!(tree.shadow_root(replacement), None);+ assert_eq!(tree.shadow_root_info(replacement), None);+ }++ #[test]+ fn cloning_a_host_omits_its_shadow_tree_and_a_root_is_not_clonable() {+ let tree = DomTree::new();+ let host = element(&tree, "x-card");+ let light = element(&tree, "span");+ tree.append_child(host, light);+ let root = tree+ .attach_shadow_root(host, ShadowRootMode::Open)+ .unwrap();+ tree.append_child(root, element(&tree, "button"));++ assert_eq!(tree.clone_node(root, true), None);+ let clone = tree.clone_node(host, true).expect("host itself is clonable");+ assert_eq!(tree.shadow_root(clone), None);+ assert_eq!(tree.children(clone).len(), 1);+ }+#[test]fn test_append_child() {let tree = DomTree::new();diff --git a/crates/obscura-dom/src/tree_sink.rs b/crates/obscura-dom/src/tree_sink.rsindex 4cf082a5..74c0e154 100644--- a/crates/obscura-dom/src/tree_sink.rs+++ b/crates/obscura-dom/src/tree_sink.rs@@ -6,7 +6,7 @@ use html5ever::tendril::StrTendril;use html5ever::tree_builder::{ElemName, ElementFlags, NodeOrText, QuirksMode, TreeSink};use html5ever::{Attribute as HtmlAttribute, LocalName, Namespace, QualName};-use crate::tree::{Attribute, DomTree, NodeData, NodeId};+use crate::tree::{Attribute, DomTree, NodeData, NodeId, ShadowRootMode};pub struct ObscuraElemName<'a> {_ref: Ref<'a, ()>,@@ -232,13 +232,51 @@ impl TreeSink for DomTree {// then appends its contents directly to the host. That leaks shadow// styles and markup into the light DOM.//- // Keep declarative shadow roots as ordinary inert templates until- // DomTree has a real shadow-root node, tree-scoped selectors, and a- // scoped stylesheet cascade. Returning false makes html5ever take its- // fully implemented ordinary-template path.+ // DomTree now has dormant native host/root identity, but selector,+ // style, layout, and paint integration are not enabled as one partial+ // feature. Returning false keeps html5ever on its fully implemented+ // ordinary-template path in the meantime.false}+ fn attach_declarative_shadow(+ &self,+ location: &NodeId,+ template: &NodeId,+ attrs: &[HtmlAttribute],+ ) -> bool {+ // This hook is intentionally dormant while+ // allow_declarative_shadow_roots returns false. Keep the implementation+ // ready for that future switch: html5ever creates an unattached+ // <template> and routes subsequent tokens into its contents fragment,+ // so that existing fragment is the native ShadowRoot identity.+ let mode = attrs.iter().find_map(|attr| {+ if attr.name.local.as_ref() != "shadowrootmode" {+ return None;+ }+ match attr.value.as_ref() {+ "open" => Some(ShadowRootMode::Open),+ "closed" => Some(ShadowRootMode::Closed),+ _ => None,+ }+ });+ let Some(mode) = mode else {+ return false;+ };+ let root = self+ .with_node(*template, |node| match &node.data {+ NodeData::Element {+ template_contents, ..+ } => *template_contents,+ _ => None,+ })+ .flatten();+ let Some(root) = root else {+ return false;+ };+ self.attach_shadow_root_node(*location, root, mode).is_ok()+ }+fn is_mathml_annotation_xml_integration_point(&self, target: &NodeId) -> bool {self.with_node(*target, |n| match &n.data {NodeData::Element { mathml_annotation_xml_integration_point, .. } => {@@ -395,6 +433,11 @@ mod tests {vec![template, light_content],"shadow markup must not be spliced into the host's light children");+ assert_eq!(+ tree.shadow_root(host),+ None,+ "the parser gate must remain disabled until scoped style/layout is ready"+ );assert!(tree.children(template).is_empty(),"template nodes keep their markup in the separate contents document"@@ -437,4 +480,44 @@ mod tests {assert_eq!(element_children(contents), vec![inside]);assert_eq!(tree.get_node(inside).unwrap().parent, Some(contents));}++ #[test]+ fn dormant_tree_sink_hook_reuses_the_template_contents_identity() {+ let tree = DomTree::new();+ let host = tree.new_node(NodeData::Element {+ name: QualName::new(None, ns!(html), LocalName::from("x-card")),+ attrs: vec![],+ template_contents: None,+ mathml_annotation_xml_integration_point: false,+ });+ tree.append_child(tree.document(), host);+ let contents = tree.new_node(NodeData::Document);+ let template = tree.new_node(NodeData::Element {+ name: QualName::new(None, ns!(html), local_name!("template")),+ attrs: vec![],+ template_contents: Some(contents),+ mathml_annotation_xml_integration_point: false,+ });+ let attrs = vec![HtmlAttribute {+ name: QualName::new(+ None,+ Namespace::default(),+ LocalName::from("shadowrootmode"),+ ),+ value: StrTendril::from("closed"),+ }];++ assert!(!TreeSink::allow_declarative_shadow_roots(&tree, &host));+ assert!(TreeSink::attach_declarative_shadow(+ &tree,+ &host,+ &template,+ &attrs,+ ));+ assert_eq!(tree.shadow_root(host), Some(contents));+ assert_eq!(+ tree.shadow_root_info(contents).unwrap().mode,+ ShadowRootMode::Closed+ );+ }}diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.jsindex 60f27144..67ed9772 100644--- a/crates/obscura-js/js/bootstrap.js+++ b/crates/obscura-js/js/bootstrap.js@@ -33,7 +33,7 @@'_bodyToUint8Array', '_arrayBufferFromBytes','_installWasmStreamingFallback', '_urlParseOp', '_urlSetOp','_urlResolveOp', '_decodeBodyWithCharset', '_utf8DecodeBytes',- '_selectionFor', '_isConstructorCE', '_isValidCustomElementName',+ '_selectionFor', '_isConstructorCE', '_isValidCustomElementName', '_shadowRootForHost','_blobPartToBytes', '_bytesToBinaryString', '_formEncode', '_hexv','_commonFonts', '_isXMLDocument', '_isValidPITarget', '_isHTMLEl','_nodeList', '_rngNodeLength', '_rngNodeIndex', '_rngSame', '_rngRoot',@@ -7310,7 +7310,82 @@ globalThis.__notifyMutation = function(type, target_nid, addedNodes, removedNode}};-globalThis.ShadowRoot = class ShadowRoot extends DocumentFragment {};+globalThis.ShadowRoot = class ShadowRoot extends DocumentFragment {+ constructor(nid, host, options) {+ super(nid);+ this._host = host;+ this._mode = options.mode;+ this._delegatesFocus = !!options.delegatesFocus;+ this._slotAssignment = options.slotAssignment === 'manual' ? 'manual' : 'named';+ this._clonable = !!options.clonable;+ this._serializable = !!options.serializable;+ }+ get host() { return this._host; }+ get mode() { return this._mode; }+ get delegatesFocus() { return this._delegatesFocus; }+ get slotAssignment() { return this._slotAssignment; }+ get clonable() { return this._clonable; }+ get serializable() { return this._serializable; }+ _assertInsertable(node, operation) {+ const createsComposedCycle = node instanceof ShadowRoot+ || node === this._host+ || !!(node?.contains && node.contains(this._host));+ if (createsComposedCycle) {+ throw new DOMException(+ `Failed to execute '${operation}' on 'Node': The new child would contain the parent.`,+ 'HierarchyRequestError'+ );+ }+ }+ appendChild(child) {+ this._assertInsertable(child, 'appendChild');+ return super.appendChild(child);+ }+ insertBefore(node, reference) {+ if (reference && reference.parentNode !== this) {+ throw new DOMException(+ "Failed to execute 'insertBefore' on 'Node': The reference node is not a child of this node.",+ 'NotFoundError'+ );+ }+ if (node === reference) return node;+ this._assertInsertable(node, 'insertBefore');+ return super.insertBefore(node, reference);+ }+ removeChild(child) {+ if (!child || child.parentNode !== this) {+ throw new DOMException(+ "Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.",+ 'NotFoundError'+ );+ }+ return super.removeChild(child);+ }+ replaceChild(node, oldChild) {+ if (!oldChild || oldChild.parentNode !== this) {+ throw new DOMException(+ "Failed to execute 'replaceChild' on 'Node': The node to be replaced is not a child of this node.",+ 'NotFoundError'+ );+ }+ if (node === oldChild) return oldChild;+ this._assertInsertable(node, 'replaceChild');+ return super.replaceChild(node, oldChild);+ }+ getRootNode(options) {+ return options?.composed ? this._host.getRootNode(options) : this;+ }+ get activeElement() { return null; }+ get styleSheets() { return []; }+ cloneNode() {+ throw new DOMException(+ 'Failed to execute cloneNode on Node: ShadowRoot nodes are not clonable.',+ 'NotSupportedError'+ );+ }+ setHTMLUnsafe(value) { this.innerHTML = String(value == null ? '' : value); }+ getHTML() { return this.innerHTML; }+};// Constructible-stylesheet adoption, mirroring Document.adoptedStyleSheets.Object.defineProperty(globalThis.ShadowRoot.prototype, 'adoptedStyleSheets', {get() { return this._adoptedStyleSheets || []; },@@ -7430,7 +7505,7 @@ globalThis.ElementInternals = class ElementInternals {get willValidate() { return true; }get form() { return this._el && this._el.closest ? this._el.closest('form') : null; }get labels() { return _nodeList([]); }- get shadowRoot() { return (this._el && this._el._shadowRoot) || null; }+ get shadowRoot() { return this._el ? _shadowRootForHost(this._el, true) : null; }get states() { return this._states; }};// Full standard constant set (issue #439). The partial version here lacked@@ -10544,171 +10619,40 @@ Element.prototype.attachShadow = function attachShadow(opts) {if (!globalThis.__obscura_shadowHostNames.has(_ln) && _ln.indexOf('-') === -1) {throw new DOMException('Failed to execute attachShadow on Element: this element does not support attachShadow', 'NotSupportedError');}- if (this._shadowRoot) {+ if (Deno.core.ops.op_shadow_root_info(this._nid)) {throw new DOMException('Failed to execute attachShadow on Element: the element already hosts a shadow tree.', 'NotSupportedError');}- const host = this;- const children = [];- const assertInsertable = (node, operation) => {- const createsComposedCycle = node instanceof ShadowRoot- || node === host- || !!(node?.contains && node.contains(host));- if (createsComposedCycle) {- throw new DOMException(- `Failed to execute '${operation}' on 'Node': The new child would contain the parent.`,- "HierarchyRequestError"- );- }- };- const shadow = {- mode: opts.mode,- host: host,- get innerHTML() { return children.map(c => c.outerHTML || c.textContent || '').join(''); },- set innerHTML(v) {- while (children.length) shadow.removeChild(children[children.length - 1]);- if (v) {- const tmp = document.createElement('div');- tmp.innerHTML = v;- for (const child of Array.from(tmp.childNodes)) shadow.appendChild(child);- }- },- get childNodes() { return children; },- get firstChild() { return children[0] || null; },- get lastChild() { return children[children.length - 1] || null; },- get firstElementChild() { return children.find(c => c.nodeType === 1) || null; },- get children() { return children.filter(c => c.nodeType === 1); },- appendChild(c) {- if (!c) return c;- assertInsertable(c, "appendChild");- if (c instanceof DocumentFragment) {- for (const child of Array.from(c.childNodes)) shadow.appendChild(child);- return c;- }- if (c._shadowParent) c._shadowParent.removeChild(c);- else if (c.parentNode) c.parentNode.removeChild(c);- children.push(c);- c._shadowParent = shadow;- return c;- },- insertBefore(n, ref) {- if (!n) return n;- if (!ref) { shadow.appendChild(n); return n; }- if (!children.includes(ref)) {- throw new DOMException(- "Failed to execute 'insertBefore' on 'Node': The reference node is not a child of this node.",- "NotFoundError"- );- }- if (n === ref) return n;- assertInsertable(n, "insertBefore");- if (n instanceof DocumentFragment) {- for (const child of Array.from(n.childNodes)) shadow.insertBefore(child, ref);- return n;- }- if (n._shadowParent) n._shadowParent.removeChild(n);- else if (n.parentNode) n.parentNode.removeChild(n);- const idx = children.indexOf(ref);- children.splice(idx, 0, n);- n._shadowParent = shadow;- return n;- },- removeChild(c) {- const idx = children.indexOf(c);- if (idx < 0) {- throw new DOMException(- "Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.",- "NotFoundError"- );- }- children.splice(idx, 1);- if (c._shadowParent === shadow) c._shadowParent = null;- return c;- },- replaceChild(n, o) {- if (!children.includes(o)) {- throw new DOMException(- "Failed to execute 'replaceChild' on 'Node': The node to be replaced is not a child of this node.",- "NotFoundError"- );- }- if (n === o) return o;- assertInsertable(n, "replaceChild");- if (n instanceof DocumentFragment) {- for (const child of Array.from(n.childNodes)) shadow.insertBefore(child, o);- shadow.removeChild(o);- return o;- }- if (n._shadowParent) n._shadowParent.removeChild(n);- else if (n.parentNode) n.parentNode.removeChild(n);- const idx = children.indexOf(o);- children[idx] = n;- n._shadowParent = shadow;- if (o._shadowParent === shadow) o._shadowParent = null;- return o;- },- querySelector(s) {- for (const c of children) {- if (c.matches && c.matches(s)) return c;- if (c.querySelector) { const r = c.querySelector(s); if (r) return r; }- }- return null;- },- querySelectorAll(s) {- const results = [];- for (const c of children) {- if (c.matches && c.matches(s)) results.push(c);- if (c.querySelectorAll) results.push(...c.querySelectorAll(s));- }- return results;- },- getElementById(id) { return shadow.querySelector('#' + id); },- contains(n) {- return n === shadow || children.some(- child => child === n || (child.contains && child.contains(n))- );- },- getRootNode(options) {- return options?.composed ? host.getRootNode(options) : shadow;- },- get ownerDocument() { return document; },- get nodeType() { return 11; }, // DOCUMENT_FRAGMENT_NODE- get nodeName() { return '#document-fragment'; },- addEventListener() {}, removeEventListener() {}, dispatchEvent() { return true; },- setHTMLUnsafe(v) { this.innerHTML = String(v == null ? "" : v); },- getHTML() { return this.innerHTML; },- // Own textContent: ShadowRoot now extends DocumentFragment, so without- // these the inherited Node accessors run against this._nid. The setter in- // particular would target the host document and wipe it. Operate on the- // shadow's own `children` store instead.- get textContent() {- return children.map(c => c.nodeType === 8 ? "" : (c.textContent || "")).join("");- },- set textContent(v) {- while (children.length) shadow.removeChild(children[children.length - 1]);- if (v != null && v !== "") shadow.appendChild(document.createTextNode(String(v)));- },- hasChildNodes() { return children.length > 0; },- // A detached fragment id backs any inherited nid-based method we do not- // override, so they stay non-destructive (operate on an empty fragment)- // rather than falling through to node 0 / the document.- _nid: +_dom("create_document_fragment"),- activeElement: null,- get styleSheets() { return []; },- cloneNode() { throw new DOMException('Failed to execute cloneNode on Node: ShadowRoot nodes are not clonable.', 'NotSupportedError'); },- };- Object.setPrototypeOf(shadow, ShadowRoot.prototype);- this._shadowRoot = shadow;+ const rootNid = Deno.core.ops.op_shadow_attach(this._nid, _mode);+ if (rootNid < 0) {+ throw new DOMException('Failed to execute attachShadow on Element: this element does not support attachShadow', 'NotSupportedError');+ }+ const shadow = new ShadowRoot(rootNid, this, opts);+ _cache.set(rootNid, shadow);return shadow;};_markNative(Element.prototype.attachShadow);+function _shadowRootForHost(host, includeClosed) {+ if (!host) return null;+ const info = Deno.core.ops.op_shadow_root_info(host._nid);+ if (!info) return null;+ const parts = info.split('\0');+ if (!includeClosed && parts[1] !== 'open') return null;+ const rootNid = +parts[0];+ let root = _cache.get(rootNid);+ if (!(root instanceof ShadowRoot)) {+ root = new ShadowRoot(rootNid, host, { mode: parts[1] });+ _cache.set(rootNid, root);+ }+ return root;+}+Object.defineProperty(Element.prototype, 'shadowRoot', {configurable: true,enumerable: true,get: function () {- var sr = this._shadowRoot;- return sr && sr.mode === 'open' ? sr : null;+ return _shadowRootForHost(this, false);},});diff --git a/crates/obscura-js/src/ops.rs b/crates/obscura-js/src/ops.rsindex a730ae56..4e561402 100644--- a/crates/obscura-js/src/ops.rs+++ b/crates/obscura-js/src/ops.rs@@ -10,6 +10,7 @@ use deno_core::Extension;use deno_core::JsBuffer;use deno_core::OpState;use obscura_dom::{DomTree, NodeData, NodeId};+use obscura_dom::tree::{AttachShadowError, ShadowRootMode};#[cfg(feature = "render")]use obscura_net::{RequestCredentials, RequestMode, ResourceRequest};#[cfg(feature = "stealth")]@@ -699,6 +700,51 @@ fn op_script_try_start(state: &OpState, nid: u32) -> bool {newly_started}+/// Attach one native shadow-tree scope without making it part of the light+/// tree. Layout intentionally remains unaware of the detached root until+/// scoped style, slot assignment, and composed-tree paint are implemented.+#[op2(fast)]+fn op_shadow_attach(state: &OpState, host_nid: u32, #[string] mode: String) -> i32 {+ let mode = match mode.as_str() {+ "open" => ShadowRootMode::Open,+ "closed" => ShadowRootMode::Closed,+ _ => return -1,+ };+ let shared = state.borrow::<SharedState>().clone();+ let state = shared.borrow();+ let Some(dom) = state.dom.as_ref() else {+ return -1;+ };+ match dom.attach_shadow_root(NodeId::new(host_nid), mode) {+ Ok(root) => root.raw() as i32,+ Err(AttachShadowError::HostAlreadyHasShadowRoot) => -2,+ Err(_) => -1,+ }+}++/// Return native host-owned shadow identity as `root-id\0mode`. Closed roots+/// are included here; the Web-facing `Element.shadowRoot` getter applies mode+/// visibility in bootstrap.js.+#[op2]+#[string]+fn op_shadow_root_info(state: &OpState, host_nid: u32) -> String {+ let shared = state.borrow::<SharedState>().clone();+ let state = shared.borrow();+ let Some(dom) = state.dom.as_ref() else {+ return String::new();+ };+ dom.shadow_root(NodeId::new(host_nid))+ .and_then(|root| dom.shadow_root_info(root))+ .map(|shadow| {+ let mode = match shadow.mode {+ ShadowRootMode::Open => "open",+ ShadowRootMode::Closed => "closed",+ };+ format!("{}\0{mode}", shadow.id.raw())+ })+ .unwrap_or_default()+}+#[op2]#[string]fn op_dom(@@ -3327,6 +3373,8 @@ pub fn build_extension() -> Extension {op_dom(),op_script_mark_started(),op_script_try_start(),+ op_shadow_attach(),+ op_shadow_root_info(),op_console_msg(),op_fetch_url(),op_get_cookies(),diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex 20e426ca..b10ddaa6 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -3446,6 +3446,93 @@ mod tests {);}+ #[test]+ fn shadow_root_identity_and_children_are_native_tree_backed() {+ let mut rt = setup_runtime("<html><body></body></html>");+ let result = rt+ .evaluate(+ r##"+ const host = document.createElement("native-shadow-host");+ document.body.appendChild(host);+ const root = host.attachShadow({ mode: "open", delegatesFocus: true });+ root.innerHTML = "<section id='inside'><span>native</span></section>";+ const inside = root.querySelector("#inside");+ const records = [];+ const observer = new MutationObserver(batch => records.push(...batch));+ observer.observe(root, { childList: true, subtree: true });+ const added = document.createElement("strong");+ inside.appendChild(added);+ records.push(...observer.takeRecords());++ host._shadowRoot = { mode: "closed" };+ let duplicateError = "none";+ try { host.attachShadow({ mode: "open" }); }+ catch (error) { duplicateError = error.name; }++ class ClosedShadowHost extends HTMLElement {+ constructor() {+ super();+ this.closedRoot = this.attachShadow({ mode: "closed" });+ this.internals = this.attachInternals();+ }+ }+ customElements.define("closed-shadow-host", ClosedShadowHost);+ const closedHost = document.createElement("closed-shadow-host");++ return [+ root instanceof ShadowRoot,+ root.nodeType,+ root.nodeName,+ root.host === host,+ root.mode,+ root.delegatesFocus,+ host.shadowRoot === root,+ duplicateError,+ inside.parentNode === root,+ inside.getRootNode() === root,+ inside.getRootNode({ composed: true }) === document,+ root.isConnected,+ inside.isConnected,+ host.contains(inside),+ document.querySelector("#inside") === null,+ root.querySelector("#inside") === inside,+ records.length,+ records[0] && records[0].target === inside,+ records[0] && records[0].addedNodes[0] === added,+ closedHost.shadowRoot,+ closedHost.internals.shadowRoot === closedHost.closedRoot+ ];+ "##,+ )+ .unwrap();+ assert_eq!(+ result,+ serde_json::json!([+ true,+ 11,+ "#document-fragment",+ true,+ "open",+ true,+ true,+ "NotSupportedError",+ true,+ true,+ true,+ true,+ true,+ false,+ true,+ true,+ 1,+ true,+ true,+ null,+ true+ ])+ );+ }+#[test]fn create_element_synchronously_constructs_an_existing_definition() {let mut rt = setup_runtime("<html><body></body></html>");