Do not store composed parent node

This commit is contained in:
Fernando Jiménez Moreno 2019-03-04 12:08:09 +01:00
parent 542699691e
commit ea1fe15dfe
2 changed files with 27 additions and 27 deletions

View file

@ -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<Node>,
/// The parent of this node, ignoring shadow roots.
composed_parent_node: MutNullableDom<Node>,
/// The first child of this node.
first_child: MutNullableDom<Node>,
@ -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::<ShadowRoot>() {
new_child
.composed_parent_node
.set(Some(shadow_root.Host().upcast::<Node>()));
} 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::<Element>()
.unwrap()
.restyle(damage),
NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::Text)) => {
if let Some(parent) = self.composed_parent_node() {
parent.downcast::<Element>().unwrap().restyle(damage)
}
},
NodeTypeId::Element(_) => self.downcast::<Element>().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<DomRoot<Node>> {
let parent = self.parent_node.get();
if let Some(ref parent) = parent {
if let Some(shadow_root) = parent.downcast::<ShadowRoot>() {
return Some(DomRoot::from_ref(shadow_root.Host().upcast::<Node>()));
}
}
parent
}
pub fn children(&self) -> impl Iterator<Item = DomRoot<Node>> {
SimpleNodeIterator {
current: self.GetFirstChild(),
@ -1224,9 +1220,13 @@ impl LayoutNodeHelpers for LayoutDom<Node> {
#[inline]
#[allow(unsafe_code)]
unsafe fn parent_node_ref(&self) -> Option<LayoutDom<Node>> {
(*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::<ShadowRoot>() {
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(),

View file

@ -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);
}