Implement serialization for transform functions.

Preserve more information from transform function parsing.
Preserve angle unit while parsing.
Simplify SpecifiedMatrix.
Use the write! macro for formatting with a helper called Css.
Implement ToCss for &T if T implements ToCss.
Add some tests and update others.

closes #15194
This commit is contained in:
Pyfisch 2017-04-03 15:19:37 +02:00
parent e2671459cb
commit 480f59c880
8 changed files with 459 additions and 315 deletions

View file

@ -26,3 +26,11 @@ fn test_will_change() {
assert!(parse(will_change::parse, "contents, inherit, initial").is_err());
assert!(parse(will_change::parse, "transform scroll-position").is_err());
}
#[test]
fn test_transform_translate() {
use style::properties::longhands::transform;
assert_roundtrip_with_context!(transform::parse, "translate(2px)");
assert_roundtrip_with_context!(transform::parse, "translate(2px, 5px)");
assert!(parse(transform::parse, "translate(2px foo)").is_err());
}

View file

@ -28,8 +28,7 @@ fn test_linear_gradient() {
assert_roundtrip_with_context!(Image::parse, "linear-gradient(to right top, red, green)");
// Parsing with <angle>
assert_roundtrip_with_context!(Image::parse, "linear-gradient(45deg, red, green)",
"linear-gradient(0.7853982rad, red, green)");
assert_roundtrip_with_context!(Image::parse, "linear-gradient(45deg, red, green)");
// Parsing with more than two entries in <color-stop-list>
assert_roundtrip_with_context!(Image::parse, "linear-gradient(red, yellow, green)");

View file

@ -9,7 +9,6 @@ use style::stylesheets::Origin;
#[test]
fn image_orientation_longhand_should_parse_properly() {
use std::f32::consts::PI;
use style::properties::longhands::image_orientation;
use style::properties::longhands::image_orientation::SpecifiedValue;
use style::values::specified::Angle;
@ -21,11 +20,11 @@ fn image_orientation_longhand_should_parse_properly() {
assert_eq!(flip, SpecifiedValue { angle: None, flipped: true });
let zero = parse_longhand!(image_orientation, "0deg");
assert_eq!(zero, SpecifiedValue { angle: Some(Angle::from_radians(0.0)), flipped: false });
assert_eq!(zero, SpecifiedValue { angle: Some(Angle::from_degrees(0.0)), flipped: false });
let negative_rad = parse_longhand!(image_orientation, "-1rad");
assert_eq!(negative_rad, SpecifiedValue { angle: Some(Angle::from_radians(-1.0)), flipped: false });
let flip_with_180 = parse_longhand!(image_orientation, "180deg flip");
assert_eq!(flip_with_180, SpecifiedValue { angle: Some(Angle::from_radians(PI)), flipped: true });
assert_eq!(flip_with_180, SpecifiedValue { angle: Some(Angle::from_degrees(180.0)), flipped: true });
}

View file

@ -962,6 +962,8 @@ mod shorthand_serialization {
mod transform {
pub use super::*;
use style::properties::longhands::transform::SpecifiedOperation;
use style::values::specified::{Angle, Number};
#[test]
fn should_serialize_none_correctly() {
@ -982,6 +984,46 @@ mod shorthand_serialization {
assert_eq!(try_serialize.is_ok(), true);
assert_eq!(s, "none");
}
#[inline(always)]
fn validate_serialization<T: ToCss>(op: &T, expected_string: &'static str) {
let css_string = op.to_css_string();
assert_eq!(css_string, expected_string);
}
#[test]
fn transform_scale() {
validate_serialization(&SpecifiedOperation::Scale(Number::new(1.3), None), "scale(1.3)");
validate_serialization(
&SpecifiedOperation::Scale(Number::new(2.0), Some(Number::new(2.0))),
"scale(2, 2)");
validate_serialization(&SpecifiedOperation::ScaleX(Number::new(42.0)), "scaleX(42)");
validate_serialization(&SpecifiedOperation::ScaleY(Number::new(0.3)), "scaleY(0.3)");
validate_serialization(&SpecifiedOperation::ScaleZ(Number::new(1.0)), "scaleZ(1)");
validate_serialization(
&SpecifiedOperation::Scale3D(Number::new(4.0), Number::new(5.0), Number::new(6.0)),
"scale3d(4, 5, 6)");
}
#[test]
fn transform_skew() {
validate_serialization(
&SpecifiedOperation::Skew(Angle::from_degrees(42.3), None),
"skew(42.3deg)");
validate_serialization(
&SpecifiedOperation::Skew(Angle::from_gradians(-50.0), Some(Angle::from_turns(0.73))),
"skew(-50grad, 0.73turn)");
validate_serialization(
&SpecifiedOperation::SkewX(Angle::from_radians(0.31)), "skewX(0.31rad)");
}
#[test]
fn transform_rotate() {
validate_serialization(
&SpecifiedOperation::Rotate(Angle::from_turns(35.0)),
"rotate(35turn)"
)
}
}
mod quotes {

View file

@ -1,26 +0,0 @@
[css-transform-style-evaluation-validation.htm]
type: testharness
[transform: Check rotate(0) input.]
expected: FAIL
[transform: Check rotateX(0) input.]
expected: FAIL
[transform: Check rotateY(0) input.]
expected: FAIL
[transform: Check rotateZ(0) input.]
expected: FAIL
[transform: Check rotate(string) input.]
expected: FAIL
[transform: Check rotateX(string) input.]
expected: FAIL
[transform: Check rotateY(string) input.]
expected: FAIL
[transform: Check rotateZ(string) input.]
expected: FAIL