diff --git a/components/layout/model.rs b/components/layout/model.rs index a6d64e557ac..27f0b2ce7a7 100644 --- a/components/layout/model.rs +++ b/components/layout/model.rs @@ -16,6 +16,7 @@ use style::logical_geometry::{LogicalMargin, WritingMode}; use style::properties::ServoComputedValues; use style::values::computed::{BorderRadiusSize, LengthOrPercentageOrAuto}; use style::values::computed::{LengthOrPercentage, LengthOrPercentageOrNone}; +use style::values::generics; /// A collapsible margin. See CSS 2.1 ยง 8.3.1. #[derive(Copy, Clone, Debug)] @@ -478,7 +479,7 @@ pub fn specified(length: LengthOrPercentage, containing_length: Au) -> Au { } pub fn specified_border_radius(radius: BorderRadiusSize, containing_length: Au) -> Size2D { - let BorderRadiusSize(size) = radius; + let generics::BorderRadiusSize(size) = radius; let w = specified(size.width, containing_length); let h = specified(size.height, containing_length); Size2D::new(w, h) diff --git a/components/style/gecko/conversions.rs b/components/style/gecko/conversions.rs index 0a14a5c3410..26027db51b5 100644 --- a/components/style/gecko/conversions.rs +++ b/components/style/gecko/conversions.rs @@ -318,7 +318,6 @@ impl nsStyleImage { pub mod basic_shape { //! Conversions from and to CSS shape representations. - use euclid::size::Size2D; use gecko::values::GeckoStyleCoordConvertible; use gecko_bindings::structs; use gecko_bindings::structs::{StyleBasicShape, StyleBasicShapeType, StyleFillRule}; @@ -329,6 +328,7 @@ pub mod basic_shape { use values::computed::{BorderRadiusSize, LengthOrPercentage}; use values::computed::basic_shape::*; use values::computed::position; + use values::generics::BorderRadiusSize as GenericBorderRadiusSize; // using Borrow so that we can have a non-moving .into() impl> From for BasicShape { @@ -391,11 +391,11 @@ pub mod basic_shape { fn from(other: T) -> Self { let other = other.borrow(); let get_corner = |index| { - BorderRadiusSize(Size2D::new( + GenericBorderRadiusSize::new( LengthOrPercentage::from_gecko_style_coord(&other.data_at(index)) .expect(" should be a length, percentage, or calc value"), LengthOrPercentage::from_gecko_style_coord(&other.data_at(index + 1)) - .expect(" should be a length, percentage, or calc value"))) + .expect(" should be a length, percentage, or calc value")) }; BorderRadius { diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index 3c2ab7280be..e74d147915f 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -543,15 +543,14 @@ fn color_to_nscolor_zero_currentcolor(color: Color) -> structs::nscolor { % if need_clone: #[allow(non_snake_case)] pub fn clone_${ident}(&self) -> longhands::${ident}::computed_value::T { - use properties::longhands::${ident}::computed_value::T; - use euclid::Size2D; + use values::generics::BorderRadiusSize; let width = GeckoStyleCoordConvertible::from_gecko_style_coord( &self.gecko.${gecko_ffi_name}.data_at(${x_index})) .expect("Failed to clone ${ident}"); let height = GeckoStyleCoordConvertible::from_gecko_style_coord( &self.gecko.${gecko_ffi_name}.data_at(${y_index})) .expect("Failed to clone ${ident}"); - T(Size2D::new(width, height)) + BorderRadiusSize::new(width, height) } % endif diff --git a/components/style/properties/helpers/animated_properties.mako.rs b/components/style/properties/helpers/animated_properties.mako.rs index ed2e10b3a54..29516f7c6fd 100644 --- a/components/style/properties/helpers/animated_properties.mako.rs +++ b/components/style/properties/helpers/animated_properties.mako.rs @@ -31,7 +31,7 @@ use std::fmt; use style_traits::ToCss; use super::ComputedValues; use values::CSSFloat; -use values::{Auto, Either, Normal}; +use values::{Auto, Either, Normal, generics}; use values::computed::{Angle, LengthOrPercentageOrAuto, LengthOrPercentageOrNone}; use values::computed::{BorderRadiusSize, ClipRect, LengthOrNone}; use values::computed::{CalcLengthOrPercentage, Context, LengthOrPercentage}; @@ -711,7 +711,7 @@ impl Interpolate for Point2D { impl Interpolate for BorderRadiusSize { #[inline] fn interpolate(&self, other: &Self, progress: f64) -> Result { - self.0.interpolate(&other.0, progress).map(BorderRadiusSize) + self.0.interpolate(&other.0, progress).map(generics::BorderRadiusSize) } } diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index 474239c56f9..4de5184d1b9 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -11,7 +11,9 @@ use media_queries::Device; use properties::ComputedValues; use std::fmt; use style_traits::ToCss; -use super::{CSSFloat, CSSInteger, RGBA, specified}; +use super::{CSSFloat, CSSInteger, RGBA}; +use super::generics::BorderRadiusSize as GenericBorderRadiusSize; +use super::specified; use super::specified::grid::{TrackBreadth as GenericTrackBreadth, TrackSize as GenericTrackSize}; pub use cssparser::Color as CSSColor; @@ -297,43 +299,19 @@ impl ComputedValueAsSpecified for specified::AlignJustifyContent {} impl ComputedValueAsSpecified for specified::AlignJustifySelf {} impl ComputedValueAsSpecified for specified::BorderStyle {} -#[derive(Debug, PartialEq, Clone, Copy)] -#[cfg_attr(feature = "servo", derive(HeapSizeOf))] -#[allow(missing_docs)] -pub struct BorderRadiusSize(pub Size2D); +/// The computed value of `BorderRadiusSize` +pub type BorderRadiusSize = GenericBorderRadiusSize; impl BorderRadiusSize { - #[allow(missing_docs)] + /// Create a null value. + #[inline] pub fn zero() -> BorderRadiusSize { - BorderRadiusSize(Size2D::new(LengthOrPercentage::Length(Au(0)), LengthOrPercentage::Length(Au(0)))) + let zero = LengthOrPercentage::zero(); + GenericBorderRadiusSize(Size2D::new(zero.clone(), zero)) } } -impl ToComputedValue for specified::BorderRadiusSize { - type ComputedValue = BorderRadiusSize; - - #[inline] - fn to_computed_value(&self, context: &Context) -> BorderRadiusSize { - let w = self.0.width.to_computed_value(context); - let h = self.0.height.to_computed_value(context); - BorderRadiusSize(Size2D::new(w, h)) - } - - #[inline] - fn from_computed_value(computed: &BorderRadiusSize) -> Self { - let w = ToComputedValue::from_computed_value(&computed.0.width); - let h = ToComputedValue::from_computed_value(&computed.0.height); - specified::BorderRadiusSize(Size2D::new(w, h)) - } -} - -impl ToCss for BorderRadiusSize { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - try!(self.0.width.to_css(dest)); - try!(dest.write_str("/")); - self.0.height.to_css(dest) - } -} +impl Copy for BorderRadiusSize {} #[derive(Debug, PartialEq, Clone, Copy)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] diff --git a/components/style/values/generics/mod.rs b/components/style/values/generics/mod.rs new file mode 100644 index 00000000000..899d7241aac --- /dev/null +++ b/components/style/values/generics/mod.rs @@ -0,0 +1,65 @@ +/* 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 types that share their serialization implementations +//! for both specified and computed values. + +use euclid::size::Size2D; +use std::fmt; +use style_traits::ToCss; +use super::HasViewportPercentage; +use super::computed::{Context, ToComputedValue}; + +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "servo", derive(HeapSizeOf))] +/// A type for representing CSS `widthh` and `height` values. +pub struct BorderRadiusSize(pub Size2D); + +impl HasViewportPercentage for BorderRadiusSize { + #[inline] + fn has_viewport_percentage(&self) -> bool { false } +} + +impl BorderRadiusSize { + #[inline] + /// Create a new `BorderRadiusSize` for an area of given width and height. + pub fn new(width: L, height: L) -> BorderRadiusSize { + BorderRadiusSize(Size2D::new(width, height)) + } +} + +impl BorderRadiusSize { + #[inline] + /// Create a new `BorderRadiusSize` for a circle of given radius. + pub fn circle(radius: L) -> BorderRadiusSize { + BorderRadiusSize(Size2D::new(radius.clone(), radius)) + } +} + +impl ToCss for BorderRadiusSize { + #[inline] + fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + self.0.width.to_css(dest)?; + dest.write_str(" ")?; + self.0.height.to_css(dest) + } +} + +impl ToComputedValue for BorderRadiusSize { + type ComputedValue = BorderRadiusSize; + + #[inline] + fn to_computed_value(&self, context: &Context) -> Self::ComputedValue { + let w = self.0.width.to_computed_value(context); + let h = self.0.height.to_computed_value(context); + BorderRadiusSize(Size2D::new(w, h)) + } + + #[inline] + fn from_computed_value(computed: &Self::ComputedValue) -> Self { + let w = ToComputedValue::from_computed_value(&computed.0.width); + let h = ToComputedValue::from_computed_value(&computed.0.height); + BorderRadiusSize(Size2D::new(w, h)) + } +} diff --git a/components/style/values/mod.rs b/components/style/values/mod.rs index 256d07dd733..2b371b74da9 100644 --- a/components/style/values/mod.rs +++ b/components/style/values/mod.rs @@ -82,6 +82,7 @@ macro_rules! add_impls_for_keyword_enum { } pub mod computed; +pub mod generics; pub mod specified; /// A CSS float value. diff --git a/components/style/values/specified/length.rs b/components/style/values/specified/length.rs index 3181f5d67b6..ff8bdaa548e 100644 --- a/components/style/values/specified/length.rs +++ b/components/style/values/specified/length.rs @@ -1146,7 +1146,9 @@ impl ToCss for LengthOrPercentage { } } } + impl LengthOrPercentage { + #[inline] /// Returns a `zero` length. pub fn zero() -> LengthOrPercentage { LengthOrPercentage::Length(NoCalcLength::zero()) diff --git a/components/style/values/specified/mod.rs b/components/style/values/specified/mod.rs index e5d4693a2d3..b610401ac32 100644 --- a/components/style/values/specified/mod.rs +++ b/components/style/values/specified/mod.rs @@ -22,6 +22,7 @@ use style_traits::values::specified::AllowedNumericType; use super::{Auto, CSSFloat, CSSInteger, HasViewportPercentage, Either, None_}; use super::computed::{self, Context}; use super::computed::{Shadow as ComputedShadow, ToComputedValue}; +use super::generics::BorderRadiusSize as GenericBorderRadiusSize; #[cfg(feature = "gecko")] pub use self::align::{AlignItems, AlignJustifyContent, AlignJustifySelf, JustifyItems}; @@ -282,30 +283,8 @@ pub fn parse_number_with_clamping_mode(context: &ParserContext, } } -#[derive(Clone, PartialEq, Debug)] -#[cfg_attr(feature = "servo", derive(HeapSizeOf))] -#[allow(missing_docs)] -pub struct BorderRadiusSize(pub Size2D); - -no_viewport_percentage!(BorderRadiusSize); - -impl BorderRadiusSize { - #[allow(missing_docs)] - pub fn zero() -> BorderRadiusSize { - let zero = LengthOrPercentage::Length(NoCalcLength::zero()); - BorderRadiusSize(Size2D::new(zero.clone(), zero)) - } - - #[allow(missing_docs)] - pub fn new(width: LengthOrPercentage, height: LengthOrPercentage) -> BorderRadiusSize { - BorderRadiusSize(Size2D::new(width, height)) - } - - #[allow(missing_docs)] - pub fn circle(radius: LengthOrPercentage) -> BorderRadiusSize { - BorderRadiusSize(Size2D::new(radius.clone(), radius)) - } -} +/// The specified value of `BorderRadiusSize` +pub type BorderRadiusSize = GenericBorderRadiusSize; impl Parse for BorderRadiusSize { #[inline] @@ -313,15 +292,7 @@ impl Parse for BorderRadiusSize { let first = try!(LengthOrPercentage::parse_non_negative(context, input)); let second = input.try(|i| LengthOrPercentage::parse_non_negative(context, i)) .unwrap_or_else(|()| first.clone()); - Ok(BorderRadiusSize(Size2D::new(first, second))) - } -} - -impl ToCss for BorderRadiusSize { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - try!(self.0.width.to_css(dest)); - try!(dest.write_str(" ")); - self.0.height.to_css(dest) + Ok(GenericBorderRadiusSize(Size2D::new(first, second))) } }