Address review comments

This commit is contained in:
Manish Goregaokar 2016-08-05 12:14:58 +05:30
parent d1e45f78af
commit 234219cd84
4 changed files with 155 additions and 147 deletions

View file

@ -8,10 +8,13 @@
//! [basic-shape]: https://drafts.csswg.org/css-shapes/#typedef-basic-shape //! [basic-shape]: https://drafts.csswg.org/css-shapes/#typedef-basic-shape
use cssparser::ToCss; use cssparser::ToCss;
use properties::shorthands::serialize_four_sides;
use std::fmt; use std::fmt;
use values::computed::position::Position; use values::computed::position::Position;
use values::computed::{BorderRadiusSize, LengthOrPercentage}; use values::computed::{BorderRadiusSize, LengthOrPercentage};
pub use values::specified::basic_shape::FillRule;
#[derive(Clone, PartialEq, Debug)] #[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum BasicShape { pub enum BasicShape {
@ -24,9 +27,9 @@ pub enum BasicShape {
impl ToCss for BasicShape { impl ToCss for BasicShape {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self { match *self {
BasicShape::Inset(rect) => rect.to_css(dest), BasicShape::Inset(ref rect) => rect.to_css(dest),
BasicShape::Circle(circle) => circle.to_css(dest), BasicShape::Circle(ref circle) => circle.to_css(dest),
BasicShape::Ellipse(e) => e.to_css(dest), BasicShape::Ellipse(ref e) => e.to_css(dest),
BasicShape::Polygon(ref poly) => poly.to_css(dest), BasicShape::Polygon(ref poly) => poly.to_css(dest),
} }
} }
@ -79,22 +82,23 @@ impl ToCss for Circle {
#[derive(Clone, PartialEq, Copy, Debug)] #[derive(Clone, PartialEq, Copy, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct Ellipse { pub struct Ellipse {
pub semiaxis_a: ShapeRadius, pub semiaxis_x: ShapeRadius,
pub semiaxis_b: ShapeRadius, pub semiaxis_y: ShapeRadius,
pub position: Position, pub position: Position,
} }
impl ToCss for Ellipse { impl ToCss for Ellipse {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
if ShapeRadius::ClosestSide != self.semiaxis_a && try!(dest.write_str("ellipse("));
ShapeRadius::ClosestSide != self.semiaxis_b { if (self.semiaxis_x, self.semiaxis_y) != Default::default() {
try!(self.semiaxis_a.to_css(dest)); try!(self.semiaxis_x.to_css(dest));
try!(dest.write_str(" ")); try!(dest.write_str(" "));
try!(self.semiaxis_b.to_css(dest)); try!(self.semiaxis_y.to_css(dest));
try!(dest.write_str(" ")); try!(dest.write_str(" "));
} }
try!(dest.write_str("at ")); try!(dest.write_str("at "));
self.position.to_css(dest) try!(self.position.to_css(dest));
dest.write_str(")")
} }
} }
@ -108,6 +112,7 @@ pub struct Polygon {
impl ToCss for Polygon { impl ToCss for Polygon {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(dest.write_str("polygon("));
let mut need_space = false; let mut need_space = false;
if self.fill != Default::default() { if self.fill != Default::default() {
try!(self.fill.to_css(dest)); try!(self.fill.to_css(dest));
@ -115,14 +120,14 @@ impl ToCss for Polygon {
} }
for coord in &self.coordinates { for coord in &self.coordinates {
if need_space { if need_space {
try!(dest.write_str(" ")); try!(dest.write_str(", "));
} }
try!(coord.0.to_css(dest)); try!(coord.0.to_css(dest));
try!(dest.write_str(" ")); try!(dest.write_str(" "));
try!(coord.1.to_css(dest)); try!(coord.1.to_css(dest));
need_space = true; need_space = true;
} }
Ok(()) dest.write_str(")")
} }
} }
@ -157,36 +162,33 @@ impl ToCss for ShapeRadius {
pub struct BorderRadius { pub struct BorderRadius {
pub top_left: BorderRadiusSize, pub top_left: BorderRadiusSize,
pub top_right: BorderRadiusSize, pub top_right: BorderRadiusSize,
pub bottom_left: BorderRadiusSize,
pub bottom_right: BorderRadiusSize, pub bottom_right: BorderRadiusSize,
pub bottom_left: BorderRadiusSize,
} }
impl ToCss for BorderRadius { impl ToCss for BorderRadius {
// XXXManishearth: We should be producing minimal output:
// if height=width for all, we should not be printing the part after
// the slash. For any set of four values,
// we should try to reduce them to one or two. This probably should be
// a helper function somewhere, for all the parse_four_sides-like
// values
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(self.top_left.0.width.to_css(dest)); if self.top_left.0.width == self.top_left.0.height &&
try!(dest.write_str(" ")); self.top_right.0.width == self.top_right.0.height &&
try!(self.top_right.0.width.to_css(dest)); self.bottom_right.0.width == self.bottom_right.0.height &&
try!(dest.write_str(" ")); self.bottom_left.0.width == self.bottom_left.0.height {
try!(self.bottom_left.0.width.to_css(dest)); serialize_four_sides((&self.top_left.0.width,
try!(dest.write_str(" ")); &self.top_right.0.width,
try!(self.bottom_right.0.width.to_css(dest)); &self.bottom_right.0.width,
try!(dest.write_str(" / ")); &self.bottom_left.0.width),
try!(self.top_left.0.height.to_css(dest)); dest)
try!(dest.write_str(" ")); } else {
try!(self.top_right.0.height.to_css(dest)); try!(serialize_four_sides((&self.top_left.0.width,
try!(dest.write_str(" ")); &self.top_right.0.width,
try!(self.bottom_left.0.height.to_css(dest)); &self.bottom_right.0.width,
try!(dest.write_str(" ")); &self.bottom_left.0.width),
try!(self.bottom_right.0.height.to_css(dest)); dest));
dest.write_str(" ") try!(dest.write_str(" / "));
serialize_four_sides((&self.top_left.0.height,
&self.top_right.0.height,
&self.bottom_right.0.height,
&self.bottom_left.0.height),
dest)
}
} }
} }
pub use values::specified::basic_shape::FillRule;

View file

@ -14,7 +14,7 @@ use std::fmt;
use values::computed::basic_shape as computed_basic_shape; use values::computed::basic_shape as computed_basic_shape;
use values::computed::{Context, ToComputedValue, ComputedValueAsSpecified}; use values::computed::{Context, ToComputedValue, ComputedValueAsSpecified};
use values::specified::position::{Position, PositionComponent}; use values::specified::position::{Position, PositionComponent};
use values::specified::{BorderRadiusSize, Length, LengthOrPercentage}; use values::specified::{BorderRadiusSize, Length, LengthOrPercentage, Percentage};
#[derive(Clone, PartialEq, Debug)] #[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
@ -27,16 +27,24 @@ pub enum BasicShape {
impl BasicShape { impl BasicShape {
pub fn parse(input: &mut Parser) -> Result<BasicShape, ()> { pub fn parse(input: &mut Parser) -> Result<BasicShape, ()> {
if let Ok(result) = input.try(InsetRect::parse) { match_ignore_ascii_case! { try!(input.expect_function()),
Ok(BasicShape::Inset(result)) "inset" => {
} else if let Ok(result) = input.try(Circle::parse) { Ok(BasicShape::Inset(
Ok(BasicShape::Circle(result)) try!(input.parse_nested_block(InsetRect::parse_function_arguments))))
} else if let Ok(result) = input.try(Ellipse::parse) { },
Ok(BasicShape::Ellipse(result)) "circle" => {
} else if let Ok(result) = input.try(Polygon::parse) { Ok(BasicShape::Circle(
Ok(BasicShape::Polygon(result)) try!(input.parse_nested_block(Circle::parse_function_arguments))))
} else { },
Err(()) "ellipse" => {
Ok(BasicShape::Ellipse(
try!(input.parse_nested_block(Ellipse::parse_function_arguments))))
},
"polygon" => {
Ok(BasicShape::Polygon(
try!(input.parse_nested_block(Polygon::parse_function_arguments))))
},
_ => Err(())
} }
} }
} }
@ -81,12 +89,12 @@ impl InsetRect {
pub fn parse(input: &mut Parser) -> Result<InsetRect, ()> { pub fn parse(input: &mut Parser) -> Result<InsetRect, ()> {
match_ignore_ascii_case! { try!(input.expect_function()), match_ignore_ascii_case! { try!(input.expect_function()),
"inset" => { "inset" => {
Ok(try!(input.parse_nested_block(InsetRect::parse_function))) Ok(try!(input.parse_nested_block(InsetRect::parse_function_arguments)))
}, },
_ => Err(()) _ => Err(())
} }
} }
pub fn parse_function(input: &mut Parser) -> Result<InsetRect, ()> { pub fn parse_function_arguments(input: &mut Parser) -> Result<InsetRect, ()> {
let (t, r, b, l) = try!(parse_four_sides(input, LengthOrPercentage::parse)); let (t, r, b, l) = try!(parse_four_sides(input, LengthOrPercentage::parse));
let mut rect = InsetRect { let mut rect = InsetRect {
top: t, top: t,
@ -148,18 +156,21 @@ impl Circle {
pub fn parse(input: &mut Parser) -> Result<Circle, ()> { pub fn parse(input: &mut Parser) -> Result<Circle, ()> {
match_ignore_ascii_case! { try!(input.expect_function()), match_ignore_ascii_case! { try!(input.expect_function()),
"circle" => { "circle" => {
Ok(try!(input.parse_nested_block(Circle::parse_function))) Ok(try!(input.parse_nested_block(Circle::parse_function_arguments)))
}, },
_ => Err(()) _ => Err(())
} }
} }
pub fn parse_function(input: &mut Parser) -> Result<Circle, ()> { pub fn parse_function_arguments(input: &mut Parser) -> Result<Circle, ()> {
let radius = input.try(ShapeRadius::parse).ok().unwrap_or_else(Default::default); let radius = input.try(ShapeRadius::parse).ok().unwrap_or_else(Default::default);
let position = if let Ok(_) = input.try(|input| input.expect_ident_matching("at")) { let position = if let Ok(_) = input.try(|input| input.expect_ident_matching("at")) {
try!(Position::parse(input)) try!(Position::parse(input))
} else { } else {
// Defaults to origin // Defaults to origin
try!(Position::new(PositionComponent::Center, PositionComponent::Center)) Position {
horizontal: LengthOrPercentage::Percentage(Percentage(0.5)),
vertical: LengthOrPercentage::Percentage(Percentage(0.5)),
}
}; };
Ok(Circle { Ok(Circle {
radius: radius, radius: radius,
@ -197,8 +208,8 @@ impl ToComputedValue for Circle {
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
/// https://drafts.csswg.org/css-shapes/#funcdef-ellipse /// https://drafts.csswg.org/css-shapes/#funcdef-ellipse
pub struct Ellipse { pub struct Ellipse {
pub semiaxis_a: ShapeRadius, pub semiaxis_x: ShapeRadius,
pub semiaxis_b: ShapeRadius, pub semiaxis_y: ShapeRadius,
pub position: Position, pub position: Position,
} }
@ -207,24 +218,27 @@ impl Ellipse {
pub fn parse(input: &mut Parser) -> Result<Ellipse, ()> { pub fn parse(input: &mut Parser) -> Result<Ellipse, ()> {
match_ignore_ascii_case! { try!(input.expect_function()), match_ignore_ascii_case! { try!(input.expect_function()),
"ellipse" => { "ellipse" => {
Ok(try!(input.parse_nested_block(Ellipse::parse_function))) Ok(try!(input.parse_nested_block(Ellipse::parse_function_arguments)))
}, },
_ => Err(()) _ => Err(())
} }
} }
pub fn parse_function(input: &mut Parser) -> Result<Ellipse, ()> { pub fn parse_function_arguments(input: &mut Parser) -> Result<Ellipse, ()> {
let (a, b) = input.try(|input| -> Result<_, ()> { let (a, b) = input.try(|input| -> Result<_, ()> {
Ok((try!(ShapeRadius::parse(input)), try!(ShapeRadius::parse(input)))) Ok((try!(ShapeRadius::parse(input)), try!(ShapeRadius::parse(input))))
}).unwrap_or((Default::default(), Default::default())); }).ok().unwrap_or_default();
let position = if let Ok(_) = input.try(|input| input.expect_ident_matching("at")) { let position = if let Ok(_) = input.try(|input| input.expect_ident_matching("at")) {
try!(Position::parse(input)) try!(Position::parse(input))
} else { } else {
// Defaults to origin // Defaults to origin
try!(Position::new(PositionComponent::Center, PositionComponent::Center)) Position {
horizontal: LengthOrPercentage::Percentage(Percentage(0.5)),
vertical: LengthOrPercentage::Percentage(Percentage(0.5)),
}
}; };
Ok(Ellipse { Ok(Ellipse {
semiaxis_a: a, semiaxis_x: a,
semiaxis_b: b, semiaxis_y: b,
position: position, position: position,
}) })
} }
@ -233,11 +247,10 @@ impl Ellipse {
impl ToCss for Ellipse { impl ToCss for Ellipse {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(dest.write_str("ellipse(")); try!(dest.write_str("ellipse("));
if ShapeRadius::ClosestSide != self.semiaxis_a || if (self.semiaxis_x, self.semiaxis_y) != Default::default() {
ShapeRadius::ClosestSide != self.semiaxis_b { try!(self.semiaxis_x.to_css(dest));
try!(self.semiaxis_a.to_css(dest));
try!(dest.write_str(" ")); try!(dest.write_str(" "));
try!(self.semiaxis_b.to_css(dest)); try!(self.semiaxis_y.to_css(dest));
try!(dest.write_str(" ")); try!(dest.write_str(" "));
} }
try!(dest.write_str("at ")); try!(dest.write_str("at "));
@ -252,8 +265,8 @@ impl ToComputedValue for Ellipse {
#[inline] #[inline]
fn to_computed_value(&self, cx: &Context) -> Self::ComputedValue { fn to_computed_value(&self, cx: &Context) -> Self::ComputedValue {
computed_basic_shape::Ellipse { computed_basic_shape::Ellipse {
semiaxis_a: self.semiaxis_a.to_computed_value(cx), semiaxis_x: self.semiaxis_x.to_computed_value(cx),
semiaxis_b: self.semiaxis_b.to_computed_value(cx), semiaxis_y: self.semiaxis_y.to_computed_value(cx),
position: self.position.to_computed_value(cx), position: self.position.to_computed_value(cx),
} }
} }
@ -272,25 +285,22 @@ impl Polygon {
pub fn parse(input: &mut Parser) -> Result<Polygon, ()> { pub fn parse(input: &mut Parser) -> Result<Polygon, ()> {
match_ignore_ascii_case! { try!(input.expect_function()), match_ignore_ascii_case! { try!(input.expect_function()),
"polygon" => { "polygon" => {
Ok(try!(input.parse_nested_block(Polygon::parse_function))) Ok(try!(input.parse_nested_block(Polygon::parse_function_arguments)))
}, },
_ => Err(()) _ => Err(())
} }
} }
pub fn parse_function(input: &mut Parser) -> Result<Polygon, ()> { pub fn parse_function_arguments(input: &mut Parser) -> Result<Polygon, ()> {
let fill = input.try(|input| { let fill = input.try(|input| {
let fill = FillRule::parse(input); let fill = FillRule::parse(input);
// only eat the comma if there is something before it // only eat the comma if there is something before it
try!(input.expect_comma()); try!(input.expect_comma());
fill fill
}).ok().unwrap_or_else(Default::default); }).ok().unwrap_or_else(Default::default);
let first = (try!(LengthOrPercentage::parse(input)), let buf = try!(input.parse_comma_separated(|input| {
try!(LengthOrPercentage::parse(input))); Ok((try!(LengthOrPercentage::parse(input)),
let mut buf = vec![first]; try!(LengthOrPercentage::parse(input))))
while !input.is_exhausted() { }));
buf.push((try!(LengthOrPercentage::parse(input)),
try!(LengthOrPercentage::parse(input))));
}
Ok(Polygon { Ok(Polygon {
fill: fill, fill: fill,
coordinates: buf, coordinates: buf,
@ -308,7 +318,7 @@ impl ToCss for Polygon {
} }
for coord in &self.coordinates { for coord in &self.coordinates {
if need_space { if need_space {
try!(dest.write_str(" ")); try!(dest.write_str(", "));
} }
try!(coord.0.to_css(dest)); try!(coord.0.to_css(dest));
try!(dest.write_str(" ")); try!(dest.write_str(" "));
@ -396,38 +406,32 @@ impl ToComputedValue for ShapeRadius {
pub struct BorderRadius { pub struct BorderRadius {
pub top_left: BorderRadiusSize, pub top_left: BorderRadiusSize,
pub top_right: BorderRadiusSize, pub top_right: BorderRadiusSize,
pub bottom_left: BorderRadiusSize,
pub bottom_right: BorderRadiusSize, pub bottom_right: BorderRadiusSize,
pub bottom_left: BorderRadiusSize,
} }
impl ToCss for BorderRadius { impl ToCss for BorderRadius {
// XXXManishearth: We should be producing minimal output:
// if height=width for all, we should not be printing the part after
// the slash. For any set of four values,
// we should try to reduce them to one or two. This probably should be
// a helper function somewhere, for all the parse_four_sides-like
// values
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
if self.top_left.0.width == self.top_left.0.height && if self.top_left.0.width == self.top_left.0.height &&
self.top_right.0.width == self.top_right.0.height && self.top_right.0.width == self.top_right.0.height &&
self.bottom_left.0.width == self.bottom_left.0.height && self.bottom_right.0.width == self.bottom_right.0.height &&
self.bottom_right.0.width == self.bottom_right.0.height { self.bottom_left.0.width == self.bottom_left.0.height {
serialize_four_sides((&self.top_left.0.width, serialize_four_sides((&self.top_left.0.width,
&self.top_right.0.width, &self.top_right.0.width,
&self.bottom_left.0.width, &self.bottom_right.0.width,
&self.bottom_right.0.width), &self.bottom_left.0.width),
dest) dest)
} else { } else {
try!(serialize_four_sides((&self.top_left.0.width, try!(serialize_four_sides((&self.top_left.0.width,
&self.top_right.0.width, &self.top_right.0.width,
&self.bottom_left.0.width, &self.bottom_right.0.width,
&self.bottom_right.0.width), &self.bottom_left.0.width),
dest)); dest));
try!(dest.write_str(" / ")); try!(dest.write_str(" / "));
serialize_four_sides((&self.top_left.0.height, serialize_four_sides((&self.top_left.0.height,
&self.top_right.0.height, &self.top_right.0.height,
&self.bottom_left.0.height, &self.bottom_right.0.height,
&self.bottom_right.0.height), &self.bottom_left.0.height),
dest) dest)
} }
} }
@ -436,38 +440,40 @@ impl ToCss for BorderRadius {
impl BorderRadius { impl BorderRadius {
pub fn parse(input: &mut Parser) -> Result<BorderRadius, ()> { pub fn parse(input: &mut Parser) -> Result<BorderRadius, ()> {
let widths = try!(parse_one_set_of_border_values(input)); let widths = try!(parse_one_set_of_border_values(input));
let mut heights = widths.clone(); let heights = if input.try(|input| input.expect_delim('/')).is_ok() {
if input.try(|input| input.expect_delim('/')).is_ok() { try!(parse_one_set_of_border_values(input))
heights = try!(parse_one_set_of_border_values(input)); } else {
} widths.clone()
};
Ok(BorderRadius { Ok(BorderRadius {
top_left: BorderRadiusSize::new(widths[0], heights[0]), top_left: BorderRadiusSize::new(widths[0], heights[0]),
top_right: BorderRadiusSize::new(widths[1], heights[1]), top_right: BorderRadiusSize::new(widths[1], heights[1]),
bottom_left: BorderRadiusSize::new(widths[2], heights[2]), bottom_right: BorderRadiusSize::new(widths[2], heights[2]),
bottom_right: BorderRadiusSize::new(widths[3], heights[3]), bottom_left: BorderRadiusSize::new(widths[3], heights[3]),
}) })
} }
} }
fn parse_one_set_of_border_values(mut input: &mut Parser) fn parse_one_set_of_border_values(mut input: &mut Parser)
-> Result<[LengthOrPercentage; 4], ()> { -> Result<[LengthOrPercentage; 4], ()> {
let mut count = 0; let a = try!(LengthOrPercentage::parse(input));
let mut values = [LengthOrPercentage::Length(Length::Absolute(Au(0))); 4];
while count < 4 {
if let Ok(value) = input.try(LengthOrPercentage::parse) {
values[count] = value;
count += 1;
} else {
break
}
}
match count { let b = if let Ok(b) = input.try(LengthOrPercentage::parse) {
1 => Ok([values[0], values[0], values[0], values[0]]), b
2 => Ok([values[0], values[1], values[0], values[1]]), } else {
3 => Ok([values[0], values[1], values[2], values[1]]), return Ok([a, a, a, a])
4 => Ok([values[0], values[1], values[2], values[3]]), };
_ => Err(()),
let c = if let Ok(c) = input.try(LengthOrPercentage::parse) {
c
} else {
return Ok([a, b, a, b])
};
if let Ok(d) = input.try(LengthOrPercentage::parse) {
Ok([a, b, c, d])
} else {
Ok([a, b, c, b])
} }
} }
@ -480,8 +486,8 @@ impl ToComputedValue for BorderRadius {
computed_basic_shape::BorderRadius { computed_basic_shape::BorderRadius {
top_left: self.top_left.to_computed_value(cx), top_left: self.top_left.to_computed_value(cx),
top_right: self.top_right.to_computed_value(cx), top_right: self.top_right.to_computed_value(cx),
bottom_left: self.bottom_left.to_computed_value(cx),
bottom_right: self.bottom_right.to_computed_value(cx), bottom_right: self.bottom_right.to_computed_value(cx),
bottom_left: self.bottom_left.to_computed_value(cx),
} }
} }
} }

View file

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use parsing::{parse, to_string}; use parsing::parse;
use style::values::specified::basic_shape::*; use style::values::specified::basic_shape::*;
// Ensure that basic-shape sub-functions parse as both basic shapes // Ensure that basic-shape sub-functions parse as both basic shapes
@ -15,19 +15,19 @@ macro_rules! assert_roundtrip_basicshape {
} }
macro_rules! assert_border_radius_values { macro_rules! assert_border_radius_values {
($input:expr; $tlw:expr, $trw:expr, $blw:expr, $brw:expr ; ($input:expr; $tlw:expr, $trw:expr, $brw:expr, $blw:expr ;
$tlh:expr, $trh:expr, $blh:expr, $brh:expr) => { $tlh:expr, $trh:expr, $brh:expr, $blh:expr) => {
let input = parse(BorderRadius::parse, $input) let input = parse(BorderRadius::parse, $input)
.expect(&format!("Failed parsing {} as border radius", .expect(&format!("Failed parsing {} as border radius",
$input)); $input));
assert_eq!(to_string(input.top_left.0.width), $tlw); assert_eq!(::cssparser::ToCss::to_css_string(&input.top_left.0.width), $tlw);
assert_eq!(to_string(input.top_right.0.width), $trw); assert_eq!(::cssparser::ToCss::to_css_string(&input.top_right.0.width), $trw);
assert_eq!(to_string(input.bottom_left.0.width), $blw); assert_eq!(::cssparser::ToCss::to_css_string(&input.bottom_right.0.width), $brw);
assert_eq!(to_string(input.bottom_right.0.width), $brw); assert_eq!(::cssparser::ToCss::to_css_string(&input.bottom_left.0.width), $blw);
assert_eq!(to_string(input.top_left.0.height), $tlh); assert_eq!(::cssparser::ToCss::to_css_string(&input.top_left.0.height), $tlh);
assert_eq!(to_string(input.top_right.0.height), $trh); assert_eq!(::cssparser::ToCss::to_css_string(&input.top_right.0.height), $trh);
assert_eq!(to_string(input.bottom_left.0.height), $blh); assert_eq!(::cssparser::ToCss::to_css_string(&input.bottom_right.0.height), $brh);
assert_eq!(to_string(input.bottom_right.0.height), $brh); assert_eq!(::cssparser::ToCss::to_css_string(&input.bottom_left.0.height), $blh);
} }
} }
@ -121,19 +121,19 @@ fn test_polygon() {
// surprisingly, polygons are only required to have at least one vertex, // surprisingly, polygons are only required to have at least one vertex,
// not at least 3 // not at least 3
assert_roundtrip_basicshape!(Polygon::parse, "polygon(10px 10px)", "polygon(10px 10px)"); assert_roundtrip_basicshape!(Polygon::parse, "polygon(10px 10px)", "polygon(10px 10px)");
assert_roundtrip_basicshape!(Polygon::parse, "polygon(10px 10px 10px 10px)", "polygon(10px 10px 10px 10px)"); assert_roundtrip_basicshape!(Polygon::parse, "polygon(10px 10px, 10px 10px)", "polygon(10px 10px, 10px 10px)");
assert_roundtrip_basicshape!(Polygon::parse, "polygon(nonzero, 10px 10px 10px 10px)", assert_roundtrip_basicshape!(Polygon::parse, "polygon(nonzero, 10px 10px, 10px 10px)",
"polygon(10px 10px 10px 10px)"); "polygon(10px 10px, 10px 10px)");
assert_roundtrip_basicshape!(Polygon::parse, "polygon(evenodd, 10px 10px 10px 10px)", assert_roundtrip_basicshape!(Polygon::parse, "polygon(evenodd, 10px 10px, 10px 10px)",
"polygon(evenodd, 10px 10px 10px 10px)"); "polygon(evenodd, 10px 10px, 10px 10px)");
assert_roundtrip_basicshape!(Polygon::parse, "polygon(evenodd, 10px 10px 10px calc(10px + 50%))", assert_roundtrip_basicshape!(Polygon::parse, "polygon(evenodd, 10px 10px, 10px calc(10px + 50%))",
"polygon(evenodd, 10px 10px 10px calc(10px + 50%))"); "polygon(evenodd, 10px 10px, 10px calc(10px + 50%))");
assert_roundtrip_basicshape!(Polygon::parse, "polygon(evenodd, 10px 10px 10px 10px 10px 10px 10px 10px 10px \ assert_roundtrip_basicshape!(Polygon::parse, "polygon(evenodd, 10px 10px, 10px 10px, 10px 10px, 10px 10px, 10px \
10px 10px 10px 10px 10px 10px 10px 10px 10px 10px 10px 10px 10px \ 10px, 10px 10px, 10px 10px, 10px 10px, 10px 10px, 10px 10px, \
10px 10px 10px 10px 10px 10px)", 10px 10px, 10px 10px, 10px 10px)",
"polygon(evenodd, 10px 10px 10px 10px 10px 10px 10px 10px 10px \ "polygon(evenodd, 10px 10px, 10px 10px, 10px 10px, 10px 10px, 10px \
10px 10px 10px 10px 10px 10px 10px 10px 10px 10px 10px 10px 10px \ 10px, 10px 10px, 10px 10px, 10px 10px, 10px 10px, 10px 10px, \
10px 10px 10px 10px 10px 10px)"); 10px 10px, 10px 10px, 10px 10px)");
assert!(parse(Polygon::parse, "polygon()").is_err()); assert!(parse(Polygon::parse, "polygon()").is_err());
} }

View file

@ -4,18 +4,13 @@
//! Tests for parsing and serialization of values/properties //! Tests for parsing and serialization of values/properties
use cssparser::{Parser, ToCss}; use cssparser::Parser;
fn parse<T, F: Fn(&mut Parser) -> Result<T, ()>>(f: F, s: &str) -> Result<T, ()> { fn parse<T, F: Fn(&mut Parser) -> Result<T, ()>>(f: F, s: &str) -> Result<T, ()> {
let mut parser = Parser::new(s); let mut parser = Parser::new(s);
f(&mut parser) f(&mut parser)
} }
fn to_string<T: ToCss>(x: T) -> String {
let mut serialized = String::new();
x.to_css(&mut serialized).expect("Failed to serialize");
serialized
}
// This is a macro so that the file/line information // This is a macro so that the file/line information
// is preserved in the panic // is preserved in the panic
@ -23,8 +18,13 @@ macro_rules! assert_roundtrip {
($fun:expr, $input:expr, $output:expr) => { ($fun:expr, $input:expr, $output:expr) => {
let parsed = $crate::parsing::parse($fun, $input) let parsed = $crate::parsing::parse($fun, $input)
.expect(&format!("Failed to parse {}", $input)); .expect(&format!("Failed to parse {}", $input));
let serialized = $crate::parsing::to_string(parsed); let serialized = ::cssparser::ToCss::to_css_string(&parsed);
assert_eq!(serialized, $output); assert_eq!(serialized, $output);
let re_parsed = $crate::parsing::parse($fun, &serialized)
.expect(&format!("Failed to parse serialization {}", $input));
let re_serialized = ::cssparser::ToCss::to_css_string(&re_parsed);
assert_eq!(serialized, re_serialized);
} }
} }