mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
style: Share code for Element::Closest.
This commit is contained in:
parent
9e6c49d479
commit
f64f8b8be2
3 changed files with 47 additions and 40 deletions
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue