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 @@ use cssparser::{Parser, ParserInput};
use cssparser::ToCss as ParserToCss;
use env_logger::LogBuilder;
use malloc_size_of::MallocSizeOfOps;
use selectors::{self, Element, NthIndexCache};
use selectors::Element;
use selectors::matching::{MatchingContext, MatchingMode, matches_selector};
use servo_arc::{Arc, ArcBorrow, RawOffsetArc};
use std::cell::RefCell;
@ -1526,28 +1526,13 @@ pub unsafe extern "C" fn Servo_SelectorList_Closest<'a>(
selectors: RawServoSelectorListBorrowed,
) -> RawGeckoElementBorrowedOrNull<'a> {
use std::borrow::Borrow;
let mut nth_index_cache = NthIndexCache::default();
use style::dom_apis;
let element = GeckoElement(element);
let mut context = MatchingContext::new(
MatchingMode::Normal,
None,
Some(&mut nth_index_cache),
element.owner_document_quirks_mode(),
);
context.scope_element = Some(element.opaque());
let selectors = ::selectors::SelectorList::from_ffi(selectors).borrow();
let mut current = Some(element);
while let Some(element) = current.take() {
if selectors::matching::matches_selector_list(&selectors, &element, &mut context) {
return Some(element.0);
}
current = element.parent_element();
}
return None;
dom_apis::element_closest(element, &selectors, element.owner_document_quirks_mode())
.map(|e| e.0)
}
#[no_mangle]