style/
selector_parser.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! The pseudo-classes and pseudo-elements supported by the style system.
6
7#![deny(missing_docs)]
8
9use crate::stylesheets::{Namespaces, Origin, UrlExtraData};
10use crate::values::serialize_atom_identifier;
11use crate::Atom;
12use cssparser::{Parser as CssParser, ParserInput};
13use dom::ElementState;
14use selectors::parser::{ParseRelative, SelectorList};
15use std::fmt::{self, Debug, Write};
16use style_traits::{CssWriter, ParseError, ToCss};
17
18/// A convenient alias for the type that represents an attribute value used for
19/// selector parser implementation.
20pub type AttrValue = <SelectorImpl as ::selectors::SelectorImpl>::AttrValue;
21
22#[cfg(feature = "servo")]
23pub use crate::servo::selector_parser::*;
24
25#[cfg(feature = "gecko")]
26pub use crate::gecko::selector_parser::*;
27
28#[cfg(feature = "servo")]
29pub use crate::servo::selector_parser::ServoElementSnapshot as Snapshot;
30
31#[cfg(feature = "gecko")]
32pub use crate::gecko::snapshot::GeckoElementSnapshot as Snapshot;
33
34#[cfg(feature = "servo")]
35pub use crate::servo::restyle_damage::ServoRestyleDamage as RestyleDamage;
36
37#[cfg(feature = "gecko")]
38pub use crate::gecko::restyle_damage::GeckoRestyleDamage as RestyleDamage;
39
40/// Servo's selector parser.
41#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
42pub struct SelectorParser<'a> {
43    /// The origin of the stylesheet we're parsing.
44    pub stylesheet_origin: Origin,
45    /// The namespace set of the stylesheet.
46    pub namespaces: &'a Namespaces,
47    /// The extra URL data of the stylesheet, which is used to look up
48    /// whether we are parsing a chrome:// URL style sheet.
49    pub url_data: &'a UrlExtraData,
50    /// Whether we're parsing selectors for `@supports`
51    pub for_supports_rule: bool,
52}
53
54impl<'a> SelectorParser<'a> {
55    /// Parse a selector list with an author origin and without taking into
56    /// account namespaces.
57    ///
58    /// This is used for some DOM APIs like `querySelector`.
59    pub fn parse_author_origin_no_namespace<'i>(
60        input: &'i str,
61        url_data: &UrlExtraData,
62    ) -> Result<SelectorList<SelectorImpl>, ParseError<'i>> {
63        let namespaces = Namespaces::default();
64        let parser = SelectorParser {
65            stylesheet_origin: Origin::Author,
66            namespaces: &namespaces,
67            url_data,
68            for_supports_rule: false,
69        };
70        let mut input = ParserInput::new(input);
71        SelectorList::parse(&parser, &mut CssParser::new(&mut input), ParseRelative::No)
72    }
73
74    /// Whether we're parsing selectors in a user-agent stylesheet.
75    pub fn in_user_agent_stylesheet(&self) -> bool {
76        matches!(self.stylesheet_origin, Origin::UserAgent)
77    }
78
79    /// Whether we're parsing selectors in a stylesheet that has chrome
80    /// privilege.
81    pub fn chrome_rules_enabled(&self) -> bool {
82        self.url_data.chrome_rules_enabled() || self.stylesheet_origin == Origin::User
83    }
84}
85
86/// This enumeration determines how a pseudo-element cascades.
87#[derive(Clone, Debug, Eq, PartialEq)]
88pub enum PseudoElementCascadeType {
89    /// Eagerly cascaded pseudo-elements are "normal" pseudo-elements (i.e.
90    /// `::before` and `::after`). They inherit styles normally as another
91    /// selector would do, and they're computed as part of the cascade.
92    ///
93    /// These kind of pseudo-elements require more up-front computation and
94    /// storage and thus should used for public pseudo-elements that can be used
95    /// on many element types (such as `::before` and `::after`).
96    Eager,
97    /// Lazy pseudo-elements are affected by selector matching, but they're only
98    /// computed when needed, and not before. They're useful for general
99    /// pseudo-elements that are not very common or that do not apply to many
100    /// elements. For instance in Servo this is used for `::backdrop` and
101    /// `::marker`.
102    Lazy,
103    /// Precomputed pseudo-elements skip the cascade process entirely, mostly as
104    /// an optimisation since they are private pseudo-elements (like
105    /// `::-servo-details-content`).
106    ///
107    /// This pseudo-elements are resolved on the fly using *only* global rules
108    /// (rules of the form `*|*`), and applying them to the parent style so are
109    /// mainly useful for user-agent stylesheets.
110    Precomputed,
111}
112
113/// A per-pseudo map, from a given pseudo to a `T`.
114#[derive(Clone, MallocSizeOf)]
115pub struct PerPseudoElementMap<T> {
116    entries: [Option<T>; PSEUDO_COUNT],
117}
118
119impl<T> Default for PerPseudoElementMap<T> {
120    fn default() -> Self {
121        Self {
122            entries: PseudoElement::pseudo_none_array(),
123        }
124    }
125}
126
127impl<T> Debug for PerPseudoElementMap<T>
128where
129    T: Debug,
130{
131    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
132        f.write_char('[')?;
133        let mut first = true;
134        for entry in self.entries.iter() {
135            if !first {
136                f.write_str(", ")?;
137            }
138            first = false;
139            entry.fmt(f)?;
140        }
141        f.write_char(']')
142    }
143}
144
145impl<T> PerPseudoElementMap<T> {
146    /// Get an entry in the map.
147    pub fn get(&self, pseudo: &PseudoElement) -> Option<&T> {
148        self.entries[pseudo.index()].as_ref()
149    }
150
151    /// Clear this enumerated array.
152    pub fn clear(&mut self) {
153        *self = Self::default();
154    }
155
156    /// Set an entry value.
157    ///
158    /// Returns an error if the element is not a simple pseudo.
159    pub fn set(&mut self, pseudo: &PseudoElement, value: T) {
160        self.entries[pseudo.index()] = Some(value);
161    }
162
163    /// Get an entry for `pseudo`, or create it with calling `f`.
164    pub fn get_or_insert_with<F>(&mut self, pseudo: &PseudoElement, f: F) -> &mut T
165    where
166        F: FnOnce() -> T,
167    {
168        let index = pseudo.index();
169        if self.entries[index].is_none() {
170            self.entries[index] = Some(f());
171        }
172        self.entries[index].as_mut().unwrap()
173    }
174
175    /// Get an iterator for the entries.
176    pub fn iter(&self) -> std::slice::Iter<Option<T>> {
177        self.entries.iter()
178    }
179
180    /// Get a mutable iterator for the entries.
181    pub fn iter_mut(&mut self) -> std::slice::IterMut<Option<T>> {
182        self.entries.iter_mut()
183    }
184}
185
186/// Values for the :dir() pseudo class
187///
188/// "ltr" and "rtl" values are normalized to lowercase.
189#[derive(Clone, Debug, Eq, MallocSizeOf, PartialEq, ToShmem)]
190pub struct Direction(pub Atom);
191
192/// Horizontal values for the :dir() pseudo class
193#[derive(Clone, Debug, Eq, PartialEq)]
194pub enum HorizontalDirection {
195    /// :dir(ltr)
196    Ltr,
197    /// :dir(rtl)
198    Rtl,
199}
200
201impl Direction {
202    /// Parse a direction value.
203    pub fn parse<'i, 't>(parser: &mut CssParser<'i, 't>) -> Result<Self, ParseError<'i>> {
204        let ident = parser.expect_ident()?;
205        Ok(Direction(match_ignore_ascii_case! { &ident,
206            "rtl" => atom!("rtl"),
207            "ltr" => atom!("ltr"),
208            _ => Atom::from(ident.as_ref()),
209        }))
210    }
211
212    /// Convert this Direction into a HorizontalDirection, if applicable
213    pub fn as_horizontal_direction(&self) -> Option<HorizontalDirection> {
214        if self.0 == atom!("ltr") {
215            Some(HorizontalDirection::Ltr)
216        } else if self.0 == atom!("rtl") {
217            Some(HorizontalDirection::Rtl)
218        } else {
219            None
220        }
221    }
222
223    /// Gets the element state relevant to this :dir() selector.
224    pub fn element_state(&self) -> ElementState {
225        match self.as_horizontal_direction() {
226            Some(HorizontalDirection::Ltr) => ElementState::LTR,
227            Some(HorizontalDirection::Rtl) => ElementState::RTL,
228            None => ElementState::empty(),
229        }
230    }
231}
232
233impl ToCss for Direction {
234    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
235    where
236        W: Write,
237    {
238        serialize_atom_identifier(&self.0, dest)
239    }
240}