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