Skip to content

Commit

Permalink
Add RepeatCount enum for handling the first component of repeat()
Browse files Browse the repository at this point in the history
  • Loading branch information
wafflespeanut committed May 18, 2017
1 parent efe1a5d commit d3e394c
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion components/style/values/specified/grid.rs
Expand Up @@ -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))]
Expand Down Expand Up @@ -363,3 +363,48 @@ pub fn parse_line_names(input: &mut Parser) -> Result<Vec<String>, ()> {
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 `<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 {
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
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<W>(&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);

0 comments on commit d3e394c

Please sign in to comment.