diff --git a/components/style/gecko/values.rs b/components/style/gecko/values.rs index 64bef9d86be..cf0e55d21f1 100644 --- a/components/style/gecko/values.rs +++ b/components/style/gecko/values.rs @@ -15,7 +15,8 @@ use values::{Auto, Either, ExtremumLength, None_, Normal}; use values::computed::{Angle, LengthOrPercentage, LengthOrPercentageOrAuto}; use values::computed::{LengthOrPercentageOrNone, Number, NumberOrPercentage}; use values::computed::{MaxLength, MinLength}; -use values::computed::basic_shape::ShapeRadius; +use values::computed::basic_shape::ShapeRadius as ComputedShapeRadius; +use values::generics::basic_shape::ShapeRadius; use values::specified::Percentage; use values::specified::grid::{TrackBreadth, TrackKeyword}; @@ -205,15 +206,13 @@ impl GeckoStyleCoordConvertible for TrackBreadth< } } -impl GeckoStyleCoordConvertible for ShapeRadius { +impl GeckoStyleCoordConvertible for ComputedShapeRadius { fn to_gecko_style_coord(&self, coord: &mut T) { match *self { - ShapeRadius::ClosestSide => { - coord.set_value(CoordDataValue::Enumerated(StyleShapeRadius::ClosestSide as u32)) - } - ShapeRadius::FarthestSide => { - coord.set_value(CoordDataValue::Enumerated(StyleShapeRadius::FarthestSide as u32)) - } + ShapeRadius::ClosestSide => + coord.set_value(CoordDataValue::Enumerated(StyleShapeRadius::ClosestSide as u32)), + ShapeRadius::FarthestSide => + coord.set_value(CoordDataValue::Enumerated(StyleShapeRadius::FarthestSide as u32)), ShapeRadius::Length(lop) => lop.to_gecko_style_coord(coord), } } diff --git a/components/style/values/computed/basic_shape.rs b/components/style/values/computed/basic_shape.rs index 33075b0e296..40ad1396fe3 100644 --- a/components/style/values/computed/basic_shape.rs +++ b/components/style/values/computed/basic_shape.rs @@ -11,7 +11,7 @@ use std::fmt; use style_traits::ToCss; use values::computed::LengthOrPercentage; use values::computed::position::Position; -use values::generics::basic_shape::BorderRadius as GenericBorderRadius; +use values::generics::basic_shape::{BorderRadius as GenericBorderRadius, ShapeRadius as GenericShapeRadius}; use values::specified::url::SpecifiedUrl; pub use values::specified::basic_shape::{self, FillRule, GeometryBox, ShapeBox}; @@ -171,31 +171,10 @@ impl ToCss for Polygon { } } -/// https://drafts.csswg.org/css-shapes/#typedef-shape-radius -#[derive(Clone, PartialEq, Copy, Debug)] -#[cfg_attr(feature = "servo", derive(HeapSizeOf))] -#[allow(missing_docs)] -pub enum ShapeRadius { - Length(LengthOrPercentage), - ClosestSide, - FarthestSide, -} - -impl Default for ShapeRadius { - fn default() -> Self { - ShapeRadius::ClosestSide - } -} - -impl ToCss for ShapeRadius { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - match *self { - ShapeRadius::Length(lop) => lop.to_css(dest), - ShapeRadius::ClosestSide => dest.write_str("closest-side"), - ShapeRadius::FarthestSide => dest.write_str("farthest-side"), - } - } -} - /// The computed value of `BorderRadius` pub type BorderRadius = GenericBorderRadius; + +/// The computed value of `ShapeRadius` +pub type ShapeRadius = GenericShapeRadius; + +impl Copy for ShapeRadius {} diff --git a/components/style/values/generics/basic_shape.rs b/components/style/values/generics/basic_shape.rs index d3f2d5b5b4f..946d64e6d87 100644 --- a/components/style/values/generics/basic_shape.rs +++ b/components/style/values/generics/basic_shape.rs @@ -78,3 +78,51 @@ impl ToComputedValue for BorderRadius { } } } + +/// https://drafts.csswg.org/css-shapes/#typedef-shape-radius +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "servo", derive(HeapSizeOf))] +#[allow(missing_docs)] +pub enum ShapeRadius { + Length(L), + ClosestSide, + FarthestSide, +} + +impl Default for ShapeRadius { + #[inline] + fn default() -> Self { ShapeRadius::ClosestSide } +} + +impl ToCss for ShapeRadius { + #[inline] + fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + match *self { + ShapeRadius::Length(ref lop) => lop.to_css(dest), + ShapeRadius::ClosestSide => dest.write_str("closest-side"), + ShapeRadius::FarthestSide => dest.write_str("farthest-side"), + } + } +} + +impl ToComputedValue for ShapeRadius { + type ComputedValue = ShapeRadius; + + #[inline] + fn to_computed_value(&self, cx: &Context) -> Self::ComputedValue { + match *self { + ShapeRadius::Length(ref lop) => ShapeRadius::Length(lop.to_computed_value(cx)), + ShapeRadius::ClosestSide => ShapeRadius::ClosestSide, + ShapeRadius::FarthestSide => ShapeRadius::FarthestSide, + } + } + + #[inline] + fn from_computed_value(computed: &Self::ComputedValue) -> Self { + match *computed { + ShapeRadius::Length(ref lop) => ShapeRadius::Length(ToComputedValue::from_computed_value(lop)), + ShapeRadius::ClosestSide => ShapeRadius::ClosestSide, + ShapeRadius::FarthestSide => ShapeRadius::FarthestSide, + } + } +} diff --git a/components/style/values/specified/basic_shape.rs b/components/style/values/specified/basic_shape.rs index 2f66c79af31..6c4b9074e8a 100644 --- a/components/style/values/specified/basic_shape.rs +++ b/components/style/values/specified/basic_shape.rs @@ -17,7 +17,7 @@ use values::HasViewportPercentage; use values::computed::{ComputedValueAsSpecified, Context, ToComputedValue}; use values::computed::basic_shape as computed_basic_shape; use values::generics::BorderRadiusSize; -use values::generics::basic_shape::BorderRadius as GenericBorderRadius; +use values::generics::basic_shape::{BorderRadius as GenericBorderRadius, ShapeRadius as GenericShapeRadius}; use values::specified::{LengthOrPercentage, Percentage}; use values::specified::position::{Keyword, Position}; use values::specified::url::SpecifiedUrl; @@ -411,7 +411,7 @@ impl Parse for Circle { impl ToCss for Circle { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { try!(dest.write_str("circle(")); - if ShapeRadius::ClosestSide != self.radius { + if GenericShapeRadius::ClosestSide != self.radius { try!(self.radius.to_css(dest)); try!(dest.write_str(" ")); } @@ -485,7 +485,7 @@ impl Parse for Ellipse { impl ToCss for Ellipse { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { try!(dest.write_str("ellipse(")); - if !self.semiaxis_x.is_default() || !self.semiaxis_y.is_default() { + if self.semiaxis_x != ShapeRadius::default() || self.semiaxis_y != ShapeRadius::default() { try!(self.semiaxis_x.to_css(dest)); try!(dest.write_str(" ")); try!(self.semiaxis_y.to_css(dest)); @@ -614,71 +614,19 @@ impl ToComputedValue for Polygon { } } -/// https://drafts.csswg.org/css-shapes/#typedef-shape-radius -#[derive(Clone, PartialEq, Debug)] -#[cfg_attr(feature = "servo", derive(HeapSizeOf))] -#[allow(missing_docs)] -pub enum ShapeRadius { - Length(LengthOrPercentage), - ClosestSide, - FarthestSide, -} - -impl ShapeRadius { - fn is_default(&self) -> bool { - *self == ShapeRadius::ClosestSide - } -} - -impl Default for ShapeRadius { - fn default() -> Self { - ShapeRadius::ClosestSide - } -} +/// The specified value of `ShapeRadius` +pub type ShapeRadius = GenericShapeRadius; impl Parse for ShapeRadius { fn parse(context: &ParserContext, input: &mut Parser) -> Result { - input.try(|i| LengthOrPercentage::parse_non_negative(context, i)).map(ShapeRadius::Length).or_else(|_| { - match_ignore_ascii_case! { &try!(input.expect_ident()), - "closest-side" => Ok(ShapeRadius::ClosestSide), - "farthest-side" => Ok(ShapeRadius::FarthestSide), - _ => Err(()) - } - }) - } -} - -impl ToCss for ShapeRadius { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - match *self { - ShapeRadius::Length(ref lop) => lop.to_css(dest), - ShapeRadius::ClosestSide => dest.write_str("closest-side"), - ShapeRadius::FarthestSide => dest.write_str("farthest-side"), + if let Ok(lop) = input.try(|i| LengthOrPercentage::parse_non_negative(context, i)) { + return Ok(GenericShapeRadius::Length(lop)) } - } -} - -impl ToComputedValue for ShapeRadius { - type ComputedValue = computed_basic_shape::ShapeRadius; - - #[inline] - fn to_computed_value(&self, cx: &Context) -> Self::ComputedValue { - match *self { - ShapeRadius::Length(ref lop) => - computed_basic_shape::ShapeRadius::Length(lop.to_computed_value(cx)), - ShapeRadius::ClosestSide => computed_basic_shape::ShapeRadius::ClosestSide, - ShapeRadius::FarthestSide => computed_basic_shape::ShapeRadius::FarthestSide, - } - } - - #[inline] - fn from_computed_value(computed: &Self::ComputedValue) -> Self { - match *computed { - computed_basic_shape::ShapeRadius::Length(ref lop) => - ShapeRadius::Length(ToComputedValue::from_computed_value(lop)), - computed_basic_shape::ShapeRadius::ClosestSide => ShapeRadius::ClosestSide, - computed_basic_shape::ShapeRadius::FarthestSide => ShapeRadius::FarthestSide, + match_ignore_ascii_case! { &input.expect_ident()?, + "closest-side" => Ok(GenericShapeRadius::ClosestSide), + "farthest-side" => Ok(GenericShapeRadius::FarthestSide), + _ => Err(()) } } }