style: Rewrite calc to be cleaner and support arbitrary expressions.

This improves Servo's calc support compliant with[1], and makes it cleaner and
more straight-forward.

[1]: https://github.com/w3c/csswg-drafts/issues/1241
This commit is contained in:
Emilio Cobos Álvarez 2017-05-04 18:51:18 +02:00
parent 36f26148e6
commit 3608dc8088
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
7 changed files with 638 additions and 596 deletions

View file

@ -14,14 +14,14 @@ fn image_orientation_longhand_should_parse_properly() {
assert_eq!(from_image, SpecifiedValue { angle: None, flipped: false });
let flip = parse_longhand!(image_orientation, "flip");
assert_eq!(flip, SpecifiedValue { angle: Some(Angle::from_degrees(0.0)), flipped: true });
assert_eq!(flip, SpecifiedValue { angle: Some(Angle::zero()), flipped: true });
let zero = parse_longhand!(image_orientation, "0deg");
assert_eq!(zero, SpecifiedValue { angle: Some(Angle::from_degrees(0.0)), flipped: false });
assert_eq!(zero, SpecifiedValue { angle: Some(Angle::zero()), flipped: false });
let negative_rad = parse_longhand!(image_orientation, "-1rad");
assert_eq!(negative_rad, SpecifiedValue { angle: Some(Angle::from_radians(-1.0)), flipped: false });
assert_eq!(negative_rad, SpecifiedValue { angle: Some(Angle::from_radians(-1.0, false)), flipped: false });
let flip_with_180 = parse_longhand!(image_orientation, "180deg flip");
assert_eq!(flip_with_180, SpecifiedValue { angle: Some(Angle::from_degrees(180.0)), flipped: true });
assert_eq!(flip_with_180, SpecifiedValue { angle: Some(Angle::from_degrees(180.0, false)), flipped: true });
}

View file

@ -20,6 +20,7 @@ fn test_calc() {
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());
assert!(parse(Length::parse, "calc( 1px + 2px / ( 1 + 2 - 1))").is_ok());
}
#[test]

View file

@ -6,7 +6,6 @@ use app_units::Au;
use parsing::parse;
use style::values::HasViewportPercentage;
use style::values::specified::{AbsoluteLength, NoCalcLength, ViewportPercentageLength};
use style::values::specified::length::{CalcLengthOrPercentage, CalcUnit};
#[test]
fn length_has_viewport_percentage() {
@ -15,21 +14,3 @@ fn length_has_viewport_percentage() {
let l = NoCalcLength::Absolute(AbsoluteLength::Px(Au(100).to_f32_px()));
assert!(!l.has_viewport_percentage());
}
#[test]
fn calc_top_level_number_with_unit() {
fn parse_value(text: &str, unit: CalcUnit) -> Result<CalcLengthOrPercentage, ()> {
parse(|context, input| CalcLengthOrPercentage::parse(context, input, unit), text)
}
assert_eq!(parse_value("1", CalcUnit::Length), Err(()));
assert_eq!(parse_value("1", CalcUnit::LengthOrPercentage), Err(()));
assert_eq!(parse_value("1", CalcUnit::Angle), Err(()));
assert_eq!(parse_value("1", CalcUnit::Time), Err(()));
assert_eq!(parse_value("1px + 1", CalcUnit::Length), Err(()));
assert_eq!(parse_value("1em + 1", CalcUnit::Length), Err(()));
assert_eq!(parse_value("1px + 1", CalcUnit::LengthOrPercentage), Err(()));
assert_eq!(parse_value("1% + 1", CalcUnit::LengthOrPercentage), Err(()));
assert_eq!(parse_value("1rad + 1", CalcUnit::Angle), Err(()));
assert_eq!(parse_value("1deg + 1", CalcUnit::Angle), Err(()));
assert_eq!(parse_value("1s + 1", CalcUnit::Time), Err(()));
}

View file

@ -1043,19 +1043,19 @@ mod shorthand_serialization {
#[test]
fn transform_skew() {
validate_serialization(
&SpecifiedOperation::Skew(Angle::from_degrees(42.3), None),
&SpecifiedOperation::Skew(Angle::from_degrees(42.3, false), None),
"skew(42.3deg)");
validate_serialization(
&SpecifiedOperation::Skew(Angle::from_gradians(-50.0), Some(Angle::from_turns(0.73))),
&SpecifiedOperation::Skew(Angle::from_gradians(-50.0, false), Some(Angle::from_turns(0.73, false))),
"skew(-50grad, 0.73turn)");
validate_serialization(
&SpecifiedOperation::SkewX(Angle::from_radians(0.31)), "skewX(0.31rad)");
&SpecifiedOperation::SkewX(Angle::from_radians(0.31, false)), "skewX(0.31rad)");
}
#[test]
fn transform_rotate() {
validate_serialization(
&SpecifiedOperation::Rotate(Angle::from_turns(35.0)),
&SpecifiedOperation::Rotate(Angle::from_turns(35.0, false)),
"rotate(35turn)"
)
}