Skip to content

Commit

Permalink
Merge Node::shadow_including_inclusive_ancestors into inclusive_ances…
Browse files Browse the repository at this point in the history
…tors
  • Loading branch information
ferjm committed Apr 26, 2019
1 parent 0ca4792 commit 8eba587
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 59 deletions.
10 changes: 5 additions & 5 deletions components/script/dom/document.rs
Expand Up @@ -934,7 +934,7 @@ impl Document {

let el = node_address.and_then(|address| {
let node = unsafe { node::from_untrusted_node_address(js_runtime, address) };
node.inclusive_ancestors()
node.inclusive_ancestors(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<Element>)
.next()
});
Expand Down Expand Up @@ -1118,7 +1118,7 @@ impl Document {

let maybe_new_target = node_address.and_then(|address| {
let node = unsafe { node::from_untrusted_node_address(js_runtime, address) };
node.inclusive_ancestors()
node.inclusive_ancestors(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<Element>)
.next()
});
Expand Down Expand Up @@ -1154,7 +1154,7 @@ impl Document {
if !old_target_is_ancestor_of_new_target {
for element in old_target
.upcast::<Node>()
.inclusive_ancestors()
.inclusive_ancestors(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<Element>)
{
element.set_hover_state(false);
Expand All @@ -1172,7 +1172,7 @@ impl Document {
if let Some(ref new_target) = maybe_new_target {
for element in new_target
.upcast::<Node>()
.inclusive_ancestors()
.inclusive_ancestors(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<Element>)
{
if element.hover_state() {
Expand Down Expand Up @@ -1214,7 +1214,7 @@ impl Document {

let el = node_address.and_then(|address| {
let node = unsafe { node::from_untrusted_node_address(js_runtime, address) };
node.inclusive_ancestors()
node.inclusive_ancestors(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<Element>)
.next()
});
Expand Down
13 changes: 8 additions & 5 deletions components/script/dom/element.rs
Expand Up @@ -73,7 +73,7 @@ use crate::dom::mutationobserver::{Mutation, MutationObserver};
use crate::dom::namednodemap::NamedNodeMap;
use crate::dom::node::{document_from_node, window_from_node};
use crate::dom::node::{BindContext, NodeDamage, NodeFlags, UnbindContext};
use crate::dom::node::{ChildrenMutation, LayoutNodeHelpers, Node};
use crate::dom::node::{ChildrenMutation, LayoutNodeHelpers, Node, ShadowIncluding};
use crate::dom::nodelist::NodeList;
use crate::dom::promise::Promise;
use crate::dom::raredata::ElementRareData;
Expand Down Expand Up @@ -1111,7 +1111,7 @@ impl Element {

let inclusive_ancestor_elements = self
.upcast::<Node>()
.inclusive_ancestors()
.inclusive_ancestors(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<Self>);

// Steps 3-4.
Expand Down Expand Up @@ -1227,7 +1227,7 @@ impl Element {
.unwrap()
} else {
self.upcast::<Node>()
.inclusive_ancestors()
.inclusive_ancestors(ShadowIncluding::No)
.filter_map(DomRoot::downcast)
.last()
.expect("We know inclusive_ancestors will return `self` which is an element")
Expand All @@ -1236,7 +1236,10 @@ impl Element {

// https://dom.spec.whatwg.org/#locate-a-namespace-prefix
pub fn lookup_prefix(&self, namespace: Namespace) -> Option<DOMString> {
for node in self.upcast::<Node>().inclusive_ancestors() {
for node in self
.upcast::<Node>()
.inclusive_ancestors(ShadowIncluding::No)
{
let element = node.downcast::<Element>()?;
// Step 1.
if *element.namespace() == namespace {
Expand Down Expand Up @@ -3235,7 +3238,7 @@ impl Element {
// https://html.spec.whatwg.org/multipage/#language
pub fn get_lang(&self) -> String {
self.upcast::<Node>()
.inclusive_ancestors()
.inclusive_ancestors(ShadowIncluding::No)
.filter_map(|node| {
node.downcast::<Element>().and_then(|el| {
el.get_attribute(&ns!(xml), &local_name!("lang"))
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/htmloptionelement.rs
Expand Up @@ -19,7 +19,7 @@ use crate::dom::htmlformelement::HTMLFormElement;
use crate::dom::htmloptgroupelement::HTMLOptGroupElement;
use crate::dom::htmlscriptelement::HTMLScriptElement;
use crate::dom::htmlselectelement::HTMLSelectElement;
use crate::dom::node::{BindContext, Node, UnbindContext};
use crate::dom::node::{BindContext, Node, ShadowIncluding, UnbindContext};
use crate::dom::text::Text;
use crate::dom::virtualmethods::VirtualMethods;
use dom_struct::dom_struct;
Expand Down Expand Up @@ -251,7 +251,7 @@ impl VirtualMethods for HTMLOptionElement {

if let Some(select) = context
.parent
.inclusive_ancestors()
.inclusive_ancestors(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<HTMLSelectElement>)
.next()
{
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/mutationobserver.rs
Expand Up @@ -13,7 +13,7 @@ use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::mutationrecord::MutationRecord;
use crate::dom::node::Node;
use crate::dom::node::{Node, ShadowIncluding};
use crate::dom::window::Window;
use crate::microtask::Microtask;
use crate::script_thread::ScriptThread;
Expand Down Expand Up @@ -131,7 +131,7 @@ impl MutationObserver {
let mut interested_observers: Vec<(DomRoot<MutationObserver>, Option<DOMString>)> = vec![];

// Step 2 & 3
for node in target.inclusive_ancestors() {
for node in target.inclusive_ancestors(ShadowIncluding::No) {
for registered in &*node.registered_mutation_observers() {
if &*node != target && !registered.options.subtree {
continue;
Expand Down
55 changes: 29 additions & 26 deletions components/script/dom/node.rs
Expand Up @@ -559,7 +559,7 @@ impl Node {
pub fn note_dirty_descendants(&self) {
debug_assert!(self.is_in_doc());

for ancestor in self.shadow_including_inclusive_ancestors() {
for ancestor in self.inclusive_ancestors(ShadowIncluding::Yes) {
if ancestor.get_flag(NodeFlags::HAS_DIRTY_DESCENDANTS) {
return;
}
Expand All @@ -582,7 +582,7 @@ impl Node {
self.inclusive_descendants_version(),
doc.inclusive_descendants_version(),
) + 1;
for ancestor in self.inclusive_ancestors() {
for ancestor in self.inclusive_ancestors(ShadowIncluding::No) {
ancestor.inclusive_descendants_version.set(version);
}
doc.inclusive_descendants_version.set(version);
Expand Down Expand Up @@ -638,7 +638,7 @@ impl Node {
}

fn is_shadow_including_inclusive_ancestor_of(&self, node: &Node) -> bool {
node.shadow_including_inclusive_ancestors()
node.inclusive_ancestors(ShadowIncluding::Yes)
.any(|ancestor| &*ancestor == self)
}

Expand Down Expand Up @@ -900,25 +900,22 @@ impl Node {
}
}

pub fn inclusive_ancestors(&self) -> Box<Iterator<Item = DomRoot<Node>>> {
Box::new(SimpleNodeIterator {
current: Some(DomRoot::from_ref(self)),
next_node: |n| n.GetParentNode(),
})
}

/// https://dom.spec.whatwg.org/#concept-shadow-including-inclusive-ancestor
pub fn shadow_including_inclusive_ancestors(&self) -> Box<Iterator<Item = DomRoot<Node>>> {
Box::new(SimpleNodeIterator {
pub fn inclusive_ancestors(
&self,
shadow_including: ShadowIncluding,
) -> impl Iterator<Item = DomRoot<Node>> {
SimpleNodeIterator {
current: Some(DomRoot::from_ref(self)),
next_node: |n| {
if let Some(shadow_root) = n.downcast::<ShadowRoot>() {
Some(DomRoot::from_ref(shadow_root.Host().upcast::<Node>()))
} else {
n.GetParentNode()
next_node: move |n| {
if shadow_including == ShadowIncluding::Yes {
if let Some(shadow_root) = n.downcast::<ShadowRoot>() {
return Some(DomRoot::from_ref(shadow_root.Host().upcast::<Node>()));
}
}
n.GetParentNode()
},
})
}
}

pub fn owner_doc(&self) -> DomRoot<Document> {
Expand Down Expand Up @@ -1436,7 +1433,7 @@ impl FollowingNodeIterator {
return current.GetNextSibling();
}

for ancestor in current.inclusive_ancestors() {
for ancestor in current.inclusive_ancestors(ShadowIncluding::No) {
if self.root == ancestor {
break;
}
Expand Down Expand Up @@ -1545,11 +1542,11 @@ impl TreeIterator {
}

fn next_skipping_children_impl(&mut self, current: DomRoot<Node>) -> Option<DomRoot<Node>> {
let iter = if self.shadow_including {
current.shadow_including_inclusive_ancestors()
let iter = current.inclusive_ancestors(if self.shadow_including {
ShadowIncluding::Yes
} else {
current.inclusive_ancestors()
};
ShadowIncluding::No
});

for ancestor in iter {
if self.depth == 0 {
Expand Down Expand Up @@ -2282,7 +2279,9 @@ impl NodeMethods for Node {
return shadow_root.Host().upcast::<Node>().GetRootNode(options);
}
}
self.inclusive_ancestors().last().unwrap()
self.inclusive_ancestors(ShadowIncluding::No)
.last()
.unwrap()
}

// https://dom.spec.whatwg.org/#dom-node-parentnode
Expand Down Expand Up @@ -2687,8 +2686,12 @@ impl NodeMethods for Node {

// FIXME(emilio): This will eventually need to handle attribute nodes.

let mut self_and_ancestors = self.inclusive_ancestors().collect::<SmallVec<[_; 20]>>();
let mut other_and_ancestors = other.inclusive_ancestors().collect::<SmallVec<[_; 20]>>();
let mut self_and_ancestors = self
.inclusive_ancestors(ShadowIncluding::No)
.collect::<SmallVec<[_; 20]>>();
let mut other_and_ancestors = other
.inclusive_ancestors(ShadowIncluding::No)
.collect::<SmallVec<[_; 20]>>();

if self_and_ancestors.last() != other_and_ancestors.last() {
let random = as_uintptr(self_and_ancestors.last().unwrap()) <
Expand Down
48 changes: 36 additions & 12 deletions components/script/dom/range.rs
Expand Up @@ -102,10 +102,10 @@ impl Range {
// https://dom.spec.whatwg.org/#partially-contained
fn partially_contains(&self, node: &Node) -> bool {
self.StartContainer()
.inclusive_ancestors()
.inclusive_ancestors(ShadowIncluding::No)
.any(|n| &*n == node) !=
self.EndContainer()
.inclusive_ancestors()
.inclusive_ancestors(ShadowIncluding::No)
.any(|n| &*n == node)
}

Expand Down Expand Up @@ -193,8 +193,14 @@ impl Range {
// https://dom.spec.whatwg.org/#dom-range-comparepointnode-offset
fn compare_point(&self, node: &Node, offset: u32) -> Fallible<Ordering> {
let start_node = self.StartContainer();
let start_node_root = start_node.inclusive_ancestors().last().unwrap();
let node_root = node.inclusive_ancestors().last().unwrap();
let start_node_root = start_node
.inclusive_ancestors(ShadowIncluding::No)
.last()
.unwrap();
let node_root = node
.inclusive_ancestors(ShadowIncluding::No)
.last()
.unwrap();
if start_node_root != node_root {
// Step 1.
return Err(Error::WrongDocument);
Expand Down Expand Up @@ -253,7 +259,10 @@ impl RangeMethods for Range {
fn CommonAncestorContainer(&self) -> DomRoot<Node> {
let end_container = self.EndContainer();
// Step 1.
for container in self.StartContainer().inclusive_ancestors() {
for container in self
.StartContainer()
.inclusive_ancestors(ShadowIncluding::No)
{
// Step 2.
if container.is_inclusive_ancestor_of(&end_container) {
// Step 3.
Expand Down Expand Up @@ -368,8 +377,16 @@ impl RangeMethods for Range {
// Step 1.
return Err(Error::NotSupported);
}
let this_root = self.StartContainer().inclusive_ancestors().last().unwrap();
let other_root = other.StartContainer().inclusive_ancestors().last().unwrap();
let this_root = self
.StartContainer()
.inclusive_ancestors(ShadowIncluding::No)
.last()
.unwrap();
let other_root = other
.StartContainer()
.inclusive_ancestors(ShadowIncluding::No)
.last()
.unwrap();
if this_root != other_root {
// Step 2.
return Err(Error::WrongDocument);
Expand Down Expand Up @@ -429,8 +446,15 @@ impl RangeMethods for Range {
// https://dom.spec.whatwg.org/#dom-range-intersectsnode
fn IntersectsNode(&self, node: &Node) -> bool {
let start_node = self.StartContainer();
let start_node_root = self.StartContainer().inclusive_ancestors().last().unwrap();
let node_root = node.inclusive_ancestors().last().unwrap();
let start_node_root = self
.StartContainer()
.inclusive_ancestors(ShadowIncluding::No)
.last()
.unwrap();
let node_root = node
.inclusive_ancestors(ShadowIncluding::No)
.last()
.unwrap();
if start_node_root != node_root {
// Step 1.
return false;
Expand Down Expand Up @@ -868,9 +892,9 @@ impl RangeMethods for Range {
let end = self.EndContainer();

if start
.inclusive_ancestors()
.inclusive_ancestors(ShadowIncluding::No)
.any(|n| !n.is_inclusive_ancestor_of(&end) && !n.is::<Text>()) ||
end.inclusive_ancestors()
end.inclusive_ancestors(ShadowIncluding::No)
.any(|n| !n.is_inclusive_ancestor_of(&start) && !n.is::<Text>())
{
return Err(Error::InvalidState);
Expand Down Expand Up @@ -1051,7 +1075,7 @@ fn bp_position(a_node: &Node, a_offset: u32, b_node: &Node, b_offset: u32) -> Op
}
} else if position & NodeConstants::DOCUMENT_POSITION_CONTAINS != 0 {
// Step 3-1, 3-2.
let mut b_ancestors = b_node.inclusive_ancestors();
let mut b_ancestors = b_node.inclusive_ancestors(ShadowIncluding::No);
let child = b_ancestors
.find(|child| &*child.GetParentNode().unwrap() == a_node)
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/servoparser/mod.rs
Expand Up @@ -27,7 +27,7 @@ use crate::dom::htmlformelement::{FormControlElementHelpers, HTMLFormElement};
use crate::dom::htmlimageelement::HTMLImageElement;
use crate::dom::htmlscriptelement::{HTMLScriptElement, ScriptResult};
use crate::dom::htmltemplateelement::HTMLTemplateElement;
use crate::dom::node::Node;
use crate::dom::node::{Node, ShadowIncluding};
use crate::dom::performanceentry::PerformanceEntry;
use crate::dom::performancenavigationtiming::PerformanceNavigationTiming;
use crate::dom::processinginstruction::ProcessingInstruction;
Expand Down Expand Up @@ -194,7 +194,7 @@ impl ServoParser {

// Step 11.
let form = context_node
.inclusive_ancestors()
.inclusive_ancestors(ShadowIncluding::No)
.find(|element| element.is::<HTMLFormElement>());

let fragment_context = FragmentContext {
Expand Down
8 changes: 6 additions & 2 deletions components/script/dom/shadowroot.rs
Expand Up @@ -14,7 +14,7 @@ use crate::dom::document::Document;
use crate::dom::documentfragment::DocumentFragment;
use crate::dom::documentorshadowroot::{DocumentOrShadowRoot, StyleSheetInDocument};
use crate::dom::element::Element;
use crate::dom::node::{Node, NodeDamage, NodeFlags};
use crate::dom::node::{Node, NodeDamage, NodeFlags, ShadowIncluding};
use crate::dom::stylesheetlist::{StyleSheetList, StyleSheetListOwner};
use crate::dom::window::Window;
use dom_struct::dom_struct;
Expand Down Expand Up @@ -140,7 +140,11 @@ impl ShadowRoot {

/// Associate an element present in this shadow tree with the provided id.
pub fn register_named_element(&self, element: &Element, id: Atom) {
let root = self.upcast::<Node>().inclusive_ancestors().last().unwrap();
let root = self
.upcast::<Node>()
.inclusive_ancestors(ShadowIncluding::No)
.last()
.unwrap();
self.document_or_shadow_root.register_named_element(
self.document_fragment.id_map(),
element,
Expand Down

0 comments on commit 8eba587

Please sign in to comment.