Skip to content

Commit

Permalink
Refactor and modify stuff to make use of NoCalcLength
Browse files Browse the repository at this point in the history
  • Loading branch information
wafflespeanut committed Jan 28, 2017
1 parent 4035bbd commit 377a23d
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 67 deletions.
19 changes: 10 additions & 9 deletions components/script/dom/element.rs
Expand Up @@ -4,7 +4,6 @@

//! Element nodes.

use app_units::Au;
use cssparser::Color;
use devtools_traits::AttrInfo;
use dom::activation::Activatable;
Expand Down Expand Up @@ -476,7 +475,7 @@ impl LayoutElementHelpers for LayoutJS<Element> {
};

if let Some(cellspacing) = cellspacing {
let width_value = specified::Length::Absolute(Au::from_px(cellspacing as i32));
let width_value = specified::Length::from_px(cellspacing as f32);
hints.push(from_declaration(
PropertyDeclaration::BorderSpacing(DeclaredValue::Value(
border_spacing::SpecifiedValue {
Expand Down Expand Up @@ -509,7 +508,8 @@ impl LayoutElementHelpers for LayoutJS<Element> {
};

if let Some(size) = size {
let value = specified::Length::ServoCharacterWidth(specified::CharacterWidth(size));
let value = specified::Length::NoCalc(
specified::NoCalcLength::ServoCharacterWidth(specified::CharacterWidth(size)));
hints.push(from_declaration(
PropertyDeclaration::Width(DeclaredValue::Value(
specified::LengthOrPercentageOrAuto::Length(value)))));
Expand Down Expand Up @@ -541,7 +541,7 @@ impl LayoutElementHelpers for LayoutJS<Element> {
}
LengthOrPercentageOrAuto::Length(length) => {
let width_value = specified::LengthOrPercentageOrAuto::Length(
specified::Length::Absolute(length));
specified::Length::NoCalc(specified::NoCalcLength::Absolute(length)));
hints.push(from_declaration(
PropertyDeclaration::Width(DeclaredValue::Value(width_value))));
}
Expand All @@ -566,7 +566,7 @@ impl LayoutElementHelpers for LayoutJS<Element> {
}
LengthOrPercentageOrAuto::Length(length) => {
let height_value = specified::LengthOrPercentageOrAuto::Length(
specified::Length::Absolute(length));
specified::Length::NoCalc(specified::NoCalcLength::Absolute(length)));
hints.push(from_declaration(
PropertyDeclaration::Height(DeclaredValue::Value(height_value))));
}
Expand All @@ -588,7 +588,8 @@ impl LayoutElementHelpers for LayoutJS<Element> {
// scrollbar size into consideration (but we don't have a scrollbar yet!)
//
// https://html.spec.whatwg.org/multipage/#textarea-effective-width
let value = specified::Length::ServoCharacterWidth(specified::CharacterWidth(cols));
let value = specified::Length::NoCalc(
specified::NoCalcLength::ServoCharacterWidth(specified::CharacterWidth(cols)));
hints.push(from_declaration(
PropertyDeclaration::Width(DeclaredValue::Value(
specified::LengthOrPercentageOrAuto::Length(value)))));
Expand All @@ -608,7 +609,8 @@ impl LayoutElementHelpers for LayoutJS<Element> {
// TODO(mttr) This should take scrollbar size into consideration.
//
// https://html.spec.whatwg.org/multipage/#textarea-effective-height
let value = specified::Length::FontRelative(specified::FontRelativeLength::Em(rows as CSSFloat));
let value = specified::Length::NoCalc(
specified::NoCalcLength::FontRelative(specified::FontRelativeLength::Em(rows as CSSFloat)));
hints.push(from_declaration(
PropertyDeclaration::Height(DeclaredValue::Value(
specified::LengthOrPercentageOrAuto::Length(value)))));
Expand All @@ -622,8 +624,7 @@ impl LayoutElementHelpers for LayoutJS<Element> {
};

if let Some(border) = border {
let width_value = specified::BorderWidth::from_length(
specified::Length::Absolute(Au::from_px(border as i32)));
let width_value = specified::BorderWidth::from_length(specified::Length::from_px(border as f32));
hints.push(from_declaration(
PropertyDeclaration::BorderTopWidth(DeclaredValue::Value(width_value.clone()))));
hints.push(from_declaration(
Expand Down
8 changes: 4 additions & 4 deletions components/style/properties/longhand/box.mako.rs
Expand Up @@ -1328,7 +1328,7 @@ ${helpers.single_keyword("animation-fill-mode",
result.push(SpecifiedOperation::Translate(TranslateKind::Translate,
tx,
ty,
specified::Length::Absolute(Au(0))));
specified::Length::zero()));
Ok(())
}))
},
Expand All @@ -1339,7 +1339,7 @@ ${helpers.single_keyword("animation-fill-mode",
TranslateKind::TranslateX,
tx,
specified::LengthOrPercentage::zero(),
specified::Length::Absolute(Au(0))));
specified::Length::zero()));
Ok(())
}))
},
Expand All @@ -1350,7 +1350,7 @@ ${helpers.single_keyword("animation-fill-mode",
TranslateKind::TranslateY,
specified::LengthOrPercentage::zero(),
ty,
specified::Length::Absolute(Au(0))));
specified::Length::zero()));
Ok(())
}))
},
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::Absolute(Au(0))),
depth: result.depth.unwrap_or(Length::zero()),
})
}

Expand Down
4 changes: 2 additions & 2 deletions components/style/properties/longhand/effects.mako.rs
Expand Up @@ -280,10 +280,10 @@ ${helpers.predefined_type("opacity",
left = try!(parse_argument(context, input));
}
Ok(SpecifiedValue(Some(SpecifiedClipRect {
top: top.unwrap_or(Length::Absolute(Au(0))),
top: top.unwrap_or(Length::zero()),
right: right,
bottom: bottom,
left: left.unwrap_or(Length::Absolute(Au(0))),
left: left.unwrap_or(Length::zero()),
})))
})
}
Expand Down
6 changes: 3 additions & 3 deletions components/style/properties/longhand/font.mako.rs
Expand Up @@ -329,7 +329,7 @@ ${helpers.single_keyword("font-variant-caps",
use std::fmt;
use style_traits::ToCss;
use values::{FONT_MEDIUM_PX, HasViewportPercentage};
use values::specified::{LengthOrPercentage, Length, Percentage};
use values::specified::{LengthOrPercentage, Length, NoCalcLength, Percentage};

impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
Expand Down 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::FontRelative(value)) => {
LengthOrPercentage::Length(Length::NoCalc(NoCalcLength::FontRelative(value))) => {
value.to_computed_value(context, /* use inherited */ true)
}
LengthOrPercentage::Length(Length::ServoCharacterWidth(value)) => {
LengthOrPercentage::Length(Length::NoCalc(NoCalcLength::ServoCharacterWidth(value))) => {
value.to_computed_value(context.inherited_style().get_font().clone_font_size())
}
LengthOrPercentage::Length(ref l) => {
Expand Down
9 changes: 4 additions & 5 deletions components/style/properties/longhand/inherited_text.mako.rs
Expand Up @@ -113,13 +113,14 @@
specified::LengthOrPercentage::Length(ref value) =>
computed_value::T::Length(value.to_computed_value(context)),
specified::LengthOrPercentage::Percentage(specified::Percentage(value)) => {
let fr = specified::Length::FontRelative(specified::FontRelativeLength::Em(value));
let fr = specified::Length::NoCalc(specified::NoCalcLength::FontRelative(
specified::FontRelativeLength::Em(value)));
computed_value::T::Length(fr.to_computed_value(context))
},
specified::LengthOrPercentage::Calc(ref calc) => {
let calc = calc.to_computed_value(context);
let fr = specified::FontRelativeLength::Em(calc.percentage());
let fr = specified::Length::FontRelative(fr);
let fr = specified::Length::NoCalc(specified::NoCalcLength::FontRelative(fr));
computed_value::T::Length(calc.length() + fr.to_computed_value(context))
}
}
Expand Down Expand Up @@ -681,9 +682,7 @@ ${helpers.single_keyword("text-align-last",

fn parse_one_text_shadow(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedTextShadow,()> {
use app_units::Au;
let mut lengths = [specified::Length::Absolute(Au(0)),
specified::Length::Absolute(Au(0)),
specified::Length::Absolute(Au(0))];
let mut lengths = [specified::Length::zero(), specified::Length::zero(), specified::Length::zero()];
let mut lengths_parsed = false;
let mut color = None;

Expand Down
Expand Up @@ -99,7 +99,7 @@
Ok(Longhands {
_webkit_text_stroke_color: color.or(Some(CSSColor { parsed: CSSParserColor::CurrentColor,
authored: None })),
_webkit_text_stroke_width: width.or(Some(BorderWidth::from_length(Length::Absolute(Au::from_px(0))))),
_webkit_text_stroke_width: width.or(Some(BorderWidth::from_length(Length::zero()))),
})
} else {
Err(())
Expand Down
3 changes: 1 addition & 2 deletions components/style/properties/shorthand/position.mako.rs
Expand Up @@ -57,7 +57,6 @@
<%helpers:shorthand name="flex" sub_properties="flex-grow flex-shrink flex-basis" extra_prefixes="webkit"
spec="https://drafts.csswg.org/css-flexbox/#flex-property">
use parser::Parse;
use app_units::Au;
use values::specified::{Number, Length, LengthOrPercentageOrAutoOrContent};

pub fn parse_flexibility(input: &mut Parser)
Expand Down Expand Up @@ -102,7 +101,7 @@
Ok(Longhands {
flex_grow: grow.or(Some(Number(1.0))),
flex_shrink: shrink.or(Some(Number(1.0))),
flex_basis: basis.or(Some(LengthOrPercentageOrAutoOrContent::Length(Length::Absolute(Au(0)))))
flex_basis: basis.or(Some(LengthOrPercentageOrAutoOrContent::Length(Length::zero())))
})
}

Expand Down
2 changes: 1 addition & 1 deletion components/style/values/specified/basic_shape.rs
Expand Up @@ -327,7 +327,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(Length::Absolute(au)) if au.0 == 0 => {
LengthOrPercentage::Length(ref l) if l == &Length::zero() => {
LengthOrPercentage::Percentage(Percentage(0.0))
}
_ => {
Expand Down
43 changes: 30 additions & 13 deletions components/style/values/specified/length.rs
Expand Up @@ -316,6 +316,18 @@ impl NoCalcLength {
_ => Err(())
}
}

#[inline]
/// Returns a `zero` length.
pub fn zero() -> NoCalcLength {
NoCalcLength::Absolute(Au(0))
}

/// Get an absolute length from a px values.
#[inline]
pub fn from_px(px_value: CSSFloat) -> NoCalcLength {
NoCalcLength::Absolute(Au((px_value * AU_PER_PX) as i32))
}
}

/// An extension to `NoCalcLength` to parse `calc` expressions.
Expand Down Expand Up @@ -395,6 +407,12 @@ impl Mul<CSSFloat> for ViewportPercentageLength {
}

impl Length {
#[inline]
/// Returns a `zero` length.
pub fn zero() -> Length {
Length::NoCalc(NoCalcLength::zero())
}

/// https://drafts.csswg.org/css-fonts-3/#font-size-prop
pub fn from_str(s: &str) -> Option<Length> {
NoCalcLength::from_str(s).map(Length::NoCalc)
Expand All @@ -411,7 +429,7 @@ impl Length {
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::Absolute(Au(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 @@ -428,16 +446,15 @@ impl Length {
/// Get an absolute length from a px values.
#[inline]
pub fn from_px(px_value: CSSFloat) -> Length {
Length::NoCalc(NoCalcLength::Absolute(Au((px_value * AU_PER_PX) as i32)))
Length::NoCalc(NoCalcLength::from_px(px_value))
}

/// Extract inner length without a clone, replacing it with a 0 Au
///
/// Use when you need to move out of a length array without cloning
#[inline]
pub fn take(&mut self) -> Self {
let new = Length::Absolute(Au(0));
mem::replace(self, new)
mem::replace(self, Length::zero())
}
}

Expand Down Expand Up @@ -473,7 +490,7 @@ pub struct CalcProductNode {
#[derive(Clone, Debug)]
#[allow(missing_docs)]
pub enum CalcValueNode {
Length(Length),
Length(LengthInternal),
Angle(Angle),
Time(Time),
Percentage(CSSFloat),
Expand Down Expand Up @@ -566,7 +583,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) => {
Length::parse_dimension(value.value, unit).map(CalcValueNode::Length)
LengthInternal::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 @@ -907,9 +924,10 @@ impl ToCss for LengthOrPercentage {
}
}
impl LengthOrPercentage {
#[inline]
/// Returns a `zero` length.
pub fn zero() -> LengthOrPercentage {
LengthOrPercentage::Length(Length::Absolute(Au(0)))
LengthOrPercentage::Length(Length::zero())
}

fn parse_internal(input: &mut Parser, context: AllowedNumericType)
Expand All @@ -921,7 +939,7 @@ impl LengthOrPercentage {
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::Absolute(Au(0)))),
Ok(LengthOrPercentage::Length(Length::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 All @@ -941,8 +959,7 @@ impl LengthOrPercentage {
/// Use when you need to move out of a length array without cloning
#[inline]
pub fn take(&mut self) -> Self {
let new = LengthOrPercentage::Length(Length::Absolute(Au(0)));
mem::replace(self, new)
mem::replace(self, LengthOrPercentage::zero())
}
}

Expand Down Expand Up @@ -996,7 +1013,7 @@ impl LengthOrPercentageOrAuto {
Token::Percentage(ref value) if context.is_ok(value.unit_value) =>
Ok(LengthOrPercentageOrAuto::Percentage(Percentage(value.unit_value))),
Token::Number(ref value) if value.value == 0. =>
Ok(LengthOrPercentageOrAuto::Length(Length::Absolute(Au(0)))),
Ok(LengthOrPercentageOrAuto::Length(Length::zero())),
Token::Ident(ref value) if value.eq_ignore_ascii_case("auto") =>
Ok(LengthOrPercentageOrAuto::Auto),
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {
Expand Down Expand Up @@ -1063,7 +1080,7 @@ impl LengthOrPercentageOrNone {
Token::Percentage(ref value) if context.is_ok(value.unit_value) =>
Ok(LengthOrPercentageOrNone::Percentage(Percentage(value.unit_value))),
Token::Number(ref value) if value.value == 0. =>
Ok(LengthOrPercentageOrNone::Length(Length::Absolute(Au(0)))),
Ok(LengthOrPercentageOrNone::Length(Length::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(LengthOrPercentageOrNone::Calc(Box::new(calc)))
Expand Down Expand Up @@ -1146,7 +1163,7 @@ impl Parse for LengthOrPercentageOrAutoOrContent {
Token::Percentage(ref value) if context.is_ok(value.unit_value) =>
Ok(LengthOrPercentageOrAutoOrContent::Percentage(Percentage(value.unit_value))),
Token::Number(ref value) if value.value == 0. =>
Ok(LengthOrPercentageOrAutoOrContent::Length(Length::Absolute(Au(0)))),
Ok(LengthOrPercentageOrAutoOrContent::Length(Length::zero())),
Token::Ident(ref value) if value.eq_ignore_ascii_case("auto") =>
Ok(LengthOrPercentageOrAutoOrContent::Auto),
Token::Ident(ref value) if value.eq_ignore_ascii_case("content") =>
Expand Down
13 changes: 6 additions & 7 deletions components/style/values/specified/mod.rs
Expand Up @@ -198,7 +198,7 @@ impl NoViewportPercentage for BorderRadiusSize {}
impl BorderRadiusSize {
#[allow(missing_docs)]
pub fn zero() -> BorderRadiusSize {
let zero = LengthOrPercentage::Length(Length::Absolute(Au(0)));
let zero = LengthOrPercentage::Length(Length::default());
BorderRadiusSize(Size2D::new(zero.clone(), zero))
}

Expand Down Expand Up @@ -605,12 +605,11 @@ impl Shadow {
// disable_spread_and_inset is for filter: drop-shadow(...)
#[allow(missing_docs)]
pub fn parse(context: &ParserContext, input: &mut Parser, disable_spread_and_inset: bool) -> Result<Shadow, ()> {
use app_units::Au;
let length_count = if disable_spread_and_inset { 3 } else { 4 };
let mut lengths = [Length::Absolute(Au(0)),
Length::Absolute(Au(0)),
Length::Absolute(Au(0)),
Length::Absolute(Au(0))];
let mut lengths = [Length::default(),
Length::default(),
Length::default(),
Length::default()];
let mut lengths_parsed = false;
let mut color = None;
let mut inset = false;
Expand Down Expand Up @@ -662,7 +661,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::Absolute(Au(0)) } else { lengths[3].take() },
spread_radius: if disable_spread_and_inset { Length::default() } else { lengths[3].take() },
color: color,
inset: inset,
})
Expand Down

0 comments on commit 377a23d

Please sign in to comment.