From 6f3c46ca61b0af8b3cccb4770e1ee1827e4b07b6 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Thu, 25 May 2017 12:22:49 +0200 Subject: [PATCH] Remove uses of parse_four_sides and serialize_four_sides --- components/style/properties/helpers.mako.rs | 27 +++++----- .../style/properties/properties.mako.rs | 49 ------------------ .../style/properties/shorthand/border.mako.rs | 19 +++---- .../properties/shorthand/serialize.mako.rs | 51 ------------------- .../style/values/generics/basic_shape.rs | 20 ++++---- .../style/values/specified/basic_shape.rs | 16 +++--- 6 files changed, 41 insertions(+), 141 deletions(-) diff --git a/components/style/properties/helpers.mako.rs b/components/style/properties/helpers.mako.rs index b93f7b89413..0c629df19f5 100644 --- a/components/style/properties/helpers.mako.rs +++ b/components/style/properties/helpers.mako.rs @@ -887,35 +887,34 @@ <%call expr="self.shorthand(name, sub_properties=sub_properties, **kwargs)"> #[allow(unused_imports)] use parser::Parse; - use super::parse_four_sides; + use values::generics::rect::Rect; use values::specified; pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result { - let (top, right, bottom, left) = + let rect = Rect::parse_with(context, input, |_c, i| { % if allow_quirks: - try!(parse_four_sides(input, |i| ${parser_function}_quirky(context, i, specified::AllowQuirks::Yes))); + ${parser_function}_quirky(_c, i, specified::AllowQuirks::Yes) % elif needs_context: - try!(parse_four_sides(input, |i| ${parser_function}(context, i))); + ${parser_function}(_c, i) % else: - try!(parse_four_sides(input, ${parser_function})); - let _unused = context; + ${parser_function}(i) % endif + })?; Ok(expanded! { % for side in ["top", "right", "bottom", "left"]: - ${to_rust_ident(sub_property_pattern % side)}: ${side}, + ${to_rust_ident(sub_property_pattern % side)}: rect.${side}, % endfor }) } impl<'a> ToCss for LonghandsToSerialize<'a> { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - super::serialize_four_sides( - dest, - self.${to_rust_ident(sub_property_pattern % 'top')}, - self.${to_rust_ident(sub_property_pattern % 'right')}, - self.${to_rust_ident(sub_property_pattern % 'bottom')}, - self.${to_rust_ident(sub_property_pattern % 'left')} - ) + let rect = Rect::new( + % for side in ["top", "right", "bottom", "left"]: + &self.${to_rust_ident(sub_property_pattern % side)}, + % endfor + ); + rect.to_css(dest) } } diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index 92429d554dd..118965673a3 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -134,55 +134,6 @@ pub mod shorthands { use parser::{Parse, ParserContext}; use values::specified; - /// Parses a property for four different sides per CSS syntax. - /// - /// * Zero or more than four values is invalid. - /// * One value sets them all - /// * Two values set (top, bottom) and (left, right) - /// * Three values set top, (left, right) and bottom - /// * Four values set them in order - /// - /// returns the values in (top, right, bottom, left) order. - pub fn parse_four_sides(input: &mut Parser, parse_one: F) -> Result<(T, T, T, T), ()> - where F: Fn(&mut Parser) -> Result, - T: Clone, - { - let top = try!(parse_one(input)); - let right; - let bottom; - let left; - match input.try(|i| parse_one(i)) { - Err(()) => { - right = top.clone(); - bottom = top.clone(); - left = top.clone(); - } - Ok(value) => { - right = value; - match input.try(|i| parse_one(i)) { - Err(()) => { - bottom = top.clone(); - left = right.clone(); - } - Ok(value) => { - bottom = value; - match input.try(|i| parse_one(i)) { - Err(()) => { - left = right.clone(); - } - Ok(value) => { - left = value; - } - } - - } - } - - } - } - Ok((top, right, bottom, left)) - } - <%include file="/shorthand/serialize.mako.rs" /> <%include file="/shorthand/background.mako.rs" /> <%include file="/shorthand/border.mako.rs" /> diff --git a/components/style/properties/shorthand/border.mako.rs b/components/style/properties/shorthand/border.mako.rs index 772105fae25..8e9aff17fc9 100644 --- a/components/style/properties/shorthand/border.mako.rs +++ b/components/style/properties/shorthand/border.mako.rs @@ -17,27 +17,28 @@ ${helpers.four_sides_shorthand("border-style", "border-%s-style", ' '.join('border-%s-width' % side for side in PHYSICAL_SIDES)}" spec="https://drafts.csswg.org/css-backgrounds/#border-width"> - use super::parse_four_sides; + use values::generics::rect::Rect; use values::specified::{AllowQuirks, BorderWidth}; pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result { - let (top, right, bottom, left) = try!(parse_four_sides(input, |i| { + let rect = Rect::parse_with(context, input, |_, i| { BorderWidth::parse_quirky(context, i, AllowQuirks::Yes) - })); + })?; Ok(expanded! { % for side in PHYSICAL_SIDES: - ${to_rust_ident('border-%s-width' % side)}: ${side}, + ${to_rust_ident('border-%s-width' % side)}: rect.${side}, % endfor }) } impl<'a> ToCss for LonghandsToSerialize<'a> { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - % for side in PHYSICAL_SIDES: - let ${side} = self.border_${side}_width.clone(); - % endfor - - super::serialize_four_sides(dest, &top, &right, &bottom, &left) + let rect = Rect::new( + % for side in PHYSICAL_SIDES: + &self.border_${side}_width, + % endfor + ); + rect.to_css(dest) } } diff --git a/components/style/properties/shorthand/serialize.mako.rs b/components/style/properties/shorthand/serialize.mako.rs index f990ca81df3..f8c1ef9bc3e 100644 --- a/components/style/properties/shorthand/serialize.mako.rs +++ b/components/style/properties/shorthand/serialize.mako.rs @@ -6,57 +6,6 @@ use style_traits::ToCss; use values::specified::{BorderStyle, Color, CSSColor}; use std::fmt; -#[allow(missing_docs)] -pub fn serialize_four_sides(dest: &mut W, - top: &I, - right: &I, - bottom: &I, - left: &I) - -> fmt::Result - where W: fmt::Write, - I: ToCss + PartialEq, -{ - - if left == right { - let horizontal_value = left; - - if top == bottom { - let vertical_value = top; - - if horizontal_value == vertical_value { - let single_value = horizontal_value; - try!(single_value.to_css(dest)); - } else { - try!(vertical_value.to_css(dest)); - try!(write!(dest, " ")); - - try!(horizontal_value.to_css(dest)); - } - } else { - try!(top.to_css(dest)); - try!(write!(dest, " ")); - - try!(horizontal_value.to_css(dest)); - try!(write!(dest, " ")); - - try!(bottom.to_css(dest)); - } - } else { - try!(top.to_css(dest)); - try!(write!(dest, " ")); - - try!(right.to_css(dest)); - try!(write!(dest, " ")); - - try!(bottom.to_css(dest)); - try!(write!(dest, " ")); - - try!(left.to_css(dest)); - } - - Ok(()) -} - fn serialize_directional_border(dest: &mut W, width: &I, style: &BorderStyle, diff --git a/components/style/values/generics/basic_shape.rs b/components/style/values/generics/basic_shape.rs index f5b87ec1dd1..40cebf945ff 100644 --- a/components/style/values/generics/basic_shape.rs +++ b/components/style/values/generics/basic_shape.rs @@ -6,12 +6,12 @@ //! types that are generic over their `ToCss` implementations. use euclid::size::Size2D; -use properties::shorthands::serialize_four_sides; use std::fmt; use style_traits::{HasViewportPercentage, ToCss}; use values::computed::ComputedValueAsSpecified; use values::generics::BorderRadiusSize; use values::generics::position::Position; +use values::generics::rect::Rect; use values::specified::url::SpecifiedUrl; /// A clipping shape, for `clip-path`. @@ -224,17 +224,17 @@ pub fn serialize_radius_values(dest: &mut W, top_left: &Size2D, bottom_left: &Size2D) -> fmt::Result where L: ToCss + PartialEq, W: fmt::Write { - if top_left.width == top_left.height && top_right.width == top_right.height && - bottom_right.width == bottom_right.height && bottom_left.width == bottom_left.height { - serialize_four_sides(dest, &top_left.width, &top_right.width, - &bottom_right.width, &bottom_left.width) - } else { - serialize_four_sides(dest, &top_left.width, &top_right.width, - &bottom_right.width, &bottom_left.width)?; + Rect::new(&top_left.width, &top_right.width, &bottom_right.width, &bottom_left.width).to_css(dest)?; + if + top_left.width != top_left.height || + top_right.width != top_right.height || + bottom_right.width != bottom_right.height || + bottom_left.width != bottom_left.height + { dest.write_str(" / ")?; - serialize_four_sides(dest, &top_left.height, &top_right.height, - &bottom_right.height, &bottom_left.height) + Rect::new(&top_left.height, &top_right.height, &bottom_right.height, &bottom_left.height).to_css(dest)?; } + Ok(()) } impl Default for ShapeRadius { diff --git a/components/style/values/specified/basic_shape.rs b/components/style/values/specified/basic_shape.rs index 02729e0e635..a33d9eac57e 100644 --- a/components/style/values/specified/basic_shape.rs +++ b/components/style/values/specified/basic_shape.rs @@ -9,7 +9,6 @@ use cssparser::Parser; use parser::{Parse, ParserContext}; -use properties::shorthands::parse_four_sides; use std::borrow::Cow; use std::fmt; use style_traits::ToCss; @@ -20,6 +19,7 @@ use values::generics::basic_shape::{FillRule, BasicShape as GenericBasicShape}; use values::generics::basic_shape::{FloatAreaShape as GenericFloatAreaShape, InsetRect as GenericInsetRect}; use values::generics::basic_shape::{GeometryBox, ShapeBox, ShapeSource}; use values::generics::basic_shape::{Polygon as GenericPolygon, ShapeRadius as GenericShapeRadius}; +use values::generics::rect::Rect; use values::specified::{LengthOrPercentage, Percentage}; use values::specified::position::{HorizontalPosition, Position, PositionComponent, Side, VerticalPosition}; use values::specified::url::SpecifiedUrl; @@ -127,18 +127,18 @@ impl Parse for InsetRect { impl InsetRect { /// Parse the inner function arguments of `inset()` pub fn parse_function_arguments(context: &ParserContext, input: &mut Parser) -> Result { - let (t, r, b, l) = parse_four_sides(input, |i| LengthOrPercentage::parse(context, i))?; - let rect = if input.try(|i| i.expect_ident_matching("round")).is_ok() { + let rect = Rect::parse_with(context, input, LengthOrPercentage::parse)?; + let round_rect = if input.try(|i| i.expect_ident_matching("round")).is_ok() { Some(BorderRadius::parse(context, input)?) } else { None }; Ok(GenericInsetRect { - top: t, - right: r, - bottom: b, - left: l, - round: rect, + top: rect.top, + right: rect.right, + bottom: rect.bottom, + left: rect.left, + round: round_rect, }) } }