style: Share code for Element::Matches.

This commit is contained in:
Emilio Cobos Álvarez 2017-10-13 12:50:38 +02:00
parent 085a0db056
commit 9e6c49d479
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
4 changed files with 47 additions and 21 deletions

View file

@ -107,6 +107,7 @@ use style::CaseSensitivityExt;
use style::applicable_declarations::ApplicableDeclarationBlock; use style::applicable_declarations::ApplicableDeclarationBlock;
use style::attr::{AttrValue, LengthOrPercentageOrAuto}; use style::attr::{AttrValue, LengthOrPercentageOrAuto};
use style::context::QuirksMode; use style::context::QuirksMode;
use style::dom_apis;
use style::element_state::*; use style::element_state::*;
use style::invalidation::element::restyle_hints::RESTYLE_SELF; use style::invalidation::element::restyle_hints::RESTYLE_SELF;
use style::properties::{Importance, PropertyDeclaration, PropertyDeclarationBlock, parse_style_attribute}; use style::properties::{Importance, PropertyDeclaration, PropertyDeclarationBlock, parse_style_attribute};
@ -2197,18 +2198,16 @@ impl ElementMethods for Element {
// https://dom.spec.whatwg.org/#dom-element-matches // https://dom.spec.whatwg.org/#dom-element-matches
fn Matches(&self, selectors: DOMString) -> Fallible<bool> { fn Matches(&self, selectors: DOMString) -> Fallible<bool> {
match SelectorParser::parse_author_origin_no_namespace(&selectors) { let selectors =
Err(_) => Err(Error::Syntax), match SelectorParser::parse_author_origin_no_namespace(&selectors) {
Ok(selectors) => { Err(_) => return Err(Error::Syntax),
let quirks_mode = document_from_node(self).quirks_mode(); Ok(selectors) => selectors,
let root = DomRoot::from_ref(self); };
// FIXME(bholley): Consider an nth-index cache here.
let mut ctx = MatchingContext::new(MatchingMode::Normal, None, None, let quirks_mode = document_from_node(self).quirks_mode();
quirks_mode); let element = DomRoot::from_ref(self);
ctx.scope_element = Some(root.opaque());
Ok(matches_selector_list(&selectors, &root, &mut ctx)) Ok(dom_apis::element_matches(&element, &selectors, quirks_mode))
}
}
} }
// https://dom.spec.whatwg.org/#dom-element-webkitmatchesselector // https://dom.spec.whatwg.org/#dom-element-webkitmatchesselector

View file

@ -0,0 +1,29 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Generic implementations of some DOM APIs so they can be shared between Servo
//! and Gecko.
use context::QuirksMode;
use selectors::{Element, SelectorList};
use selectors::matching::{self, MatchingContext, MatchingMode};
/// https://dom.spec.whatwg.org/#dom-element-matches
pub fn element_matches<E>(
element: &E,
selector_list: &SelectorList<E::Impl>,
quirks_mode: QuirksMode,
) -> bool
where
E: Element,
{
let mut context = MatchingContext::new(
MatchingMode::Normal,
None,
None,
quirks_mode,
);
context.scope_element = Some(element.opaque());
matching::matches_selector_list(selector_list, element, &mut context)
}

View file

@ -107,6 +107,7 @@ pub mod counter_style;
pub mod custom_properties; pub mod custom_properties;
pub mod data; pub mod data;
pub mod dom; pub mod dom;
pub mod dom_apis;
pub mod driver; pub mod driver;
pub mod element_state; pub mod element_state;
#[cfg(feature = "servo")] mod encoding_support; #[cfg(feature = "servo")] mod encoding_support;

View file

@ -1556,18 +1556,15 @@ pub unsafe extern "C" fn Servo_SelectorList_Matches(
selectors: RawServoSelectorListBorrowed, selectors: RawServoSelectorListBorrowed,
) -> bool { ) -> bool {
use std::borrow::Borrow; use std::borrow::Borrow;
use style::dom_apis;
let element = GeckoElement(element); let element = GeckoElement(element);
let mut context = MatchingContext::new(
MatchingMode::Normal,
None,
None,
element.owner_document_quirks_mode(),
);
context.scope_element = Some(element.opaque());
let selectors = ::selectors::SelectorList::from_ffi(selectors).borrow(); let selectors = ::selectors::SelectorList::from_ffi(selectors).borrow();
selectors::matching::matches_selector_list(&selectors, &element, &mut context) dom_apis::element_matches(
&element,
&selectors,
element.owner_document_quirks_mode(),
)
} }
#[no_mangle] #[no_mangle]