diff options
Diffstat (limited to 'components/script')
-rw-r--r-- | components/script/dom/element.rs | 51 | ||||
-rw-r--r-- | components/script/dom/node.rs | 65 |
2 files changed, 57 insertions, 59 deletions
diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index 1b78b67cbd9..445fd05bed3 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -79,6 +79,7 @@ use html5ever::serialize::TraversalScope::{IncludeNode, ChildrenOnly}; use html5ever::tree_builder::{NoQuirks, LimitedQuirks, Quirks}; use selectors::matching::{matches, DeclarationBlock}; use selectors::parser::parse_author_origin_selector_list_from_str; +use selectors::parser::{AttrSelector, NamespaceConstraint}; use string_cache::{Atom, Namespace, QualName}; use url::UrlParser; @@ -1454,8 +1455,7 @@ impl<'a> ElementMethods for &'a Element { match parse_author_origin_selector_list_from_str(&selectors) { Err(()) => Err(Syntax), Ok(ref selectors) => { - let root = NodeCast::from_ref(self); - Ok(matches(selectors, &root, &mut None)) + Ok(matches(selectors, &self, &mut None)) } } } @@ -1468,7 +1468,7 @@ impl<'a> ElementMethods for &'a Element { let root = NodeCast::from_ref(self); for element in root.inclusive_ancestors() { if let Some(element) = ElementCast::to_ref(element.r()) { - if matches(selectors, &NodeCast::from_ref(element), &mut None) { + if matches(selectors, &element, &mut None) { return Ok(Some(Root::from_ref(element))); } } @@ -1621,7 +1621,13 @@ impl<'a> VirtualMethods for &'a Element { } } -impl<'a> style::node::TElement for &'a Element { +impl<'a> ::selectors::Element for &'a Element { + type Node = &'a Node; + + fn as_node(&self) -> &'a Node { + NodeCast::from_ref(*self) + } + fn is_link(&self) -> bool { // FIXME: This is HTML only. let node = NodeCast::from_ref(*self); @@ -1740,6 +1746,43 @@ impl<'a> style::node::TElement for &'a Element { } } } + + fn match_attr<F>(&self, attr: &AttrSelector, test: F) -> bool + where F: Fn(&str) -> bool + { + let local_name = { + if self.is_html_element_in_html_document() { + &attr.lower_name + } else { + &attr.name + } + }; + match attr.namespace { + NamespaceConstraint::Specific(ref ns) => { + self.get_attribute(ns, local_name) + .map_or(false, |attr| { + // FIXME(https://github.com/rust-lang/rust/issues/23338) + let attr = attr.r(); + let value = attr.value(); + test(&value) + }) + }, + NamespaceConstraint::Any => { + let mut attributes: RootedVec<JS<Attr>> = RootedVec::new(); + self.get_attributes(local_name, &mut attributes); + attributes.iter().any(|attr| { + // FIXME(https://github.com/rust-lang/rust/issues/23338) + let attr = attr.root(); + let value = attr.r().value(); + test(&value) + }) + } + } + } + + fn is_html_element_in_html_document(&self) -> bool { + self.html_element_in_html_document() + } } pub trait ActivationElementHelpers<'a> { diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index 919136a4afd..b8282ce1de0 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -54,7 +54,7 @@ use util::geometry::Au; use util::namespace; use util::str::DOMString; use util::task_state; -use selectors::parser::{Selector, AttrSelector, NamespaceConstraint}; +use selectors::parser::Selector; use selectors::parser::parse_author_origin_selector_list_from_str; use selectors::matching::matches; use style::properties::ComputedValues; @@ -423,7 +423,11 @@ impl<'a> Iterator for QuerySelectorIterator { // TODO(cgaebel): Is it worth it to build a bloom filter here // (instead of passing `None`)? Probably. self.iterator.find(|node| { - node.r().is_element() && matches(selectors, &node.r(), &mut None) + if let Some(element) = ElementCast::to_ref(node.r()) { + matches(selectors, &element, &mut None) + } else { + false + } }) } } @@ -896,7 +900,7 @@ impl<'a> NodeHelpers for &'a Node { let root = self.ancestors().last(); let root = root.r().unwrap_or(self.clone()); Ok(root.traverse_preorder().filter_map(ElementCast::to_root).find(|element| { - matches(selectors, &NodeCast::from_ref(element.r()), &mut None) + matches(selectors, &element.r(), &mut None) })) } } @@ -2506,9 +2510,7 @@ impl<'a> VirtualMethods for &'a Node { } } -impl<'a> style::node::TNode for &'a Node { - type Element = &'a Element; - +impl<'a> ::selectors::Node<&'a Element> for &'a Node { fn parent_node(&self) -> Option<&'a Node> { (*self).parent_node.get() .map(|node| node.root().get_unsound_ref_forever()) @@ -2544,55 +2546,8 @@ impl<'a> style::node::TNode for &'a Node { is_document(*self) } - fn is_element(&self) -> bool { - // FIXME(zwarich): Remove this when UFCS lands and there is a better way - // of disambiguating methods. - fn is_element<'a, T: ElementDerived>(this: &T) -> bool { - this.is_element() - } - - is_element(*self) - } - - fn as_element(&self) -> &'a Element { - ElementCast::to_ref(*self).unwrap() - } - - fn match_attr<F>(&self, attr: &AttrSelector, test: F) -> bool - where F: Fn(&str) -> bool - { - let local_name = { - if self.is_html_element_in_html_document() { - &attr.lower_name - } else { - &attr.name - } - }; - match attr.namespace { - NamespaceConstraint::Specific(ref ns) => { - self.as_element().get_attribute(ns, local_name) - .map_or(false, |attr| { - // FIXME(https://github.com/rust-lang/rust/issues/23338) - let attr = attr.r(); - let value = attr.value(); - test(&value) - }) - }, - NamespaceConstraint::Any => { - let mut attributes: RootedVec<JS<Attr>> = RootedVec::new(); - self.as_element().get_attributes(local_name, &mut attributes); - attributes.iter().any(|attr| { - // FIXME(https://github.com/rust-lang/rust/issues/23338) - let attr = attr.root(); - let value = attr.r().value(); - test(&value) - }) - } - } - } - - fn is_html_element_in_html_document(&self) -> bool { - self.as_element().html_element_in_html_document() + fn as_element(&self) -> Option<&'a Element> { + ElementCast::to_ref(*self) } } |