diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index a87e561f912..32e9fc3545e 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -107,6 +107,7 @@ use style::CaseSensitivityExt; use style::applicable_declarations::ApplicableDeclarationBlock; use style::attr::{AttrValue, LengthOrPercentageOrAuto}; use style::context::QuirksMode; +use style::dom_apis; use style::element_state::*; use style::invalidation::element::restyle_hints::RESTYLE_SELF; 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 fn Matches(&self, selectors: DOMString) -> Fallible { - match SelectorParser::parse_author_origin_no_namespace(&selectors) { - Err(_) => Err(Error::Syntax), - Ok(selectors) => { - let quirks_mode = document_from_node(self).quirks_mode(); - let root = DomRoot::from_ref(self); - // FIXME(bholley): Consider an nth-index cache here. - let mut ctx = MatchingContext::new(MatchingMode::Normal, None, None, - quirks_mode); - ctx.scope_element = Some(root.opaque()); - Ok(matches_selector_list(&selectors, &root, &mut ctx)) - } - } + 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(); + let element = DomRoot::from_ref(self); + + Ok(dom_apis::element_matches(&element, &selectors, quirks_mode)) } // https://dom.spec.whatwg.org/#dom-element-webkitmatchesselector diff --git a/components/style/dom_apis.rs b/components/style/dom_apis.rs new file mode 100644 index 00000000000..056f5dd2a12 --- /dev/null +++ b/components/style/dom_apis.rs @@ -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( + element: &E, + selector_list: &SelectorList, + 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) +} diff --git a/components/style/lib.rs b/components/style/lib.rs index 997efddb026..b4b40cf56db 100644 --- a/components/style/lib.rs +++ b/components/style/lib.rs @@ -107,6 +107,7 @@ pub mod counter_style; pub mod custom_properties; pub mod data; pub mod dom; +pub mod dom_apis; pub mod driver; pub mod element_state; #[cfg(feature = "servo")] mod encoding_support; diff --git a/ports/geckolib/glue.rs b/ports/geckolib/glue.rs index f7a1357549b..53d65991e70 100644 --- a/ports/geckolib/glue.rs +++ b/ports/geckolib/glue.rs @@ -1556,18 +1556,15 @@ pub unsafe extern "C" fn Servo_SelectorList_Matches( selectors: RawServoSelectorListBorrowed, ) -> bool { use std::borrow::Borrow; + use style::dom_apis; 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(); - selectors::matching::matches_selector_list(&selectors, &element, &mut context) + dom_apis::element_matches( + &element, + &selectors, + element.owner_document_quirks_mode(), + ) } #[no_mangle]