style/values/specified/
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//! CSS handling for the computed value of
6//! [grids](https://drafts.csswg.org/css-grid/)
7
8use crate::parser::{Parse, ParserContext};
9use crate::values::generics::grid::{GridTemplateComponent, ImplicitGridTracks, RepeatCount};
10use crate::values::generics::grid::{LineNameList, LineNameListValue, NameRepeat, TrackBreadth};
11use crate::values::generics::grid::{TrackList, TrackListValue, TrackRepeat, TrackSize};
12use crate::values::specified::{Integer, LengthPercentage};
13use crate::values::{CSSFloat, CustomIdent};
14use cssparser::{Parser, Token};
15use std::mem;
16use style_traits::{ParseError, StyleParseErrorKind};
17
18/// Parse a single flexible length.
19pub fn parse_flex<'i, 't>(input: &mut Parser<'i, 't>) -> Result<CSSFloat, ParseError<'i>> {
20    let location = input.current_source_location();
21    match *input.next()? {
22        Token::Dimension {
23            value, ref unit, ..
24        } if unit.eq_ignore_ascii_case("fr") && value.is_sign_positive() => Ok(value),
25        ref t => Err(location.new_unexpected_token_error(t.clone())),
26    }
27}
28
29impl<L> TrackBreadth<L> {
30    fn parse_keyword<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
31        #[derive(Parse)]
32        enum TrackKeyword {
33            Auto,
34            MaxContent,
35            MinContent,
36        }
37
38        Ok(match TrackKeyword::parse(input)? {
39            TrackKeyword::Auto => TrackBreadth::Auto,
40            TrackKeyword::MaxContent => TrackBreadth::MaxContent,
41            TrackKeyword::MinContent => TrackBreadth::MinContent,
42        })
43    }
44}
45
46impl Parse for TrackBreadth<LengthPercentage> {
47    fn parse<'i, 't>(
48        context: &ParserContext,
49        input: &mut Parser<'i, 't>,
50    ) -> Result<Self, ParseError<'i>> {
51        // FIXME: This and other callers in this file should use
52        // NonNegativeLengthPercentage instead.
53        //
54        // Though it seems these cannot be animated so it's ~ok.
55        if let Ok(lp) = input.try_parse(|i| LengthPercentage::parse_non_negative(context, i)) {
56            return Ok(TrackBreadth::Breadth(lp));
57        }
58
59        if let Ok(f) = input.try_parse(parse_flex) {
60            return Ok(TrackBreadth::Fr(f));
61        }
62
63        Self::parse_keyword(input)
64    }
65}
66
67impl Parse for TrackSize<LengthPercentage> {
68    fn parse<'i, 't>(
69        context: &ParserContext,
70        input: &mut Parser<'i, 't>,
71    ) -> Result<Self, ParseError<'i>> {
72        if let Ok(b) = input.try_parse(|i| TrackBreadth::parse(context, i)) {
73            return Ok(TrackSize::Breadth(b));
74        }
75
76        if input
77            .try_parse(|i| i.expect_function_matching("minmax"))
78            .is_ok()
79        {
80            return input.parse_nested_block(|input| {
81                let inflexible_breadth =
82                    match input.try_parse(|i| LengthPercentage::parse_non_negative(context, i)) {
83                        Ok(lp) => TrackBreadth::Breadth(lp),
84                        Err(..) => TrackBreadth::parse_keyword(input)?,
85                    };
86
87                input.expect_comma()?;
88                Ok(TrackSize::Minmax(
89                    inflexible_breadth,
90                    TrackBreadth::parse(context, input)?,
91                ))
92            });
93        }
94
95        input.expect_function_matching("fit-content")?;
96        let lp = input.parse_nested_block(|i| LengthPercentage::parse_non_negative(context, i))?;
97        Ok(TrackSize::FitContent(TrackBreadth::Breadth(lp)))
98    }
99}
100
101impl Parse for ImplicitGridTracks<TrackSize<LengthPercentage>> {
102    fn parse<'i, 't>(
103        context: &ParserContext,
104        input: &mut Parser<'i, 't>,
105    ) -> Result<Self, ParseError<'i>> {
106        use style_traits::{Separator, Space};
107        let track_sizes = Space::parse(input, |i| TrackSize::parse(context, i))?;
108        if track_sizes.len() == 1 && track_sizes[0].is_initial() {
109            // A single track with the initial value is always represented by an empty slice.
110            return Ok(Default::default());
111        }
112        return Ok(ImplicitGridTracks(track_sizes.into()));
113    }
114}
115
116/// Parse the grid line names into a vector of owned strings.
117///
118/// <https://drafts.csswg.org/css-grid/#typedef-line-names>
119pub fn parse_line_names<'i, 't>(
120    input: &mut Parser<'i, 't>,
121) -> Result<crate::OwnedSlice<CustomIdent>, ParseError<'i>> {
122    input.expect_square_bracket_block()?;
123    input.parse_nested_block(|input| {
124        let mut values = vec![];
125        while let Ok(ident) = input.try_parse(|i| CustomIdent::parse(i, &["span", "auto"])) {
126            values.push(ident);
127        }
128
129        Ok(values.into())
130    })
131}
132
133/// The type of `repeat` function (only used in parsing).
134///
135/// <https://drafts.csswg.org/css-grid/#typedef-track-repeat>
136#[derive(Clone, Copy, Debug, PartialEq, SpecifiedValueInfo)]
137#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
138enum RepeatType {
139    /// [`<auto-repeat>`](https://drafts.csswg.org/css-grid/#typedef-auto-repeat)
140    Auto,
141    /// [`<track-repeat>`](https://drafts.csswg.org/css-grid/#typedef-track-repeat)
142    Normal,
143    /// [`<fixed-repeat>`](https://drafts.csswg.org/css-grid/#typedef-fixed-repeat)
144    Fixed,
145}
146
147impl TrackRepeat<LengthPercentage, Integer> {
148    fn parse_with_repeat_type<'i, 't>(
149        context: &ParserContext,
150        input: &mut Parser<'i, 't>,
151    ) -> Result<(Self, RepeatType), ParseError<'i>> {
152        input
153            .try_parse(|i| i.expect_function_matching("repeat").map_err(|e| e.into()))
154            .and_then(|_| {
155                input.parse_nested_block(|input| {
156                    let count = RepeatCount::parse(context, input)?;
157                    input.expect_comma()?;
158
159                    let is_auto = count == RepeatCount::AutoFit || count == RepeatCount::AutoFill;
160                    let mut repeat_type = if is_auto {
161                        RepeatType::Auto
162                    } else {
163                        // <fixed-size> is a subset of <track-size>, so it should work for both
164                        RepeatType::Fixed
165                    };
166
167                    let mut names = vec![];
168                    let mut values = vec![];
169                    let mut current_names;
170
171                    loop {
172                        current_names = input.try_parse(parse_line_names).unwrap_or_default();
173                        if let Ok(track_size) = input.try_parse(|i| TrackSize::parse(context, i)) {
174                            if !track_size.is_fixed() {
175                                if is_auto {
176                                    // should be <fixed-size> for <auto-repeat>
177                                    return Err(input
178                                        .new_custom_error(StyleParseErrorKind::UnspecifiedError));
179                                }
180
181                                if repeat_type == RepeatType::Fixed {
182                                    repeat_type = RepeatType::Normal // <track-size> for sure
183                                }
184                            }
185
186                            values.push(track_size);
187                            names.push(current_names);
188                        } else {
189                            if values.is_empty() {
190                                // expecting at least one <track-size>
191                                return Err(
192                                    input.new_custom_error(StyleParseErrorKind::UnspecifiedError)
193                                );
194                            }
195
196                            names.push(current_names); // final `<line-names>`
197                            break; // no more <track-size>, breaking
198                        }
199                    }
200
201                    let repeat = TrackRepeat {
202                        count,
203                        track_sizes: values.into(),
204                        line_names: names.into(),
205                    };
206
207                    Ok((repeat, repeat_type))
208                })
209            })
210    }
211}
212
213impl Parse for TrackList<LengthPercentage, Integer> {
214    fn parse<'i, 't>(
215        context: &ParserContext,
216        input: &mut Parser<'i, 't>,
217    ) -> Result<Self, ParseError<'i>> {
218        let mut current_names = vec![];
219        let mut names = vec![];
220        let mut values = vec![];
221
222        // Whether we've parsed an `<auto-repeat>` value.
223        let mut auto_repeat_index = None;
224        // assume that everything is <fixed-size>. This flag is useful when we encounter <auto-repeat>
225        let mut at_least_one_not_fixed = false;
226        loop {
227            current_names
228                .extend_from_slice(&mut input.try_parse(parse_line_names).unwrap_or_default());
229            if let Ok(track_size) = input.try_parse(|i| TrackSize::parse(context, i)) {
230                if !track_size.is_fixed() {
231                    at_least_one_not_fixed = true;
232                    if auto_repeat_index.is_some() {
233                        // <auto-track-list> only accepts <fixed-size> and <fixed-repeat>
234                        return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
235                    }
236                }
237
238                let vec = mem::replace(&mut current_names, vec![]);
239                names.push(vec.into());
240                values.push(TrackListValue::TrackSize(track_size));
241            } else if let Ok((repeat, type_)) =
242                input.try_parse(|i| TrackRepeat::parse_with_repeat_type(context, i))
243            {
244                match type_ {
245                    RepeatType::Normal => {
246                        at_least_one_not_fixed = true;
247                        if auto_repeat_index.is_some() {
248                            // only <fixed-repeat>
249                            return Err(
250                                input.new_custom_error(StyleParseErrorKind::UnspecifiedError)
251                            );
252                        }
253                    },
254                    RepeatType::Auto => {
255                        if auto_repeat_index.is_some() || at_least_one_not_fixed {
256                            // We've either seen <auto-repeat> earlier, or there's at least one non-fixed value
257                            return Err(
258                                input.new_custom_error(StyleParseErrorKind::UnspecifiedError)
259                            );
260                        }
261                        auto_repeat_index = Some(values.len());
262                    },
263                    RepeatType::Fixed => {},
264                }
265
266                let vec = mem::replace(&mut current_names, vec![]);
267                names.push(vec.into());
268                values.push(TrackListValue::TrackRepeat(repeat));
269            } else {
270                if values.is_empty() && auto_repeat_index.is_none() {
271                    return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
272                }
273
274                names.push(current_names.into());
275                break;
276            }
277        }
278
279        Ok(TrackList {
280            auto_repeat_index: auto_repeat_index.unwrap_or(std::usize::MAX),
281            values: values.into(),
282            line_names: names.into(),
283        })
284    }
285}
286
287#[cfg(feature = "gecko")]
288#[inline]
289fn allow_grid_template_subgrids() -> bool {
290    true
291}
292
293#[cfg(feature = "servo")]
294#[inline]
295fn allow_grid_template_subgrids() -> bool {
296    false
297}
298
299#[cfg(feature = "gecko")]
300#[inline]
301fn allow_grid_template_masonry() -> bool {
302    static_prefs::pref!("layout.css.grid-template-masonry-value.enabled")
303}
304
305#[cfg(feature = "servo")]
306#[inline]
307fn allow_grid_template_masonry() -> bool {
308    false
309}
310
311impl Parse for GridTemplateComponent<LengthPercentage, Integer> {
312    fn parse<'i, 't>(
313        context: &ParserContext,
314        input: &mut Parser<'i, 't>,
315    ) -> Result<Self, ParseError<'i>> {
316        if input.try_parse(|i| i.expect_ident_matching("none")).is_ok() {
317            return Ok(GridTemplateComponent::None);
318        }
319
320        Self::parse_without_none(context, input)
321    }
322}
323
324impl GridTemplateComponent<LengthPercentage, Integer> {
325    /// Parses a `GridTemplateComponent<LengthPercentage>` except `none` keyword.
326    pub fn parse_without_none<'i, 't>(
327        context: &ParserContext,
328        input: &mut Parser<'i, 't>,
329    ) -> Result<Self, ParseError<'i>> {
330        if allow_grid_template_subgrids() {
331            if let Ok(t) = input.try_parse(|i| LineNameList::parse(context, i)) {
332                return Ok(GridTemplateComponent::Subgrid(Box::new(t)));
333            }
334        }
335        if allow_grid_template_masonry() {
336            if input
337                .try_parse(|i| i.expect_ident_matching("masonry"))
338                .is_ok()
339            {
340                return Ok(GridTemplateComponent::Masonry);
341            }
342        }
343        let track_list = TrackList::parse(context, input)?;
344        Ok(GridTemplateComponent::TrackList(Box::new(track_list)))
345    }
346}
347
348impl Parse for NameRepeat<Integer> {
349    fn parse<'i, 't>(
350        context: &ParserContext,
351        input: &mut Parser<'i, 't>,
352    ) -> Result<Self, ParseError<'i>> {
353        input.expect_function_matching("repeat")?;
354        input.parse_nested_block(|i| {
355            let count = RepeatCount::parse(context, i)?;
356            // NameRepeat doesn't accept `auto-fit`
357            // https://drafts.csswg.org/css-grid/#typedef-name-repeat
358            if matches!(count, RepeatCount::AutoFit) {
359                return Err(i.new_custom_error(StyleParseErrorKind::UnspecifiedError));
360            }
361
362            i.expect_comma()?;
363            let mut names_list = vec![];
364            names_list.push(parse_line_names(i)?); // there should be at least one
365            while let Ok(names) = i.try_parse(parse_line_names) {
366                names_list.push(names);
367            }
368
369            Ok(NameRepeat {
370                count,
371                line_names: names_list.into(),
372            })
373        })
374    }
375}
376
377impl Parse for LineNameListValue<Integer> {
378    fn parse<'i, 't>(
379        context: &ParserContext,
380        input: &mut Parser<'i, 't>,
381    ) -> Result<Self, ParseError<'i>> {
382        if let Ok(repeat) = input.try_parse(|i| NameRepeat::parse(context, i)) {
383            return Ok(LineNameListValue::Repeat(repeat));
384        }
385
386        parse_line_names(input).map(LineNameListValue::LineNames)
387    }
388}
389
390impl LineNameListValue<Integer> {
391    /// Returns the length of `<line-names>` after expanding repeat(N, ...). This returns zero for
392    /// repeat(auto-fill, ...).
393    #[inline]
394    pub fn line_names_length(&self) -> usize {
395        match *self {
396            Self::LineNames(..) => 1,
397            Self::Repeat(ref r) => {
398                match r.count {
399                    // Note: RepeatCount is always >= 1.
400                    RepeatCount::Number(v) => r.line_names.len() * v.value() as usize,
401                    _ => 0,
402                }
403            },
404        }
405    }
406}
407
408impl Parse for LineNameList<Integer> {
409    fn parse<'i, 't>(
410        context: &ParserContext,
411        input: &mut Parser<'i, 't>,
412    ) -> Result<Self, ParseError<'i>> {
413        input.expect_ident_matching("subgrid")?;
414
415        let mut auto_repeat = false;
416        let mut expanded_line_names_length = 0;
417        let mut line_names = vec![];
418        while let Ok(value) = input.try_parse(|i| LineNameListValue::parse(context, i)) {
419            match value {
420                LineNameListValue::Repeat(ref r) if r.is_auto_fill() => {
421                    if auto_repeat {
422                        // On a subgridded axis, the auto-fill keyword is only valid once per
423                        // <line-name-list>.
424                        // https://drafts.csswg.org/css-grid/#auto-repeat
425                        return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
426                    }
427                    auto_repeat = true;
428                },
429                _ => (),
430            };
431
432            expanded_line_names_length += value.line_names_length();
433            line_names.push(value);
434        }
435
436        Ok(LineNameList {
437            expanded_line_names_length,
438            line_names: line_names.into(),
439        })
440    }
441}