From f9c06d79329cb9e036889eab834899633cf68bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 15 Sep 2017 18:11:27 +0200 Subject: [PATCH] style: Generalise BorderCornerRadius as `Size`. --- components/style/values/computed/border.rs | 4 +- components/style/values/generics/border.rs | 54 ++-------- components/style/values/generics/mod.rs | 1 + components/style/values/generics/size.rs | 103 ++++++++++++++++++++ components/style/values/specified/border.rs | 4 +- tests/unit/style/parsing/border.rs | 16 +-- 6 files changed, 122 insertions(+), 60 deletions(-) create mode 100644 components/style/values/generics/size.rs diff --git a/components/style/values/computed/border.rs b/components/style/values/computed/border.rs index 9ca98787730..630f015d281 100644 --- a/components/style/values/computed/border.rs +++ b/components/style/values/computed/border.rs @@ -7,11 +7,11 @@ use values::animated::ToAnimatedZero; use values::computed::{Number, NumberOrPercentage}; use values::computed::length::LengthOrPercentage; -use values::generics::border::BorderCornerRadius as GenericBorderCornerRadius; use values::generics::border::BorderImageSideWidth as GenericBorderImageSideWidth; use values::generics::border::BorderImageSlice as GenericBorderImageSlice; use values::generics::border::BorderRadius as GenericBorderRadius; use values::generics::rect::Rect; +use values::generics::size::Size; /// A computed value for the `border-image-width` property. pub type BorderImageWidth = Rect; @@ -26,7 +26,7 @@ pub type BorderImageSlice = GenericBorderImageSlice; pub type BorderRadius = GenericBorderRadius; /// A computed value for the `border-*-radius` longhand properties. -pub type BorderCornerRadius = GenericBorderCornerRadius; +pub type BorderCornerRadius = Size; impl BorderImageSideWidth { /// Returns `1`. diff --git a/components/style/values/generics/border.rs b/components/style/values/generics/border.rs index 215c2fd23e5..f50cd3ded96 100644 --- a/components/style/values/generics/border.rs +++ b/components/style/values/generics/border.rs @@ -4,10 +4,10 @@ //! Generic types for CSS values related to borders. -use euclid::Size2D; use std::fmt; use style_traits::ToCss; use values::generics::rect::Rect; +use values::generics::size::Size; /// A generic value for a single side of a `border-image-width` property. #[cfg_attr(feature = "gecko", derive(MallocSizeOf))] @@ -42,22 +42,15 @@ pub struct BorderImageSlice { #[derive(PartialEq, ToComputedValue)] pub struct BorderRadius { /// The top left radius. - pub top_left: BorderCornerRadius, + pub top_left: Size, /// The top right radius. - pub top_right: BorderCornerRadius, + pub top_right: Size, /// The bottom right radius. - pub bottom_right: BorderCornerRadius, + pub bottom_right: Size, /// The bottom left radius. - pub bottom_left: BorderCornerRadius, + pub bottom_left: Size, } -/// A generic value for `border-*-radius` longhand properties. -#[cfg_attr(feature = "gecko", derive(MallocSizeOf))] -#[cfg_attr(feature = "servo", derive(HeapSizeOf))] -#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug)] -#[derive(PartialEq, ToComputedValue)] -pub struct BorderCornerRadius(pub Size2D); - impl From for BorderImageSlice where N: Clone, { @@ -87,11 +80,7 @@ impl ToCss for BorderImageSlice impl BorderRadius { /// Returns a new `BorderRadius`. #[inline] - pub fn new(tl: BorderCornerRadius, - tr: BorderCornerRadius, - br: BorderCornerRadius, - bl: BorderCornerRadius) - -> Self { + pub fn new(tl: Size, tr: Size, br: Size, bl: Size) -> Self { BorderRadius { top_left: tl, top_right: tr, @@ -135,34 +124,3 @@ impl ToCss for BorderRadius Self::serialize_rects(widths, heights, dest) } } - -impl BorderCornerRadius { - #[inline] - /// Create a new `BorderCornerRadius` for an area of given width and height. - pub fn new(width: L, height: L) -> BorderCornerRadius { - BorderCornerRadius(Size2D::new(width, height)) - } -} - -impl From for BorderCornerRadius { - fn from(radius: L) -> Self { - Self::new(radius.clone(), radius) - } -} - -impl ToCss for BorderCornerRadius - where L: ToCss + PartialEq, -{ - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write - { - self.0.width.to_css(dest)?; - - if self.0.height != self.0.width { - dest.write_str(" ")?; - self.0.height.to_css(dest)?; - } - - Ok(()) - } -} diff --git a/components/style/values/generics/mod.rs b/components/style/values/generics/mod.rs index b28147da3d5..4603523d920 100644 --- a/components/style/values/generics/mod.rs +++ b/components/style/values/generics/mod.rs @@ -25,6 +25,7 @@ pub mod grid; pub mod image; pub mod position; pub mod rect; +pub mod size; pub mod svg; pub mod text; pub mod transform; diff --git a/components/style/values/generics/size.rs b/components/style/values/generics/size.rs new file mode 100644 index 00000000000..e28db54afab --- /dev/null +++ b/components/style/values/generics/size.rs @@ -0,0 +1,103 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +//! Generic type for CSS properties that are composed by two dimensions. + +use cssparser::Parser; +use euclid::Size2D; +use parser::ParserContext; +use std::fmt; +use style_traits::{ToCss, ParseError}; +use values::animated::ToAnimatedValue; + +/// A generic size, for `border-*-radius` longhand properties, or +/// `border-spacing`. +#[cfg_attr(feature = "gecko", derive(MallocSizeOf))] +#[cfg_attr(feature = "servo", derive(HeapSizeOf))] +#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug)] +#[derive(PartialEq, ToComputedValue)] +pub struct Size(pub Size2D); + +impl Size { + #[inline] + /// Create a new `Size` for an area of given width and height. + pub fn new(width: L, height: L) -> Size { + Size(Size2D::new(width, height)) + } + + /// Returns the width component. + pub fn width(&self) -> &L { + &self.0.width + } + + /// Returns the height component. + pub fn height(&self) -> &L { + &self.0.height + } + + /// Parse a `Size` with a given parsing function. + pub fn parse_with<'i, 't, F>( + context: &ParserContext, + input: &mut Parser<'i, 't>, + parse_one: F, + ) -> Result> + where + L: Clone, + F: Fn(&ParserContext, &mut Parser<'i, 't>) -> Result> + { + let first = parse_one(context, input)?; + let second = input + .try(|i| parse_one(context, i)) + .unwrap_or_else(|_| first.clone()); + Ok(Self::new(first, second)) + } +} + +impl From for Size { + fn from(size: L) -> Self { + Self::new(size.clone(), size) + } +} + +impl ToCss for Size +where L: + ToCss + PartialEq, +{ + fn to_css(&self, dest: &mut W) -> fmt::Result + where W: + fmt::Write + { + self.0.width.to_css(dest)?; + + if self.0.height != self.0.width { + dest.write_str(" ")?; + self.0.height.to_css(dest)?; + } + + Ok(()) + } +} + +impl ToAnimatedValue for Size +where L: + ToAnimatedValue, +{ + type AnimatedValue = Size; + + #[inline] + fn to_animated_value(self) -> Self::AnimatedValue { + Size(Size2D::new( + self.0.width.to_animated_value(), + self.0.height.to_animated_value(), + )) + } + + #[inline] + fn from_animated_value(animated: Self::AnimatedValue) -> Self { + Size(Size2D::new( + L::from_animated_value(animated.0.width), + L::from_animated_value(animated.0.height), + )) + } +} diff --git a/components/style/values/specified/border.rs b/components/style/values/specified/border.rs index 918f822dc41..41a8b5138d8 100644 --- a/components/style/values/specified/border.rs +++ b/components/style/values/specified/border.rs @@ -8,7 +8,7 @@ use cssparser::Parser; use parser::{Parse, ParserContext}; use style_traits::ParseError; use values::computed::{Context, NonNegativeLength, ToComputedValue}; -use values::generics::border::BorderCornerRadius as GenericBorderCornerRadius; +use values::generics::size::Size; use values::generics::border::BorderImageSideWidth as GenericBorderImageSideWidth; use values::generics::border::BorderImageSlice as GenericBorderImageSlice; use values::generics::border::BorderRadius as GenericBorderRadius; @@ -44,7 +44,7 @@ pub type BorderImageSlice = GenericBorderImageSlice; pub type BorderRadius = GenericBorderRadius; /// A specified value for the `border-*-radius` longhand properties. -pub type BorderCornerRadius = GenericBorderCornerRadius; +pub type BorderCornerRadius = Size; impl BorderSideWidth { /// Parses, with quirks. diff --git a/tests/unit/style/parsing/border.rs b/tests/unit/style/parsing/border.rs index 9c2790cc886..e92b59dd2fb 100644 --- a/tests/unit/style/parsing/border.rs +++ b/tests/unit/style/parsing/border.rs @@ -29,14 +29,14 @@ macro_rules! assert_border_radius_values { let input = parse(BorderRadius::parse, $input) .expect(&format!("Failed parsing {} as border radius", $input)); - assert_eq!(::style_traits::ToCss::to_css_string(&input.top_left.0.width), $tlw); - assert_eq!(::style_traits::ToCss::to_css_string(&input.top_right.0.width), $trw); - assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_right.0.width), $brw); - assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_left.0.width), $blw); - assert_eq!(::style_traits::ToCss::to_css_string(&input.top_left.0.height), $tlh); - assert_eq!(::style_traits::ToCss::to_css_string(&input.top_right.0.height), $trh); - assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_right.0.height), $brh); - assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_left.0.height), $blh); + assert_eq!(::style_traits::ToCss::to_css_string(&input.top_left.0.width()), $tlw); + assert_eq!(::style_traits::ToCss::to_css_string(&input.top_right.0.width()), $trw); + assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_right.0.width()), $brw); + assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_left.0.width()), $blw); + assert_eq!(::style_traits::ToCss::to_css_string(&input.top_left.0.height()), $tlh); + assert_eq!(::style_traits::ToCss::to_css_string(&input.top_right.0.height()), $trh); + assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_right.0.height()), $brh); + assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_left.0.height()), $blh); } }