style: Add infrastructure to match :host.

This commit is contained in:
Emilio Cobos Álvarez 2018-03-08 22:03:42 +01:00
parent 1654f297ca
commit 9fa2618197
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
9 changed files with 159 additions and 41 deletions

View file

@ -274,6 +274,20 @@ impl NonTSPseudoClass {
}
}
impl ::selectors::parser::NonTSPseudoClass for NonTSPseudoClass {
type Impl = SelectorImpl;
#[inline]
fn is_active_or_hover(&self) -> bool {
matches!(*self, NonTSPseudoClass::Active | NonTSPseudoClass::Hover)
}
#[inline]
fn is_host(&self) -> bool {
false // TODO(emilio)
}
}
/// The dummy struct we use to implement our selector parsing.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SelectorImpl;
@ -291,12 +305,6 @@ impl ::selectors::SelectorImpl for SelectorImpl {
type PseudoElement = PseudoElement;
type NonTSPseudoClass = NonTSPseudoClass;
#[inline]
fn is_active_or_hover(pseudo_class: &Self::NonTSPseudoClass) -> bool {
matches!(*pseudo_class, NonTSPseudoClass::Active |
NonTSPseudoClass::Hover)
}
}
impl<'a> SelectorParser<'a> {

View file

@ -1822,12 +1822,21 @@ impl<'le> ::selectors::Element for GeckoElement<'le> {
#[inline]
fn parent_element(&self) -> Option<Self> {
// FIXME(emilio): This will need to jump across if the parent node is a
// shadow root to get the shadow host.
let parent_node = self.as_node().parent_node();
parent_node.and_then(|n| n.as_element())
}
#[inline]
fn parent_node_is_shadow_root(&self) -> bool {
self.as_node().parent_node().map_or(false, |p| p.is_shadow_root())
}
#[inline]
fn containing_shadow_host(&self) -> Option<Self> {
let shadow = self.containing_shadow()?;
Some(shadow.host())
}
#[inline]
fn pseudo_element_originating_element(&self) -> Option<Self> {
debug_assert!(self.implemented_pseudo_element().is_some());

View file

@ -271,28 +271,37 @@ where
}
fn parent_element(&self) -> Option<Self> {
self.element.parent_element()
.map(|e| ElementWrapper::new(e, self.snapshot_map))
let parent = self.element.parent_element()?;
Some(Self::new(parent, self.snapshot_map))
}
fn parent_node_is_shadow_root(&self) -> bool {
self.element.parent_node_is_shadow_root()
}
fn containing_shadow_host(&self) -> Option<Self> {
let host = self.element.containing_shadow_host()?;
Some(Self::new(host, self.snapshot_map))
}
fn first_child_element(&self) -> Option<Self> {
self.element.first_child_element()
.map(|e| ElementWrapper::new(e, self.snapshot_map))
let child = self.element.first_child_element()?;
Some(Self::new(child, self.snapshot_map))
}
fn last_child_element(&self) -> Option<Self> {
self.element.last_child_element()
.map(|e| ElementWrapper::new(e, self.snapshot_map))
let child = self.element.last_child_element()?;
Some(Self::new(child, self.snapshot_map))
}
fn prev_sibling_element(&self) -> Option<Self> {
self.element.prev_sibling_element()
.map(|e| ElementWrapper::new(e, self.snapshot_map))
let sibling = self.element.prev_sibling_element()?;
Some(Self::new(sibling, self.snapshot_map))
}
fn next_sibling_element(&self) -> Option<Self> {
self.element.next_sibling_element()
.map(|e| ElementWrapper::new(e, self.snapshot_map))
let sibling = self.element.next_sibling_element()?;
Some(Self::new(sibling, self.snapshot_map))
}
#[inline]

View file

@ -308,6 +308,20 @@ pub enum NonTSPseudoClass {
Visited,
}
impl ::selectors::parser::NonTSPseudoClass for NonTSPseudoClass {
type Impl = SelectorImpl;
#[inline]
fn is_host(&self) -> bool {
false
}
#[inline]
fn is_active_or_hover(&self) -> bool {
matches!(*self, NonTSPseudoClass::Active | NonTSPseudoClass::Hover)
}
}
impl ToCss for NonTSPseudoClass {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
use self::NonTSPseudoClass::*;
@ -423,12 +437,6 @@ impl ::selectors::SelectorImpl for SelectorImpl {
type NamespaceUrl = Namespace;
type BorrowedLocalName = LocalName;
type BorrowedNamespaceUrl = Namespace;
#[inline]
fn is_active_or_hover(pseudo_class: &Self::NonTSPseudoClass) -> bool {
matches!(*pseudo_class, NonTSPseudoClass::Active |
NonTSPseudoClass::Hover)
}
}
impl<'a, 'i> ::selectors::Parser<'i> for SelectorParser<'a> {