diff --git a/components/style/gecko_selector_impl.rs b/components/style/gecko_selector_impl.rs index 0966e157db1..055bf5f7ff6 100644 --- a/components/style/gecko_selector_impl.rs +++ b/components/style/gecko_selector_impl.rs @@ -6,7 +6,7 @@ use element_state::ElementState; use selector_impl::PseudoElementCascadeType; use selector_impl::{attr_exists_selector_is_shareable, attr_equals_selector_is_shareable}; use selectors::parser::{ParserContext, SelectorImpl, AttrSelector}; -use string_cache::{Atom, WeakAtom}; +use string_cache::{Atom, WeakAtom, Namespace, WeakNamespace}; use stylesheets::Stylesheet; #[derive(Debug, Clone, PartialEq, Eq)] @@ -154,8 +154,8 @@ impl SelectorImpl for GeckoSelectorImpl { type Identifier = Atom; type ClassName = Atom; type LocalName = Atom; - type Namespace = Atom; - type BorrowedNamespace = WeakAtom; + type Namespace = Namespace; + type BorrowedNamespace = WeakNamespace; type BorrowedLocalName = WeakAtom; type PseudoElement = PseudoElement; diff --git a/ports/geckolib/string_cache/lib.rs b/ports/geckolib/string_cache/lib.rs index e02c14fc60b..3d3e5709674 100644 --- a/ports/geckolib/string_cache/lib.rs +++ b/ports/geckolib/string_cache/lib.rs @@ -30,19 +30,9 @@ use std::slice; #[macro_use] pub mod atom_macro; +pub mod namespace; -#[macro_export] -macro_rules! ns { - () => { atom!("") } -} - -pub type Namespace = Atom; - -#[allow(non_snake_case)] -#[inline] -pub fn Namespace(atom: Atom) -> Atom { - atom -} +pub use namespace::{Namespace, WeakNamespace}; /// A strong reference to a Gecko atom. #[derive(PartialEq, Eq)] diff --git a/ports/geckolib/string_cache/namespace.rs b/ports/geckolib/string_cache/namespace.rs new file mode 100644 index 00000000000..6aadd0e3203 --- /dev/null +++ b/ports/geckolib/string_cache/namespace.rs @@ -0,0 +1,73 @@ +/* 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/. */ + +use gecko_bindings::structs::nsIAtom; +use selectors::bloom::BloomHash; +use std::borrow::Borrow; +use std::ops::Deref; +use {Atom, WeakAtom}; + +#[macro_export] +macro_rules! ns { + () => { $crate::Namespace(atom!("")) } +} + +#[derive(Debug, PartialEq, Eq, Clone, Default, Hash)] +pub struct Namespace(pub Atom); +pub struct WeakNamespace(WeakAtom); + +impl Deref for Namespace { + type Target = WeakNamespace; + + #[inline] + fn deref(&self) -> &WeakNamespace { + let weak: *const WeakAtom = &*self.0; + unsafe { + &*(weak as *const WeakNamespace) + } + } +} + +impl Borrow for Namespace { + #[inline] + fn borrow(&self) -> &WeakNamespace { + self + } +} + +impl WeakNamespace { + #[inline] + pub unsafe fn new<'a>(atom: *mut nsIAtom) -> &'a Self { + &*(atom as *const WeakNamespace) + } + + #[inline] + pub fn clone(&self) -> Namespace { + Namespace(self.0.clone()) + } +} + +impl Eq for WeakNamespace {} +impl PartialEq for WeakNamespace { + #[inline] + fn eq(&self, other: &Self) -> bool { + let weak: *const WeakNamespace = self; + let other: *const WeakNamespace = other; + weak == other + } +} + +impl BloomHash for Namespace { + #[inline] + fn bloom_hash(&self) -> u32 { + self.0.get_hash() + } +} + +impl BloomHash for WeakNamespace { + #[inline] + fn bloom_hash(&self) -> u32 { + self.0.get_hash() + } +} diff --git a/ports/geckolib/wrapper.rs b/ports/geckolib/wrapper.rs index 518cc061089..b1299ff7bc5 100644 --- a/ports/geckolib/wrapper.rs +++ b/ports/geckolib/wrapper.rs @@ -26,7 +26,7 @@ use gecko_bindings::bindings::{Gecko_LocalName, Gecko_Namespace, Gecko_NodeIsEle use gecko_bindings::bindings::{RawGeckoDocument, RawGeckoElement, RawGeckoNode}; use gecko_bindings::structs::{NODE_HAS_DIRTY_DESCENDANTS_FOR_SERVO, NODE_IS_DIRTY_FOR_SERVO}; use gecko_bindings::structs::{nsIAtom, nsChangeHint, nsStyleContext}; -use gecko_string_cache::{Atom, Namespace, WeakAtom}; +use gecko_string_cache::{Atom, Namespace, WeakAtom, WeakNamespace}; use glue::GeckoDeclarationBlock; use libc::uintptr_t; use selectors::Element; @@ -433,7 +433,7 @@ impl<'le> TElement for GeckoElement<'le> { fn has_attr(&self, namespace: &Namespace, attr: &Atom) -> bool { unsafe { bindings::Gecko_HasAttr(self.element, - namespace.as_ptr(), + namespace.0.as_ptr(), attr.as_ptr()) } } @@ -442,7 +442,7 @@ impl<'le> TElement for GeckoElement<'le> { fn attr_equals(&self, namespace: &Namespace, attr: &Atom, val: &Atom) -> bool { unsafe { bindings::Gecko_AttrEquals(self.element, - namespace.as_ptr(), + namespace.0.as_ptr(), attr.as_ptr(), val.as_ptr(), /* ignoreCase = */ false) @@ -506,9 +506,9 @@ impl<'le> ::selectors::Element for GeckoElement<'le> { } } - fn get_namespace(&self) -> &WeakAtom { + fn get_namespace(&self) -> &WeakNamespace { unsafe { - WeakAtom::new(Gecko_Namespace(self.element)) + WeakNamespace::new(Gecko_Namespace(self.element)) } } @@ -577,7 +577,7 @@ impl AttrSelectorHelpers for AttrSelector { fn ns_or_null(&self) -> *mut nsIAtom { match self.namespace { NamespaceConstraint::Any => ptr::null_mut(), - NamespaceConstraint::Specific(ref ns) => ns.as_ptr(), + NamespaceConstraint::Specific(ref ns) => ns.0.as_ptr(), } }