Skip to content

Commit

Permalink
style: Make PercentageOrNumber also reject negative percentages.
Browse files Browse the repository at this point in the history
  • Loading branch information
emilio committed Mar 26, 2017
1 parent c2d9f66 commit 3723794
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
23 changes: 17 additions & 6 deletions components/style/values/specified/length.rs
Expand Up @@ -909,16 +909,27 @@ impl ToCss for Percentage {
}
}

impl Parse for Percentage {
#[inline]
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
let context = AllowedNumericType::All;
impl Percentage {
fn parse_internal(input: &mut Parser, context: AllowedNumericType) -> Result<Self, ()> {
match try!(input.next()) {
Token::Percentage(ref value) if context.is_ok(value.unit_value) =>
Ok(Percentage(value.unit_value)),
Token::Percentage(ref value) if context.is_ok(value.unit_value) => {
Ok(Percentage(value.unit_value))
}
_ => Err(())
}
}

/// Parses a percentage token, but rejects it if it's negative.
pub fn parse_non_negative(input: &mut Parser) -> Result<Self, ()> {
Self::parse_internal(input, AllowedNumericType::NonNegative)
}
}

impl Parse for Percentage {
#[inline]
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
Self::parse_internal(input, AllowedNumericType::All)
}
}

impl ComputedValueAsSpecified for Percentage {}
Expand Down
7 changes: 3 additions & 4 deletions components/style/values/specified/mod.rs
Expand Up @@ -585,13 +585,12 @@ pub enum NumberOrPercentage {
no_viewport_percentage!(NumberOrPercentage);

impl Parse for NumberOrPercentage {
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
if let Ok(per) = input.try(|input| Percentage::parse(context, input)) {
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
if let Ok(per) = input.try(Percentage::parse_non_negative) {
return Ok(NumberOrPercentage::Percentage(per));
}

let num = try!(Number::parse_non_negative(input));
Ok(NumberOrPercentage::Number(num))
Number::parse_non_negative(input).map(NumberOrPercentage::Number)
}
}

Expand Down

0 comments on commit 3723794

Please sign in to comment.