From d3e394c68a816bf879db557e4963c57d30bad162 Mon Sep 17 00:00:00 2001 From: Ravi Shankar Date: Tue, 21 Mar 2017 14:41:49 +0530 Subject: [PATCH] Add RepeatCount enum for handling the first component of repeat() --- components/style/values/specified/grid.rs | 47 ++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/components/style/values/specified/grid.rs b/components/style/values/specified/grid.rs index 5b8225a1147b..cd25e678aac3 100644 --- a/components/style/values/specified/grid.rs +++ b/components/style/values/specified/grid.rs @@ -11,7 +11,7 @@ use std::fmt; use style_traits::ToCss; use values::{CSSFloat, CustomIdent, HasViewportPercentage}; use values::computed::{ComputedValueAsSpecified, Context, ToComputedValue}; -use values::specified::LengthOrPercentage; +use values::specified::{Integer, LengthOrPercentage}; #[derive(PartialEq, Clone, Debug)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] @@ -363,3 +363,48 @@ pub fn parse_line_names(input: &mut Parser) -> Result, ()> { Ok(values) }) } + +/// The initial argument of the `repeat` function. +/// +/// https://drafts.csswg.org/css-grid/#typedef-track-repeat +#[derive(Clone, Copy, PartialEq, Debug)] +#[cfg_attr(feature = "servo", derive(HeapSizeOf))] +pub enum RepeatCount { + /// A positive integer. This is allowed only for `` and `` + Number(Integer), + /// An `` keyword allowed only for `` + AutoFill, + /// An `` keyword allowed only for `` + AutoFit, +} + +impl Parse for RepeatCount { + fn parse(context: &ParserContext, input: &mut Parser) -> Result { + if let Ok(i) = input.try(|i| Integer::parse(context, i)) { + if i.value > 0 { + Ok(RepeatCount::Number(i)) + } else { + Err(()) + } + } else { + match_ignore_ascii_case! { &input.expect_ident()?, + "auto-fill" => Ok(RepeatCount::AutoFill), + "auto-fit" => Ok(RepeatCount::AutoFit), + _ => Err(()), + } + } + } +} + +impl ToCss for RepeatCount { + fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + match *self { + RepeatCount::Number(ref c) => c.to_css(dest), + RepeatCount::AutoFill => dest.write_str("auto-fill"), + RepeatCount::AutoFit => dest.write_str("auto-fit"), + } + } +} + +impl ComputedValueAsSpecified for RepeatCount {} +no_viewport_percentage!(RepeatCount);