Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: Share code between Gecko and Servo for DOM APIs. #18863

Merged
merged 2 commits into from Oct 13, 2017
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Prev

style: Share code for Element::Closest.

  • Loading branch information
emilio committed Oct 13, 2017
commit f64f8b8be242ccb3aeb8b5e1da9da2f942d98760
@@ -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
@@ -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;
}
@@ -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]
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.