style/values/generics/
grid.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//! Generic types for the handling of
6//! [grids](https://drafts.csswg.org/css-grid/).
7
8use crate::parser::{Parse, ParserContext};
9use crate::values::specified;
10use crate::values::{CSSFloat, CustomIdent};
11use crate::{One, Zero};
12use cssparser::Parser;
13use std::fmt::{self, Write};
14use std::{cmp, usize};
15use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss};
16
17/// These are the limits that we choose to clamp grid line numbers to.
18/// http://drafts.csswg.org/css-grid/#overlarge-grids
19/// line_num is clamped to this range at parse time.
20pub const MIN_GRID_LINE: i32 = -10000;
21/// See above.
22pub const MAX_GRID_LINE: i32 = 10000;
23
24/// A `<grid-line>` type.
25///
26/// <https://drafts.csswg.org/css-grid/#typedef-grid-row-start-grid-line>
27#[derive(
28    Clone,
29    Debug,
30    Default,
31    MallocSizeOf,
32    PartialEq,
33    SpecifiedValueInfo,
34    ToComputedValue,
35    ToResolvedValue,
36    ToShmem,
37    ToTyped,
38)]
39#[repr(C)]
40pub struct GenericGridLine<Integer> {
41    /// A custom identifier for named lines, or the empty atom otherwise.
42    ///
43    /// <https://drafts.csswg.org/css-grid/#grid-placement-slot>
44    pub ident: CustomIdent,
45    /// Denotes the nth grid line from grid item's placement.
46    ///
47    /// This is clamped by MIN_GRID_LINE and MAX_GRID_LINE.
48    ///
49    /// NOTE(emilio): If we ever allow animating these we need to either do
50    /// something more complicated for the clamping, or do this clamping at
51    /// used-value time.
52    pub line_num: Integer,
53    /// Flag to check whether it's a `span` keyword.
54    pub is_span: bool,
55}
56
57pub use self::GenericGridLine as GridLine;
58
59impl<Integer> GridLine<Integer>
60where
61    Integer: PartialEq + Zero,
62{
63    /// The `auto` value.
64    pub fn auto() -> Self {
65        Self {
66            is_span: false,
67            line_num: Zero::zero(),
68            ident: CustomIdent(atom!("")),
69        }
70    }
71
72    /// Check whether this `<grid-line>` represents an `auto` value.
73    pub fn is_auto(&self) -> bool {
74        self.ident.0 == atom!("") && self.line_num.is_zero() && !self.is_span
75    }
76
77    /// Check whether this `<grid-line>` represents a `<custom-ident>` value.
78    pub fn is_ident_only(&self) -> bool {
79        self.ident.0 != atom!("") && self.line_num.is_zero() && !self.is_span
80    }
81
82    /// Check if `self` makes `other` omittable according to the rules at:
83    /// https://drafts.csswg.org/css-grid/#propdef-grid-column
84    /// https://drafts.csswg.org/css-grid/#propdef-grid-area
85    pub fn can_omit(&self, other: &Self) -> bool {
86        if self.is_ident_only() {
87            self == other
88        } else {
89            other.is_auto()
90        }
91    }
92}
93
94impl<Integer> ToCss for GridLine<Integer>
95where
96    Integer: ToCss + PartialEq + Zero + One,
97{
98    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
99    where
100        W: Write,
101    {
102        // 1. `auto`
103        if self.is_auto() {
104            return dest.write_str("auto");
105        }
106
107        // 2. `<custom-ident>`
108        if self.is_ident_only() {
109            return self.ident.to_css(dest);
110        }
111
112        // 3. `[ span && [ <integer [1,∞]> || <custom-ident> ] ]`
113        let has_ident = self.ident.0 != atom!("");
114        if self.is_span {
115            dest.write_str("span")?;
116            debug_assert!(!self.line_num.is_zero() || has_ident);
117
118            // We omit `line_num` if
119            // 1. we don't specify it, or
120            // 2. it is the default value, i.e. 1.0, and the ident is specified.
121            // https://drafts.csswg.org/css-grid/#grid-placement-span-int
122            if !self.line_num.is_zero() && !(self.line_num.is_one() && has_ident) {
123                dest.write_char(' ')?;
124                self.line_num.to_css(dest)?;
125            }
126
127            if has_ident {
128                dest.write_char(' ')?;
129                self.ident.to_css(dest)?;
130            }
131            return Ok(());
132        }
133
134        // 4. `[ <integer [-∞,-1]> | <integer [1,∞]> ] && <custom-ident>? ]`
135        debug_assert!(!self.line_num.is_zero());
136        self.line_num.to_css(dest)?;
137        if has_ident {
138            dest.write_char(' ')?;
139            self.ident.to_css(dest)?;
140        }
141        Ok(())
142    }
143}
144
145impl Parse for GridLine<specified::Integer> {
146    fn parse<'i, 't>(
147        context: &ParserContext,
148        input: &mut Parser<'i, 't>,
149    ) -> Result<Self, ParseError<'i>> {
150        let mut grid_line = Self::auto();
151        if input.try_parse(|i| i.expect_ident_matching("auto")).is_ok() {
152            return Ok(grid_line);
153        }
154
155        // <custom-ident> | [ <integer> && <custom-ident>? ] | [ span && [ <integer> || <custom-ident> ] ]
156        // This <grid-line> horror is simply,
157        // [ span? && [ <custom-ident> || <integer> ] ]
158        // And, for some magical reason, "span" should be the first or last value and not in-between.
159        let mut val_before_span = false;
160
161        for _ in 0..3 {
162            // Maximum possible entities for <grid-line>
163            let location = input.current_source_location();
164            if input.try_parse(|i| i.expect_ident_matching("span")).is_ok() {
165                if grid_line.is_span {
166                    return Err(location.new_custom_error(StyleParseErrorKind::UnspecifiedError));
167                }
168
169                if !grid_line.line_num.is_zero() || grid_line.ident.0 != atom!("") {
170                    val_before_span = true;
171                }
172
173                grid_line.is_span = true;
174            } else if let Ok(i) = input.try_parse(|i| specified::Integer::parse(context, i)) {
175                // FIXME(emilio): Probably shouldn't reject if it's calc()...
176                let value = i.value();
177                if value == 0 || val_before_span || !grid_line.line_num.is_zero() {
178                    return Err(location.new_custom_error(StyleParseErrorKind::UnspecifiedError));
179                }
180
181                grid_line.line_num = specified::Integer::new(cmp::max(
182                    MIN_GRID_LINE,
183                    cmp::min(value, MAX_GRID_LINE),
184                ));
185            } else if let Ok(name) = input.try_parse(|i| CustomIdent::parse(i, &["auto"])) {
186                if val_before_span || grid_line.ident.0 != atom!("") {
187                    return Err(location.new_custom_error(StyleParseErrorKind::UnspecifiedError));
188                }
189                // NOTE(emilio): `span` is consumed above, so we only need to
190                // reject `auto`.
191                grid_line.ident = name;
192            } else {
193                break;
194            }
195        }
196
197        if grid_line.is_auto() {
198            return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
199        }
200
201        if grid_line.is_span {
202            if !grid_line.line_num.is_zero() {
203                if grid_line.line_num.value() <= 0 {
204                    // disallow negative integers for grid spans
205                    return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
206                }
207            } else if grid_line.ident.0 == atom!("") {
208                // integer could be omitted
209                return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
210            }
211        }
212
213        Ok(grid_line)
214    }
215}
216
217/// A track breadth for explicit grid track sizing. It's generic solely to
218/// avoid re-implementing it for the computed type.
219///
220/// <https://drafts.csswg.org/css-grid/#typedef-track-breadth>
221#[derive(
222    Animate,
223    Clone,
224    Debug,
225    MallocSizeOf,
226    PartialEq,
227    SpecifiedValueInfo,
228    ToAnimatedValue,
229    ToComputedValue,
230    ToCss,
231    ToResolvedValue,
232    ToShmem,
233)]
234#[repr(C, u8)]
235pub enum GenericTrackBreadth<L> {
236    /// The generic type is almost always a non-negative `<length-percentage>`
237    Breadth(L),
238    /// A flex fraction specified in `fr` units.
239    #[css(dimension)]
240    Fr(CSSFloat),
241    /// `auto`
242    Auto,
243    /// `min-content`
244    MinContent,
245    /// `max-content`
246    MaxContent,
247}
248
249pub use self::GenericTrackBreadth as TrackBreadth;
250
251impl<L> TrackBreadth<L> {
252    /// Check whether this is a `<fixed-breadth>` (i.e., it only has `<length-percentage>`)
253    ///
254    /// <https://drafts.csswg.org/css-grid/#typedef-fixed-breadth>
255    #[inline]
256    pub fn is_fixed(&self) -> bool {
257        matches!(*self, TrackBreadth::Breadth(..))
258    }
259}
260
261/// A `<track-size>` type for explicit grid track sizing. Like `<track-breadth>`, this is
262/// generic only to avoid code bloat. It only takes `<length-percentage>`
263///
264/// <https://drafts.csswg.org/css-grid/#typedef-track-size>
265#[derive(
266    Clone,
267    Debug,
268    MallocSizeOf,
269    PartialEq,
270    SpecifiedValueInfo,
271    ToAnimatedValue,
272    ToComputedValue,
273    ToResolvedValue,
274    ToShmem,
275)]
276#[repr(C, u8)]
277pub enum GenericTrackSize<L> {
278    /// A flexible `<track-breadth>`
279    Breadth(GenericTrackBreadth<L>),
280    /// A `minmax` function for a range over an inflexible `<track-breadth>`
281    /// and a flexible `<track-breadth>`
282    ///
283    /// <https://drafts.csswg.org/css-grid/#valdef-grid-template-columns-minmax>
284    #[css(function)]
285    Minmax(GenericTrackBreadth<L>, GenericTrackBreadth<L>),
286    /// A `fit-content` function.
287    ///
288    /// This stores a TrackBreadth<L> for convenience, but it can only be a
289    /// LengthPercentage.
290    ///
291    /// <https://drafts.csswg.org/css-grid/#valdef-grid-template-columns-fit-content>
292    #[css(function)]
293    FitContent(GenericTrackBreadth<L>),
294}
295
296pub use self::GenericTrackSize as TrackSize;
297
298impl<L> TrackSize<L> {
299    /// The initial value.
300    const INITIAL_VALUE: Self = TrackSize::Breadth(TrackBreadth::Auto);
301
302    /// Returns the initial value.
303    pub const fn initial_value() -> Self {
304        Self::INITIAL_VALUE
305    }
306
307    /// Returns true if `self` is the initial value.
308    pub fn is_initial(&self) -> bool {
309        matches!(*self, TrackSize::Breadth(TrackBreadth::Auto)) // FIXME: can't use Self::INITIAL_VALUE here yet: https://github.com/rust-lang/rust/issues/66585
310    }
311
312    /// Check whether this is a `<fixed-size>`
313    ///
314    /// <https://drafts.csswg.org/css-grid/#typedef-fixed-size>
315    pub fn is_fixed(&self) -> bool {
316        match *self {
317            TrackSize::Breadth(ref breadth) => breadth.is_fixed(),
318            // For minmax function, it could be either
319            // minmax(<fixed-breadth>, <track-breadth>) or minmax(<inflexible-breadth>, <fixed-breadth>),
320            // and since both variants are a subset of minmax(<inflexible-breadth>, <track-breadth>), we only
321            // need to make sure that they're fixed. So, we don't have to modify the parsing function.
322            TrackSize::Minmax(ref breadth_1, ref breadth_2) => {
323                if breadth_1.is_fixed() {
324                    return true; // the second value is always a <track-breadth>
325                }
326
327                match *breadth_1 {
328                    TrackBreadth::Fr(_) => false, // should be <inflexible-breadth> at this point
329                    _ => breadth_2.is_fixed(),
330                }
331            },
332            TrackSize::FitContent(_) => false,
333        }
334    }
335}
336
337impl<L> Default for TrackSize<L> {
338    fn default() -> Self {
339        Self::initial_value()
340    }
341}
342
343impl<L: ToCss> ToCss for TrackSize<L> {
344    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
345    where
346        W: Write,
347    {
348        match *self {
349            TrackSize::Breadth(ref breadth) => breadth.to_css(dest),
350            TrackSize::Minmax(ref min, ref max) => {
351                // According to gecko minmax(auto, <flex>) is equivalent to <flex>,
352                // and both are serialized as <flex>.
353                if let TrackBreadth::Auto = *min {
354                    if let TrackBreadth::Fr(_) = *max {
355                        return max.to_css(dest);
356                    }
357                }
358
359                dest.write_str("minmax(")?;
360                min.to_css(dest)?;
361                dest.write_str(", ")?;
362                max.to_css(dest)?;
363                dest.write_char(')')
364            },
365            TrackSize::FitContent(ref lp) => {
366                dest.write_str("fit-content(")?;
367                lp.to_css(dest)?;
368                dest.write_char(')')
369            },
370        }
371    }
372}
373
374/// A `<track-size>+`.
375/// We use the empty slice as `auto`, and always parse `auto` as an empty slice.
376/// This means it's impossible to have a slice containing only one auto item.
377#[derive(
378    Clone,
379    Debug,
380    Default,
381    MallocSizeOf,
382    PartialEq,
383    SpecifiedValueInfo,
384    ToComputedValue,
385    ToCss,
386    ToResolvedValue,
387    ToShmem,
388    ToTyped,
389)]
390#[repr(transparent)]
391pub struct GenericImplicitGridTracks<T>(
392    #[css(if_empty = "auto", iterable)] pub crate::OwnedSlice<T>,
393);
394
395pub use self::GenericImplicitGridTracks as ImplicitGridTracks;
396
397impl<T: fmt::Debug + Default + PartialEq> ImplicitGridTracks<T> {
398    /// Returns true if current value is same as its initial value (i.e. auto).
399    pub fn is_initial(&self) -> bool {
400        debug_assert_ne!(
401            *self,
402            ImplicitGridTracks(crate::OwnedSlice::from(vec![Default::default()]))
403        );
404        self.0.is_empty()
405    }
406}
407
408/// Helper function for serializing identifiers with a prefix and suffix, used
409/// for serializing <line-names> (in grid).
410pub fn concat_serialize_idents<W>(
411    prefix: &str,
412    suffix: &str,
413    slice: &[CustomIdent],
414    sep: &str,
415    dest: &mut CssWriter<W>,
416) -> fmt::Result
417where
418    W: Write,
419{
420    if let Some((ref first, rest)) = slice.split_first() {
421        dest.write_str(prefix)?;
422        first.to_css(dest)?;
423        for thing in rest {
424            dest.write_str(sep)?;
425            thing.to_css(dest)?;
426        }
427
428        dest.write_str(suffix)?;
429    }
430
431    Ok(())
432}
433
434/// The initial argument of the `repeat` function.
435///
436/// <https://drafts.csswg.org/css-grid/#typedef-track-repeat>
437#[derive(
438    Clone,
439    Copy,
440    Debug,
441    MallocSizeOf,
442    PartialEq,
443    SpecifiedValueInfo,
444    ToAnimatedValue,
445    ToComputedValue,
446    ToCss,
447    ToResolvedValue,
448    ToShmem,
449)]
450#[repr(C, u8)]
451pub enum RepeatCount<Integer> {
452    /// A positive integer. This is allowed only for `<track-repeat>` and `<fixed-repeat>`
453    Number(Integer),
454    /// An `<auto-fill>` keyword allowed only for `<auto-repeat>`
455    AutoFill,
456    /// An `<auto-fit>` keyword allowed only for `<auto-repeat>`
457    AutoFit,
458}
459
460impl Parse for RepeatCount<specified::Integer> {
461    fn parse<'i, 't>(
462        context: &ParserContext,
463        input: &mut Parser<'i, 't>,
464    ) -> Result<Self, ParseError<'i>> {
465        if let Ok(mut i) = input.try_parse(|i| specified::Integer::parse_positive(context, i)) {
466            if i.value() > MAX_GRID_LINE {
467                i = specified::Integer::new(MAX_GRID_LINE);
468            }
469            return Ok(RepeatCount::Number(i));
470        }
471        try_match_ident_ignore_ascii_case! { input,
472            "auto-fill" => Ok(RepeatCount::AutoFill),
473            "auto-fit" => Ok(RepeatCount::AutoFit),
474        }
475    }
476}
477
478/// The structure containing `<line-names>` and `<track-size>` values.
479#[derive(
480    Clone,
481    Debug,
482    MallocSizeOf,
483    PartialEq,
484    SpecifiedValueInfo,
485    ToAnimatedValue,
486    ToComputedValue,
487    ToResolvedValue,
488    ToShmem,
489)]
490#[css(function = "repeat")]
491#[repr(C)]
492pub struct GenericTrackRepeat<L, I> {
493    /// The number of times for the value to be repeated (could also be `auto-fit` or `auto-fill`)
494    pub count: RepeatCount<I>,
495    /// `<line-names>` accompanying `<track_size>` values.
496    ///
497    /// If there's no `<line-names>`, then it's represented by an empty vector.
498    /// For N `<track-size>` values, there will be N+1 `<line-names>`, and so this vector's
499    /// length is always one value more than that of the `<track-size>`.
500    pub line_names: crate::OwnedSlice<crate::OwnedSlice<CustomIdent>>,
501    /// `<track-size>` values.
502    pub track_sizes: crate::OwnedSlice<GenericTrackSize<L>>,
503}
504
505pub use self::GenericTrackRepeat as TrackRepeat;
506
507impl<L: ToCss, I: ToCss> ToCss for TrackRepeat<L, I> {
508    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
509    where
510        W: Write,
511    {
512        dest.write_str("repeat(")?;
513        self.count.to_css(dest)?;
514        dest.write_str(", ")?;
515
516        let mut line_names_iter = self.line_names.iter();
517        for (i, (ref size, ref names)) in self
518            .track_sizes
519            .iter()
520            .zip(&mut line_names_iter)
521            .enumerate()
522        {
523            if i > 0 {
524                dest.write_char(' ')?;
525            }
526
527            concat_serialize_idents("[", "] ", names, " ", dest)?;
528            size.to_css(dest)?;
529        }
530
531        if let Some(line_names_last) = line_names_iter.next() {
532            concat_serialize_idents(" [", "]", line_names_last, " ", dest)?;
533        }
534
535        dest.write_char(')')?;
536
537        Ok(())
538    }
539}
540
541/// Track list values. Can be <track-size> or <track-repeat>
542#[derive(
543    Animate,
544    Clone,
545    Debug,
546    MallocSizeOf,
547    PartialEq,
548    SpecifiedValueInfo,
549    ToAnimatedValue,
550    ToComputedValue,
551    ToCss,
552    ToResolvedValue,
553    ToShmem,
554)]
555#[repr(C, u8)]
556pub enum GenericTrackListValue<LengthPercentage, Integer> {
557    /// A <track-size> value.
558    TrackSize(#[animation(field_bound)] GenericTrackSize<LengthPercentage>),
559    /// A <track-repeat> value.
560    TrackRepeat(#[animation(field_bound)] GenericTrackRepeat<LengthPercentage, Integer>),
561}
562
563pub use self::GenericTrackListValue as TrackListValue;
564
565impl<L, I> TrackListValue<L, I> {
566    // FIXME: can't use TrackSize::initial_value() here b/c rustc error "is not yet stable as a const fn"
567    const INITIAL_VALUE: Self = TrackListValue::TrackSize(TrackSize::Breadth(TrackBreadth::Auto));
568
569    fn is_repeat(&self) -> bool {
570        matches!(*self, TrackListValue::TrackRepeat(..))
571    }
572
573    /// Returns true if `self` is the initial value.
574    pub fn is_initial(&self) -> bool {
575        matches!(
576            *self,
577            TrackListValue::TrackSize(TrackSize::Breadth(TrackBreadth::Auto))
578        ) // FIXME: can't use Self::INITIAL_VALUE here yet: https://github.com/rust-lang/rust/issues/66585
579    }
580}
581
582impl<L, I> Default for TrackListValue<L, I> {
583    #[inline]
584    fn default() -> Self {
585        Self::INITIAL_VALUE
586    }
587}
588
589/// A grid `<track-list>` type.
590///
591/// <https://drafts.csswg.org/css-grid/#typedef-track-list>
592#[derive(
593    Clone,
594    Debug,
595    MallocSizeOf,
596    PartialEq,
597    SpecifiedValueInfo,
598    ToAnimatedValue,
599    ToComputedValue,
600    ToResolvedValue,
601    ToShmem,
602)]
603#[repr(C)]
604pub struct GenericTrackList<LengthPercentage, Integer> {
605    /// The index in `values` where our `<auto-repeat>` value is, if in bounds.
606    #[css(skip)]
607    pub auto_repeat_index: usize,
608    /// A vector of `<track-size> | <track-repeat>` values.
609    pub values: crate::OwnedSlice<GenericTrackListValue<LengthPercentage, Integer>>,
610    /// `<line-names>` accompanying `<track-size> | <track-repeat>` values.
611    ///
612    /// If there's no `<line-names>`, then it's represented by an empty vector.
613    /// For N values, there will be N+1 `<line-names>`, and so this vector's
614    /// length is always one value more than that of the `<track-size>`.
615    pub line_names: crate::OwnedSlice<crate::OwnedSlice<CustomIdent>>,
616}
617
618pub use self::GenericTrackList as TrackList;
619
620impl<L, I> TrackList<L, I> {
621    /// Whether this track list is an explicit track list (that is, doesn't have
622    /// any repeat values).
623    pub fn is_explicit(&self) -> bool {
624        !self.values.iter().any(|v| v.is_repeat())
625    }
626
627    /// Whether this track list has an `<auto-repeat>` value.
628    pub fn has_auto_repeat(&self) -> bool {
629        self.auto_repeat_index < self.values.len()
630    }
631}
632
633impl<L: ToCss, I: ToCss> ToCss for TrackList<L, I> {
634    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
635    where
636        W: Write,
637    {
638        let mut values_iter = self.values.iter().peekable();
639        let mut line_names_iter = self.line_names.iter().peekable();
640
641        for idx in 0.. {
642            let names = line_names_iter.next().unwrap(); // This should exist!
643            concat_serialize_idents("[", "]", names, " ", dest)?;
644
645            match values_iter.next() {
646                Some(value) => {
647                    if !names.is_empty() {
648                        dest.write_char(' ')?;
649                    }
650
651                    value.to_css(dest)?;
652                },
653                None => break,
654            }
655
656            if values_iter.peek().is_some()
657                || line_names_iter.peek().map_or(false, |v| !v.is_empty())
658                || (idx + 1 == self.auto_repeat_index)
659            {
660                dest.write_char(' ')?;
661            }
662        }
663
664        Ok(())
665    }
666}
667
668/// The `<name-repeat>` for subgrids.
669///
670/// <name-repeat> = repeat( [ <integer [1,∞]> | auto-fill ], <line-names>+)
671///
672/// https://drafts.csswg.org/css-grid/#typedef-name-repeat
673#[derive(
674    Clone,
675    Debug,
676    MallocSizeOf,
677    PartialEq,
678    SpecifiedValueInfo,
679    ToAnimatedValue,
680    ToComputedValue,
681    ToResolvedValue,
682    ToShmem,
683)]
684#[repr(C)]
685pub struct GenericNameRepeat<I> {
686    /// The number of times for the value to be repeated (could also be `auto-fill`).
687    /// Note: `RepeatCount` accepts `auto-fit`, so we should reject it after parsing it.
688    pub count: RepeatCount<I>,
689    /// This represents `<line-names>+`. The length of the outer vector is at least one.
690    pub line_names: crate::OwnedSlice<crate::OwnedSlice<CustomIdent>>,
691}
692
693pub use self::GenericNameRepeat as NameRepeat;
694
695impl<I: ToCss> ToCss for NameRepeat<I> {
696    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
697    where
698        W: Write,
699    {
700        dest.write_str("repeat(")?;
701        self.count.to_css(dest)?;
702        dest.write_char(',')?;
703
704        for ref names in self.line_names.iter() {
705            if names.is_empty() {
706                // Note: concat_serialize_idents() skip the empty list so we have to handle it
707                // manually for NameRepeat.
708                dest.write_str(" []")?;
709            } else {
710                concat_serialize_idents(" [", "]", names, " ", dest)?;
711            }
712        }
713
714        dest.write_char(')')
715    }
716}
717
718impl<I> NameRepeat<I> {
719    /// Returns true if it is auto-fill.
720    #[inline]
721    pub fn is_auto_fill(&self) -> bool {
722        matches!(self.count, RepeatCount::AutoFill)
723    }
724}
725
726/// A single value for `<line-names>` or `<name-repeat>`.
727#[derive(
728    Clone,
729    Debug,
730    MallocSizeOf,
731    PartialEq,
732    SpecifiedValueInfo,
733    ToAnimatedValue,
734    ToComputedValue,
735    ToResolvedValue,
736    ToShmem,
737)]
738#[repr(C, u8)]
739pub enum GenericLineNameListValue<I> {
740    /// `<line-names>`.
741    LineNames(crate::OwnedSlice<CustomIdent>),
742    /// `<name-repeat>`.
743    Repeat(GenericNameRepeat<I>),
744}
745
746pub use self::GenericLineNameListValue as LineNameListValue;
747
748impl<I: ToCss> ToCss for LineNameListValue<I> {
749    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
750    where
751        W: Write,
752    {
753        match *self {
754            Self::Repeat(ref r) => r.to_css(dest),
755            Self::LineNames(ref names) => {
756                dest.write_char('[')?;
757
758                if let Some((ref first, rest)) = names.split_first() {
759                    first.to_css(dest)?;
760                    for name in rest {
761                        dest.write_char(' ')?;
762                        name.to_css(dest)?;
763                    }
764                }
765
766                dest.write_char(']')
767            },
768        }
769    }
770}
771
772/// The `<line-name-list>` for subgrids.
773///
774/// <line-name-list> = [ <line-names> | <name-repeat> ]+
775/// <name-repeat> = repeat( [ <integer [1,∞]> | auto-fill ], <line-names>+)
776///
777/// https://drafts.csswg.org/css-grid/#typedef-line-name-list
778#[derive(
779    Clone,
780    Debug,
781    Default,
782    MallocSizeOf,
783    PartialEq,
784    SpecifiedValueInfo,
785    ToAnimatedValue,
786    ToComputedValue,
787    ToResolvedValue,
788    ToShmem,
789)]
790#[repr(C)]
791pub struct GenericLineNameList<I> {
792    /// The pre-computed length of line_names, without the length of repeat(auto-fill, ...).
793    // We precomputed this at parsing time, so we can avoid an extra loop when expanding
794    // repeat(auto-fill).
795    pub expanded_line_names_length: usize,
796    /// The line name list.
797    pub line_names: crate::OwnedSlice<GenericLineNameListValue<I>>,
798}
799
800pub use self::GenericLineNameList as LineNameList;
801
802impl<I: ToCss> ToCss for LineNameList<I> {
803    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
804    where
805        W: Write,
806    {
807        dest.write_str("subgrid")?;
808
809        for value in self.line_names.iter() {
810            dest.write_char(' ')?;
811            value.to_css(dest)?;
812        }
813
814        Ok(())
815    }
816}
817
818/// Variants for `<grid-template-rows> | <grid-template-columns>`
819#[derive(
820    Animate,
821    Clone,
822    Debug,
823    MallocSizeOf,
824    PartialEq,
825    SpecifiedValueInfo,
826    ToAnimatedValue,
827    ToComputedValue,
828    ToCss,
829    ToResolvedValue,
830    ToShmem,
831    ToTyped,
832)]
833#[value_info(other_values = "subgrid")]
834#[repr(C, u8)]
835pub enum GenericGridTemplateComponent<L, I> {
836    /// `none` value.
837    None,
838    /// The grid `<track-list>`
839    TrackList(
840        #[animation(field_bound)]
841        #[compute(field_bound)]
842        #[resolve(field_bound)]
843        #[shmem(field_bound)]
844        Box<GenericTrackList<L, I>>,
845    ),
846    /// A `subgrid <line-name-list>?`
847    /// TODO: Support animations for this after subgrid is addressed in [grid-2] spec.
848    #[animation(error)]
849    Subgrid(Box<GenericLineNameList<I>>),
850    /// `masonry` value.
851    /// https://github.com/w3c/csswg-drafts/issues/4650
852    Masonry,
853}
854
855pub use self::GenericGridTemplateComponent as GridTemplateComponent;
856
857impl<L, I> GridTemplateComponent<L, I> {
858    /// The initial value.
859    const INITIAL_VALUE: Self = Self::None;
860
861    /// Returns length of the <track-list>s <track-size>
862    pub fn track_list_len(&self) -> usize {
863        match *self {
864            GridTemplateComponent::TrackList(ref tracklist) => tracklist.values.len(),
865            _ => 0,
866        }
867    }
868
869    /// Returns true if `self` is the initial value.
870    pub fn is_initial(&self) -> bool {
871        matches!(*self, Self::None) // FIXME: can't use Self::INITIAL_VALUE here yet: https://github.com/rust-lang/rust/issues/66585
872    }
873}
874
875impl<L, I> Default for GridTemplateComponent<L, I> {
876    #[inline]
877    fn default() -> Self {
878        Self::INITIAL_VALUE
879    }
880}