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
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue