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

@ -6,7 +6,7 @@
//! and Gecko.
use context::QuirksMode;
use selectors::{Element, SelectorList};
use selectors::{Element, NthIndexCache, SelectorList};
use selectors::matching::{self, MatchingContext, MatchingMode};
/// https://dom.spec.whatwg.org/#dom-element-matches
@ -27,3 +27,33 @@ where
context.scope_element = Some(element.opaque());
matching::matches_selector_list(selector_list, element, &mut context)
}
/// https://dom.spec.whatwg.org/#dom-element-closest
pub fn element_closest<E>(
element: E,
selector_list: &SelectorList<E::Impl>,
quirks_mode: QuirksMode,
) -> Option<E>
where
E: Element,
{
let mut nth_index_cache = NthIndexCache::default();
let mut context = MatchingContext::new(
MatchingMode::Normal,
None,
Some(&mut nth_index_cache),
quirks_mode,
);
context.scope_element = Some(element.opaque());
let mut current = Some(element);
while let Some(element) = current.take() {
if matching::matches_selector_list(selector_list, &element, &mut context) {
return Some(element);
}
current = element.parent_element();
}
return None;
}