diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index 04843e502c4..7c50e4ae69d 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -53,7 +53,7 @@ use crate::dom::mutationobserver::{Mutation, MutationObserver, RegisteredObserve use crate::dom::nodelist::NodeList; use crate::dom::processinginstruction::ProcessingInstruction; use crate::dom::range::WeakRangeVec; -use crate::dom::shadowroot::ShadowRoot; +use crate::dom::shadowroot::{LayoutShadowRootHelpers, ShadowRoot}; use crate::dom::stylesheetlist::StyleSheetListOwner; use crate::dom::svgsvgelement::{LayoutSVGSVGElementHelpers, SVGSVGElement}; use crate::dom::text::Text; @@ -109,9 +109,6 @@ pub struct Node { /// The parent of this node. parent_node: MutNullableDom, - /// The parent of this node, ignoring shadow roots. - composed_parent_node: MutNullableDom, - /// The first child of this node. first_child: MutNullableDom, @@ -244,7 +241,6 @@ impl Node { /// Fails unless `new_child` is disconnected from the tree. fn add_child(&self, new_child: &Node, before: Option<&Node>) { assert!(new_child.parent_node.get().is_none()); - assert!(new_child.composed_parent_node.get().is_none()); assert!(new_child.prev_sibling.get().is_none()); assert!(new_child.next_sibling.get().is_none()); match before { @@ -280,13 +276,6 @@ impl Node { } new_child.parent_node.set(Some(self)); - if let Some(shadow_root) = self.downcast::() { - new_child - .composed_parent_node - .set(Some(shadow_root.Host().upcast::())); - } else { - new_child.composed_parent_node.set(Some(self)); - } self.children_count.set(self.children_count.get() + 1); let parent_in_doc = self.is_in_doc(); @@ -348,7 +337,6 @@ impl Node { child.prev_sibling.set(None); child.next_sibling.set(None); child.parent_node.set(None); - child.composed_parent_node.set(None); self.children_count.set(self.children_count.get() - 1); for node in child.traverse_preorder(ShadowIncluding::Yes) { @@ -607,13 +595,11 @@ impl Node { } match self.type_id() { - NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::Text)) => self - .composed_parent_node - .get() - .unwrap() - .downcast::() - .unwrap() - .restyle(damage), + NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::Text)) => { + if let Some(parent) = self.composed_parent_node() { + parent.downcast::().unwrap().restyle(damage) + } + }, NodeTypeId::Element(_) => self.downcast::().unwrap().restyle(damage), _ => {}, }; @@ -959,6 +945,16 @@ impl Node { self.is_connected() && self.owner_doc().browsing_context().is_some() } + fn composed_parent_node(&self) -> Option> { + let parent = self.parent_node.get(); + if let Some(ref parent) = parent { + if let Some(shadow_root) = parent.downcast::() { + return Some(DomRoot::from_ref(shadow_root.Host().upcast::())); + } + } + parent + } + pub fn children(&self) -> impl Iterator> { SimpleNodeIterator { current: self.GetFirstChild(), @@ -1224,9 +1220,13 @@ impl LayoutNodeHelpers for LayoutDom { #[inline] #[allow(unsafe_code)] unsafe fn parent_node_ref(&self) -> Option> { - (*self.unsafe_get()) - .composed_parent_node - .get_inner_as_layout() + let parent = (*self.unsafe_get()).parent_node.get_inner_as_layout(); + if let Some(ref parent) = parent { + if let Some(shadow_root) = parent.downcast::() { + return Some(shadow_root.get_host_for_layout().upcast()); + } + } + parent } #[inline] @@ -1629,7 +1629,6 @@ impl Node { eventtarget: EventTarget::new_inherited(), parent_node: Default::default(), - composed_parent_node: Default::default(), first_child: Default::default(), last_child: Default::default(), next_sibling: Default::default(), diff --git a/components/style/dom_apis.rs b/components/style/dom_apis.rs index 1ef07e24a4a..c66bb66c9f5 100644 --- a/components/style/dom_apis.rs +++ b/components/style/dom_apis.rs @@ -231,7 +231,7 @@ where // Optimize for when the root is a document or a shadow root and the element // is connected to that root. if root.as_document().is_some() { - debug_assert!(element.as_node().is_connected(), "Not connected?"); + debug_assert!(element.as_node().is_in_document(), "Not connected?"); debug_assert_eq!( root, root.owner_doc().as_node(), @@ -241,10 +241,11 @@ where } if root.as_shadow_root().is_some() { + debug_assert!(element.as_node().is_in_document(), "Not connected?"); debug_assert_eq!( element.containing_shadow().unwrap().as_node(), root, - "Not connected?" + "Where did this element come from?" ); return true; } @@ -275,7 +276,7 @@ where return Err(()); } - if root.is_connected() { + if root.is_in_document() { if let Some(shadow) = root.as_shadow_root() { return shadow.elements_with_id(id); }