style: Do not use borrowed types in the selectors::Element trait.

Closes #22972
Closes #23463
This commit is contained in:
Evgeniy Reizner 2019-05-26 19:10:24 +03:00 committed by Emilio Cobos Álvarez
parent 2e5078e9c5
commit 6f1df517e0
8 changed files with 100 additions and 42 deletions

View file

@ -715,6 +715,14 @@ impl<'le> TElement for ServoLayoutElement<'le> {
.map(ServoShadowRoot::from_layout_js) .map(ServoShadowRoot::from_layout_js)
} }
} }
fn local_name(&self) -> &LocalName {
self.element.local_name()
}
fn namespace(&self) -> &Namespace {
self.element.namespace()
}
} }
impl<'le> PartialEq for ServoLayoutElement<'le> { impl<'le> PartialEq for ServoLayoutElement<'le> {
@ -879,13 +887,19 @@ impl<'le> ::selectors::Element for ServoLayoutElement<'le> {
} }
#[inline] #[inline]
fn local_name(&self) -> &LocalName { fn has_local_name(&self, name: &LocalName) -> bool {
self.element.local_name() self.element.local_name() == name
} }
#[inline] #[inline]
fn namespace(&self) -> &Namespace { fn has_namespace(&self, ns: &Namespace) -> bool {
self.element.namespace() self.element.namespace() == ns
}
#[inline]
fn is_same_type(&self, other: &Self) -> bool {
self.element.local_name() == other.element.local_name() &&
self.element.namespace() == other.element.namespace()
} }
fn is_pseudo_element(&self) -> bool { fn is_pseudo_element(&self) -> bool {
@ -1266,8 +1280,8 @@ where
loop { loop {
let next_node = if let Some(ref node) = current_node { let next_node = if let Some(ref node) = current_node {
if let Some(element) = node.as_element() { if let Some(element) = node.as_element() {
if element.local_name() == &local_name!("summary") && if element.has_local_name(&local_name!("summary")) &&
element.namespace() == &ns!(html) element.has_namespace(&ns!(html))
{ {
self.current_node = None; self.current_node = None;
return Some(node.clone()); return Some(node.clone());
@ -1286,8 +1300,10 @@ where
let node = self.current_node.clone(); let node = self.current_node.clone();
let node = node.and_then(|node| { let node = node.and_then(|node| {
if node.is_element() && if node.is_element() &&
node.as_element().unwrap().local_name() == &local_name!("summary") && node.as_element()
node.as_element().unwrap().namespace() == &ns!(html) .unwrap()
.has_local_name(&local_name!("summary")) &&
node.as_element().unwrap().has_namespace(&ns!(html))
{ {
unsafe { node.dangerous_next_sibling() } unsafe { node.dangerous_next_sibling() }
} else { } else {
@ -1437,13 +1453,19 @@ impl<'le> ::selectors::Element for ServoThreadSafeLayoutElement<'le> {
} }
#[inline] #[inline]
fn local_name(&self) -> &LocalName { fn has_local_name(&self, name: &LocalName) -> bool {
self.element.local_name() self.element.local_name() == name
} }
#[inline] #[inline]
fn namespace(&self) -> &Namespace { fn has_namespace(&self, ns: &Namespace) -> bool {
self.element.namespace() self.element.namespace() == ns
}
#[inline]
fn is_same_type(&self, other: &Self) -> bool {
self.element.local_name() == other.element.local_name() &&
self.element.namespace() == other.element.namespace()
} }
fn match_pseudo_element( fn match_pseudo_element(

View file

@ -3004,12 +3004,17 @@ impl<'a> SelectorsElement for DomRoot<Element> {
}) })
} }
fn local_name(&self) -> &LocalName { fn has_local_name(&self, local_name: &LocalName) -> bool {
Element::local_name(self) Element::local_name(self) == local_name
} }
fn namespace(&self) -> &Namespace { fn has_namespace(&self, ns: &Namespace) -> bool {
Element::namespace(self) Element::namespace(self) == ns
}
fn is_same_type(&self, other: &Self) -> bool {
Element::local_name(self) == Element::local_name(other) &&
Element::namespace(self) == Element::namespace(other)
} }
fn match_non_ts_pseudo_class<F>( fn match_non_ts_pseudo_class<F>(

View file

@ -391,7 +391,7 @@ pub trait ThreadSafeLayoutElement:
#[inline] #[inline]
fn get_details_summary_pseudo(&self) -> Option<Self> { fn get_details_summary_pseudo(&self) -> Option<Self> {
if self.local_name() == &local_name!("details") && self.namespace() == &ns!(html) { if self.has_local_name(&local_name!("details")) && self.has_namespace(&ns!(html)) {
Some(self.with_pseudo(PseudoElementType::DetailsSummary)) Some(self.with_pseudo(PseudoElementType::DetailsSummary))
} else { } else {
None None
@ -400,8 +400,8 @@ pub trait ThreadSafeLayoutElement:
#[inline] #[inline]
fn get_details_content_pseudo(&self) -> Option<Self> { fn get_details_content_pseudo(&self) -> Option<Self> {
if self.local_name() == &local_name!("details") && if self.has_local_name(&local_name!("details")) &&
self.namespace() == &ns!(html) && self.has_namespace(&ns!(html)) &&
self.get_attr(&ns!(), &local_name!("open")).is_some() self.get_attr(&ns!(), &local_name!("open")).is_some()
{ {
Some(self.with_pseudo(PseudoElementType::DetailsContent)) Some(self.with_pseudo(PseudoElementType::DetailsContent))

View file

@ -596,7 +596,7 @@ where
&local_name.lower_name, &local_name.lower_name,
) )
.borrow(); .borrow();
element.local_name() == name element.has_local_name(name)
} }
/// Determines whether the given element matches the given compound selector. /// Determines whether the given element matches the given compound selector.
@ -681,11 +681,11 @@ where
Component::LocalName(ref local_name) => matches_local_name(element, local_name), Component::LocalName(ref local_name) => matches_local_name(element, local_name),
Component::ExplicitUniversalType | Component::ExplicitAnyNamespace => true, Component::ExplicitUniversalType | Component::ExplicitAnyNamespace => true,
Component::Namespace(_, ref url) | Component::DefaultNamespace(ref url) => { Component::Namespace(_, ref url) | Component::DefaultNamespace(ref url) => {
element.namespace() == url.borrow() element.has_namespace(&url.borrow())
}, },
Component::ExplicitNoNamespace => { Component::ExplicitNoNamespace => {
let ns = crate::parser::namespace_empty_string::<E::Impl>(); let ns = crate::parser::namespace_empty_string::<E::Impl>();
element.namespace() == ns.borrow() element.has_namespace(&ns.borrow())
}, },
Component::ID(ref id) => { Component::ID(ref id) => {
element.has_id(id, context.shared.classes_and_ids_case_sensitivity()) element.has_id(id, context.shared.classes_and_ids_case_sensitivity())
@ -897,11 +897,6 @@ where
} }
} }
#[inline]
fn same_type<E: Element>(a: &E, b: &E) -> bool {
a.local_name() == b.local_name() && a.namespace() == b.namespace()
}
#[inline] #[inline]
fn nth_child_index<E>( fn nth_child_index<E>(
element: &E, element: &E,
@ -924,7 +919,7 @@ where
let mut curr = element.clone(); let mut curr = element.clone();
while let Some(e) = curr.prev_sibling_element() { while let Some(e) = curr.prev_sibling_element() {
curr = e; curr = e;
if !is_of_type || same_type(element, &curr) { if !is_of_type || element.is_same_type(&curr) {
if let Some(i) = c.lookup(curr.opaque()) { if let Some(i) = c.lookup(curr.opaque()) {
return i - index; return i - index;
} }
@ -945,7 +940,7 @@ where
}; };
while let Some(e) = next(curr) { while let Some(e) = next(curr) {
curr = e; curr = e;
if !is_of_type || same_type(element, &curr) { if !is_of_type || element.is_same_type(&curr) {
// If we're computing indices from the left, check each element in the // If we're computing indices from the left, check each element in the
// cache. We handle the indices-from-the-right case at the top of this // cache. We handle the indices-from-the-right case at the top of this
// function. // function.

View file

@ -62,10 +62,13 @@ pub trait Element: Sized + Clone + Debug {
fn is_html_element_in_html_document(&self) -> bool; fn is_html_element_in_html_document(&self) -> bool;
fn local_name(&self) -> &<Self::Impl as SelectorImpl>::BorrowedLocalName; fn has_local_name(&self, local_name: &<Self::Impl as SelectorImpl>::BorrowedLocalName) -> bool;
/// Empty string for no namespace /// Empty string for no namespace
fn namespace(&self) -> &<Self::Impl as SelectorImpl>::BorrowedNamespaceUrl; fn has_namespace(&self, ns: &<Self::Impl as SelectorImpl>::BorrowedNamespaceUrl) -> bool;
/// Whether this element and the `other` element have the same local name and namespace.
fn is_same_type(&self, other: &Self) -> bool;
fn attr_matches( fn attr_matches(
&self, &self,

View file

@ -882,6 +882,13 @@ pub trait TElement:
hints: &mut V, hints: &mut V,
) where ) where
V: Push<ApplicableDeclarationBlock>; V: Push<ApplicableDeclarationBlock>;
/// Returns element's local name.
fn local_name(&self) -> &<SelectorImpl as selectors::parser::SelectorImpl>::BorrowedLocalName;
/// Returns element's namespace.
fn namespace(&self)
-> &<SelectorImpl as selectors::parser::SelectorImpl>::BorrowedNamespaceUrl;
} }
/// TNode and TElement aren't Send because we want to be careful and explicit /// TNode and TElement aren't Send because we want to be careful and explicit

View file

@ -1166,6 +1166,19 @@ impl<'le> TElement for GeckoElement<'le> {
self.namespace_id() == structs::root::kNameSpaceID_XUL as i32 self.namespace_id() == structs::root::kNameSpaceID_XUL as i32
} }
#[inline]
fn local_name(&self) -> &WeakAtom {
unsafe { WeakAtom::new(self.as_node().node_info().mInner.mName) }
}
#[inline]
fn namespace(&self) -> &WeakNamespace {
unsafe {
let namespace_manager = structs::nsContentUtils_sNameSpaceManager;
WeakNamespace::new((*namespace_manager).mURIArray[self.namespace_id() as usize].mRawPtr)
}
}
/// Return the list of slotted nodes of this node. /// Return the list of slotted nodes of this node.
#[inline] #[inline]
fn slotted_nodes(&self) -> &[Self::ConcreteNode] { fn slotted_nodes(&self) -> &[Self::ConcreteNode] {
@ -2071,16 +2084,18 @@ impl<'le> ::selectors::Element for GeckoElement<'le> {
} }
#[inline] #[inline]
fn local_name(&self) -> &WeakAtom { fn has_local_name(&self, name: &WeakAtom) -> bool {
unsafe { WeakAtom::new(self.as_node().node_info().mInner.mName) } self.local_name() == name
} }
#[inline] #[inline]
fn namespace(&self) -> &WeakNamespace { fn has_namespace(&self, ns: &WeakNamespace) -> bool {
unsafe { self.namespace() == ns
let namespace_manager = structs::nsContentUtils_sNameSpaceManager; }
WeakNamespace::new((*namespace_manager).mURIArray[self.namespace_id() as usize].mRawPtr)
} #[inline]
fn is_same_type(&self, other: &Self) -> bool {
self.local_name() == other.local_name() && self.namespace() == other.namespace()
} }
fn match_non_ts_pseudo_class<F>( fn match_non_ts_pseudo_class<F>(

View file

@ -312,13 +312,24 @@ where
} }
#[inline] #[inline]
fn local_name(&self) -> &<Self::Impl as ::selectors::SelectorImpl>::BorrowedLocalName { fn has_local_name(
self.element.local_name() &self,
local_name: &<Self::Impl as ::selectors::SelectorImpl>::BorrowedLocalName,
) -> bool {
self.element.has_local_name(local_name)
} }
#[inline] #[inline]
fn namespace(&self) -> &<Self::Impl as ::selectors::SelectorImpl>::BorrowedNamespaceUrl { fn has_namespace(
self.element.namespace() &self,
ns: &<Self::Impl as ::selectors::SelectorImpl>::BorrowedNamespaceUrl,
) -> bool {
self.element.has_namespace(ns)
}
#[inline]
fn is_same_type(&self, other: &Self) -> bool {
self.element.is_same_type(&other.element)
} }
fn attr_matches( fn attr_matches(