Introduce ShadowIncluding enum for tree traversals

This commit is contained in:
Fernando Jiménez Moreno 2019-02-21 13:19:01 +01:00
parent 2674a3e717
commit 3ccd622c9b
14 changed files with 58 additions and 50 deletions

View file

@ -15,7 +15,7 @@ use crate::dom::bindings::str::DOMString;
use crate::dom::document::AnimationFrameCallback;
use crate::dom::element::Element;
use crate::dom::globalscope::GlobalScope;
use crate::dom::node::{window_from_node, Node};
use crate::dom::node::{window_from_node, Node, ShadowIncluding};
use crate::dom::window::Window;
use crate::script_thread::Documents;
use devtools_traits::TimelineMarkerType;
@ -103,7 +103,7 @@ fn find_node_by_unique_id(
documents.find_document(pipeline).and_then(|document| {
document
.upcast::<Node>()
.traverse_preorder(/* shadow including */ false)
.traverse_preorder(ShadowIncluding::No)
.find(|candidate| candidate.unique_id() == node_id)
})
}

View file

@ -26,7 +26,7 @@ use crate::dom::domexception::{DOMErrorName, DOMException};
use crate::dom::element::{CustomElementState, Element};
use crate::dom::globalscope::GlobalScope;
use crate::dom::htmlelement::HTMLElement;
use crate::dom::node::{document_from_node, window_from_node, Node};
use crate::dom::node::{document_from_node, window_from_node, Node, ShadowIncluding};
use crate::dom::promise::Promise;
use crate::dom::window::Window;
use crate::microtask::Microtask;
@ -364,7 +364,7 @@ impl CustomElementRegistryMethods for CustomElementRegistry {
// Steps 14-15
for candidate in document
.upcast::<Node>()
.traverse_preorder(/* shadow including */ true)
.traverse_preorder(ShadowIncluding::Yes)
.filter_map(DomRoot::downcast::<Element>)
{
let is = candidate.get_is();

View file

@ -76,7 +76,7 @@ use crate::dom::messageevent::MessageEvent;
use crate::dom::mouseevent::MouseEvent;
use crate::dom::node::VecPreOrderInsertionHelper;
use crate::dom::node::{self, document_from_node, window_from_node, CloneChildrenFlag};
use crate::dom::node::{LayoutNodeHelpers, Node, NodeDamage, NodeFlags};
use crate::dom::node::{LayoutNodeHelpers, Node, NodeDamage, NodeFlags, ShadowIncluding};
use crate::dom::nodeiterator::NodeIterator;
use crate::dom::nodelist::NodeList;
use crate::dom::pagetransitionevent::PageTransitionEvent;
@ -596,7 +596,7 @@ impl Document {
pub fn refresh_base_element(&self) {
let base = self
.upcast::<Node>()
.traverse_preorder(/* shadow including */ false)
.traverse_preorder(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<HTMLBaseElement>)
.find(|element| {
element
@ -834,7 +834,7 @@ impl Document {
};
let doc_node = self.upcast::<Node>();
doc_node
.traverse_preorder(/* shadow including */ false)
.traverse_preorder(ShadowIncluding::No)
.filter_map(DomRoot::downcast)
.find(|node| check_anchor(&node))
.map(DomRoot::upcast)
@ -943,7 +943,7 @@ impl Document {
pub fn dirty_all_nodes(&self) {
let root = self.upcast::<Node>();
for node in root.traverse_preorder(/* shadow including */ true) {
for node in root.traverse_preorder(ShadowIncluding::Yes) {
node.dirty(NodeDamage::OtherNodeDamage)
}
}
@ -2238,7 +2238,7 @@ impl Document {
/// Iterate over all iframes in the document.
pub fn iter_iframes(&self) -> impl Iterator<Item = DomRoot<HTMLIFrameElement>> {
self.upcast::<Node>()
.traverse_preorder(/* shadow including */ true)
.traverse_preorder(ShadowIncluding::Yes)
.filter_map(DomRoot::downcast::<HTMLIFrameElement>)
}
@ -2827,7 +2827,7 @@ impl Document {
let maybe_node = doc.deref().map(Castable::upcast::<Node>);
let iter = maybe_node
.iter()
.flat_map(|node| node.traverse_preorder(/* shadow including */ false))
.flat_map(|node| node.traverse_preorder(ShadowIncluding::No))
.filter(|node| callback(&node));
NodeList::new_simple_list(&self.window, iter)
}
@ -3726,7 +3726,7 @@ impl DocumentMethods for Document {
} else {
// Step 2.
root.upcast::<Node>()
.traverse_preorder(/* shadow including */ false)
.traverse_preorder(ShadowIncluding::No)
.find(|node| node.is::<HTMLTitleElement>())
}
});
@ -3773,7 +3773,7 @@ impl DocumentMethods for Document {
} else if root.namespace() == &ns!(html) {
let elem = root
.upcast::<Node>()
.traverse_preorder(/* shadow including */ false)
.traverse_preorder(ShadowIncluding::No)
.find(|node| node.is::<HTMLTitleElement>());
match elem {
Some(elem) => elem,
@ -4140,7 +4140,7 @@ impl DocumentMethods for Document {
{
// Step 1.
let mut elements = root
.traverse_preorder(/* shadow including */ false)
.traverse_preorder(ShadowIncluding::No)
.filter(|node| filter_by_name(&name, &node))
.peekable();
if let Some(first) = elements.next() {
@ -4268,7 +4268,7 @@ impl DocumentMethods for Document {
// Step 8
for node in self
.upcast::<Node>()
.traverse_preorder(/* shadow including */ true)
.traverse_preorder(ShadowIncluding::Yes)
{
node.upcast::<EventTarget>().remove_all_listeners();
}

View file

@ -13,7 +13,7 @@ use crate::dom::bindings::str::DOMString;
use crate::dom::document::Document;
use crate::dom::element::Element;
use crate::dom::htmlcollection::HTMLCollection;
use crate::dom::node::{window_from_node, Node};
use crate::dom::node::{window_from_node, Node, ShadowIncluding};
use crate::dom::nodelist::NodeList;
use crate::dom::window::Window;
use dom_struct::dom_struct;
@ -59,7 +59,7 @@ impl DocumentFragmentMethods for DocumentFragment {
fn GetElementById(&self, id: DOMString) -> Option<DomRoot<Element>> {
let node = self.upcast::<Node>();
let id = Atom::from(id);
node.traverse_preorder(/* shadow including */ false)
node.traverse_preorder(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<Element>)
.find(
|descendant| match descendant.get_attribute(&ns!(), &local_name!("id")) {

View file

@ -28,7 +28,7 @@ use crate::dom::htmlhtmlelement::HTMLHtmlElement;
use crate::dom::htmlinputelement::{HTMLInputElement, InputType};
use crate::dom::htmllabelelement::HTMLLabelElement;
use crate::dom::node::{document_from_node, window_from_node};
use crate::dom::node::{Node, NodeFlags};
use crate::dom::node::{Node, NodeFlags, ShadowIncluding};
use crate::dom::nodelist::NodeList;
use crate::dom::text::Text;
use crate::dom::virtualmethods::VirtualMethods;
@ -707,7 +707,7 @@ impl HTMLElement {
let root_element = element.root_element();
let root_node = root_element.upcast::<Node>();
let children = root_node
.traverse_preorder(/* shadow including */ false)
.traverse_preorder(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<Element>)
.filter(|elem| elem.is::<HTMLLabelElement>())
.filter(|elem| elem.get_string_attribute(&local_name!("for")) == id)

View file

@ -13,7 +13,7 @@ use crate::dom::htmlcollection::{CollectionFilter, HTMLCollection};
use crate::dom::htmlelement::HTMLElement;
use crate::dom::htmlformelement::{FormControl, HTMLFormElement};
use crate::dom::htmllegendelement::HTMLLegendElement;
use crate::dom::node::{window_from_node, Node};
use crate::dom::node::{window_from_node, Node, ShadowIncluding};
use crate::dom::validitystate::ValidityState;
use crate::dom::virtualmethods::VirtualMethods;
use dom_struct::dom_struct;
@ -128,7 +128,7 @@ impl VirtualMethods for HTMLFieldSetElement {
});
let fields = children.flat_map(|child| {
child
.traverse_preorder(/* shadow including */ false)
.traverse_preorder(ShadowIncluding::No)
.filter(|descendant| match descendant.type_id() {
NodeTypeId::Element(ElementTypeId::HTMLElement(
HTMLElementTypeId::HTMLButtonElement,

View file

@ -41,7 +41,8 @@ use crate::dom::htmloutputelement::HTMLOutputElement;
use crate::dom::htmlselectelement::HTMLSelectElement;
use crate::dom::htmltextareaelement::HTMLTextAreaElement;
use crate::dom::node::{document_from_node, window_from_node};
use crate::dom::node::{Node, NodeFlags, UnbindContext, VecPreOrderInsertionHelper};
use crate::dom::node::{Node, NodeFlags, ShadowIncluding};
use crate::dom::node::{UnbindContext, VecPreOrderInsertionHelper};
use crate::dom::validitystate::ValidationFlags;
use crate::dom::virtualmethods::VirtualMethods;
use crate::dom::window::Window;
@ -582,7 +583,7 @@ impl HTMLFormElement {
// form, refactor this when html5ever's form owner PR lands
// Step 1-3
let invalid_controls = node
.traverse_preorder(/* shadow including */ false)
.traverse_preorder(ShadowIncluding::No)
.filter_map(|field| {
if let Some(el) = field.downcast::<Element>() {
if el.disabled_state() {

View file

@ -10,7 +10,7 @@ use crate::dom::document::{determine_policy_for_token, Document};
use crate::dom::element::Element;
use crate::dom::htmlelement::HTMLElement;
use crate::dom::htmlmetaelement::HTMLMetaElement;
use crate::dom::node::{document_from_node, Node};
use crate::dom::node::{document_from_node, Node, ShadowIncluding};
use crate::dom::userscripts::load_script;
use crate::dom::virtualmethods::VirtualMethods;
use dom_struct::dom_struct;
@ -55,7 +55,7 @@ impl HTMLHeadElement {
let node = self.upcast::<Node>();
let candidates = node
.traverse_preorder(/* shadow including */ false)
.traverse_preorder(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<Element>)
.filter(|elem| elem.is::<HTMLMetaElement>())
.filter(|elem| elem.get_string_attribute(&local_name!("name")) == "referrer")

View file

@ -32,7 +32,8 @@ use crate::dom::htmlmapelement::HTMLMapElement;
use crate::dom::htmlpictureelement::HTMLPictureElement;
use crate::dom::htmlsourceelement::HTMLSourceElement;
use crate::dom::mouseevent::MouseEvent;
use crate::dom::node::{document_from_node, window_from_node, Node, NodeDamage, UnbindContext};
use crate::dom::node::UnbindContext;
use crate::dom::node::{document_from_node, window_from_node, Node, NodeDamage, ShadowIncluding};
use crate::dom::performanceresourcetiming::InitiatorType;
use crate::dom::progressevent::ProgressEvent;
use crate::dom::values::UNSIGNED_LONG_MAX;
@ -1259,7 +1260,7 @@ impl HTMLImageElement {
let useMapElements = document_from_node(self)
.upcast::<Node>()
.traverse_preorder(/* shadow including */ false)
.traverse_preorder(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<HTMLMapElement>)
.find(|n| {
n.upcast::<Element>()

View file

@ -15,7 +15,7 @@ use crate::dom::event::Event;
use crate::dom::eventtarget::EventTarget;
use crate::dom::htmlelement::HTMLElement;
use crate::dom::htmlformelement::{FormControl, FormControlElementHelpers, HTMLFormElement};
use crate::dom::node::{document_from_node, Node};
use crate::dom::node::{document_from_node, Node, ShadowIncluding};
use crate::dom::virtualmethods::VirtualMethods;
use dom_struct::dom_struct;
use html5ever::{LocalName, Prefix};
@ -162,7 +162,7 @@ impl VirtualMethods for HTMLLabelElement {
impl HTMLLabelElement {
pub fn first_labelable_descendant(&self) -> Option<DomRoot<HTMLElement>> {
self.upcast::<Node>()
.traverse_preorder(/* shadow including */ false)
.traverse_preorder(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<HTMLElement>)
.filter(|elem| elem.is_labelable_element())
.next()

View file

@ -8,7 +8,7 @@ use crate::dom::bindings::root::DomRoot;
use crate::dom::document::Document;
use crate::dom::htmlareaelement::HTMLAreaElement;
use crate::dom::htmlelement::HTMLElement;
use crate::dom::node::Node;
use crate::dom::node::{Node, ShadowIncluding};
use dom_struct::dom_struct;
use html5ever::{LocalName, Prefix};
@ -43,7 +43,7 @@ impl HTMLMapElement {
pub fn get_area_elements(&self) -> Vec<DomRoot<HTMLAreaElement>> {
self.upcast::<Node>()
.traverse_preorder(/* shadow including */ false)
.traverse_preorder(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<HTMLAreaElement>)
.collect()
}

View file

@ -293,7 +293,7 @@ impl Node {
let parent_in_shadow_tree = self.is_in_shadow_tree();
let parent_is_connected = self.is_connected();
for node in new_child.traverse_preorder(/* shadow including */ false) {
for node in new_child.traverse_preorder(ShadowIncluding::No) {
if parent_in_shadow_tree {
if let Some(shadow_root) = self.downcast::<ShadowRoot>() {
node.set_owner_shadow_root(&*shadow_root);
@ -351,7 +351,7 @@ impl Node {
child.composed_parent_node.set(None);
self.children_count.set(self.children_count.get() - 1);
for node in child.traverse_preorder(/* shadow including */ true) {
for node in child.traverse_preorder(ShadowIncluding::Yes) {
// Out-of-document elements never have the descendants flag set.
node.set_flag(
NodeFlags::IS_IN_DOC |
@ -362,7 +362,7 @@ impl Node {
false,
);
}
for node in child.traverse_preorder(/* shadow including */ true) {
for node in child.traverse_preorder(ShadowIncluding::Yes) {
// This needs to be in its own loop, because unbind_from_tree may
// rely on the state of IS_IN_DOC of the context node's descendants,
// e.g. when removing a <form>.
@ -625,7 +625,7 @@ impl Node {
}
/// Iterates over this node and all its descendants, in preorder.
pub fn traverse_preorder(&self, shadow_including: bool) -> TreeIterator {
pub fn traverse_preorder(&self, shadow_including: ShadowIncluding) -> TreeIterator {
TreeIterator::new(self, shadow_including)
}
@ -873,7 +873,7 @@ impl Node {
self.owner_doc().quirks_mode(),
);
Ok(self
.traverse_preorder(/* shadow including */ false)
.traverse_preorder(ShadowIncluding::No)
.filter_map(DomRoot::downcast)
.find(|element| matches_selector_list(&selectors, element, &mut ctx)))
},
@ -891,7 +891,7 @@ impl Node {
Err(_) => Err(Error::Syntax),
// Step 3.
Ok(selectors) => {
let mut descendants = self.traverse_preorder(/* shadow including */ false);
let mut descendants = self.traverse_preorder(ShadowIncluding::No);
// Skip the root of the tree.
assert!(&*descendants.next().unwrap() == self);
Ok(QuerySelectorIterator::new(descendants, selectors))
@ -1510,6 +1510,13 @@ where
}
}
/// Whether a tree traversal should pass shadow tree boundaries.
#[derive(PartialEq)]
pub enum ShadowIncluding {
No,
Yes,
}
pub struct TreeIterator {
current: Option<DomRoot<Node>>,
depth: usize,
@ -1517,11 +1524,11 @@ pub struct TreeIterator {
}
impl TreeIterator {
fn new(root: &Node, shadow_including: bool) -> TreeIterator {
fn new(root: &Node, shadow_including: ShadowIncluding) -> TreeIterator {
TreeIterator {
current: Some(DomRoot::from_ref(root)),
depth: 0,
shadow_including,
shadow_including: shadow_including == ShadowIncluding::Yes,
}
}
@ -1655,11 +1662,11 @@ impl Node {
// Step 3.
if &*old_doc != document {
// Step 3.1.
for descendant in node.traverse_preorder(/* shadow including */ true) {
for descendant in node.traverse_preorder(ShadowIncluding::Yes) {
descendant.set_owner_doc(document);
}
for descendant in node
.traverse_preorder(/* shadow including */ true)
.traverse_preorder(ShadowIncluding::Yes)
.filter_map(|d| d.as_custom_element())
{
// Step 3.2.
@ -1669,7 +1676,7 @@ impl Node {
None,
);
}
for descendant in node.traverse_preorder(/* shadow including */ true) {
for descendant in node.traverse_preorder(ShadowIncluding::Yes) {
// Step 3.3.
vtable_for(&descendant).adopting_steps(&old_doc);
}
@ -1890,7 +1897,7 @@ impl Node {
parent.add_child(*kid, child);
// Step 7.7.
for descendant in kid
.traverse_preorder(/* shadow including */ true)
.traverse_preorder(ShadowIncluding::Yes)
.filter_map(DomRoot::downcast::<Element>)
{
// Step 7.7.2.
@ -2335,9 +2342,8 @@ impl NodeMethods for Node {
fn GetTextContent(&self) -> Option<DOMString> {
match self.type_id() {
NodeTypeId::DocumentFragment(_) | NodeTypeId::Element(..) => {
let content = Node::collect_text_contents(
self.traverse_preorder(/* shadow including */ false),
);
let content =
Node::collect_text_contents(self.traverse_preorder(ShadowIncluding::No));
Some(content)
},
NodeTypeId::CharacterData(..) => {
@ -3190,7 +3196,7 @@ where
let elem_node = elem.upcast::<Node>();
let mut head: usize = 0;
for node in tree_root.traverse_preorder(/* shadow including */ false) {
for node in tree_root.traverse_preorder(ShadowIncluding::No) {
let head_node = DomRoot::upcast::<Node>(DomRoot::from_ref(&*self[head]));
if head_node == node {
head += 1;

View file

@ -24,7 +24,7 @@ use crate::dom::document::Document;
use crate::dom::documentfragment::DocumentFragment;
use crate::dom::element::Element;
use crate::dom::htmlscriptelement::HTMLScriptElement;
use crate::dom::node::{Node, UnbindContext};
use crate::dom::node::{Node, ShadowIncluding, UnbindContext};
use crate::dom::text::Text;
use crate::dom::window::Window;
use dom_struct::dom_struct;
@ -978,7 +978,7 @@ impl RangeMethods for Range {
// Step 4.
for node in fragment_node
.upcast::<Node>()
.traverse_preorder(/* shadow incluing */ false)
.traverse_preorder(ShadowIncluding::No)
{
if let Some(script) = node.downcast::<HTMLScriptElement>() {
script.set_already_started(false);

View file

@ -22,7 +22,7 @@ use crate::dom::htmlelement::HTMLElement;
use crate::dom::htmliframeelement::HTMLIFrameElement;
use crate::dom::htmlinputelement::HTMLInputElement;
use crate::dom::htmloptionelement::HTMLOptionElement;
use crate::dom::node::{window_from_node, Node};
use crate::dom::node::{window_from_node, Node, ShadowIncluding};
use crate::script_thread::Documents;
use cookie::Cookie;
use euclid::{Point2D, Rect, Size2D};
@ -50,7 +50,7 @@ fn find_node_by_unique_id(
documents.find_document(pipeline).and_then(|document| {
document
.upcast::<Node>()
.traverse_preorder(/* shadow including */ true)
.traverse_preorder(ShadowIncluding::Yes)
.find(|candidate| candidate.unique_id() == node_id)
})
}