style/properties/
mod.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//! Supported CSS properties and the cascade.
6
7pub mod cascade;
8pub mod declaration_block;
9
10pub use self::cascade::*;
11pub use self::declaration_block::*;
12pub use self::generated::*;
13/// The CSS properties supported by the style system.
14/// Generated from the properties.mako.rs template by build.rs
15#[macro_use]
16#[allow(unsafe_code)]
17#[deny(missing_docs)]
18pub mod generated {
19    include!(concat!(env!("OUT_DIR"), "/properties.rs"));
20}
21
22use crate::custom_properties::{self, ComputedCustomProperties};
23#[cfg(feature = "gecko")]
24use crate::gecko_bindings::structs::{nsCSSPropertyID, AnimatedPropertyID, RefPtr};
25use crate::logical_geometry::WritingMode;
26use crate::parser::ParserContext;
27use crate::stylesheets::CssRuleType;
28use crate::stylesheets::Origin;
29use crate::stylist::Stylist;
30use crate::values::{computed, serialize_atom_name};
31use arrayvec::{ArrayVec, Drain as ArrayVecDrain};
32use cssparser::{Parser, ParserInput};
33use rustc_hash::FxHashMap;
34use servo_arc::Arc;
35use std::{
36    borrow::Cow,
37    fmt::{self, Write},
38    mem,
39};
40use style_traits::{
41    CssString, CssWriter, KeywordsCollectFn, ParseError, ParsingMode, SpecifiedValueInfo, ToCss,
42    ToTyped, TypedValue,
43};
44
45bitflags! {
46    /// A set of flags for properties.
47    #[derive(Clone, Copy)]
48    pub struct PropertyFlags: u16 {
49        /// This longhand property applies to ::first-letter.
50        const APPLIES_TO_FIRST_LETTER = 1 << 1;
51        /// This longhand property applies to ::first-line.
52        const APPLIES_TO_FIRST_LINE = 1 << 2;
53        /// This longhand property applies to ::placeholder.
54        const APPLIES_TO_PLACEHOLDER = 1 << 3;
55        ///  This longhand property applies to ::cue.
56        const APPLIES_TO_CUE = 1 << 4;
57        /// This longhand property applies to ::marker.
58        const APPLIES_TO_MARKER = 1 << 5;
59        /// This property is a legacy shorthand.
60        ///
61        /// https://drafts.csswg.org/css-cascade/#legacy-shorthand
62        const IS_LEGACY_SHORTHAND = 1 << 6;
63
64        /* The following flags are currently not used in Rust code, they
65         * only need to be listed in corresponding properties so that
66         * they can be checked in the C++ side via ServoCSSPropList.h. */
67
68        /// This property can be animated on the compositor.
69        const CAN_ANIMATE_ON_COMPOSITOR = 0;
70        /// See data.py's documentation about the affects_flags.
71        const AFFECTS_LAYOUT = 0;
72        #[allow(missing_docs)]
73        const AFFECTS_OVERFLOW = 0;
74        #[allow(missing_docs)]
75        const AFFECTS_PAINT = 0;
76    }
77}
78
79/// An enum to represent a CSS Wide keyword.
80#[derive(
81    Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToCss, ToShmem, ToTyped,
82)]
83pub enum CSSWideKeyword {
84    /// The `initial` keyword.
85    Initial,
86    /// The `inherit` keyword.
87    Inherit,
88    /// The `unset` keyword.
89    Unset,
90    /// The `revert` keyword.
91    Revert,
92    /// The `revert-layer` keyword.
93    RevertLayer,
94}
95
96impl CSSWideKeyword {
97    /// Returns the string representation of the keyword.
98    pub fn to_str(&self) -> &'static str {
99        match *self {
100            CSSWideKeyword::Initial => "initial",
101            CSSWideKeyword::Inherit => "inherit",
102            CSSWideKeyword::Unset => "unset",
103            CSSWideKeyword::Revert => "revert",
104            CSSWideKeyword::RevertLayer => "revert-layer",
105        }
106    }
107}
108
109impl CSSWideKeyword {
110    /// Parses a CSS wide keyword from a CSS identifier.
111    pub fn from_ident(ident: &str) -> Result<Self, ()> {
112        Ok(match_ignore_ascii_case! { ident,
113            "initial" => CSSWideKeyword::Initial,
114            "inherit" => CSSWideKeyword::Inherit,
115            "unset" => CSSWideKeyword::Unset,
116            "revert" => CSSWideKeyword::Revert,
117            "revert-layer" => CSSWideKeyword::RevertLayer,
118            _ => return Err(()),
119        })
120    }
121
122    /// Parses a CSS wide keyword completely.
123    pub fn parse(input: &mut Parser) -> Result<Self, ()> {
124        let keyword = {
125            let ident = input.expect_ident().map_err(|_| ())?;
126            Self::from_ident(ident)?
127        };
128        input.expect_exhausted().map_err(|_| ())?;
129        Ok(keyword)
130    }
131}
132
133/// A declaration using a CSS-wide keyword.
134#[derive(Clone, PartialEq, ToCss, ToShmem, MallocSizeOf)]
135pub struct WideKeywordDeclaration {
136    #[css(skip)]
137    id: LonghandId,
138    /// The CSS-wide keyword.
139    pub keyword: CSSWideKeyword,
140}
141
142// XXX Switch back to ToTyped derive once it can automatically handle structs
143// Tracking in bug 1991631
144impl ToTyped for WideKeywordDeclaration {
145    fn to_typed(&self) -> Option<TypedValue> {
146        self.keyword.to_typed()
147    }
148}
149
150/// An unparsed declaration that contains `var()` functions.
151#[derive(Clone, PartialEq, ToCss, ToShmem, MallocSizeOf, ToTyped)]
152pub struct VariableDeclaration {
153    /// The id of the property this declaration represents.
154    #[css(skip)]
155    id: LonghandId,
156    /// The unparsed value of the variable.
157    #[ignore_malloc_size_of = "Arc"]
158    pub value: Arc<UnparsedValue>,
159}
160
161/// A custom property declaration value is either an unparsed value or a CSS
162/// wide-keyword.
163#[derive(Clone, PartialEq, ToCss, ToShmem)]
164pub enum CustomDeclarationValue {
165    /// An unparsed value.
166    Unparsed(Arc<custom_properties::SpecifiedValue>),
167    /// An already-parsed value.
168    Parsed(Arc<crate::properties_and_values::value::SpecifiedValue>),
169    /// A wide keyword.
170    CSSWideKeyword(CSSWideKeyword),
171}
172
173/// A custom property declaration with the property name and the declared value.
174#[derive(Clone, PartialEq, ToCss, ToShmem, MallocSizeOf, ToTyped)]
175pub struct CustomDeclaration {
176    /// The name of the custom property.
177    #[css(skip)]
178    pub name: custom_properties::Name,
179    /// The value of the custom property.
180    #[ignore_malloc_size_of = "Arc"]
181    pub value: CustomDeclarationValue,
182}
183
184impl fmt::Debug for PropertyDeclaration {
185    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
186        self.id().to_css(&mut CssWriter::new(f))?;
187        f.write_str(": ")?;
188
189        // Because PropertyDeclaration::to_css requires CssStringWriter, we can't write
190        // it directly to f, and need to allocate an intermediate string. This is
191        // fine for debug-only code.
192        let mut s = CssString::new();
193        self.to_css(&mut s)?;
194        write!(f, "{}", s)
195    }
196}
197
198/// A longhand or shorthand property.
199#[derive(
200    Clone, Copy, Debug, PartialEq, Eq, Hash, ToComputedValue, ToResolvedValue, ToShmem, MallocSizeOf,
201)]
202#[repr(C)]
203pub struct NonCustomPropertyId(u16);
204
205impl ToCss for NonCustomPropertyId {
206    #[inline]
207    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
208    where
209        W: Write,
210    {
211        dest.write_str(self.name())
212    }
213}
214
215impl NonCustomPropertyId {
216    /// Returns the underlying index, used for use counter.
217    pub fn bit(self) -> usize {
218        self.0 as usize
219    }
220
221    /// Convert a `NonCustomPropertyId` into a `nsCSSPropertyID`.
222    #[cfg(feature = "gecko")]
223    #[inline]
224    pub fn to_nscsspropertyid(self) -> nsCSSPropertyID {
225        // unsafe: guaranteed by static_assert_nscsspropertyid.
226        unsafe { mem::transmute(self.0 as i32) }
227    }
228
229    /// Convert an `nsCSSPropertyID` into a `NonCustomPropertyId`.
230    #[cfg(feature = "gecko")]
231    #[inline]
232    pub fn from_nscsspropertyid(prop: nsCSSPropertyID) -> Option<Self> {
233        let prop = prop as i32;
234        if prop < 0 || prop >= property_counts::NON_CUSTOM as i32 {
235            return None;
236        }
237        // guaranteed by static_assert_nscsspropertyid above.
238        Some(NonCustomPropertyId(prop as u16))
239    }
240
241    /// Resolves the alias of a given property if needed.
242    pub fn unaliased(self) -> Self {
243        let Some(alias_id) = self.as_alias() else {
244            return self;
245        };
246        alias_id.aliased_property()
247    }
248
249    /// Turns this `NonCustomPropertyId` into a `PropertyId`.
250    #[inline]
251    pub fn to_property_id(self) -> PropertyId {
252        PropertyId::NonCustom(self)
253    }
254
255    /// Returns a longhand id, if this property is one.
256    #[inline]
257    pub fn as_longhand(self) -> Option<LonghandId> {
258        if self.0 < property_counts::LONGHANDS as u16 {
259            return Some(unsafe { mem::transmute(self.0 as u16) });
260        }
261        None
262    }
263
264    /// Returns a shorthand id, if this property is one.
265    #[inline]
266    pub fn as_shorthand(self) -> Option<ShorthandId> {
267        if self.0 >= property_counts::LONGHANDS as u16
268            && self.0 < property_counts::LONGHANDS_AND_SHORTHANDS as u16
269        {
270            return Some(unsafe { mem::transmute(self.0 - (property_counts::LONGHANDS as u16)) });
271        }
272        None
273    }
274
275    /// Returns an alias id, if this property is one.
276    #[inline]
277    pub fn as_alias(self) -> Option<AliasId> {
278        debug_assert!((self.0 as usize) < property_counts::NON_CUSTOM);
279        if self.0 >= property_counts::LONGHANDS_AND_SHORTHANDS as u16 {
280            return Some(unsafe {
281                mem::transmute(self.0 - (property_counts::LONGHANDS_AND_SHORTHANDS as u16))
282            });
283        }
284        None
285    }
286
287    /// Returns either a longhand or a shorthand, resolving aliases.
288    #[inline]
289    pub fn longhand_or_shorthand(self) -> Result<LonghandId, ShorthandId> {
290        let id = self.unaliased();
291        match id.as_longhand() {
292            Some(lh) => Ok(lh),
293            None => Err(id.as_shorthand().unwrap()),
294        }
295    }
296
297    /// Converts a longhand id into a non-custom property id.
298    #[inline]
299    pub const fn from_longhand(id: LonghandId) -> Self {
300        Self(id as u16)
301    }
302
303    /// Converts a shorthand id into a non-custom property id.
304    #[inline]
305    pub const fn from_shorthand(id: ShorthandId) -> Self {
306        Self((id as u16) + (property_counts::LONGHANDS as u16))
307    }
308
309    /// Converts an alias id into a non-custom property id.
310    #[inline]
311    pub const fn from_alias(id: AliasId) -> Self {
312        Self((id as u16) + (property_counts::LONGHANDS_AND_SHORTHANDS as u16))
313    }
314}
315
316impl From<LonghandId> for NonCustomPropertyId {
317    #[inline]
318    fn from(id: LonghandId) -> Self {
319        Self::from_longhand(id)
320    }
321}
322
323impl From<ShorthandId> for NonCustomPropertyId {
324    #[inline]
325    fn from(id: ShorthandId) -> Self {
326        Self::from_shorthand(id)
327    }
328}
329
330impl From<AliasId> for NonCustomPropertyId {
331    #[inline]
332    fn from(id: AliasId) -> Self {
333        Self::from_alias(id)
334    }
335}
336
337/// Representation of a CSS property, that is, either a longhand, a shorthand, or a custom
338/// property.
339#[derive(Clone, Eq, PartialEq, Debug)]
340pub enum PropertyId {
341    /// An alias for a shorthand property.
342    NonCustom(NonCustomPropertyId),
343    /// A custom property.
344    Custom(custom_properties::Name),
345}
346
347impl ToCss for PropertyId {
348    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
349    where
350        W: Write,
351    {
352        match *self {
353            PropertyId::NonCustom(id) => dest.write_str(id.name()),
354            PropertyId::Custom(ref name) => {
355                dest.write_str("--")?;
356                serialize_atom_name(name, dest)
357            },
358        }
359    }
360}
361
362impl PropertyId {
363    /// Return the longhand id that this property id represents.
364    #[inline]
365    pub fn longhand_id(&self) -> Option<LonghandId> {
366        self.non_custom_non_alias_id()?.as_longhand()
367    }
368
369    /// Returns true if this property is one of the animatable properties.
370    pub fn is_animatable(&self) -> bool {
371        match self {
372            Self::NonCustom(id) => id.is_animatable(),
373            Self::Custom(_) => cfg!(feature = "gecko"),
374        }
375    }
376
377    /// Returns a given property from the given name, _regardless of whether it is enabled or
378    /// not_, or Err(()) for unknown properties.
379    ///
380    /// Do not use for non-testing purposes.
381    pub fn parse_unchecked_for_testing(name: &str) -> Result<Self, ()> {
382        Self::parse_unchecked(name, None)
383    }
384
385    /// Parses a property name, and returns an error if it's unknown or isn't enabled for all
386    /// content.
387    #[inline]
388    pub fn parse_enabled_for_all_content(name: &str) -> Result<Self, ()> {
389        let id = Self::parse_unchecked(name, None)?;
390
391        if !id.enabled_for_all_content() {
392            return Err(());
393        }
394
395        Ok(id)
396    }
397
398    /// Parses a property name, and returns an error if it's unknown or isn't allowed in this
399    /// context.
400    #[inline]
401    pub fn parse(name: &str, context: &ParserContext) -> Result<Self, ()> {
402        let id = Self::parse_unchecked(name, context.use_counters)?;
403        if !id.allowed_in(context) {
404            return Err(());
405        }
406        Ok(id)
407    }
408
409    /// Parses a property name, and returns an error if it's unknown or isn't allowed in this
410    /// context, ignoring the rule_type checks.
411    ///
412    /// This is useful for parsing stuff from CSS values, for example.
413    #[inline]
414    pub fn parse_ignoring_rule_type(name: &str, context: &ParserContext) -> Result<Self, ()> {
415        let id = Self::parse_unchecked(name, None)?;
416        if !id.allowed_in_ignoring_rule_type(context) {
417            return Err(());
418        }
419        Ok(id)
420    }
421
422    /// Returns a property id from Gecko's nsCSSPropertyID.
423    #[cfg(feature = "gecko")]
424    #[inline]
425    pub fn from_nscsspropertyid(id: nsCSSPropertyID) -> Option<Self> {
426        Some(NonCustomPropertyId::from_nscsspropertyid(id)?.to_property_id())
427    }
428
429    /// Returns a property id from Gecko's AnimatedPropertyID.
430    #[cfg(feature = "gecko")]
431    #[inline]
432    pub fn from_gecko_animated_property_id(property: &AnimatedPropertyID) -> Option<Self> {
433        Some(
434            if property.mID == nsCSSPropertyID::eCSSPropertyExtra_variable {
435                debug_assert!(!property.mCustomName.mRawPtr.is_null());
436                Self::Custom(unsafe { crate::Atom::from_raw(property.mCustomName.mRawPtr) })
437            } else {
438                Self::NonCustom(NonCustomPropertyId::from_nscsspropertyid(property.mID)?)
439            },
440        )
441    }
442
443    /// Returns true if the property is a shorthand or shorthand alias.
444    #[inline]
445    pub fn is_shorthand(&self) -> bool {
446        self.as_shorthand().is_ok()
447    }
448
449    /// Given this property id, get it either as a shorthand or as a
450    /// `PropertyDeclarationId`.
451    pub fn as_shorthand(&self) -> Result<ShorthandId, PropertyDeclarationId<'_>> {
452        match *self {
453            Self::NonCustom(id) => match id.longhand_or_shorthand() {
454                Ok(lh) => Err(PropertyDeclarationId::Longhand(lh)),
455                Err(sh) => Ok(sh),
456            },
457            Self::Custom(ref name) => Err(PropertyDeclarationId::Custom(name)),
458        }
459    }
460
461    /// Returns the `NonCustomPropertyId` corresponding to this property id.
462    pub fn non_custom_id(&self) -> Option<NonCustomPropertyId> {
463        match *self {
464            Self::Custom(_) => None,
465            Self::NonCustom(id) => Some(id),
466        }
467    }
468
469    /// Returns non-alias NonCustomPropertyId corresponding to this
470    /// property id.
471    fn non_custom_non_alias_id(&self) -> Option<NonCustomPropertyId> {
472        self.non_custom_id().map(NonCustomPropertyId::unaliased)
473    }
474
475    /// Whether the property is enabled for all content regardless of the
476    /// stylesheet it was declared on (that is, in practice only checks prefs).
477    #[inline]
478    pub fn enabled_for_all_content(&self) -> bool {
479        let id = match self.non_custom_id() {
480            // Custom properties are allowed everywhere
481            None => return true,
482            Some(id) => id,
483        };
484
485        id.enabled_for_all_content()
486    }
487
488    /// Converts this PropertyId in nsCSSPropertyID, resolving aliases to the
489    /// resolved property, and returning eCSSPropertyExtra_variable for custom
490    /// properties.
491    #[cfg(feature = "gecko")]
492    #[inline]
493    pub fn to_nscsspropertyid_resolving_aliases(&self) -> nsCSSPropertyID {
494        match self.non_custom_non_alias_id() {
495            Some(id) => id.to_nscsspropertyid(),
496            None => nsCSSPropertyID::eCSSPropertyExtra_variable,
497        }
498    }
499
500    fn allowed_in(&self, context: &ParserContext) -> bool {
501        let id = match self.non_custom_id() {
502            // Custom properties are allowed everywhere, except `position-try`.
503            None => {
504                return !context
505                    .nesting_context
506                    .rule_types
507                    .contains(CssRuleType::PositionTry)
508            },
509            Some(id) => id,
510        };
511        id.allowed_in(context)
512    }
513
514    #[inline]
515    fn allowed_in_ignoring_rule_type(&self, context: &ParserContext) -> bool {
516        let id = match self.non_custom_id() {
517            // Custom properties are allowed everywhere
518            None => return true,
519            Some(id) => id,
520        };
521        id.allowed_in_ignoring_rule_type(context)
522    }
523
524    /// Whether the property supports the given CSS type.
525    /// `ty` should a bitflags of constants in style_traits::CssType.
526    pub fn supports_type(&self, ty: u8) -> bool {
527        let id = self.non_custom_non_alias_id();
528        id.map_or(0, |id| id.supported_types()) & ty != 0
529    }
530
531    /// Collect supported starting word of values of this property.
532    ///
533    /// See style_traits::SpecifiedValueInfo::collect_completion_keywords for more
534    /// details.
535    pub fn collect_property_completion_keywords(&self, f: KeywordsCollectFn) {
536        if let Some(id) = self.non_custom_non_alias_id() {
537            id.collect_property_completion_keywords(f);
538        }
539        CSSWideKeyword::collect_completion_keywords(f);
540    }
541}
542
543impl ToCss for LonghandId {
544    #[inline]
545    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
546    where
547        W: Write,
548    {
549        dest.write_str(self.name())
550    }
551}
552
553impl fmt::Debug for LonghandId {
554    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
555        formatter.write_str(self.name())
556    }
557}
558
559impl LonghandId {
560    /// Get the name of this longhand property.
561    #[inline]
562    pub fn name(&self) -> &'static str {
563        NonCustomPropertyId::from(*self).name()
564    }
565
566    /// Returns whether the longhand property is inherited by default.
567    #[inline]
568    pub fn inherited(self) -> bool {
569        !LonghandIdSet::reset().contains(self)
570    }
571
572    /// Returns whether the longhand property is zoom-dependent.
573    #[inline]
574    pub fn zoom_dependent(self) -> bool {
575        LonghandIdSet::zoom_dependent().contains(self)
576    }
577
578    /// Returns true if the property is one that is ignored when document
579    /// colors are disabled.
580    #[inline]
581    pub fn ignored_when_document_colors_disabled(self) -> bool {
582        LonghandIdSet::ignored_when_colors_disabled().contains(self)
583    }
584
585    /// Returns whether this longhand is `non_custom` or is a longhand of it.
586    pub fn is_or_is_longhand_of(self, non_custom: NonCustomPropertyId) -> bool {
587        match non_custom.longhand_or_shorthand() {
588            Ok(lh) => self == lh,
589            Err(sh) => self.is_longhand_of(sh),
590        }
591    }
592
593    /// Returns whether this longhand is a longhand of `shorthand`.
594    pub fn is_longhand_of(self, shorthand: ShorthandId) -> bool {
595        self.shorthands().any(|s| s == shorthand)
596    }
597
598    /// Returns whether this property is animatable.
599    #[inline]
600    pub fn is_animatable(self) -> bool {
601        NonCustomPropertyId::from(self).is_animatable()
602    }
603
604    /// Returns whether this property is animatable in a discrete way.
605    #[inline]
606    pub fn is_discrete_animatable(self) -> bool {
607        LonghandIdSet::discrete_animatable().contains(self)
608    }
609
610    /// Converts from a LonghandId to an adequate nsCSSPropertyID.
611    #[cfg(feature = "gecko")]
612    #[inline]
613    pub fn to_nscsspropertyid(self) -> nsCSSPropertyID {
614        NonCustomPropertyId::from(self).to_nscsspropertyid()
615    }
616
617    #[cfg(feature = "gecko")]
618    /// Returns a longhand id from Gecko's nsCSSPropertyID.
619    pub fn from_nscsspropertyid(id: nsCSSPropertyID) -> Option<Self> {
620        NonCustomPropertyId::from_nscsspropertyid(id)?
621            .unaliased()
622            .as_longhand()
623    }
624
625    /// Return whether this property is logical.
626    #[inline]
627    pub fn is_logical(self) -> bool {
628        LonghandIdSet::logical().contains(self)
629    }
630}
631
632impl ToCss for ShorthandId {
633    #[inline]
634    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
635    where
636        W: Write,
637    {
638        dest.write_str(self.name())
639    }
640}
641
642impl ShorthandId {
643    /// Get the name for this shorthand property.
644    #[inline]
645    pub fn name(&self) -> &'static str {
646        NonCustomPropertyId::from(*self).name()
647    }
648
649    /// Converts from a ShorthandId to an adequate nsCSSPropertyID.
650    #[cfg(feature = "gecko")]
651    #[inline]
652    pub fn to_nscsspropertyid(self) -> nsCSSPropertyID {
653        NonCustomPropertyId::from(self).to_nscsspropertyid()
654    }
655
656    /// Converts from a nsCSSPropertyID to a ShorthandId.
657    #[cfg(feature = "gecko")]
658    #[inline]
659    pub fn from_nscsspropertyid(id: nsCSSPropertyID) -> Option<Self> {
660        NonCustomPropertyId::from_nscsspropertyid(id)?
661            .unaliased()
662            .as_shorthand()
663    }
664
665    /// Finds and returns an appendable value for the given declarations.
666    ///
667    /// Returns the optional appendable value.
668    pub fn get_shorthand_appendable_value<'a, 'b: 'a>(
669        self,
670        declarations: &'a [&'b PropertyDeclaration],
671    ) -> Option<AppendableValue<'a, 'b>> {
672        let first_declaration = declarations.get(0)?;
673        let rest = || declarations.iter().skip(1);
674
675        // https://drafts.csswg.org/css-variables/#variables-in-shorthands
676        if let Some(css) = first_declaration.with_variables_from_shorthand(self) {
677            if rest().all(|d| d.with_variables_from_shorthand(self) == Some(css)) {
678                return Some(AppendableValue::Css(css));
679            }
680            return None;
681        }
682
683        // Check whether they are all the same CSS-wide keyword.
684        if let Some(keyword) = first_declaration.get_css_wide_keyword() {
685            if rest().all(|d| d.get_css_wide_keyword() == Some(keyword)) {
686                return Some(AppendableValue::Css(keyword.to_str()));
687            }
688            return None;
689        }
690
691        if self == ShorthandId::All {
692            // 'all' only supports variables and CSS wide keywords.
693            return None;
694        }
695
696        // Check whether all declarations can be serialized as part of shorthand.
697        if declarations
698            .iter()
699            .all(|d| d.may_serialize_as_part_of_shorthand())
700        {
701            return Some(AppendableValue::DeclarationsForShorthand(
702                self,
703                declarations,
704            ));
705        }
706
707        None
708    }
709
710    /// Returns whether this property is a legacy shorthand.
711    #[inline]
712    pub fn is_legacy_shorthand(self) -> bool {
713        self.flags().contains(PropertyFlags::IS_LEGACY_SHORTHAND)
714    }
715}
716
717fn parse_non_custom_property_declaration_value_into<'i>(
718    declarations: &mut SourcePropertyDeclaration,
719    context: &ParserContext,
720    input: &mut Parser<'i, '_>,
721    start: &cssparser::ParserState,
722    parse_entirely_into: impl FnOnce(
723        &mut SourcePropertyDeclaration,
724        &mut Parser<'i, '_>,
725    ) -> Result<(), ParseError<'i>>,
726    parsed_wide_keyword: impl FnOnce(&mut SourcePropertyDeclaration, CSSWideKeyword),
727    parsed_custom: impl FnOnce(&mut SourcePropertyDeclaration, custom_properties::VariableValue),
728) -> Result<(), ParseError<'i>> {
729    let mut starts_with_curly_block = false;
730    if let Ok(token) = input.next() {
731        match token {
732            cssparser::Token::Ident(ref ident) => match CSSWideKeyword::from_ident(ident) {
733                Ok(wk) => {
734                    if input.expect_exhausted().is_ok() {
735                        return Ok(parsed_wide_keyword(declarations, wk));
736                    }
737                },
738                Err(()) => {},
739            },
740            cssparser::Token::CurlyBracketBlock => {
741                starts_with_curly_block = true;
742            },
743            _ => {},
744        }
745    };
746
747    input.reset(&start);
748    input.look_for_var_or_env_functions();
749    let err = match parse_entirely_into(declarations, input) {
750        Ok(()) => {
751            input.seen_var_or_env_functions();
752            return Ok(());
753        },
754        Err(e) => e,
755    };
756
757    // Look for var(), env() and top-level curly blocks after the error.
758    let start_pos = start.position();
759    let mut at_start = start_pos == input.position();
760    let mut invalid = false;
761    while let Ok(token) = input.next() {
762        if matches!(token, cssparser::Token::CurlyBracketBlock) {
763            if !starts_with_curly_block || !at_start {
764                invalid = true;
765                break;
766            }
767        } else if starts_with_curly_block {
768            invalid = true;
769            break;
770        }
771        at_start = false;
772    }
773    if !input.seen_var_or_env_functions() || invalid {
774        return Err(err);
775    }
776    input.reset(start);
777    let value = custom_properties::VariableValue::parse(input, &context.url_data)?;
778    parsed_custom(declarations, value);
779    Ok(())
780}
781
782impl PropertyDeclaration {
783    fn with_variables_from_shorthand(&self, shorthand: ShorthandId) -> Option<&str> {
784        match *self {
785            PropertyDeclaration::WithVariables(ref declaration) => {
786                let s = declaration.value.from_shorthand?;
787                if s != shorthand {
788                    return None;
789                }
790                Some(&*declaration.value.variable_value.css)
791            },
792            _ => None,
793        }
794    }
795
796    /// Returns a CSS-wide keyword declaration for a given property.
797    #[inline]
798    pub fn css_wide_keyword(id: LonghandId, keyword: CSSWideKeyword) -> Self {
799        Self::CSSWideKeyword(WideKeywordDeclaration { id, keyword })
800    }
801
802    /// Returns a CSS-wide keyword if the declaration's value is one.
803    #[inline]
804    pub fn get_css_wide_keyword(&self) -> Option<CSSWideKeyword> {
805        match *self {
806            PropertyDeclaration::CSSWideKeyword(ref declaration) => Some(declaration.keyword),
807            _ => None,
808        }
809    }
810
811    /// Returns whether the declaration may be serialized as part of a shorthand.
812    ///
813    /// This method returns false if this declaration contains variable or has a
814    /// CSS-wide keyword value, since these values cannot be serialized as part
815    /// of a shorthand.
816    ///
817    /// Caller should check `with_variables_from_shorthand()` and whether all
818    /// needed declarations has the same CSS-wide keyword first.
819    ///
820    /// Note that, serialization of a shorthand may still fail because of other
821    /// property-specific requirement even when this method returns true for all
822    /// the longhand declarations.
823    pub fn may_serialize_as_part_of_shorthand(&self) -> bool {
824        match *self {
825            PropertyDeclaration::CSSWideKeyword(..) | PropertyDeclaration::WithVariables(..) => {
826                false
827            },
828            PropertyDeclaration::Custom(..) => {
829                unreachable!("Serializing a custom property as part of shorthand?")
830            },
831            _ => true,
832        }
833    }
834
835    /// Returns true if this property declaration is for one of the animatable properties.
836    pub fn is_animatable(&self) -> bool {
837        self.id().is_animatable()
838    }
839
840    /// Returns true if this property is a custom property, false
841    /// otherwise.
842    pub fn is_custom(&self) -> bool {
843        matches!(*self, PropertyDeclaration::Custom(..))
844    }
845
846    /// The `context` parameter controls this:
847    ///
848    /// <https://drafts.csswg.org/css-animations/#keyframes>
849    /// > The <declaration-list> inside of <keyframe-block> accepts any CSS property
850    /// > except those defined in this specification,
851    /// > but does accept the `animation-play-state` property and interprets it specially.
852    ///
853    /// This will not actually parse Importance values, and will always set things
854    /// to Importance::Normal. Parsing Importance values is the job of PropertyDeclarationParser,
855    /// we only set them here so that we don't have to reallocate
856    pub fn parse_into<'i, 't>(
857        declarations: &mut SourcePropertyDeclaration,
858        id: PropertyId,
859        context: &ParserContext,
860        input: &mut Parser<'i, 't>,
861    ) -> Result<(), ParseError<'i>> {
862        assert!(declarations.is_empty());
863        debug_assert!(id.allowed_in(context), "{:?}", id);
864        input.skip_whitespace();
865
866        let start = input.state();
867        let non_custom_id = match id {
868            PropertyId::Custom(property_name) => {
869                let value = match input.try_parse(CSSWideKeyword::parse) {
870                    Ok(keyword) => CustomDeclarationValue::CSSWideKeyword(keyword),
871                    Err(()) => CustomDeclarationValue::Unparsed(Arc::new(
872                        custom_properties::VariableValue::parse(input, &context.url_data)?,
873                    )),
874                };
875                declarations.push(PropertyDeclaration::Custom(CustomDeclaration {
876                    name: property_name,
877                    value,
878                }));
879                return Ok(());
880            },
881            PropertyId::NonCustom(id) => id,
882        };
883        match non_custom_id.longhand_or_shorthand() {
884            Ok(longhand_id) => {
885                parse_non_custom_property_declaration_value_into(
886                    declarations,
887                    context,
888                    input,
889                    &start,
890                    |declarations, input| {
891                        let decl = input
892                            .parse_entirely(|input| longhand_id.parse_value(context, input))?;
893                        declarations.push(decl);
894                        Ok(())
895                    },
896                    |declarations, wk| {
897                        declarations.push(PropertyDeclaration::css_wide_keyword(longhand_id, wk));
898                    },
899                    |declarations, variable_value| {
900                        declarations.push(PropertyDeclaration::WithVariables(VariableDeclaration {
901                            id: longhand_id,
902                            value: Arc::new(UnparsedValue {
903                                variable_value,
904                                from_shorthand: None,
905                            }),
906                        }))
907                    },
908                )?;
909            },
910            Err(shorthand_id) => {
911                parse_non_custom_property_declaration_value_into(
912                    declarations,
913                    context,
914                    input,
915                    &start,
916                    // Not using parse_entirely here: each ShorthandId::parse_into function needs
917                    // to do so *before* pushing to `declarations`.
918                    |declarations, input| shorthand_id.parse_into(declarations, context, input),
919                    |declarations, wk| {
920                        if shorthand_id == ShorthandId::All {
921                            declarations.all_shorthand = AllShorthand::CSSWideKeyword(wk)
922                        } else {
923                            for longhand in shorthand_id.longhands() {
924                                declarations
925                                    .push(PropertyDeclaration::css_wide_keyword(longhand, wk));
926                            }
927                        }
928                    },
929                    |declarations, variable_value| {
930                        let unparsed = Arc::new(UnparsedValue {
931                            variable_value,
932                            from_shorthand: Some(shorthand_id),
933                        });
934                        if shorthand_id == ShorthandId::All {
935                            declarations.all_shorthand = AllShorthand::WithVariables(unparsed)
936                        } else {
937                            for id in shorthand_id.longhands() {
938                                declarations.push(PropertyDeclaration::WithVariables(
939                                    VariableDeclaration {
940                                        id,
941                                        value: unparsed.clone(),
942                                    },
943                                ))
944                            }
945                        }
946                    },
947                )?;
948            },
949        }
950        if let Some(use_counters) = context.use_counters {
951            use_counters.non_custom_properties.record(non_custom_id);
952        }
953        Ok(())
954    }
955}
956
957/// A PropertyDeclarationId without references, for use as a hash map key.
958#[derive(Clone, Debug, PartialEq, Eq, Hash)]
959pub enum OwnedPropertyDeclarationId {
960    /// A longhand.
961    Longhand(LonghandId),
962    /// A custom property declaration.
963    Custom(custom_properties::Name),
964}
965
966impl OwnedPropertyDeclarationId {
967    /// Return whether this property is logical.
968    #[inline]
969    pub fn is_logical(&self) -> bool {
970        self.as_borrowed().is_logical()
971    }
972
973    /// Returns the corresponding PropertyDeclarationId.
974    #[inline]
975    pub fn as_borrowed(&self) -> PropertyDeclarationId<'_> {
976        match self {
977            Self::Longhand(id) => PropertyDeclarationId::Longhand(*id),
978            Self::Custom(name) => PropertyDeclarationId::Custom(name),
979        }
980    }
981
982    /// Convert an `AnimatedPropertyID` into an `OwnedPropertyDeclarationId`.
983    #[cfg(feature = "gecko")]
984    #[inline]
985    pub fn from_gecko_animated_property_id(property: &AnimatedPropertyID) -> Option<Self> {
986        Some(
987            match PropertyId::from_gecko_animated_property_id(property)? {
988                PropertyId::Custom(name) => Self::Custom(name),
989                PropertyId::NonCustom(id) => Self::Longhand(id.as_longhand()?),
990            },
991        )
992    }
993}
994
995/// An identifier for a given property declaration, which can be either a
996/// longhand or a custom property.
997#[derive(Clone, Copy, Debug, PartialEq, MallocSizeOf)]
998pub enum PropertyDeclarationId<'a> {
999    /// A longhand.
1000    Longhand(LonghandId),
1001    /// A custom property declaration.
1002    Custom(&'a custom_properties::Name),
1003}
1004
1005impl<'a> ToCss for PropertyDeclarationId<'a> {
1006    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
1007    where
1008        W: Write,
1009    {
1010        match *self {
1011            PropertyDeclarationId::Longhand(id) => dest.write_str(id.name()),
1012            PropertyDeclarationId::Custom(name) => {
1013                dest.write_str("--")?;
1014                serialize_atom_name(name, dest)
1015            },
1016        }
1017    }
1018}
1019
1020impl<'a> PropertyDeclarationId<'a> {
1021    /// Returns PropertyFlags for given property.
1022    #[inline(always)]
1023    pub fn flags(&self) -> PropertyFlags {
1024        match self {
1025            Self::Longhand(id) => id.flags(),
1026            Self::Custom(_) => PropertyFlags::empty(),
1027        }
1028    }
1029
1030    /// Convert to an OwnedPropertyDeclarationId.
1031    pub fn to_owned(&self) -> OwnedPropertyDeclarationId {
1032        match self {
1033            PropertyDeclarationId::Longhand(id) => OwnedPropertyDeclarationId::Longhand(*id),
1034            PropertyDeclarationId::Custom(name) => {
1035                OwnedPropertyDeclarationId::Custom((*name).clone())
1036            },
1037        }
1038    }
1039
1040    /// Whether a given declaration id is either the same as `other`, or a
1041    /// longhand of it.
1042    pub fn is_or_is_longhand_of(&self, other: &PropertyId) -> bool {
1043        match *self {
1044            PropertyDeclarationId::Longhand(id) => match *other {
1045                PropertyId::NonCustom(non_custom_id) => id.is_or_is_longhand_of(non_custom_id),
1046                PropertyId::Custom(_) => false,
1047            },
1048            PropertyDeclarationId::Custom(name) => {
1049                matches!(*other, PropertyId::Custom(ref other_name) if name == other_name)
1050            },
1051        }
1052    }
1053
1054    /// Whether a given declaration id is a longhand belonging to this
1055    /// shorthand.
1056    pub fn is_longhand_of(&self, shorthand: ShorthandId) -> bool {
1057        match *self {
1058            PropertyDeclarationId::Longhand(ref id) => id.is_longhand_of(shorthand),
1059            _ => false,
1060        }
1061    }
1062
1063    /// Returns the name of the property without CSS escaping.
1064    pub fn name(&self) -> Cow<'static, str> {
1065        match *self {
1066            PropertyDeclarationId::Longhand(id) => id.name().into(),
1067            PropertyDeclarationId::Custom(name) => {
1068                let mut s = String::new();
1069                write!(&mut s, "--{}", name).unwrap();
1070                s.into()
1071            },
1072        }
1073    }
1074
1075    /// Returns longhand id if it is, None otherwise.
1076    #[inline]
1077    pub fn as_longhand(&self) -> Option<LonghandId> {
1078        match *self {
1079            PropertyDeclarationId::Longhand(id) => Some(id),
1080            _ => None,
1081        }
1082    }
1083
1084    /// Return whether this property is logical.
1085    #[inline]
1086    pub fn is_logical(&self) -> bool {
1087        match self {
1088            PropertyDeclarationId::Longhand(id) => id.is_logical(),
1089            PropertyDeclarationId::Custom(_) => false,
1090        }
1091    }
1092
1093    /// If this is a logical property, return the corresponding physical one in
1094    /// the given writing mode.
1095    ///
1096    /// Otherwise, return unchanged.
1097    #[inline]
1098    pub fn to_physical(&self, wm: WritingMode) -> Self {
1099        match self {
1100            Self::Longhand(id) => Self::Longhand(id.to_physical(wm)),
1101            Self::Custom(_) => self.clone(),
1102        }
1103    }
1104
1105    /// Returns whether this property is animatable.
1106    #[inline]
1107    pub fn is_animatable(&self) -> bool {
1108        match self {
1109            Self::Longhand(id) => id.is_animatable(),
1110            Self::Custom(_) => cfg!(feature = "gecko"),
1111        }
1112    }
1113
1114    /// Returns whether this property is animatable in a discrete way.
1115    #[inline]
1116    pub fn is_discrete_animatable(&self) -> bool {
1117        match self {
1118            Self::Longhand(longhand) => longhand.is_discrete_animatable(),
1119            // TODO(bug 1885995): Refine this.
1120            Self::Custom(_) => cfg!(feature = "gecko"),
1121        }
1122    }
1123
1124    /// Converts from a to an adequate nsCSSPropertyID, returning
1125    /// eCSSPropertyExtra_variable for custom properties.
1126    #[cfg(feature = "gecko")]
1127    #[inline]
1128    pub fn to_nscsspropertyid(self) -> nsCSSPropertyID {
1129        match self {
1130            PropertyDeclarationId::Longhand(id) => id.to_nscsspropertyid(),
1131            PropertyDeclarationId::Custom(_) => nsCSSPropertyID::eCSSPropertyExtra_variable,
1132        }
1133    }
1134
1135    /// Convert a `PropertyDeclarationId` into an `AnimatedPropertyID`
1136    ///
1137    /// FIXME(emilio, bug 1870107): We should consider using cbindgen to generate the property id
1138    /// representation or so.
1139    #[cfg(feature = "gecko")]
1140    #[inline]
1141    pub fn to_gecko_animated_property_id(&self) -> AnimatedPropertyID {
1142        match self {
1143            Self::Longhand(id) => AnimatedPropertyID {
1144                mID: id.to_nscsspropertyid(),
1145                mCustomName: RefPtr::null(),
1146            },
1147            Self::Custom(name) => {
1148                let mut property_id = AnimatedPropertyID {
1149                    mID: nsCSSPropertyID::eCSSPropertyExtra_variable,
1150                    mCustomName: RefPtr::null(),
1151                };
1152                property_id.mCustomName.mRawPtr = (*name).clone().into_addrefed();
1153                property_id
1154            },
1155        }
1156    }
1157}
1158
1159/// A set of all properties.
1160#[derive(Clone, PartialEq, Default)]
1161pub struct NonCustomPropertyIdSet {
1162    storage: [u32; ((property_counts::NON_CUSTOM as usize) - 1 + 32) / 32],
1163}
1164
1165impl NonCustomPropertyIdSet {
1166    /// Creates an empty `NonCustomPropertyIdSet`.
1167    pub fn new() -> Self {
1168        Self {
1169            storage: Default::default(),
1170        }
1171    }
1172
1173    /// Insert a non-custom-property in the set.
1174    #[inline]
1175    pub fn insert(&mut self, id: NonCustomPropertyId) {
1176        let bit = id.0 as usize;
1177        self.storage[bit / 32] |= 1 << (bit % 32);
1178    }
1179
1180    /// Return whether the given property is in the set
1181    #[inline]
1182    pub fn contains(&self, id: NonCustomPropertyId) -> bool {
1183        let bit = id.0 as usize;
1184        (self.storage[bit / 32] & (1 << (bit % 32))) != 0
1185    }
1186}
1187
1188/// A set of longhand properties
1189#[derive(Clone, Copy, Debug, Default, MallocSizeOf, PartialEq)]
1190pub struct LonghandIdSet {
1191    storage: [u32; ((property_counts::LONGHANDS as usize) - 1 + 32) / 32],
1192}
1193
1194to_shmem::impl_trivial_to_shmem!(LonghandIdSet);
1195
1196impl LonghandIdSet {
1197    /// Return an empty LonghandIdSet.
1198    #[inline]
1199    pub fn new() -> Self {
1200        Self {
1201            storage: Default::default(),
1202        }
1203    }
1204
1205    /// Iterate over the current longhand id set.
1206    pub fn iter(&self) -> LonghandIdSetIterator<'_> {
1207        LonghandIdSetIterator {
1208            chunks: &self.storage,
1209            cur_chunk: 0,
1210            cur_bit: 0,
1211        }
1212    }
1213
1214    /// Returns whether this set contains at least every longhand that `other`
1215    /// also contains.
1216    pub fn contains_all(&self, other: &Self) -> bool {
1217        for (self_cell, other_cell) in self.storage.iter().zip(other.storage.iter()) {
1218            if (*self_cell & *other_cell) != *other_cell {
1219                return false;
1220            }
1221        }
1222        true
1223    }
1224
1225    /// Returns whether this set contains any longhand that `other` also contains.
1226    pub fn contains_any(&self, other: &Self) -> bool {
1227        for (self_cell, other_cell) in self.storage.iter().zip(other.storage.iter()) {
1228            if (*self_cell & *other_cell) != 0 {
1229                return true;
1230            }
1231        }
1232        false
1233    }
1234
1235    /// Remove all the given properties from the set.
1236    #[inline]
1237    pub fn remove_all(&mut self, other: &Self) {
1238        for (self_cell, other_cell) in self.storage.iter_mut().zip(other.storage.iter()) {
1239            *self_cell &= !*other_cell;
1240        }
1241    }
1242
1243    /// Return whether the given property is in the set
1244    #[inline]
1245    pub fn contains(&self, id: LonghandId) -> bool {
1246        let bit = id as usize;
1247        (self.storage[bit / 32] & (1 << (bit % 32))) != 0
1248    }
1249
1250    /// Return whether this set contains any reset longhand.
1251    #[inline]
1252    pub fn contains_any_reset(&self) -> bool {
1253        self.contains_any(Self::reset())
1254    }
1255
1256    /// Add the given property to the set
1257    #[inline]
1258    pub fn insert(&mut self, id: LonghandId) {
1259        let bit = id as usize;
1260        self.storage[bit / 32] |= 1 << (bit % 32);
1261    }
1262
1263    /// Remove the given property from the set
1264    #[inline]
1265    pub fn remove(&mut self, id: LonghandId) {
1266        let bit = id as usize;
1267        self.storage[bit / 32] &= !(1 << (bit % 32));
1268    }
1269
1270    /// Clear all bits
1271    #[inline]
1272    pub fn clear(&mut self) {
1273        for cell in &mut self.storage {
1274            *cell = 0
1275        }
1276    }
1277
1278    /// Returns whether the set is empty.
1279    #[inline]
1280    pub fn is_empty(&self) -> bool {
1281        self.storage.iter().all(|c| *c == 0)
1282    }
1283}
1284
1285/// An iterator over a set of longhand ids.
1286pub struct LonghandIdSetIterator<'a> {
1287    chunks: &'a [u32],
1288    cur_chunk: u32,
1289    cur_bit: u32, // [0..31], note that zero means the end-most bit
1290}
1291
1292impl<'a> Iterator for LonghandIdSetIterator<'a> {
1293    type Item = LonghandId;
1294
1295    fn next(&mut self) -> Option<Self::Item> {
1296        loop {
1297            debug_assert!(self.cur_bit < 32);
1298            let cur_chunk = self.cur_chunk;
1299            let cur_bit = self.cur_bit;
1300            let chunk = *self.chunks.get(cur_chunk as usize)?;
1301            let next_bit = (chunk >> cur_bit).trailing_zeros();
1302            if next_bit == 32 {
1303                // Totally empty chunk, skip it.
1304                self.cur_bit = 0;
1305                self.cur_chunk += 1;
1306                continue;
1307            }
1308            debug_assert!(cur_bit + next_bit < 32);
1309            let longhand_id = cur_chunk * 32 + cur_bit + next_bit;
1310            debug_assert!(longhand_id as usize <= property_counts::LONGHANDS);
1311            let id: LonghandId = unsafe { mem::transmute(longhand_id as u16) };
1312            self.cur_bit += next_bit + 1;
1313            if self.cur_bit == 32 {
1314                self.cur_bit = 0;
1315                self.cur_chunk += 1;
1316            }
1317            return Some(id);
1318        }
1319    }
1320}
1321
1322/// An ArrayVec of subproperties, contains space for the longest shorthand except all.
1323pub type SubpropertiesVec<T> = ArrayVec<T, { property_counts::MAX_SHORTHAND_EXPANDED }>;
1324
1325/// A stack-allocated vector of `PropertyDeclaration`
1326/// large enough to parse one CSS `key: value` declaration.
1327/// (Shorthands expand to multiple `PropertyDeclaration`s.)
1328#[derive(Default)]
1329pub struct SourcePropertyDeclaration {
1330    /// The storage for the actual declarations (except for all).
1331    pub declarations: SubpropertiesVec<PropertyDeclaration>,
1332    /// Stored separately to keep SubpropertiesVec smaller.
1333    pub all_shorthand: AllShorthand,
1334}
1335
1336// This is huge, but we allocate it on the stack and then never move it,
1337// we only pass `&mut SourcePropertyDeclaration` references around.
1338#[cfg(feature = "gecko")]
1339size_of_test!(SourcePropertyDeclaration, 632);
1340#[cfg(feature = "servo")]
1341size_of_test!(SourcePropertyDeclaration, 568);
1342
1343impl SourcePropertyDeclaration {
1344    /// Create one with a single PropertyDeclaration.
1345    #[inline]
1346    pub fn with_one(decl: PropertyDeclaration) -> Self {
1347        let mut result = Self::default();
1348        result.declarations.push(decl);
1349        result
1350    }
1351
1352    /// Similar to Vec::drain: leaves this empty when the return value is dropped.
1353    pub fn drain(&mut self) -> SourcePropertyDeclarationDrain<'_> {
1354        SourcePropertyDeclarationDrain {
1355            declarations: self.declarations.drain(..),
1356            all_shorthand: mem::replace(&mut self.all_shorthand, AllShorthand::NotSet),
1357        }
1358    }
1359
1360    /// Reset to initial state
1361    pub fn clear(&mut self) {
1362        self.declarations.clear();
1363        self.all_shorthand = AllShorthand::NotSet;
1364    }
1365
1366    /// Whether we're empty.
1367    pub fn is_empty(&self) -> bool {
1368        self.declarations.is_empty() && matches!(self.all_shorthand, AllShorthand::NotSet)
1369    }
1370
1371    /// Push a single declaration.
1372    pub fn push(&mut self, declaration: PropertyDeclaration) {
1373        let _result = self.declarations.try_push(declaration);
1374        debug_assert!(_result.is_ok());
1375    }
1376}
1377
1378/// Return type of SourcePropertyDeclaration::drain
1379pub struct SourcePropertyDeclarationDrain<'a> {
1380    /// A drain over the non-all declarations.
1381    pub declarations:
1382        ArrayVecDrain<'a, PropertyDeclaration, { property_counts::MAX_SHORTHAND_EXPANDED }>,
1383    /// The all shorthand that was set.
1384    pub all_shorthand: AllShorthand,
1385}
1386
1387/// An unparsed property value that contains `var()` functions.
1388#[derive(Debug, Eq, PartialEq, ToShmem)]
1389pub struct UnparsedValue {
1390    /// The variable value, references and so on.
1391    pub(super) variable_value: custom_properties::VariableValue,
1392    /// The shorthand this came from.
1393    from_shorthand: Option<ShorthandId>,
1394}
1395
1396impl ToCss for UnparsedValue {
1397    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
1398    where
1399        W: Write,
1400    {
1401        // https://drafts.csswg.org/css-variables/#variables-in-shorthands
1402        if self.from_shorthand.is_none() {
1403            self.variable_value.to_css(dest)?;
1404        }
1405        Ok(())
1406    }
1407}
1408
1409/// A simple cache for properties that come from a shorthand and have variable
1410/// references.
1411///
1412/// This cache works because of the fact that you can't have competing values
1413/// for a given longhand coming from the same shorthand (but note that this is
1414/// why the shorthand needs to be part of the cache key).
1415pub type ShorthandsWithPropertyReferencesCache =
1416    FxHashMap<(ShorthandId, LonghandId), PropertyDeclaration>;
1417
1418impl UnparsedValue {
1419    fn substitute_variables<'cache>(
1420        &self,
1421        longhand_id: LonghandId,
1422        custom_properties: &ComputedCustomProperties,
1423        stylist: &Stylist,
1424        computed_context: &computed::Context,
1425        shorthand_cache: &'cache mut ShorthandsWithPropertyReferencesCache,
1426    ) -> Cow<'cache, PropertyDeclaration> {
1427        let invalid_at_computed_value_time = || {
1428            let keyword = if longhand_id.inherited() {
1429                CSSWideKeyword::Inherit
1430            } else {
1431                CSSWideKeyword::Initial
1432            };
1433            Cow::Owned(PropertyDeclaration::css_wide_keyword(longhand_id, keyword))
1434        };
1435
1436        if computed_context
1437            .builder
1438            .invalid_non_custom_properties
1439            .contains(longhand_id)
1440        {
1441            return invalid_at_computed_value_time();
1442        }
1443
1444        if let Some(shorthand_id) = self.from_shorthand {
1445            let key = (shorthand_id, longhand_id);
1446            if shorthand_cache.contains_key(&key) {
1447                // FIXME: This double lookup should be avoidable, but rustc
1448                // doesn't like that, see:
1449                //
1450                // https://github.com/rust-lang/rust/issues/82146
1451                return Cow::Borrowed(&shorthand_cache[&key]);
1452            }
1453        }
1454
1455        let css = match custom_properties::substitute(
1456            &self.variable_value,
1457            custom_properties,
1458            stylist,
1459            computed_context,
1460        ) {
1461            Ok(css) => css,
1462            Err(..) => return invalid_at_computed_value_time(),
1463        };
1464
1465        // As of this writing, only the base URL is used for property
1466        // values.
1467        //
1468        // NOTE(emilio): we intentionally pase `None` as the rule type here.
1469        // If something starts depending on it, it's probably a bug, since
1470        // it'd change how values are parsed depending on whether we're in a
1471        // @keyframes rule or not, for example... So think twice about
1472        // whether you want to do this!
1473        //
1474        // FIXME(emilio): ParsingMode is slightly fishy...
1475        let context = ParserContext::new(
1476            Origin::Author,
1477            &self.variable_value.url_data,
1478            None,
1479            ParsingMode::DEFAULT,
1480            computed_context.quirks_mode,
1481            /* namespaces = */ Default::default(),
1482            None,
1483            None,
1484        );
1485
1486        let mut input = ParserInput::new(&css);
1487        let mut input = Parser::new(&mut input);
1488        input.skip_whitespace();
1489
1490        if let Ok(keyword) = input.try_parse(CSSWideKeyword::parse) {
1491            return Cow::Owned(PropertyDeclaration::css_wide_keyword(longhand_id, keyword));
1492        }
1493
1494        let shorthand = match self.from_shorthand {
1495            None => {
1496                return match input.parse_entirely(|input| longhand_id.parse_value(&context, input))
1497                {
1498                    Ok(decl) => Cow::Owned(decl),
1499                    Err(..) => invalid_at_computed_value_time(),
1500                }
1501            },
1502            Some(shorthand) => shorthand,
1503        };
1504
1505        let mut decls = SourcePropertyDeclaration::default();
1506        // parse_into takes care of doing `parse_entirely` for us.
1507        if shorthand
1508            .parse_into(&mut decls, &context, &mut input)
1509            .is_err()
1510        {
1511            return invalid_at_computed_value_time();
1512        }
1513
1514        for declaration in decls.declarations.drain(..) {
1515            let longhand = declaration.id().as_longhand().unwrap();
1516            if longhand.is_logical() {
1517                let writing_mode = computed_context.builder.writing_mode;
1518                shorthand_cache.insert(
1519                    (shorthand, longhand.to_physical(writing_mode)),
1520                    declaration.clone(),
1521                );
1522            }
1523            shorthand_cache.insert((shorthand, longhand), declaration);
1524        }
1525
1526        let key = (shorthand, longhand_id);
1527        match shorthand_cache.get(&key) {
1528            Some(decl) => Cow::Borrowed(decl),
1529            // NOTE: Under normal circumstances we should always have a value, but when prefs
1530            // change we might hit this case. Consider something like `animation-timeline`, which
1531            // is a conditionally-enabled longhand of `animation`:
1532            //
1533            // If we have a sheet with `animation: var(--foo)`, and the `animation-timeline` pref
1534            // enabled, then that expands to an `animation-timeline` declaration at parse time.
1535            //
1536            // If the user disables the pref and, some time later, we get here wanting to compute
1537            // `animation-timeline`, parse_into won't generate any declaration for it anymore, so
1538            // we haven't inserted in the cache. Computing to invalid / initial seems like the most
1539            // sensible thing to do here.
1540            None => invalid_at_computed_value_time(),
1541        }
1542    }
1543}
1544/// A parsed all-shorthand value.
1545pub enum AllShorthand {
1546    /// Not present.
1547    NotSet,
1548    /// A CSS-wide keyword.
1549    CSSWideKeyword(CSSWideKeyword),
1550    /// An all shorthand with var() references that we can't resolve right now.
1551    WithVariables(Arc<UnparsedValue>),
1552}
1553
1554impl Default for AllShorthand {
1555    fn default() -> Self {
1556        Self::NotSet
1557    }
1558}
1559
1560impl AllShorthand {
1561    /// Iterates property declarations from the given all shorthand value.
1562    #[inline]
1563    pub fn declarations(&self) -> AllShorthandDeclarationIterator<'_> {
1564        AllShorthandDeclarationIterator {
1565            all_shorthand: self,
1566            longhands: ShorthandId::All.longhands(),
1567        }
1568    }
1569}
1570
1571/// An iterator over the all shorthand's shorthand declarations.
1572pub struct AllShorthandDeclarationIterator<'a> {
1573    all_shorthand: &'a AllShorthand,
1574    longhands: NonCustomPropertyIterator<LonghandId>,
1575}
1576
1577impl<'a> Iterator for AllShorthandDeclarationIterator<'a> {
1578    type Item = PropertyDeclaration;
1579
1580    #[inline]
1581    fn next(&mut self) -> Option<Self::Item> {
1582        match *self.all_shorthand {
1583            AllShorthand::NotSet => None,
1584            AllShorthand::CSSWideKeyword(ref keyword) => Some(
1585                PropertyDeclaration::css_wide_keyword(self.longhands.next()?, *keyword),
1586            ),
1587            AllShorthand::WithVariables(ref unparsed) => {
1588                Some(PropertyDeclaration::WithVariables(VariableDeclaration {
1589                    id: self.longhands.next()?,
1590                    value: unparsed.clone(),
1591                }))
1592            },
1593        }
1594    }
1595}
1596
1597/// An iterator over all the property ids that are enabled for a given
1598/// shorthand, if that shorthand is enabled for all content too.
1599pub struct NonCustomPropertyIterator<Item: 'static> {
1600    filter: bool,
1601    iter: std::slice::Iter<'static, Item>,
1602}
1603
1604impl<Item> Iterator for NonCustomPropertyIterator<Item>
1605where
1606    Item: 'static + Copy + Into<NonCustomPropertyId>,
1607{
1608    type Item = Item;
1609
1610    fn next(&mut self) -> Option<Self::Item> {
1611        loop {
1612            let id = *self.iter.next()?;
1613            if !self.filter || id.into().enabled_for_all_content() {
1614                return Some(id);
1615            }
1616        }
1617    }
1618}