diff --git a/components/style/values/specified/length.rs b/components/style/values/specified/length.rs index 2b75773a6ad..1c3f7948f47 100644 --- a/components/style/values/specified/length.rs +++ b/components/style/values/specified/length.rs @@ -585,33 +585,32 @@ impl CalcLengthOrPercentage { products.push(try!(CalcLengthOrPercentage::parse_product(input, expected_unit))); loop { - let position = input.position(); - match input.next_including_whitespace() { - Ok(Token::WhiteSpace(_)) => { - match input.next() { - Ok(Token::Delim('+')) => { - products.push(try!(CalcLengthOrPercentage::parse_product(input, expected_unit))); + let position = input.position(); + match input.next_including_whitespace() { + Ok(Token::WhiteSpace(_)) => { + if input.is_exhausted() { + break; // allow trailing whitespace + } + match input.next() { + Ok(Token::Delim('+')) => { + products.push(try!(CalcLengthOrPercentage::parse_product(input, expected_unit))); + } + Ok(Token::Delim('-')) => { + let mut right = try!(CalcLengthOrPercentage::parse_product(input, expected_unit)); + right.values.push(CalcValueNode::Number(-1.)); + products.push(right); + } + _ => { + return Err(()); + } + } } - - Ok(Token::Delim('-')) => { - let mut right = try!(CalcLengthOrPercentage::parse_product(input, expected_unit)); - right.values.push(CalcValueNode::Number(-1.)); - products.push(right); - } - _ => { - return Err(()); + input.reset(position); + break } - } } - _ => { - input.reset(position); - break - } - } - } - - + } Ok(CalcSumNode { products: products }) } diff --git a/tests/unit/style/parsing/length.rs b/tests/unit/style/parsing/length.rs index a76f26d826b..eda3b3425e3 100644 --- a/tests/unit/style/parsing/length.rs +++ b/tests/unit/style/parsing/length.rs @@ -9,4 +9,7 @@ use style::values::specified::length::Length; #[test] fn test_calc() { assert!(parse(Length::parse, "calc(1px+ 2px)").is_err()); + assert!(parse(Length::parse, "calc( 1px + 2px )").is_ok()); + assert!(parse(Length::parse, "calc(1px + 2px )").is_ok()); + assert!(parse(Length::parse, "calc( 1px + 2px)").is_ok()); }