style: Share code for Element::Closest.

This commit is contained in:
Emilio Cobos Álvarez 2017-10-13 13:04:35 +02:00
parent 9e6c49d479
commit f64f8b8be2
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
3 changed files with 47 additions and 40 deletions

View file

@ -2217,26 +2217,18 @@ impl ElementMethods for Element {
// https://dom.spec.whatwg.org/#dom-element-closest
fn Closest(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> {
match SelectorParser::parse_author_origin_no_namespace(&selectors) {
Err(_) => Err(Error::Syntax),
Ok(selectors) => {
let self_root = DomRoot::from_ref(self);
let root = self.upcast::<Node>();
for element in root.inclusive_ancestors() {
if let Some(element) = DomRoot::downcast::<Element>(element) {
let quirks_mode = document_from_node(self).quirks_mode();
// FIXME(bholley): Consider an nth-index cache here.
let mut ctx = MatchingContext::new(MatchingMode::Normal, None, None,
quirks_mode);
ctx.scope_element = Some(self_root.opaque());
if matches_selector_list(&selectors, &element, &mut ctx) {
return Ok(Some(element));
}
}
}
Ok(None)
}
}
let selectors =
match SelectorParser::parse_author_origin_no_namespace(&selectors) {
Err(_) => return Err(Error::Syntax),
Ok(selectors) => selectors,
};
let quirks_mode = document_from_node(self).quirks_mode();
Ok(dom_apis::element_closest(
DomRoot::from_ref(self),
&selectors,
quirks_mode,
))
}
// https://dom.spec.whatwg.org/#dom-element-insertadjacentelement