Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Change LengthOrPercentage to make use of NoCalcLength
  • Loading branch information
wafflespeanut committed Jan 28, 2017
1 parent 377a23d commit 590c957
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 60 deletions.
9 changes: 2 additions & 7 deletions components/script/dom/element.rs
Expand Up @@ -454,18 +454,13 @@ impl LayoutElementHelpers for LayoutJS<Element> {
font_family)])))));
}

let font_size = if let Some(this) = self.downcast::<HTMLFontElement>() {
this.get_size()
} else {
None
};
let font_size = self.downcast::<HTMLFontElement>().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::<HTMLTableElement>() {
Expand Down
3 changes: 1 addition & 2 deletions components/style/gecko/media_queries.rs
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions components/style/properties/longhand/box.mako.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()),
})
}

Expand Down
2 changes: 1 addition & 1 deletion components/style/properties/longhand/effects.mako.rs
Expand Up @@ -613,7 +613,7 @@ ${helpers.predefined_type("opacity",
pub struct OriginParseResult {
pub horizontal: Option<specified::LengthOrPercentage>,
pub vertical: Option<specified::LengthOrPercentage>,
pub depth: Option<specified::Length>
pub depth: Option<specified::NoCalcLength>
}

pub fn parse_origin(context: &ParserContext, input: &mut Parser) -> Result<OriginParseResult,()> {
Expand Down
13 changes: 6 additions & 7 deletions components/style/properties/longhand/font.mako.rs
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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)
}
</%helpers:longhand>

Expand Down
Expand Up @@ -74,7 +74,6 @@

pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
use values::specified::{BorderWidth, Length};
use app_units::Au;

let mut color = None;
let mut width = None;
Expand Down
3 changes: 1 addition & 2 deletions components/style/values/specified/basic_shape.rs
Expand Up @@ -299,7 +299,6 @@ impl ToComputedValue for InsetRect {
/// the keywords are folded into the percentages
fn serialize_basicshape_position<W>(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
Expand Down Expand Up @@ -327,7 +326,7 @@ fn serialize_basicshape_position<W>(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))
}
_ => {
Expand Down
37 changes: 25 additions & 12 deletions components/style/values/specified/length.rs
Expand Up @@ -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))
Expand All @@ -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.
///
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<CalcLengthOrPercentage>),
}

impl From<Length> 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 {
Expand All @@ -924,22 +938,21 @@ 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)
-> Result<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)))
Expand Down
17 changes: 7 additions & 10 deletions components/style/values/specified/mod.rs
Expand Up @@ -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)]
Expand Down Expand Up @@ -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(())
}
})
Expand Down Expand Up @@ -606,10 +606,7 @@ impl Shadow {
#[allow(missing_docs)]
pub fn parse(context: &ParserContext, input: &mut Parser, disable_spread_and_inset: bool) -> Result<Shadow, ()> {
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;
Expand Down Expand Up @@ -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,
})
Expand Down
30 changes: 15 additions & 15 deletions tests/unit/style/properties/serialization.rs
Expand Up @@ -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;
Expand All @@ -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(
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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))),
}
);

Expand Down Expand Up @@ -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))),
}
);

Expand Down Expand Up @@ -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))),
}
);

Expand Down Expand Up @@ -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))),
},
}
);
Expand Down Expand Up @@ -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))),
},
}
);
Expand Down

0 comments on commit 590c957

Please sign in to comment.