From 24226af794f514f24cb3b11853e1f0722c6e506f Mon Sep 17 00:00:00 2001 From: Ravi Shankar Date: Mon, 17 Apr 2017 21:55:10 +0530 Subject: [PATCH] Make InsetRect generic --- .../style/values/computed/basic_shape.rs | 33 +-------- .../style/values/generics/basic_shape.rs | 58 ++++++++++++++++ .../style/values/specified/basic_shape.rs | 69 ++----------------- 3 files changed, 68 insertions(+), 92 deletions(-) diff --git a/components/style/values/computed/basic_shape.rs b/components/style/values/computed/basic_shape.rs index 8c4256b56327..784293b95d36 100644 --- a/components/style/values/computed/basic_shape.rs +++ b/components/style/values/computed/basic_shape.rs @@ -12,7 +12,7 @@ use style_traits::ToCss; use values::computed::LengthOrPercentage; use values::computed::position::Position; use values::generics::basic_shape::{BorderRadius as GenericBorderRadius, ShapeRadius as GenericShapeRadius}; -use values::generics::basic_shape::Polygon as GenericPolygon; +use values::generics::basic_shape::{InsetRect as GenericInsetRect, Polygon as GenericPolygon}; use values::specified::url::SpecifiedUrl; pub use values::generics::basic_shape::FillRule; @@ -73,35 +73,8 @@ impl ToCss for BasicShape { } } -#[derive(Clone, PartialEq, Copy, Debug)] -#[cfg_attr(feature = "servo", derive(HeapSizeOf))] -#[allow(missing_docs)] -pub struct InsetRect { - pub top: LengthOrPercentage, - pub right: LengthOrPercentage, - pub bottom: LengthOrPercentage, - pub left: LengthOrPercentage, - pub round: Option, -} - -impl ToCss for InsetRect { - // XXXManishearth again, we should try to reduce the number of values printed here - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - try!(dest.write_str("inset(")); - try!(self.top.to_css(dest)); - try!(dest.write_str(" ")); - try!(self.right.to_css(dest)); - try!(dest.write_str(" ")); - try!(self.bottom.to_css(dest)); - try!(dest.write_str(" ")); - try!(self.left.to_css(dest)); - if let Some(ref radius) = self.round { - try!(dest.write_str(" round ")); - try!(radius.to_css(dest)); - } - dest.write_str(")") - } -} +/// The computed value of `inset()` +pub type InsetRect = GenericInsetRect; #[derive(Clone, PartialEq, Copy, Debug)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] diff --git a/components/style/values/generics/basic_shape.rs b/components/style/values/generics/basic_shape.rs index f7c0c0ef748e..9bfa973f85a7 100644 --- a/components/style/values/generics/basic_shape.rs +++ b/components/style/values/generics/basic_shape.rs @@ -234,3 +234,61 @@ impl ToComputedValue for Polygon { } } } + +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "servo", derive(HeapSizeOf))] +/// https://drafts.csswg.org/css-shapes/#funcdef-inset +#[allow(missing_docs)] +pub struct InsetRect { + pub top: L, + pub right: L, + pub bottom: L, + pub left: L, + pub round: Option>, +} + +impl ToCss for InsetRect { + // XXXManishearth We should try to reduce the number of values printed here + fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + dest.write_str("inset(")?; + self.top.to_css(dest)?; + dest.write_str(" ")?; + self.right.to_css(dest)?; + dest.write_str(" ")?; + self.bottom.to_css(dest)?; + dest.write_str(" ")?; + self.left.to_css(dest)?; + if let Some(ref radius) = self.round { + dest.write_str(" round ")?; + radius.to_css(dest)?; + } + + dest.write_str(")") + } +} + +impl ToComputedValue for InsetRect { + type ComputedValue = InsetRect; + + #[inline] + fn to_computed_value(&self, cx: &Context) -> Self::ComputedValue { + InsetRect { + top: self.top.to_computed_value(cx), + right: self.right.to_computed_value(cx), + bottom: self.bottom.to_computed_value(cx), + left: self.left.to_computed_value(cx), + round: self.round.as_ref().map(|r| r.to_computed_value(cx)), + } + } + + #[inline] + fn from_computed_value(computed: &Self::ComputedValue) -> Self { + InsetRect { + top: ToComputedValue::from_computed_value(&computed.top), + right: ToComputedValue::from_computed_value(&computed.right), + bottom: ToComputedValue::from_computed_value(&computed.bottom), + left: ToComputedValue::from_computed_value(&computed.left), + round: computed.round.as_ref().map(|r| ToComputedValue::from_computed_value(r)), + } + } +} diff --git a/components/style/values/specified/basic_shape.rs b/components/style/values/specified/basic_shape.rs index 989f5ce1a3c5..4941655d5d11 100644 --- a/components/style/values/specified/basic_shape.rs +++ b/components/style/values/specified/basic_shape.rs @@ -18,7 +18,7 @@ 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, ShapeRadius as GenericShapeRadius}; -use values::generics::basic_shape::Polygon as GenericPolygon; +use values::generics::basic_shape::{InsetRect as GenericInsetRect, Polygon as GenericPolygon}; use values::specified::{LengthOrPercentage, Percentage}; use values::specified::position::{Keyword, Position}; use values::specified::url::SpecifiedUrl; @@ -202,23 +202,14 @@ impl ToComputedValue for BasicShape { } } -#[derive(Clone, PartialEq, Debug)] -#[cfg_attr(feature = "servo", derive(HeapSizeOf))] -/// https://drafts.csswg.org/css-shapes/#funcdef-inset -#[allow(missing_docs)] -pub struct InsetRect { - pub top: LengthOrPercentage, - pub right: LengthOrPercentage, - pub bottom: LengthOrPercentage, - pub left: LengthOrPercentage, - pub round: Option, -} +/// The specified value of `inset()` +pub type InsetRect = GenericInsetRect; impl InsetRect { - #[allow(missing_docs)] + /// Parse the inner function arguments of `inset()` pub fn parse_function_arguments(context: &ParserContext, input: &mut Parser) -> Result { - let (t, r, b, l) = try!(parse_four_sides(input, |i| LengthOrPercentage::parse(context, i))); - let mut rect = InsetRect { + let (t, r, b, l) = parse_four_sides(input, |i| LengthOrPercentage::parse(context, i))?; + let mut rect = GenericInsetRect { top: t, right: r, bottom: b, @@ -238,58 +229,12 @@ impl Parse for InsetRect { fn parse(context: &ParserContext, input: &mut Parser) -> Result { match input.try(|i| i.expect_function()) { Ok(ref s) if s.eq_ignore_ascii_case("inset") => - input.parse_nested_block(|i| InsetRect::parse_function_arguments(context, i)), + input.parse_nested_block(|i| GenericInsetRect::parse_function_arguments(context, i)), _ => Err(()) } } } -impl ToCss for InsetRect { - // XXXManishearth again, we should try to reduce the number of values printed here - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - try!(dest.write_str("inset(")); - try!(self.top.to_css(dest)); - try!(dest.write_str(" ")); - try!(self.right.to_css(dest)); - try!(dest.write_str(" ")); - try!(self.bottom.to_css(dest)); - try!(dest.write_str(" ")); - try!(self.left.to_css(dest)); - if let Some(ref radius) = self.round { - try!(dest.write_str(" round ")); - try!(radius.to_css(dest)); - } - - dest.write_str(")") - } -} - -impl ToComputedValue for InsetRect { - type ComputedValue = computed_basic_shape::InsetRect; - - #[inline] - fn to_computed_value(&self, cx: &Context) -> Self::ComputedValue { - computed_basic_shape::InsetRect { - top: self.top.to_computed_value(cx), - right: self.right.to_computed_value(cx), - bottom: self.bottom.to_computed_value(cx), - left: self.left.to_computed_value(cx), - round: self.round.as_ref().map(|r| r.to_computed_value(cx)), - } - } - - #[inline] - fn from_computed_value(computed: &Self::ComputedValue) -> Self { - InsetRect { - top: ToComputedValue::from_computed_value(&computed.top), - right: ToComputedValue::from_computed_value(&computed.right), - bottom: ToComputedValue::from_computed_value(&computed.bottom), - left: ToComputedValue::from_computed_value(&computed.left), - round: computed.round.map(|ref r| ToComputedValue::from_computed_value(r)), - } - } -} - /// https://drafts.csswg.org/css-shapes/#basic-shape-serialization /// /// Positions get serialized differently with basic shapes. Keywords