diff options
author | bors-servo <metajack+bors@gmail.com> | 2015-01-18 08:09:43 -0700 |
---|---|---|
committer | bors-servo <metajack+bors@gmail.com> | 2015-01-18 08:09:43 -0700 |
commit | ea83ffdb980d290a910e78b96c3f056280487ac7 (patch) | |
tree | 99bbd28cc2b98520055285f650f72a10571a760f /components/script/dom | |
parent | 2a9acdcb73685f2c5c14b51f33b741690b60cb23 (diff) | |
parent | 7759358e0978909986216409884b9d1d66afe87f (diff) | |
download | servo-ea83ffdb980d290a910e78b96c3f056280487ac7.tar.gz servo-ea83ffdb980d290a910e78b96c3f056280487ac7.zip |
auto merge of #4621 : jimrhoskins/servo/closest, r=jdm
fixes #4603
- Add definition to the Element.webidl and implementation to element.rs.
- Create inclusive_ancestors helper in NodeHelpers
- Update test expectations
Diffstat (limited to 'components/script/dom')
-rw-r--r-- | components/script/dom/element.rs | 17 | ||||
-rw-r--r-- | components/script/dom/node.rs | 7 | ||||
-rw-r--r-- | components/script/dom/webidls/Element.webidl | 3 |
3 files changed, 27 insertions, 0 deletions
diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index 129756c1575..14f48f5f3b6 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -1120,6 +1120,23 @@ impl<'a> ElementMethods for JSRef<'a, Element> { } } } + + // https://dom.spec.whatwg.org/#dom-element-closest + fn Closest(self, selectors: DOMString) -> Fallible<Option<Temporary<Element>>> { + let parser_context = ParserContext { + origin: StylesheetOrigin::Author, + }; + match style::parse_selector_list_from_str(&parser_context, selectors.as_slice()) { + Err(()) => Err(Syntax), + Ok(ref selectors) => { + let root: JSRef<Node> = NodeCast::from_ref(self); + Ok(root.inclusive_ancestors() + .filter_map(ElementCast::to_ref) + .find(|element| matches(selectors, &NodeCast::from_ref(*element), &mut None)) + .map(Temporary::from_rooted)) + } + } + } } pub fn get_attribute_parts<'a>(name: &'a str) -> (Option<&'a str>, &'a str) { diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index bc44aeda8ba..fb336efd85b 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -397,6 +397,7 @@ impl<'a> Iterator<JSRef<'a, Node>> for QuerySelectorIterator<'a> { pub trait NodeHelpers<'a> { fn ancestors(self) -> AncestorIterator<'a>; + fn inclusive_ancestors(self) -> AncestorIterator<'a>; fn children(self) -> NodeChildrenIterator<'a>; fn rev_children(self) -> ReverseChildrenIterator; fn child_elements(self) -> ChildElementIterator<'a>; @@ -798,6 +799,12 @@ impl<'a> NodeHelpers<'a> for JSRef<'a, Node> { } } + fn inclusive_ancestors(self) -> AncestorIterator<'a> { + AncestorIterator { + current: Some(self.clone()) + } + } + fn owner_doc(self) -> Temporary<Document> { self.owner_doc.get().unwrap() } diff --git a/components/script/dom/webidls/Element.webidl b/components/script/dom/webidls/Element.webidl index fc737bdb9ec..820d8b5b4f2 100644 --- a/components/script/dom/webidls/Element.webidl +++ b/components/script/dom/webidls/Element.webidl @@ -45,6 +45,9 @@ interface Element : Node { boolean hasAttributeNS(DOMString? namespace, DOMString localName); [Throws] + Element? closest(DOMString selectors); + + [Throws] boolean matches(DOMString selectors); HTMLCollection getElementsByTagName(DOMString localName); |