From dae17dcaf3be874f14f459333cdb738abe019bf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Wed, 8 Mar 2017 16:54:10 +0100 Subject: [PATCH 1/3] style: Fix indentation in calc parsing code. --- components/style/values/specified/length.rs | 42 ++++++++++----------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/components/style/values/specified/length.rs b/components/style/values/specified/length.rs index 2b75773a6ad..42d4a9c1188 100644 --- a/components/style/values/specified/length.rs +++ b/components/style/values/specified/length.rs @@ -585,33 +585,29 @@ 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(_)) => { + 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 }) } From 20d0e71232717c59c64f2537b184d9ef670554f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Wed, 8 Mar 2017 16:54:30 +0100 Subject: [PATCH 2/3] style: Add a test for other valid uses of calc() --- tests/unit/style/parsing/length.rs | 3 +++ 1 file changed, 3 insertions(+) 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()); } From 1eb24f1adc2ec87a82430bed121ba0fd2d462fc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Wed, 8 Mar 2017 17:07:54 +0100 Subject: [PATCH 3/3] style: Allow trailing whitespace in a calc expression. --- components/style/values/specified/length.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/components/style/values/specified/length.rs b/components/style/values/specified/length.rs index 42d4a9c1188..1c3f7948f47 100644 --- a/components/style/values/specified/length.rs +++ b/components/style/values/specified/length.rs @@ -588,6 +588,9 @@ impl CalcLengthOrPercentage { 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)));