diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index e0003ceae30..581272c98d0 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -454,18 +454,13 @@ impl LayoutElementHelpers for LayoutJS { font_family)]))))); } - let font_size = if let Some(this) = self.downcast::() { - this.get_size() - } else { - None - }; + let font_size = self.downcast::().and_then(|this| this.get_size()); if let Some(font_size) = font_size { hints.push(from_declaration( PropertyDeclaration::FontSize( DeclaredValue::Value( - font_size::SpecifiedValue( - LengthOrPercentage::Length(font_size)))))) + font_size::SpecifiedValue(font_size.into()))))) } let cellspacing = if let Some(this) = self.downcast::() { diff --git a/components/style/gecko/media_queries.rs b/components/style/gecko/media_queries.rs index 342baa002f1..dd9f5a19643 100644 --- a/components/style/gecko/media_queries.rs +++ b/components/style/gecko/media_queries.rs @@ -236,8 +236,7 @@ impl MediaExpressionValue { nsMediaFeature_ValueType::eLength => { debug_assert!(css_value.mUnit == nsCSSUnit::eCSSUnit_Pixel); let pixels = css_value.float_unchecked(); - Some(MediaExpressionValue::Length( - specified::Length::Absolute(Au::from_f32_px(pixels)))) + Some(MediaExpressionValue::Length(specified::Length::from_px(pixels))) } nsMediaFeature_ValueType::eInteger => { let i = css_value.integer_unchecked(); diff --git a/components/style/properties/longhand/box.mako.rs b/components/style/properties/longhand/box.mako.rs index 9bec8f87b21..1558e597ead 100644 --- a/components/style/properties/longhand/box.mako.rs +++ b/components/style/properties/longhand/box.mako.rs @@ -1761,7 +1761,7 @@ ${helpers.single_keyword("transform-style", use std::fmt; use style_traits::ToCss; use values::HasViewportPercentage; - use values::specified::{Length, LengthOrPercentage, Percentage}; + use values::specified::{NoCalcLength, LengthOrPercentage, Percentage}; pub mod computed_value { use properties::animated_properties::Interpolate; @@ -1799,7 +1799,7 @@ ${helpers.single_keyword("transform-style", pub struct SpecifiedValue { horizontal: LengthOrPercentage, vertical: LengthOrPercentage, - depth: Length, + depth: NoCalcLength, } impl ToCss for computed_value::T { @@ -1836,7 +1836,7 @@ ${helpers.single_keyword("transform-style", Ok(SpecifiedValue { horizontal: result.horizontal.unwrap_or(LengthOrPercentage::Percentage(Percentage(0.5))), vertical: result.vertical.unwrap_or(LengthOrPercentage::Percentage(Percentage(0.5))), - depth: result.depth.unwrap_or(Length::zero()), + depth: result.depth.unwrap_or(NoCalcLength::zero()), }) } diff --git a/components/style/properties/longhand/effects.mako.rs b/components/style/properties/longhand/effects.mako.rs index 01771f36315..9bc62bc5e7d 100644 --- a/components/style/properties/longhand/effects.mako.rs +++ b/components/style/properties/longhand/effects.mako.rs @@ -613,7 +613,7 @@ ${helpers.predefined_type("opacity", pub struct OriginParseResult { pub horizontal: Option, pub vertical: Option, - pub depth: Option + pub depth: Option } pub fn parse_origin(context: &ParserContext, input: &mut Parser) -> Result { diff --git a/components/style/properties/longhand/font.mako.rs b/components/style/properties/longhand/font.mako.rs index 3bb3a09db89..b25d40fefea 100644 --- a/components/style/properties/longhand/font.mako.rs +++ b/components/style/properties/longhand/font.mako.rs @@ -363,10 +363,10 @@ ${helpers.single_keyword("font-variant-caps", #[inline] fn to_computed_value(&self, context: &Context) -> computed_value::T { match self.0 { - LengthOrPercentage::Length(Length::NoCalc(NoCalcLength::FontRelative(value))) => { + LengthOrPercentage::Length(NoCalcLength::FontRelative(value)) => { value.to_computed_value(context, /* use inherited */ true) } - LengthOrPercentage::Length(Length::NoCalc(NoCalcLength::ServoCharacterWidth(value))) => { + LengthOrPercentage::Length(NoCalcLength::ServoCharacterWidth(value)) => { value.to_computed_value(context.inherited_style().get_font().clone_font_size()) } LengthOrPercentage::Length(ref l) => { @@ -397,11 +397,10 @@ ${helpers.single_keyword("font-variant-caps", input.try(specified::LengthOrPercentage::parse_non_negative) .or_else(|()| { let ident = try!(input.expect_ident()); - specified::Length::from_str(&ident as &str) - .ok_or(()) - .map(specified::LengthOrPercentage::Length) - }) - .map(SpecifiedValue) + NoCalcLength::from_str(&ident as &str) + .ok_or(()) + .map(specified::LengthOrPercentage::Length) + }).map(SpecifiedValue) } diff --git a/components/style/properties/shorthand/inherited_text.mako.rs b/components/style/properties/shorthand/inherited_text.mako.rs index d2b0bce7d76..f97c129590d 100644 --- a/components/style/properties/shorthand/inherited_text.mako.rs +++ b/components/style/properties/shorthand/inherited_text.mako.rs @@ -74,7 +74,6 @@ pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result { use values::specified::{BorderWidth, Length}; - use app_units::Au; let mut color = None; let mut width = None; diff --git a/components/style/values/specified/basic_shape.rs b/components/style/values/specified/basic_shape.rs index c7175b5352d..7f77337218c 100644 --- a/components/style/values/specified/basic_shape.rs +++ b/components/style/values/specified/basic_shape.rs @@ -299,7 +299,6 @@ impl ToComputedValue for InsetRect { /// the keywords are folded into the percentages fn serialize_basicshape_position(position: &Position, dest: &mut W) -> fmt::Result where W: fmt::Write { - use values::specified::Length; use values::specified::position::Keyword; // keyword-percentage pairs can be folded into a single percentage @@ -327,7 +326,7 @@ fn serialize_basicshape_position(position: &Position, dest: &mut W) // 0 length should be replaced with 0% fn replace_with_percent(input: LengthOrPercentage) -> LengthOrPercentage { match input { - LengthOrPercentage::Length(ref l) if l == &Length::zero() => { + LengthOrPercentage::Length(ref l) if l.is_zero() => { LengthOrPercentage::Percentage(Percentage(0.0)) } _ => { diff --git a/components/style/values/specified/length.rs b/components/style/values/specified/length.rs index 8e9a96067d6..0cd6d9499f7 100644 --- a/components/style/values/specified/length.rs +++ b/components/style/values/specified/length.rs @@ -323,7 +323,13 @@ impl NoCalcLength { NoCalcLength::Absolute(Au(0)) } - /// Get an absolute length from a px values. + #[inline] + /// Checks whether the length value is zero. + pub fn is_zero(&self) -> bool { + *self == NoCalcLength::Absolute(Au(0)) + } + + /// Get an absolute length from a px value. #[inline] pub fn from_px(px_value: CSSFloat) -> NoCalcLength { NoCalcLength::Absolute(Au((px_value * AU_PER_PX) as i32)) @@ -337,7 +343,7 @@ impl NoCalcLength { #[derive(Clone, PartialEq, Debug)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub enum Length { - /// The `NoCalcLength` type that cannot parse `calc` + /// The internal length type that cannot parse `calc` NoCalc(NoCalcLength), /// A calc expression. /// @@ -428,8 +434,7 @@ impl Length { match try!(input.next()) { Token::Dimension(ref value, ref unit) if context.is_ok(value.value) => Length::parse_dimension(value.value, unit), - Token::Number(ref value) if value.value == 0. => - Ok(Length::zero()), + Token::Number(ref value) if value.value == 0. => Ok(Length::zero()), Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => input.parse_nested_block(|input| { CalcLengthOrPercentage::parse_length(input, context) @@ -443,7 +448,7 @@ impl Length { Length::parse_internal(input, AllowedNumericType::NonNegative) } - /// Get an absolute length from a px values. + /// Get an absolute length from a px value. #[inline] pub fn from_px(px_value: CSSFloat) -> Length { Length::NoCalc(NoCalcLength::from_px(px_value)) @@ -490,7 +495,7 @@ pub struct CalcProductNode { #[derive(Clone, Debug)] #[allow(missing_docs)] pub enum CalcValueNode { - Length(LengthInternal), + Length(NoCalcLength), Angle(Angle), Time(Time), Percentage(CSSFloat), @@ -583,7 +588,7 @@ impl CalcLengthOrPercentage { (Token::Number(ref value), _) => Ok(CalcValueNode::Number(value.value)), (Token::Dimension(ref value, ref unit), CalcUnit::Length) | (Token::Dimension(ref value, ref unit), CalcUnit::LengthOrPercentage) => { - LengthInternal::parse_dimension(value.value, unit).map(CalcValueNode::Length) + NoCalcLength::parse_dimension(value.value, unit).map(CalcValueNode::Length) } (Token::Dimension(ref value, ref unit), CalcUnit::Angle) => { Angle::parse_dimension(value.value, unit).map(CalcValueNode::Angle) @@ -899,11 +904,20 @@ impl Parse for Percentage { #[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[allow(missing_docs)] pub enum LengthOrPercentage { - Length(Length), + Length(NoCalcLength), Percentage(Percentage), Calc(Box), } +impl From for LengthOrPercentage { + fn from(len: Length) -> LengthOrPercentage { + match len { + Length::NoCalc(l) => LengthOrPercentage::Length(l), + Length::Calc(l, _) => LengthOrPercentage::Calc(l), + } + } +} + impl HasViewportPercentage for LengthOrPercentage { fn has_viewport_percentage(&self) -> bool { match *self { @@ -924,10 +938,9 @@ impl ToCss for LengthOrPercentage { } } impl LengthOrPercentage { - #[inline] /// Returns a `zero` length. pub fn zero() -> LengthOrPercentage { - LengthOrPercentage::Length(Length::zero()) + LengthOrPercentage::Length(NoCalcLength::zero()) } fn parse_internal(input: &mut Parser, context: AllowedNumericType) @@ -935,11 +948,11 @@ impl LengthOrPercentage { { match try!(input.next()) { Token::Dimension(ref value, ref unit) if context.is_ok(value.value) => - Length::parse_dimension(value.value, unit).map(LengthOrPercentage::Length), + NoCalcLength::parse_dimension(value.value, unit).map(LengthOrPercentage::Length), Token::Percentage(ref value) if context.is_ok(value.unit_value) => Ok(LengthOrPercentage::Percentage(Percentage(value.unit_value))), Token::Number(ref value) if value.value == 0. => - Ok(LengthOrPercentage::Length(Length::zero())), + Ok(LengthOrPercentage::zero()), Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => { let calc = try!(input.parse_nested_block(CalcLengthOrPercentage::parse_length_or_percentage)); Ok(LengthOrPercentage::Calc(Box::new(calc))) diff --git a/components/style/values/specified/mod.rs b/components/style/values/specified/mod.rs index 8a57f52d99a..076324cebe6 100644 --- a/components/style/values/specified/mod.rs +++ b/components/style/values/specified/mod.rs @@ -198,8 +198,8 @@ impl NoViewportPercentage for BorderRadiusSize {} impl BorderRadiusSize { #[allow(missing_docs)] pub fn zero() -> BorderRadiusSize { - let zero = LengthOrPercentage::Length(Length::default()); - BorderRadiusSize(Size2D::new(zero.clone(), zero)) + let zero = LengthOrPercentage::Length(NoCalcLength::zero()); + BorderRadiusSize(Size2D::new(zero.clone(), zero)) } #[allow(missing_docs)] @@ -292,11 +292,11 @@ pub fn parse_border_radius(context: &ParserContext, input: &mut Parser) -> Resul input.try(|i| BorderRadiusSize::parse(context, i)).or_else(|_| { match_ignore_ascii_case! { try!(input.expect_ident()), "thin" => Ok(BorderRadiusSize::circle( - LengthOrPercentage::Length(Length::from_px(1.)))), + LengthOrPercentage::Length(NoCalcLength::from_px(1.)))), "medium" => Ok(BorderRadiusSize::circle( - LengthOrPercentage::Length(Length::from_px(3.)))), + LengthOrPercentage::Length(NoCalcLength::from_px(3.)))), "thick" => Ok(BorderRadiusSize::circle( - LengthOrPercentage::Length(Length::from_px(5.)))), + LengthOrPercentage::Length(NoCalcLength::from_px(5.)))), _ => Err(()) } }) @@ -606,10 +606,7 @@ impl Shadow { #[allow(missing_docs)] pub fn parse(context: &ParserContext, input: &mut Parser, disable_spread_and_inset: bool) -> Result { let length_count = if disable_spread_and_inset { 3 } else { 4 }; - let mut lengths = [Length::default(), - Length::default(), - Length::default(), - Length::default()]; + let mut lengths = [Length::zero(), Length::zero(), Length::zero(), Length::zero()]; let mut lengths_parsed = false; let mut color = None; let mut inset = false; @@ -661,7 +658,7 @@ impl Shadow { offset_x: lengths[0].take(), offset_y: lengths[1].take(), blur_radius: lengths[2].take(), - spread_radius: if disable_spread_and_inset { Length::default() } else { lengths[3].take() }, + spread_radius: if disable_spread_and_inset { Length::zero() } else { lengths[3].take() }, color: color, inset: inset, }) diff --git a/tests/unit/style/properties/serialization.rs b/tests/unit/style/properties/serialization.rs index ec608fa6ccb..7a8ca391a9c 100644 --- a/tests/unit/style/properties/serialization.rs +++ b/tests/unit/style/properties/serialization.rs @@ -5,7 +5,7 @@ pub use std::sync::Arc; pub use style::computed_values::display::T::inline_block; pub use style::properties::{DeclaredValue, PropertyDeclaration, PropertyDeclarationBlock, Importance, PropertyId}; -pub use style::values::specified::{BorderStyle, BorderWidth, CSSColor, Length}; +pub use style::values::specified::{BorderStyle, BorderWidth, CSSColor, Length, NoCalcLength}; pub use style::values::specified::{LengthOrPercentage, LengthOrPercentageOrAuto, LengthOrPercentageOrAutoOrContent}; pub use style::properties::longhands::outline_color::computed_value::T as ComputedColor; pub use style::values::RGBA; @@ -23,7 +23,7 @@ fn property_declaration_block_should_serialize_correctly() { Importance::Normal), (PropertyDeclaration::MinHeight( - DeclaredValue::Value(LengthOrPercentage::Length(Length::from_px(20f32)))), + DeclaredValue::Value(LengthOrPercentage::Length(NoCalcLength::from_px(20f32)))), Importance::Normal), (PropertyDeclaration::Height( @@ -178,8 +178,8 @@ mod shorthand_serialization { fn padding_should_serialize_correctly() { let mut properties = Vec::new(); - let px_10 = DeclaredValue::Value(LengthOrPercentage::Length(Length::from_px(10f32))); - let px_15 = DeclaredValue::Value(LengthOrPercentage::Length(Length::from_px(15f32))); + let px_10 = DeclaredValue::Value(LengthOrPercentage::Length(NoCalcLength::from_px(10f32))); + let px_15 = DeclaredValue::Value(LengthOrPercentage::Length(NoCalcLength::from_px(15f32))); properties.push(PropertyDeclaration::PaddingTop(px_10.clone())); properties.push(PropertyDeclaration::PaddingRight(px_15.clone())); properties.push(PropertyDeclaration::PaddingBottom(px_10)); @@ -637,7 +637,7 @@ mod shorthand_serialization { let font_variant = DeclaredValue::Value(FontVariant::normal); let font_weight = DeclaredValue::Value(FontWeight::Bolder); let font_size = DeclaredValue::Value(FontSizeContainer( - LengthOrPercentage::Length(Length::from_px(4f32))) + LengthOrPercentage::Length(NoCalcLength::from_px(4f32))) ); let font_stretch = DeclaredValue::Value(FontStretch::expanded); let line_height = DeclaredValue::Value(LineHeight::Number(3f32)); @@ -718,14 +718,14 @@ mod shorthand_serialization { let position_x = single_vec_value_typedef!(position_x, HorizontalPosition { keyword: None, - position: Some(LengthOrPercentage::Length(Length::from_px(7f32))), + position: Some(LengthOrPercentage::Length(NoCalcLength::from_px(7f32))), } ); let position_y = single_vec_value_typedef!(position_y, VerticalPosition { keyword: None, - position: Some(LengthOrPercentage::Length(Length::from_px(4f32))), + position: Some(LengthOrPercentage::Length(NoCalcLength::from_px(4f32))), } ); @@ -778,14 +778,14 @@ mod shorthand_serialization { let position_x = single_vec_value_typedef!(position_x, HorizontalPosition { keyword: None, - position: Some(LengthOrPercentage::Length(Length::from_px(7f32))), + position: Some(LengthOrPercentage::Length(NoCalcLength::from_px(7f32))), } ); let position_y = single_vec_value_typedef!(position_y, VerticalPosition { keyword: None, - position: Some(LengthOrPercentage::Length(Length::from_px(4f32))), + position: Some(LengthOrPercentage::Length(NoCalcLength::from_px(4f32))), } ); @@ -837,14 +837,14 @@ mod shorthand_serialization { let position_x = single_vec_value_typedef!(position_x, HorizontalPosition { keyword: None, - position: Some(LengthOrPercentage::Length(Length::from_px(0f32))), + position: Some(LengthOrPercentage::Length(NoCalcLength::from_px(0f32))), } ); let position_y = single_vec_value_typedef!(position_y, VerticalPosition { keyword: None, - position: Some(LengthOrPercentage::Length(Length::from_px(0f32))), + position: Some(LengthOrPercentage::Length(NoCalcLength::from_px(0f32))), } ); @@ -922,11 +922,11 @@ mod shorthand_serialization { Position { horizontal: HorizontalPosition { keyword: None, - position: Some(LengthOrPercentage::Length(Length::from_px(7f32))), + position: Some(LengthOrPercentage::Length(NoCalcLength::from_px(7f32))), }, vertical: VerticalPosition { keyword: None, - position: Some(LengthOrPercentage::Length(Length::from_px(4f32))), + position: Some(LengthOrPercentage::Length(NoCalcLength::from_px(4f32))), }, } ); @@ -976,11 +976,11 @@ mod shorthand_serialization { Position { horizontal: HorizontalPosition { keyword: None, - position: Some(LengthOrPercentage::Length(Length::from_px(7f32))), + position: Some(LengthOrPercentage::Length(NoCalcLength::from_px(7f32))), }, vertical: VerticalPosition { keyword: None, - position: Some(LengthOrPercentage::Length(Length::from_px(4f32))), + position: Some(LengthOrPercentage::Length(NoCalcLength::from_px(4f32))), }, } );