style/servo/
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#![deny(missing_docs)]
6
7//! Servo's selector parser.
8
9use crate::attr::{AttrIdentifier, AttrValue};
10use crate::computed_value_flags::ComputedValueFlags;
11use crate::derives::*;
12use crate::dom::{OpaqueNode, TElement, TNode};
13use crate::invalidation::element::document_state::InvalidationMatchingData;
14use crate::invalidation::element::element_wrapper::ElementSnapshot;
15use crate::properties::longhands::display::computed_value::T as Display;
16use crate::properties::{ComputedValues, PropertyFlags};
17use crate::selector_parser::AttrValue as SelectorAttrValue;
18use crate::selector_parser::{PseudoElementCascadeType, SelectorParser};
19use crate::values::{AtomIdent, AtomString};
20use crate::{Atom, CaseSensitivityExt, LocalName, Namespace, Prefix};
21use cssparser::{
22    match_ignore_ascii_case, serialize_identifier, CowRcStr, Parser as CssParser, SourceLocation,
23    ToCss,
24};
25use dom::{DocumentState, ElementState};
26use rustc_hash::FxHashMap;
27use selectors::attr::{AttrSelectorOperation, CaseSensitivity, NamespaceConstraint};
28use selectors::parser::SelectorParseErrorKind;
29use selectors::visitor::SelectorVisitor;
30use std::fmt;
31use std::mem;
32use std::ops::{Deref, DerefMut};
33use style_traits::{ParseError, StyleParseErrorKind};
34
35/// A pseudo-element, both public and private.
36///
37/// NB: If you add to this list, be sure to update `each_simple_pseudo_element` too.
38#[derive(
39    Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, PartialEq, Serialize, ToShmem,
40)]
41#[allow(missing_docs)]
42#[repr(usize)]
43pub enum PseudoElement {
44    // Eager pseudos. Keep these first so that eager_index() works.
45    After = 0,
46    Before,
47    Selection,
48    // If/when :first-letter is added, update is_first_letter accordingly.
49
50    // If/when :first-line is added, update is_first_line accordingly.
51
52    // If/when ::first-letter, ::first-line, or ::placeholder are added, adjust
53    // our property_restriction implementation to do property filtering for
54    // them.  Also, make sure the UA sheet has the !important rules some of the
55    // APPLIES_TO_PLACEHOLDER properties expect!
56
57    // Non-eager pseudos.
58    Backdrop,
59    DetailsSummary,
60    DetailsContent,
61    Marker,
62
63    // Implemented pseudos. These pseudo elements are representing the
64    // elements within an UA shadow DOM, and matching the elements with
65    // their appropriate styles.
66    ColorSwatch,
67    Placeholder,
68
69    // Private, Servo-specific implemented pseudos. Only matchable in UA sheet.
70    ServoTextControlInnerContainer,
71    ServoTextControlInnerEditor,
72
73    // Other Servo-specific pseudos.
74    ServoAnonymousBox,
75    ServoAnonymousTable,
76    ServoAnonymousTableCell,
77    ServoAnonymousTableRow,
78    ServoTableGrid,
79    ServoTableWrapper,
80}
81
82/// The count of all pseudo-elements.
83pub const PSEUDO_COUNT: usize = PseudoElement::ServoTableWrapper as usize + 1;
84
85impl ToCss for PseudoElement {
86    fn to_css<W>(&self, dest: &mut W) -> fmt::Result
87    where
88        W: fmt::Write,
89    {
90        use self::PseudoElement::*;
91        dest.write_str(match *self {
92            After => "::after",
93            Before => "::before",
94            Selection => "::selection",
95            Backdrop => "::backdrop",
96            DetailsSummary => "::-servo-details-summary",
97            DetailsContent => "::details-content",
98            Marker => "::marker",
99            ColorSwatch => "::color-swatch",
100            Placeholder => "::placeholder",
101            ServoTextControlInnerContainer => "::-servo-text-control-inner-container",
102            ServoTextControlInnerEditor => "::-servo-text-control-inner-editor",
103            ServoAnonymousBox => "::-servo-anonymous-box",
104            ServoAnonymousTable => "::-servo-anonymous-table",
105            ServoAnonymousTableCell => "::-servo-anonymous-table-cell",
106            ServoAnonymousTableRow => "::-servo-anonymous-table-row",
107            ServoTableGrid => "::-servo-table-grid",
108            ServoTableWrapper => "::-servo-table-wrapper",
109        })
110    }
111}
112
113impl ::selectors::parser::PseudoElement for PseudoElement {
114    type Impl = SelectorImpl;
115}
116
117/// The number of eager pseudo-elements. Keep this in sync with cascade_type.
118pub const EAGER_PSEUDO_COUNT: usize = 3;
119
120impl PseudoElement {
121    /// Gets the canonical index of this eagerly-cascaded pseudo-element.
122    #[inline]
123    pub fn eager_index(&self) -> usize {
124        debug_assert!(self.is_eager());
125        self.clone() as usize
126    }
127
128    /// An index for this pseudo-element to be indexed in an enumerated array.
129    #[inline]
130    pub fn index(&self) -> usize {
131        self.clone() as usize
132    }
133
134    /// An array of `None`, one per pseudo-element.
135    pub fn pseudo_none_array<T>() -> [Option<T>; PSEUDO_COUNT] {
136        Default::default()
137    }
138
139    /// Creates a pseudo-element from an eager index.
140    #[inline]
141    pub fn from_eager_index(i: usize) -> Self {
142        assert!(i < EAGER_PSEUDO_COUNT);
143        let result: PseudoElement = unsafe { mem::transmute(i) };
144        debug_assert!(result.is_eager());
145        result
146    }
147
148    /// Whether the current pseudo element is ::before or ::after.
149    #[inline]
150    pub fn is_before_or_after(&self) -> bool {
151        self.is_before() || self.is_after()
152    }
153
154    /// Whether this is an unknown ::-webkit- pseudo-element.
155    #[inline]
156    pub fn is_unknown_webkit_pseudo_element(&self) -> bool {
157        false
158    }
159
160    /// Whether this pseudo-element is the ::marker pseudo.
161    #[inline]
162    pub fn is_marker(&self) -> bool {
163        *self == PseudoElement::Marker
164    }
165
166    /// Whether this pseudo-element is the ::selection pseudo.
167    #[inline]
168    pub fn is_selection(&self) -> bool {
169        *self == PseudoElement::Selection
170    }
171
172    /// Whether this pseudo-element is the ::before pseudo.
173    #[inline]
174    pub fn is_before(&self) -> bool {
175        *self == PseudoElement::Before
176    }
177
178    /// Whether this pseudo-element is the ::after pseudo.
179    #[inline]
180    pub fn is_after(&self) -> bool {
181        *self == PseudoElement::After
182    }
183
184    /// Whether the current pseudo element is :first-letter
185    #[inline]
186    pub fn is_first_letter(&self) -> bool {
187        false
188    }
189
190    /// Whether the current pseudo element is :first-line
191    #[inline]
192    pub fn is_first_line(&self) -> bool {
193        false
194    }
195
196    /// Whether this pseudo-element is representing the color swatch
197    /// inside an `<input>` element.
198    #[inline]
199    pub fn is_color_swatch(&self) -> bool {
200        *self == PseudoElement::ColorSwatch
201    }
202
203    /// Whether this pseudo-element is eagerly-cascaded.
204    #[inline]
205    pub fn is_eager(&self) -> bool {
206        self.cascade_type() == PseudoElementCascadeType::Eager
207    }
208
209    /// Whether this pseudo-element is lazily-cascaded.
210    #[inline]
211    pub fn is_lazy(&self) -> bool {
212        self.cascade_type() == PseudoElementCascadeType::Lazy
213    }
214
215    /// Whether this pseudo-element is for an anonymous box.
216    pub fn is_anon_box(&self) -> bool {
217        self.is_precomputed()
218    }
219
220    /// Whether this pseudo-element skips flex/grid container display-based
221    /// fixup.
222    #[inline]
223    pub fn skip_item_display_fixup(&self) -> bool {
224        !self.is_before_or_after()
225    }
226
227    /// Whether this pseudo-element is precomputed.
228    #[inline]
229    pub fn is_precomputed(&self) -> bool {
230        self.cascade_type() == PseudoElementCascadeType::Precomputed
231    }
232
233    /// Returns which kind of cascade type has this pseudo.
234    ///
235    /// See the documentation for `PseudoElementCascadeType` for how we choose
236    /// which cascade type to use.
237    ///
238    /// Note: Keep eager pseudos in sync with `EAGER_PSEUDO_COUNT` and
239    /// `EMPTY_PSEUDO_ARRAY` in `style/data.rs`
240    #[inline]
241    pub fn cascade_type(&self) -> PseudoElementCascadeType {
242        match *self {
243            PseudoElement::After | PseudoElement::Before | PseudoElement::Selection => {
244                PseudoElementCascadeType::Eager
245            },
246            PseudoElement::Backdrop
247            | PseudoElement::ColorSwatch
248            | PseudoElement::DetailsSummary
249            | PseudoElement::Marker
250            | PseudoElement::Placeholder
251            | PseudoElement::DetailsContent
252            | PseudoElement::ServoTextControlInnerContainer
253            | PseudoElement::ServoTextControlInnerEditor => PseudoElementCascadeType::Lazy,
254            PseudoElement::ServoAnonymousBox
255            | PseudoElement::ServoAnonymousTable
256            | PseudoElement::ServoAnonymousTableCell
257            | PseudoElement::ServoAnonymousTableRow
258            | PseudoElement::ServoTableGrid
259            | PseudoElement::ServoTableWrapper => PseudoElementCascadeType::Precomputed,
260        }
261    }
262
263    /// Covert non-canonical pseudo-element to canonical one, and keep a
264    /// canonical one as it is.
265    pub fn canonical(&self) -> PseudoElement {
266        self.clone()
267    }
268
269    /// Stub, only Gecko needs this
270    pub fn pseudo_info(&self) {
271        ()
272    }
273
274    /// Property flag that properties must have to apply to this pseudo-element.
275    #[inline]
276    pub fn property_restriction(&self) -> Option<PropertyFlags> {
277        None
278    }
279
280    /// Whether this pseudo-element should actually exist if it has
281    /// the given styles.
282    pub fn should_exist(&self, style: &ComputedValues) -> bool {
283        let display = style.get_box().clone_display();
284        if display == Display::None {
285            return false;
286        }
287        if self.is_before_or_after() && style.ineffective_content_property() {
288            return false;
289        }
290
291        true
292    }
293}
294
295/// The type used for storing `:lang` arguments.
296pub type Lang = Box<str>;
297
298/// The type used to store the state argument to the `:state` pseudo-class.
299#[derive(Clone, Debug, Eq, Hash, MallocSizeOf, PartialEq, ToCss, ToShmem)]
300pub struct CustomState(pub AtomIdent);
301
302/// A non tree-structural pseudo-class.
303/// See https://drafts.csswg.org/selectors-4/#structural-pseudos
304#[derive(Clone, Debug, Eq, Hash, MallocSizeOf, PartialEq, ToShmem)]
305#[allow(missing_docs)]
306pub enum NonTSPseudoClass {
307    Active,
308    AnyLink,
309    Autofill,
310    Checked,
311    /// The :state` pseudo-class.
312    CustomState(CustomState),
313    Default,
314    Defined,
315    Disabled,
316    Enabled,
317    Focus,
318    FocusWithin,
319    FocusVisible,
320    Fullscreen,
321    Hover,
322    InRange,
323    Indeterminate,
324    Invalid,
325    Lang(Lang),
326    Link,
327    Modal,
328    MozMeterOptimum,
329    MozMeterSubOptimum,
330    MozMeterSubSubOptimum,
331    Open,
332    Optional,
333    OutOfRange,
334    PlaceholderShown,
335    PopoverOpen,
336    ReadOnly,
337    ReadWrite,
338    Required,
339    ServoNonZeroBorder,
340    Target,
341    UserInvalid,
342    UserValid,
343    Valid,
344    Visited,
345}
346
347impl ::selectors::parser::NonTSPseudoClass for NonTSPseudoClass {
348    type Impl = SelectorImpl;
349
350    #[inline]
351    fn is_active_or_hover(&self) -> bool {
352        matches!(*self, NonTSPseudoClass::Active | NonTSPseudoClass::Hover)
353    }
354
355    #[inline]
356    fn is_user_action_state(&self) -> bool {
357        matches!(
358            *self,
359            NonTSPseudoClass::Active | NonTSPseudoClass::Hover | NonTSPseudoClass::Focus
360        )
361    }
362
363    fn visit<V>(&self, _: &mut V) -> bool
364    where
365        V: SelectorVisitor<Impl = Self::Impl>,
366    {
367        true
368    }
369}
370
371impl ToCss for NonTSPseudoClass {
372    fn to_css<W>(&self, dest: &mut W) -> fmt::Result
373    where
374        W: fmt::Write,
375    {
376        use self::NonTSPseudoClass::*;
377        if let Lang(ref lang) = *self {
378            dest.write_str(":lang(")?;
379            serialize_identifier(lang, dest)?;
380            return dest.write_char(')');
381        }
382
383        dest.write_str(match *self {
384            Self::Active => ":active",
385            Self::AnyLink => ":any-link",
386            Self::Autofill => ":autofill",
387            Self::Checked => ":checked",
388            Self::CustomState(ref state) => {
389                dest.write_str(":state(")?;
390                state.0.to_css(dest)?;
391                return dest.write_char(')');
392            },
393            Self::Default => ":default",
394            Self::Defined => ":defined",
395            Self::Disabled => ":disabled",
396            Self::Enabled => ":enabled",
397            Self::Focus => ":focus",
398            Self::FocusVisible => ":focus-visible",
399            Self::FocusWithin => ":focus-within",
400            Self::Fullscreen => ":fullscreen",
401            Self::Hover => ":hover",
402            Self::InRange => ":in-range",
403            Self::Indeterminate => ":indeterminate",
404            Self::Invalid => ":invalid",
405            Self::Link => ":link",
406            Self::Modal => ":modal",
407            Self::MozMeterOptimum => ":-moz-meter-optimum",
408            Self::MozMeterSubOptimum => ":-moz-meter-sub-optimum",
409            Self::MozMeterSubSubOptimum => ":-moz-meter-sub-sub-optimum",
410            Self::Open => ":open",
411            Self::Optional => ":optional",
412            Self::OutOfRange => ":out-of-range",
413            Self::PlaceholderShown => ":placeholder-shown",
414            Self::PopoverOpen => ":popover-open",
415            Self::ReadOnly => ":read-only",
416            Self::ReadWrite => ":read-write",
417            Self::Required => ":required",
418            Self::ServoNonZeroBorder => ":-servo-nonzero-border",
419            Self::Target => ":target",
420            Self::UserInvalid => ":user-invalid",
421            Self::UserValid => ":user-valid",
422            Self::Valid => ":valid",
423            Self::Visited => ":visited",
424            Self::Lang(_) => unreachable!(),
425        })
426    }
427}
428
429impl NonTSPseudoClass {
430    /// Gets a given state flag for this pseudo-class. This is used to do
431    /// selector matching, and it's set from the DOM.
432    pub fn state_flag(&self) -> ElementState {
433        match *self {
434            Self::Active => ElementState::ACTIVE,
435            Self::AnyLink => ElementState::VISITED_OR_UNVISITED,
436            Self::Autofill => ElementState::AUTOFILL,
437            Self::Checked => ElementState::CHECKED,
438            Self::Default => ElementState::DEFAULT,
439            Self::Defined => ElementState::DEFINED,
440            Self::Disabled => ElementState::DISABLED,
441            Self::Enabled => ElementState::ENABLED,
442            Self::Focus => ElementState::FOCUS,
443            Self::FocusVisible => ElementState::FOCUSRING,
444            Self::FocusWithin => ElementState::FOCUS_WITHIN,
445            Self::Fullscreen => ElementState::FULLSCREEN,
446            Self::Hover => ElementState::HOVER,
447            Self::InRange => ElementState::INRANGE,
448            Self::Indeterminate => ElementState::INDETERMINATE,
449            Self::Invalid => ElementState::INVALID,
450            Self::Link => ElementState::UNVISITED,
451            Self::Modal => ElementState::MODAL,
452            Self::MozMeterOptimum => ElementState::OPTIMUM,
453            Self::MozMeterSubOptimum => ElementState::SUB_OPTIMUM,
454            Self::MozMeterSubSubOptimum => ElementState::SUB_SUB_OPTIMUM,
455            Self::Open => ElementState::OPEN,
456            Self::Optional => ElementState::OPTIONAL_,
457            Self::OutOfRange => ElementState::OUTOFRANGE,
458            Self::PlaceholderShown => ElementState::PLACEHOLDER_SHOWN,
459            Self::PopoverOpen => ElementState::POPOVER_OPEN,
460            Self::ReadOnly => ElementState::READONLY,
461            Self::ReadWrite => ElementState::READWRITE,
462            Self::Required => ElementState::REQUIRED,
463            Self::Target => ElementState::URLTARGET,
464            Self::UserInvalid => ElementState::USER_INVALID,
465            Self::UserValid => ElementState::USER_VALID,
466            Self::Valid => ElementState::VALID,
467            Self::Visited => ElementState::VISITED,
468            Self::CustomState(_) | Self::Lang(_) | Self::ServoNonZeroBorder => {
469                ElementState::empty()
470            },
471        }
472    }
473
474    /// Get the document state flag associated with a pseudo-class, if any.
475    pub fn document_state_flag(&self) -> DocumentState {
476        DocumentState::empty()
477    }
478
479    /// Returns true if the given pseudoclass should trigger style sharing cache revalidation.
480    pub fn needs_cache_revalidation(&self) -> bool {
481        self.state_flag().is_empty()
482    }
483}
484
485/// The abstract struct we implement the selector parser implementation on top
486/// of.
487#[derive(Clone, Debug, PartialEq)]
488#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
489pub struct SelectorImpl;
490
491/// A set of extra data to carry along with the matching context, either for
492/// selector-matching or invalidation.
493#[derive(Debug, Default)]
494pub struct ExtraMatchingData<'a> {
495    /// The invalidation data to invalidate doc-state pseudo-classes correctly.
496    pub invalidation_data: InvalidationMatchingData,
497
498    /// The invalidation bits from matching container queries. These are here
499    /// just for convenience mostly.
500    pub cascade_input_flags: ComputedValueFlags,
501
502    /// The style of the originating element in order to evaluate @container
503    /// size queries affecting pseudo-elements.
504    pub originating_element_style: Option<&'a ComputedValues>,
505}
506
507impl ::selectors::SelectorImpl for SelectorImpl {
508    type PseudoElement = PseudoElement;
509    type NonTSPseudoClass = NonTSPseudoClass;
510
511    type ExtraMatchingData<'a> = ExtraMatchingData<'a>;
512    type AttrValue = AtomString;
513    type Identifier = AtomIdent;
514    type LocalName = LocalName;
515    type NamespacePrefix = Prefix;
516    type NamespaceUrl = Namespace;
517    type BorrowedLocalName = web_atoms::LocalName;
518    type BorrowedNamespaceUrl = web_atoms::Namespace;
519}
520
521impl<'a, 'i> ::selectors::Parser<'i> for SelectorParser<'a> {
522    type Impl = SelectorImpl;
523    type Error = StyleParseErrorKind<'i>;
524
525    #[inline]
526    fn parse_nth_child_of(&self) -> bool {
527        false
528    }
529
530    #[inline]
531    fn parse_is_and_where(&self) -> bool {
532        true
533    }
534
535    #[inline]
536    fn parse_has(&self) -> bool {
537        false
538    }
539
540    #[inline]
541    fn parse_parent_selector(&self) -> bool {
542        true
543    }
544
545    #[inline]
546    fn parse_part(&self) -> bool {
547        true
548    }
549
550    #[inline]
551    fn allow_forgiving_selectors(&self) -> bool {
552        !self.for_supports_rule
553    }
554
555    fn parse_non_ts_pseudo_class(
556        &self,
557        location: SourceLocation,
558        name: CowRcStr<'i>,
559    ) -> Result<NonTSPseudoClass, ParseError<'i>> {
560        let pseudo_class = match_ignore_ascii_case! { &name,
561            "active" => NonTSPseudoClass::Active,
562            "any-link" => NonTSPseudoClass::AnyLink,
563            "autofill" => NonTSPseudoClass::Autofill,
564            "checked" => NonTSPseudoClass::Checked,
565            "default" => NonTSPseudoClass::Default,
566            "defined" => NonTSPseudoClass::Defined,
567            "disabled" => NonTSPseudoClass::Disabled,
568            "enabled" => NonTSPseudoClass::Enabled,
569            "focus" => NonTSPseudoClass::Focus,
570            "focus-visible" => NonTSPseudoClass::FocusVisible,
571            "focus-within" => NonTSPseudoClass::FocusWithin,
572            "fullscreen" => NonTSPseudoClass::Fullscreen,
573            "hover" => NonTSPseudoClass::Hover,
574            "indeterminate" => NonTSPseudoClass::Indeterminate,
575            "invalid" => NonTSPseudoClass::Invalid,
576            "link" => NonTSPseudoClass::Link,
577            "modal" => NonTSPseudoClass::Modal,
578            "open" => NonTSPseudoClass::Open,
579            "optional" => NonTSPseudoClass::Optional,
580            "out-of-range" => NonTSPseudoClass::OutOfRange,
581            "placeholder-shown" => NonTSPseudoClass::PlaceholderShown,
582            "popover-open" => NonTSPseudoClass::PopoverOpen,
583            "read-only" => NonTSPseudoClass::ReadOnly,
584            "read-write" => NonTSPseudoClass::ReadWrite,
585            "required" => NonTSPseudoClass::Required,
586            "target" => NonTSPseudoClass::Target,
587            "user-invalid" => NonTSPseudoClass::UserInvalid,
588            "user-valid" => NonTSPseudoClass::UserValid,
589            "valid" => NonTSPseudoClass::Valid,
590            "visited" => NonTSPseudoClass::Visited,
591            "-moz-meter-optimum" => NonTSPseudoClass::MozMeterOptimum,
592            "-moz-meter-sub-optimum" => NonTSPseudoClass::MozMeterSubOptimum,
593            "-moz-meter-sub-sub-optimum" => NonTSPseudoClass::MozMeterSubSubOptimum,
594            "-servo-nonzero-border" => {
595                if !self.in_user_agent_stylesheet() {
596                    return Err(location.new_custom_error(
597                        SelectorParseErrorKind::UnexpectedIdent("-servo-nonzero-border".into())
598                    ))
599                }
600                NonTSPseudoClass::ServoNonZeroBorder
601            },
602            _ => return Err(location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(name.clone()))),
603        };
604
605        Ok(pseudo_class)
606    }
607
608    fn parse_non_ts_functional_pseudo_class<'t>(
609        &self,
610        name: CowRcStr<'i>,
611        parser: &mut CssParser<'i, 't>,
612        after_part: bool,
613    ) -> Result<NonTSPseudoClass, ParseError<'i>> {
614        let pseudo_class = match_ignore_ascii_case! { &name,
615            "lang" if !after_part => {
616                NonTSPseudoClass::Lang(parser.expect_ident_or_string()?.as_ref().into())
617            },
618            "state" => {
619                let result = AtomIdent::from(parser.expect_ident()?.as_ref());
620                NonTSPseudoClass::CustomState(CustomState(result))
621            },
622            _ => return Err(parser.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(name.clone()))),
623        };
624
625        Ok(pseudo_class)
626    }
627
628    fn parse_pseudo_element(
629        &self,
630        location: SourceLocation,
631        name: CowRcStr<'i>,
632    ) -> Result<PseudoElement, ParseError<'i>> {
633        use self::PseudoElement::*;
634        let pseudo_element = match_ignore_ascii_case! { &name,
635            "before" => Before,
636            "after" => After,
637            "backdrop" => Backdrop,
638            "selection" => Selection,
639            "marker" => Marker,
640            "-servo-details-summary" => {
641                if !self.in_user_agent_stylesheet() {
642                    return Err(location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(name.clone())))
643                }
644                DetailsSummary
645            },
646            "details-content" => DetailsContent,
647            "color-swatch" => ColorSwatch,
648            "placeholder" => {
649                if !self.in_user_agent_stylesheet() {
650                    return Err(location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(name.clone())))
651                }
652                Placeholder
653            },
654            "-servo-text-control-inner-container" => {
655                if !self.in_user_agent_stylesheet() {
656                    return Err(location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(name.clone())))
657                }
658                ServoTextControlInnerContainer
659            },
660            "-servo-text-control-inner-editor" => {
661                if !self.in_user_agent_stylesheet() {
662                    return Err(location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(name.clone())))
663                }
664                ServoTextControlInnerEditor
665            },
666            "-servo-anonymous-box" => {
667                if !self.in_user_agent_stylesheet() {
668                    return Err(location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(name.clone())))
669                }
670                ServoAnonymousBox
671            },
672            "-servo-anonymous-table" => {
673                if !self.in_user_agent_stylesheet() {
674                    return Err(location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(name.clone())))
675                }
676                ServoAnonymousTable
677            },
678            "-servo-anonymous-table-row" => {
679                if !self.in_user_agent_stylesheet() {
680                    return Err(location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(name.clone())))
681                }
682                ServoAnonymousTableRow
683            },
684            "-servo-anonymous-table-cell" => {
685                if !self.in_user_agent_stylesheet() {
686                    return Err(location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(name.clone())))
687                }
688                ServoAnonymousTableCell
689            },
690            "-servo-table-grid" => {
691                if !self.in_user_agent_stylesheet() {
692                    return Err(location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(name.clone())))
693                }
694                ServoTableGrid
695            },
696            "-servo-table-wrapper" => {
697                if !self.in_user_agent_stylesheet() {
698                    return Err(location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(name.clone())))
699                }
700                ServoTableWrapper
701            },
702            _ => return Err(location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(name.clone())))
703
704        };
705
706        Ok(pseudo_element)
707    }
708
709    fn default_namespace(&self) -> Option<Namespace> {
710        self.namespaces.default.as_ref().map(|ns| ns.clone())
711    }
712
713    fn namespace_for_prefix(&self, prefix: &Prefix) -> Option<Namespace> {
714        self.namespaces.prefixes.get(prefix).cloned()
715    }
716
717    fn parse_host(&self) -> bool {
718        true
719    }
720
721    fn parse_slotted(&self) -> bool {
722        true
723    }
724}
725
726impl SelectorImpl {
727    /// A helper to traverse each eagerly cascaded pseudo-element, executing
728    /// `fun` on it.
729    #[inline]
730    pub fn each_eagerly_cascaded_pseudo_element<F>(mut fun: F)
731    where
732        F: FnMut(PseudoElement),
733    {
734        for i in 0..EAGER_PSEUDO_COUNT {
735            fun(PseudoElement::from_eager_index(i));
736        }
737    }
738}
739
740/// A map from elements to snapshots for the Servo style back-end.
741#[derive(Debug)]
742pub struct SnapshotMap(FxHashMap<OpaqueNode, ServoElementSnapshot>);
743
744impl SnapshotMap {
745    /// Create a new empty `SnapshotMap`.
746    pub fn new() -> Self {
747        SnapshotMap(FxHashMap::default())
748    }
749
750    /// Get a snapshot given an element.
751    pub fn get<T: TElement>(&self, el: &T) -> Option<&ServoElementSnapshot> {
752        self.0.get(&el.as_node().opaque())
753    }
754}
755
756impl Deref for SnapshotMap {
757    type Target = FxHashMap<OpaqueNode, ServoElementSnapshot>;
758
759    fn deref(&self) -> &Self::Target {
760        &self.0
761    }
762}
763
764impl DerefMut for SnapshotMap {
765    fn deref_mut(&mut self) -> &mut Self::Target {
766        &mut self.0
767    }
768}
769
770/// Servo's version of an element snapshot.
771#[derive(Debug, Default, MallocSizeOf)]
772pub struct ServoElementSnapshot {
773    /// The stored state of the element.
774    pub state: Option<ElementState>,
775    /// The set of stored attributes and its values.
776    pub attrs: Option<Vec<(AttrIdentifier, AttrValue)>>,
777    /// The set of changed attributes and its values.
778    pub changed_attrs: Vec<LocalName>,
779    /// Whether the class attribute changed or not.
780    pub class_changed: bool,
781    /// Whether the id attribute changed or not.
782    pub id_changed: bool,
783    /// Whether other attributes other than id or class changed or not.
784    pub other_attributes_changed: bool,
785}
786
787impl ServoElementSnapshot {
788    /// Create an empty element snapshot.
789    pub fn new() -> Self {
790        Self::default()
791    }
792
793    /// Returns whether the id attribute changed or not.
794    pub fn id_changed(&self) -> bool {
795        self.id_changed
796    }
797
798    /// Returns whether the class attribute changed or not.
799    pub fn class_changed(&self) -> bool {
800        self.class_changed
801    }
802
803    /// Returns whether other attributes other than id or class changed or not.
804    pub fn other_attr_changed(&self) -> bool {
805        self.other_attributes_changed
806    }
807
808    fn get_attr(&self, namespace: &Namespace, name: &LocalName) -> Option<&AttrValue> {
809        self.attrs
810            .as_ref()
811            .unwrap()
812            .iter()
813            .find(|&&(ref ident, _)| ident.local_name == *name && ident.namespace == *namespace)
814            .map(|&(_, ref v)| v)
815    }
816
817    /// Executes the callback once for each attribute that changed.
818    #[inline]
819    pub fn each_attr_changed<F>(&self, mut callback: F)
820    where
821        F: FnMut(&LocalName),
822    {
823        for name in &self.changed_attrs {
824            callback(name)
825        }
826    }
827
828    fn any_attr_ignore_ns<F>(&self, name: &LocalName, mut f: F) -> bool
829    where
830        F: FnMut(&AttrValue) -> bool,
831    {
832        self.attrs
833            .as_ref()
834            .unwrap()
835            .iter()
836            .any(|&(ref ident, ref v)| ident.local_name == *name && f(v))
837    }
838}
839
840impl ElementSnapshot for ServoElementSnapshot {
841    fn state(&self) -> Option<ElementState> {
842        self.state.clone()
843    }
844
845    fn has_attrs(&self) -> bool {
846        self.attrs.is_some()
847    }
848
849    fn id_attr(&self) -> Option<&Atom> {
850        self.get_attr(&ns!(), &local_name!("id"))
851            .map(|v| v.as_atom())
852    }
853
854    fn is_part(&self, part_name: &AtomIdent) -> bool {
855        self.get_attr(&ns!(), &local_name!("part"))
856            .is_some_and(|v| {
857                v.as_tokens()
858                    .iter()
859                    .any(|atom| CaseSensitivity::CaseSensitive.eq_atom(atom, part_name))
860            })
861    }
862
863    fn imported_part(&self, _: &AtomIdent) -> Option<AtomIdent> {
864        None
865    }
866
867    fn has_class(&self, name: &AtomIdent, case_sensitivity: CaseSensitivity) -> bool {
868        self.get_attr(&ns!(), &local_name!("class"))
869            .map_or(false, |v| {
870                v.as_tokens()
871                    .iter()
872                    .any(|atom| case_sensitivity.eq_atom(atom, name))
873            })
874    }
875
876    fn each_class<F>(&self, mut callback: F)
877    where
878        F: FnMut(&AtomIdent),
879    {
880        if let Some(v) = self.get_attr(&ns!(), &local_name!("class")) {
881            for class in v.as_tokens() {
882                callback(AtomIdent::cast(class));
883            }
884        }
885    }
886
887    fn lang_attr(&self) -> Option<SelectorAttrValue> {
888        self.get_attr(&ns!(xml), &local_name!("lang"))
889            .or_else(|| self.get_attr(&ns!(), &local_name!("lang")))
890            .map(|v| SelectorAttrValue::from(v as &str))
891    }
892
893    /// Returns true if the snapshot has stored state for custom states
894    #[inline]
895    fn has_custom_states(&self) -> bool {
896        false
897    }
898
899    /// Returns true if the snapshot has a given CustomState
900    #[inline]
901    fn has_custom_state(&self, _state: &AtomIdent) -> bool {
902        false
903    }
904
905    #[inline]
906    fn each_custom_state<F>(&self, mut _callback: F)
907    where
908        F: FnMut(&AtomIdent),
909    {
910    }
911}
912
913impl ServoElementSnapshot {
914    /// selectors::Element::attr_matches
915    pub fn attr_matches(
916        &self,
917        ns: &NamespaceConstraint<&Namespace>,
918        local_name: &LocalName,
919        operation: &AttrSelectorOperation<&AtomString>,
920    ) -> bool {
921        match *ns {
922            NamespaceConstraint::Specific(ref ns) => self
923                .get_attr(ns, local_name)
924                .map_or(false, |value| value.eval_selector(operation)),
925            NamespaceConstraint::Any => {
926                self.any_attr_ignore_ns(local_name, |value| value.eval_selector(operation))
927            },
928        }
929    }
930}
931
932/// Returns whether the language is matched, as defined by
933/// [RFC 4647](https://tools.ietf.org/html/rfc4647#section-3.3.2).
934pub fn extended_filtering(tag: &str, range: &str) -> bool {
935    range.split(',').any(|lang_range| {
936        // step 1
937        let mut range_subtags = lang_range.split('\x2d');
938        let mut tag_subtags = tag.split('\x2d');
939
940        // step 2
941        // Note: [Level-4 spec](https://drafts.csswg.org/selectors/#lang-pseudo) check for wild card
942        if let (Some(range_subtag), Some(tag_subtag)) = (range_subtags.next(), tag_subtags.next()) {
943            if !(range_subtag.eq_ignore_ascii_case(tag_subtag)
944                || range_subtag.eq_ignore_ascii_case("*"))
945            {
946                return false;
947            }
948        }
949
950        let mut current_tag_subtag = tag_subtags.next();
951
952        // step 3
953        for range_subtag in range_subtags {
954            // step 3a
955            if range_subtag == "*" {
956                continue;
957            }
958            match current_tag_subtag.clone() {
959                Some(tag_subtag) => {
960                    // step 3c
961                    if range_subtag.eq_ignore_ascii_case(tag_subtag) {
962                        current_tag_subtag = tag_subtags.next();
963                        continue;
964                    }
965                    // step 3d
966                    if tag_subtag.len() == 1 {
967                        return false;
968                    }
969                    // else step 3e - continue with loop
970                    current_tag_subtag = tag_subtags.next();
971                    if current_tag_subtag.is_none() {
972                        return false;
973                    }
974                },
975                // step 3b
976                None => {
977                    return false;
978                },
979            }
980        }
981        // step 4
982        true
983    })
984}