summaryrefslogtreecommitdiffstats
path: root/servo/components/style/values/generics/grid.rs
blob: e35c96a28ca1e4a88c28c5d1338d89b385a96228 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! Generic types for the handling of
//! [grids](https://drafts.csswg.org/css-grid/).

use crate::parser::{Parse, ParserContext};
use crate::values::specified;
use crate::values::specified::grid::parse_line_names;
use crate::values::{CSSFloat, CustomIdent};
use crate::{Atom, Zero};
use cssparser::Parser;
use std::fmt::{self, Write};
use std::{cmp, usize};
use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss};

/// These are the limits that we choose to clamp grid line numbers to.
/// http://drafts.csswg.org/css-grid/#overlarge-grids
/// line_num is clamped to this range at parse time.
pub const MIN_GRID_LINE: i32 = -10000;
/// See above.
pub const MAX_GRID_LINE: i32 = 10000;

/// A `<grid-line>` type.
///
/// <https://drafts.csswg.org/css-grid/#typedef-grid-row-start-grid-line>
#[derive(
    Clone,
    Debug,
    Default,
    MallocSizeOf,
    PartialEq,
    SpecifiedValueInfo,
    ToComputedValue,
    ToResolvedValue,
    ToShmem,
)]
#[repr(C)]
pub struct GenericGridLine<Integer> {
    /// A custom identifier for named lines, or the empty atom otherwise.
    ///
    /// <https://drafts.csswg.org/css-grid/#grid-placement-slot>
    pub ident: Atom,
    /// Denotes the nth grid line from grid item's placement.
    ///
    /// This is clamped by MIN_GRID_LINE and MAX_GRID_LINE.
    ///
    /// NOTE(emilio): If we ever allow animating these we need to either do
    /// something more complicated for the clamping, or do this clamping at
    /// used-value time.
    pub line_num: Integer,
    /// Flag to check whether it's a `span` keyword.
    pub is_span: bool,
}

pub use self::GenericGridLine as GridLine;

impl<Integer> GridLine<Integer>
where
    Integer: PartialEq + Zero,
{
    /// The `auto` value.
    pub fn auto() -> Self {
        Self {
            is_span: false,
            line_num: Zero::zero(),
            ident: atom!(""),
        }
    }

    /// Check whether this `<grid-line>` represents an `auto` value.
    pub fn is_auto(&self) -> bool {
        self.ident == atom!("") && self.line_num.is_zero() && !self.is_span
    }

    /// Check whether this `<grid-line>` represents a `<custom-ident>` value.
    pub fn is_ident_only(&self) -> bool {
        self.ident != atom!("") && self.line_num.is_zero() && !self.is_span
    }

    /// Check if `self` makes `other` omittable according to the rules at:
    /// https://drafts.csswg.org/css-grid/#propdef-grid-column
    /// https://drafts.csswg.org/css-grid/#propdef-grid-area
    pub fn can_omit(&self, other: &Self) -> bool {
        if self.is_ident_only() {
            self == other
        } else {
            other.is_auto()
        }
    }
}

impl<Integer> ToCss for GridLine<Integer>
where
    Integer: ToCss + PartialEq + Zero,
{
    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
    where
        W: Write,
    {
        if self.is_auto() {
            return dest.write_str("auto");
        }

        if self.is_span {
            dest.write_str("span")?;
        }

        if !self.line_num.is_zero() {
            if self.is_span {
                dest.write_char(' ')?;
            }
            self.line_num.to_css(dest)?;
        }

        if self.ident != atom!("") {
            if self.is_span || !self.line_num.is_zero() {
                dest.write_char(' ')?;
            }
            CustomIdent(self.ident.clone()).to_css(dest)?;
        }

        Ok(())
    }
}

impl Parse for GridLine<specified::Integer> {
    fn parse<'i, 't>(
        context: &ParserContext,
        input: &mut Parser<'i, 't>,
    ) -> Result<Self, ParseError<'i>> {
        let mut grid_line = Self::auto();
        if input.try_parse(|i| i.expect_ident_matching("auto")).is_ok() {
            return Ok(grid_line);
        }

        // <custom-ident> | [ <integer> && <custom-ident>? ] | [ span && [ <integer> || <custom-ident> ] ]
        // This <grid-line> horror is simply,
        // [ span? && [ <custom-ident> || <integer> ] ]
        // And, for some magical reason, "span" should be the first or last value and not in-between.
        let mut val_before_span = false;

        for _ in 0..3 {
            // Maximum possible entities for <grid-line>
            let location = input.current_source_location();
            if input.try_parse(|i| i.expect_ident_matching("span")).is_ok() {
                if grid_line.is_span {
                    return Err(location.new_custom_error(StyleParseErrorKind::UnspecifiedError));
                }

                if !grid_line.line_num.is_zero() || grid_line.ident != atom!("") {
                    val_before_span = true;
                }

                grid_line.is_span = true;
            } else if let Ok(i) = input.try_parse(|i| specified::Integer::parse(context, i)) {
                // FIXME(emilio): Probably shouldn't reject if it's calc()...
                let value = i.value();
                if value == 0 || val_before_span || !grid_line.line_num.is_zero() {
                    return Err(location.new_custom_error(StyleParseErrorKind::UnspecifiedError));
                }

                grid_line.line_num = specified::Integer::new(cmp::max(
                    MIN_GRID_LINE,
                    cmp::min(value, MAX_GRID_LINE),
                ));
            } else if let Ok(name) = input.try_parse(|i| i.expect_ident_cloned()) {
                if val_before_span || grid_line.ident != atom!("") {
                    return Err(location.new_custom_error(StyleParseErrorKind::UnspecifiedError));
                }
                // NOTE(emilio): `span` is consumed above, so we only need to
                // reject `auto`.
                grid_line.ident = CustomIdent::from_ident(location, &name, &["auto"])?.0;
            } else {
                break;
            }
        }

        if grid_line.is_auto() {
            return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
        }

        if grid_line.is_span {
            if !grid_line.line_num.is_zero() {
                if grid_line.line_num.value() <= 0 {
                    // disallow negative integers for grid spans
                    return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
                }
            } else if grid_line.ident == atom!("") {
                // integer could be omitted
                return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
            }
        }

        Ok(grid_line)
    }
}

/// A track breadth for explicit grid track sizing. It's generic solely to
/// avoid re-implementing it for the computed type.
///
/// <https://drafts.csswg.org/css-grid/#typedef-track-breadth>
#[derive(
    Animate,
    Clone,
    Debug,
    MallocSizeOf,
    PartialEq,
    SpecifiedValueInfo,
    ToComputedValue,
    ToCss,
    ToResolvedValue,
    ToShmem,
)]
#[repr(C, u8)]
pub enum GenericTrackBreadth<L> {
    /// The generic type is almost always a non-negative `<length-percentage>`
    Breadth(L),
    /// A flex fraction specified in `fr` units.
    #[css(dimension)]
    Fr(CSSFloat),
    /// `auto`
    Auto,
    /// `min-content`
    MinContent,
    /// `max-content`
    MaxContent,
}

pub use self::GenericTrackBreadth as TrackBreadth;

impl<L> TrackBreadth<L> {
    /// Check whether this is a `<fixed-breadth>` (i.e., it only has `<length-percentage>`)
    ///
    /// <https://drafts.csswg.org/css-grid/#typedef-fixed-breadth>
    #[inline]
    pub fn is_fixed(&self) -> bool {
        matches!(*self, TrackBreadth::Breadth(..))
    }
}

/// A `<track-size>` type for explicit grid track sizing. Like `<track-breadth>`, this is
/// generic only to avoid code bloat. It only takes `<length-percentage>`
///
/// <https://drafts.csswg.org/css-grid/#typedef-track-size>
#[derive(
    Clone,
    Debug,
    MallocSizeOf,
    PartialEq,
    SpecifiedValueInfo,
    ToComputedValue,
    ToResolvedValue,
    ToShmem,
)]
#[repr(C, u8)]
pub enum GenericTrackSize<L> {
    /// A flexible `<track-breadth>`
    Breadth(GenericTrackBreadth<L>),
    /// A `minmax` function for a range over an inflexible `<track-breadth>`
    /// and a flexible `<track-breadth>`
    ///
    /// <https://drafts.csswg.org/css-grid/#valdef-grid-template-columns-minmax>
    #[css(function)]
    Minmax(GenericTrackBreadth<L>, GenericTrackBreadth<L>),
    /// A `fit-content` function.
    ///
    /// This stores a TrackBreadth<L> for convenience, but it can only be a
    /// LengthPercentage.
    ///
    /// <https://drafts.csswg.org/css-grid/#valdef-grid-template-columns-fit-content>
    #[css(function)]
    FitContent(GenericTrackBreadth<L>),
}

pub use self::GenericTrackSize as TrackSize;

impl<L> TrackSize<L> {
    /// The initial value.
    const INITIAL_VALUE: Self = TrackSize::Breadth(TrackBreadth::Auto);

    /// Returns the initial value.
    pub const fn initial_value() -> Self {
        Self::INITIAL_VALUE
    }

    /// Returns true if `self` is the initial value.
    pub fn is_initial(&self) -> bool {
        matches!(*self, TrackSize::Breadth(TrackBreadth::Auto)) // FIXME: can't use Self::INITIAL_VALUE here yet: https://github.com/rust-lang/rust/issues/66585
    }

    /// Check whether this is a `<fixed-size>`
    ///
    /// <https://drafts.csswg.org/css-grid/#typedef-fixed-size>
    pub fn is_fixed(&self) -> bool {
        match *self {
            TrackSize::Breadth(ref breadth) => breadth.is_fixed(),
            // For minmax function, it could be either
            // minmax(<fixed-breadth>, <track-breadth>) or minmax(<inflexible-breadth>, <fixed-breadth>),
            // and since both variants are a subset of minmax(<inflexible-breadth>, <track-breadth>), we only
            // need to make sure that they're fixed. So, we don't have to modify the parsing function.
            TrackSize::Minmax(ref breadth_1, ref breadth_2) => {
                if breadth_1.is_fixed() {
                    return true; // the second value is always a <track-breadth>
                }

                match *breadth_1 {
                    TrackBreadth::Fr(_) => false, // should be <inflexible-breadth> at this point
                    _ => breadth_2.is_fixed(),
                }
            },
            TrackSize::FitContent(_) => false,
        }
    }
}

impl<L> Default for TrackSize<L> {
    fn default() -> Self {
        Self::initial_value()
    }
}

impl<L: ToCss> ToCss for TrackSize<L> {
    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
    where
        W: Write,
    {
        match *self {
            TrackSize::Breadth(ref breadth) => breadth.to_css(dest),
            TrackSize::Minmax(ref min, ref max) => {
                // According to gecko minmax(auto, <flex>) is equivalent to <flex>,
                // and both are serialized as <flex>.
                if let TrackBreadth::Auto = *min {
                    if let TrackBreadth::Fr(_) = *max {
                        return max.to_css(dest);
                    }
                }

                dest.write_str("minmax(")?;
                min.to_css(dest)?;
                dest.write_str(", ")?;
                max.to_css(dest)?;
                dest.write_char(')')
            },
            TrackSize::FitContent(ref lp) => {
                dest.write_str("fit-content(")?;
                lp.to_css(dest)?;
                dest.write_char(')')
            },
        }
    }
}

/// A `<track-size>+`.
/// We use the empty slice as `auto`, and always parse `auto` as an empty slice.
/// This means it's impossible to have a slice containing only one auto item.
#[derive(
    Clone,
    Debug,
    Default,
    MallocSizeOf,
    PartialEq,
    SpecifiedValueInfo,
    ToComputedValue,
    ToCss,
    ToResolvedValue,
    ToShmem,
)]
#[repr(transparent)]
pub struct GenericImplicitGridTracks<T>(
    #[css(if_empty = "auto", iterable)] pub crate::OwnedSlice<T>,
);

pub use self::GenericImplicitGridTracks as ImplicitGridTracks;

impl<T: fmt::Debug + Default + PartialEq> ImplicitGridTracks<T> {
    /// Returns true if current value is same as its initial value (i.e. auto).
    pub fn is_initial(&self) -> bool {
        debug_assert_ne!(
            *self,
            ImplicitGridTracks(crate::OwnedSlice::from(vec![Default::default()]))
        );
        self.0.is_empty()
    }
}

/// Helper function for serializing identifiers with a prefix and suffix, used
/// for serializing <line-names> (in grid).
pub fn concat_serialize_idents<W>(
    prefix: &str,
    suffix: &str,
    slice: &[CustomIdent],
    sep: &str,
    dest: &mut CssWriter<W>,
) -> fmt::Result
where
    W: Write,
{
    if let Some((ref first, rest)) = slice.split_first() {
        dest.write_str(prefix)?;
        first.to_css(dest)?;
        for thing in rest {
            dest.write_str(sep)?;
            thing.to_css(dest)?;
        }

        dest.write_str(suffix)?;
    }

    Ok(())
}

/// The initial argument of the `repeat` function.
///
/// <https://drafts.csswg.org/css-grid/#typedef-track-repeat>
#[derive(
    Clone, Copy, Debug, MallocSizeOf, PartialEq, ToComputedValue, ToCss, ToResolvedValue, ToShmem,
)]
#[repr(C, u8)]
pub enum RepeatCount<Integer> {
    /// A positive integer. This is allowed only for `<track-repeat>` and `<fixed-repeat>`
    Number(Integer),
    /// An `<auto-fill>` keyword allowed only for `<auto-repeat>`
    AutoFill,
    /// An `<auto-fit>` keyword allowed only for `<auto-repeat>`
    AutoFit,
}

impl Parse for RepeatCount<specified::Integer> {
    fn parse<'i, 't>(
        context: &ParserContext,
        input: &mut Parser<'i, 't>,
    ) -> Result<Self, ParseError<'i>> {
        if let Ok(mut i) = input.try_parse(|i| specified::Integer::parse_positive(context, i)) {
            if i.value() > MAX_GRID_LINE {
                i = specified::Integer::new(MAX_GRID_LINE);
            }
            return Ok(RepeatCount::Number(i));
        }
        try_match_ident_ignore_ascii_case! { input,
            "auto-fill" => Ok(RepeatCount::AutoFill),
            "auto-fit" => Ok(RepeatCount::AutoFit),
        }
    }
}

/// The structure containing `<line-names>` and `<track-size>` values.
///
/// It can also hold `repeat()` function parameters, which expands into the respective
/// values in its computed form.
#[derive(
    Clone,
    Debug,
    MallocSizeOf,
    PartialEq,
    SpecifiedValueInfo,
    ToComputedValue,
    ToResolvedValue,
    ToShmem,
)]
#[css(function = "repeat")]
#[repr(C)]
pub struct GenericTrackRepeat<L, I> {
    /// The number of times for the value to be repeated (could also be `auto-fit` or `auto-fill`)
    pub count: RepeatCount<I>,
    /// `<line-names>` accompanying `<track_size>` values.
    ///
    /// If there's no `<line-names>`, then it's represented by an empty vector.
    /// For N `<track-size>` values, there will be N+1 `<line-names>`, and so this vector's
    /// length is always one value more than that of the `<track-size>`.
    pub line_names: crate::OwnedSlice<crate::OwnedSlice<CustomIdent>>,
    /// `<track-size>` values.
    pub track_sizes: crate::OwnedSlice<GenericTrackSize<L>>,
}

pub use self::GenericTrackRepeat as TrackRepeat;

impl<L: ToCss, I: ToCss> ToCss for TrackRepeat<L, I> {
    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
    where
        W: Write,
    {
        dest.write_str("repeat(")?;
        self.count.to_css(dest)?;
        dest.write_str(", ")?;

        let mut line_names_iter = self.line_names.iter();
        for (i, (ref size, ref names)) in self
            .track_sizes
            .iter()
            .zip(&mut line_names_iter)
            .enumerate()
        {
            if i > 0 {
                dest.write_char(' ')?;
            }

            concat_serialize_idents("[", "] ", names, " ", dest)?;
            size.to_css(dest)?;
        }

        if let Some(line_names_last) = line_names_iter.next() {
            concat_serialize_idents(" [", "]", line_names_last, " ", dest)?;
        }

        dest.write_char(')')?;

        Ok(())
    }
}

/// Track list values. Can be <track-size> or <track-repeat>
#[derive(
    Animate,
    Clone,
    Debug,
    MallocSizeOf,
    PartialEq,
    SpecifiedValueInfo,
    ToComputedValue,
    ToCss,
    ToResolvedValue,
    ToShmem,
)]
#[repr(C, u8)]
pub enum GenericTrackListValue<LengthPercentage, Integer> {
    /// A <track-size> value.
    TrackSize(#[animation(field_bound)] GenericTrackSize<LengthPercentage>),
    /// A <track-repeat> value.
    TrackRepeat(#[animation(field_bound)] GenericTrackRepeat<LengthPercentage, Integer>),
}

pub use self::GenericTrackListValue as TrackListValue;

impl<L, I> TrackListValue<L, I> {
    // FIXME: can't use TrackSize::initial_value() here b/c rustc error "is not yet stable as a const fn"
    const INITIAL_VALUE: Self = TrackListValue::TrackSize(TrackSize::Breadth(TrackBreadth::Auto));

    fn is_repeat(&self) -> bool {
        matches!(*self, TrackListValue::TrackRepeat(..))
    }

    /// Returns true if `self` is the initial value.
    pub fn is_initial(&self) -> bool {
        matches!(
            *self,
            TrackListValue::TrackSize(TrackSize::Breadth(TrackBreadth::Auto))
        ) // FIXME: can't use Self::INITIAL_VALUE here yet: https://github.com/rust-lang/rust/issues/66585
    }
}

impl<L, I> Default for TrackListValue<L, I> {
    #[inline]
    fn default() -> Self {
        Self::INITIAL_VALUE
    }
}

/// A grid `<track-list>` type.
///
/// <https://drafts.csswg.org/css-grid/#typedef-track-list>
#[derive(
    Clone,
    Debug,
    MallocSizeOf,
    PartialEq,
    SpecifiedValueInfo,
    ToComputedValue,
    ToResolvedValue,
    ToShmem,
)]
#[repr(C)]
pub struct GenericTrackList<LengthPercentage, Integer> {
    /// The index in `values` where our `<auto-repeat>` value is, if in bounds.
    #[css(skip)]
    pub auto_repeat_index: usize,
    /// A vector of `<track-size> | <track-repeat>` values.
    pub values: crate::OwnedSlice<GenericTrackListValue<LengthPercentage, Integer>>,
    /// `<line-names>` accompanying `<track-size> | <track-repeat>` values.
    ///
    /// If there's no `<line-names>`, then it's represented by an empty vector.
    /// For N values, there will be N+1 `<line-names>`, and so this vector's
    /// length is always one value more than that of the `<track-size>`.
    pub line_names: crate::OwnedSlice<crate::OwnedSlice<CustomIdent>>,
}

pub use self::GenericTrackList as TrackList;

impl<L, I> TrackList<L, I> {
    /// Whether this track list is an explicit track list (that is, doesn't have
    /// any repeat values).
    pub fn is_explicit(&self) -> bool {
        !self.values.iter().any(|v| v.is_repeat())
    }

    /// Whether this track list has an `<auto-repeat>` value.
    pub fn has_auto_repeat(&self) -> bool {
        self.auto_repeat_index < self.values.len()
    }
}

impl<L: ToCss, I: ToCss> ToCss for TrackList<L, I> {
    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
    where
        W: Write,
    {
        let mut values_iter = self.values.iter().peekable();
        let mut line_names_iter = self.line_names.iter().peekable();

        for idx in 0.. {
            let names = line_names_iter.next().unwrap(); // This should exist!
            concat_serialize_idents("[", "]", names, " ", dest)?;

            match values_iter.next() {
                Some(value) => {
                    if !names.is_empty() {
                        dest.write_char(' ')?;
                    }

                    value.to_css(dest)?;
                },
                None => break,
            }

            if values_iter.peek().is_some() ||
                line_names_iter.peek().map_or(false, |v| !v.is_empty()) ||
                (idx + 1 == self.auto_repeat_index)
            {
                dest.write_char(' ')?;
            }
        }

        Ok(())
    }
}

/// The `<line-name-list>` for subgrids.
///
/// `subgrid [ <line-names> | repeat(<positive-integer> | auto-fill, <line-names>+) ]+`
///
/// https://drafts.csswg.org/css-grid-2/#typedef-line-name-list
#[derive(
    Clone,
    Debug,
    Default,
    MallocSizeOf,
    PartialEq,
    SpecifiedValueInfo,
    ToComputedValue,
    ToResolvedValue,
    ToShmem,
)]
#[repr(C)]
pub struct LineNameList {
    /// The optional `<line-name-list>`
    pub names: crate::OwnedSlice<crate::OwnedSlice<CustomIdent>>,
    /// Indicates the starting line names that requires `auto-fill`, if in bounds.
    pub fill_start: usize,
    /// Indicates the number of line names in the auto-fill
    pub fill_len: usize,
}

impl Parse for LineNameList {
    fn parse<'i, 't>(
        context: &ParserContext,
        input: &mut Parser<'i, 't>,
    ) -> Result<Self, ParseError<'i>> {
        input.expect_ident_matching("subgrid")?;
        let mut line_names = vec![];
        let mut fill_data = None;
        // Rather than truncating the result after inserting values, just
        // have a maximum number of values. This gives us an early out on very
        // large name lists, but more importantly prevents OOM on huge repeat
        // expansions. (bug 1583429)
        let mut max_remaining = MAX_GRID_LINE as usize;

        loop {
            let repeat_parse_result = input.try_parse(|input| {
                input.expect_function_matching("repeat")?;
                input.parse_nested_block(|input| {
                    let count = RepeatCount::parse(context, input)?;
                    input.expect_comma()?;
                    let mut names_list = vec![];
                    names_list.push(parse_line_names(input)?); // there should be at least one
                    while let Ok(names) = input.try_parse(parse_line_names) {
                        names_list.push(names);
                    }
                    Ok((names_list, count))
                })
            });
            if let Ok((names_list, count)) = repeat_parse_result {
                let mut handle_size = |n| {
                    let n = cmp::min(n, max_remaining);
                    max_remaining -= n;
                    n
                };
                match count {
                    // FIXME(emilio): we shouldn't expand repeat() at
                    // parse time for subgrid. (bug 1583429)
                    RepeatCount::Number(num) => {
                        let n = handle_size(num.value() as usize * names_list.len());
                        line_names.extend(names_list.iter().cloned().cycle().take(n));
                    },
                    RepeatCount::AutoFill if fill_data.is_none() => {
                        let fill_idx = line_names.len();
                        let fill_len = names_list.len();
                        fill_data = Some((fill_idx, fill_len));
                        let n = handle_size(fill_len);
                        line_names.extend(names_list.into_iter().take(n));
                    },
                    _ => return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)),
                }
            } else if let Ok(names) = input.try_parse(parse_line_names) {
                if max_remaining > 0 {
                    line_names.push(names);
                    max_remaining -= 1;
                }
            } else {
                break;
            }
        }

        debug_assert!(line_names.len() <= MAX_GRID_LINE as usize);

        let (fill_start, fill_len) = fill_data.unwrap_or((0, 0));

        Ok(LineNameList {
            names: line_names.into(),
            fill_start: fill_start,
            fill_len: fill_len,
        })
    }
}

impl ToCss for LineNameList {
    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
    where
        W: Write,
    {
        dest.write_str("subgrid")?;
        let fill_start = self.fill_start;
        let fill_len = self.fill_len;
        for (i, names) in self.names.iter().enumerate() {
            if fill_len > 0 && i == fill_start {
                dest.write_str(" repeat(auto-fill,")?;
            }

            dest.write_str(" [")?;

            if let Some((ref first, rest)) = names.split_first() {
                first.to_css(dest)?;
                for name in rest {
                    dest.write_char(' ')?;
                    name.to_css(dest)?;
                }
            }

            dest.write_char(']')?;
            if fill_len > 0 && i == fill_start + fill_len - 1 {
                dest.write_char(')')?;
            }
        }

        Ok(())
    }
}

/// Variants for `<grid-template-rows> | <grid-template-columns>`
#[derive(
    Animate,
    Clone,
    Debug,
    MallocSizeOf,
    PartialEq,
    SpecifiedValueInfo,
    ToComputedValue,
    ToCss,
    ToResolvedValue,
    ToShmem,
)]
#[value_info(other_values = "subgrid")]
#[repr(C, u8)]
pub enum GenericGridTemplateComponent<L, I> {
    /// `none` value.
    None,
    /// The grid `<track-list>`
    TrackList(
        #[animation(field_bound)]
        #[compute(field_bound)]
        #[resolve(field_bound)]
        #[shmem(field_bound)]
        Box<GenericTrackList<L, I>>,
    ),
    /// A `subgrid <line-name-list>?`
    /// TODO: Support animations for this after subgrid is addressed in [grid-2] spec.
    #[animation(error)]
    Subgrid(Box<LineNameList>),
    /// `masonry` value.
    /// https://github.com/w3c/csswg-drafts/issues/4650
    Masonry,
}

pub use self::GenericGridTemplateComponent as GridTemplateComponent;

impl<L, I> GridTemplateComponent<L, I> {
    /// The initial value.
    const INITIAL_VALUE: Self = Self::None;

    /// Returns length of the <track-list>s <track-size>
    pub fn track_list_len(&self) -> usize {
        match *self {
            GridTemplateComponent::TrackList(ref tracklist) => tracklist.values.len(),
            _ => 0,
        }
    }

    /// Returns true if `self` is the initial value.
    pub fn is_initial(&self) -> bool {
        matches!(*self, Self::None) // FIXME: can't use Self::INITIAL_VALUE here yet: https://github.com/rust-lang/rust/issues/66585
    }
}

impl<L, I> Default for GridTemplateComponent<L, I> {
    #[inline]
    fn default() -> Self {
        Self::INITIAL_VALUE
    }
}