Skip to content

Commit

Permalink
Change LengthOrPercentageOrAuto 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 590c957 commit bdb08b9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 42 deletions.
15 changes: 5 additions & 10 deletions components/script/dom/element.rs
Expand Up @@ -503,14 +503,12 @@ impl LayoutElementHelpers for LayoutJS<Element> {
};

if let Some(size) = size {
let value = specified::Length::NoCalc(
specified::NoCalcLength::ServoCharacterWidth(specified::CharacterWidth(size)));
let value = specified::NoCalcLength::ServoCharacterWidth(specified::CharacterWidth(size));
hints.push(from_declaration(
PropertyDeclaration::Width(DeclaredValue::Value(
specified::LengthOrPercentageOrAuto::Length(value)))));
}


let width = if let Some(this) = self.downcast::<HTMLIFrameElement>() {
this.get_width()
} else if let Some(this) = self.downcast::<HTMLImageElement>() {
Expand All @@ -536,7 +534,7 @@ impl LayoutElementHelpers for LayoutJS<Element> {
}
LengthOrPercentageOrAuto::Length(length) => {
let width_value = specified::LengthOrPercentageOrAuto::Length(
specified::Length::NoCalc(specified::NoCalcLength::Absolute(length)));
specified::NoCalcLength::Absolute(length));
hints.push(from_declaration(
PropertyDeclaration::Width(DeclaredValue::Value(width_value))));
}
Expand All @@ -561,7 +559,7 @@ impl LayoutElementHelpers for LayoutJS<Element> {
}
LengthOrPercentageOrAuto::Length(length) => {
let height_value = specified::LengthOrPercentageOrAuto::Length(
specified::Length::NoCalc(specified::NoCalcLength::Absolute(length)));
specified::NoCalcLength::Absolute(length));
hints.push(from_declaration(
PropertyDeclaration::Height(DeclaredValue::Value(height_value))));
}
Expand All @@ -583,14 +581,12 @@ 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::NoCalc(
specified::NoCalcLength::ServoCharacterWidth(specified::CharacterWidth(cols)));
let value = specified::NoCalcLength::ServoCharacterWidth(specified::CharacterWidth(cols));
hints.push(from_declaration(
PropertyDeclaration::Width(DeclaredValue::Value(
specified::LengthOrPercentageOrAuto::Length(value)))));
}


let rows = if let Some(this) = self.downcast::<HTMLTextAreaElement>() {
match this.get_rows() {
0 => None,
Expand All @@ -604,8 +600,7 @@ 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::NoCalc(
specified::NoCalcLength::FontRelative(specified::FontRelativeLength::Em(rows as CSSFloat)));
let value = specified::NoCalcLength::FontRelative(specified::FontRelativeLength::Em(rows as CSSFloat));
hints.push(from_declaration(
PropertyDeclaration::Height(DeclaredValue::Value(
specified::LengthOrPercentageOrAuto::Length(value)))));
Expand Down
6 changes: 3 additions & 3 deletions components/style/values/specified/length.rs
Expand Up @@ -989,7 +989,7 @@ impl Parse for LengthOrPercentage {
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)]
pub enum LengthOrPercentageOrAuto {
Length(Length),
Length(NoCalcLength),
Percentage(Percentage),
Auto,
Calc(Box<CalcLengthOrPercentage>),
Expand Down Expand Up @@ -1022,11 +1022,11 @@ impl LengthOrPercentageOrAuto {
{
match try!(input.next()) {
Token::Dimension(ref value, ref unit) if context.is_ok(value.value) =>
Length::parse_dimension(value.value, unit).map(LengthOrPercentageOrAuto::Length),
NoCalcLength::parse_dimension(value.value, unit).map(LengthOrPercentageOrAuto::Length),
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::zero())),
Ok(LengthOrPercentageOrAuto::Length(NoCalcLength::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
12 changes: 5 additions & 7 deletions components/style/viewport.rs
Expand Up @@ -25,7 +25,7 @@ use style_traits::ToCss;
use style_traits::viewport::{Orientation, UserZoom, ViewportConstraints, Zoom};
use stylesheets::{Stylesheet, Origin};
use values::computed::{Context, ToComputedValue};
use values::specified::{Length, NoCalcLength, LengthOrPercentageOrAuto, ViewportPercentageLength};
use values::specified::{NoCalcLength, LengthOrPercentageOrAuto, ViewportPercentageLength};

macro_rules! declare_viewport_descriptor {
( $( $variant_name: expr => $variant: ident($data: ident), )+ ) => {
Expand Down Expand Up @@ -150,16 +150,14 @@ impl FromMeta for ViewportLength {

Some(match value {
v if v.eq_ignore_ascii_case("device-width") =>
specified!(Length::NoCalc(
NoCalcLength::ViewportPercentage(ViewportPercentageLength::Vw(100.)))),
specified!(NoCalcLength::ViewportPercentage(ViewportPercentageLength::Vw(100.))),
v if v.eq_ignore_ascii_case("device-height") =>
specified!(Length::NoCalc(
NoCalcLength::ViewportPercentage(ViewportPercentageLength::Vh(100.)))),
specified!(NoCalcLength::ViewportPercentage(ViewportPercentageLength::Vh(100.))),
_ => {
match value.parse::<f32>() {
Ok(n) if n >= 0. => specified!(Length::from_px(n.max(1.).min(10000.))),
Ok(n) if n >= 0. => specified!(NoCalcLength::from_px(n.max(1.).min(10000.))),
Ok(_) => return None,
Err(_) => specified!(Length::from_px(1.))
Err(_) => specified!(NoCalcLength::from_px(1.))
}
}
})
Expand Down
40 changes: 20 additions & 20 deletions tests/unit/style/properties/serialization.rs
Expand Up @@ -19,15 +19,15 @@ fn property_declaration_block_should_serialize_correctly() {

let declarations = vec![
(PropertyDeclaration::Width(
DeclaredValue::Value(LengthOrPercentageOrAuto::Length(Length::from_px(70f32)))),
DeclaredValue::Value(LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(70f32)))),
Importance::Normal),

(PropertyDeclaration::MinHeight(
DeclaredValue::Value(LengthOrPercentage::Length(NoCalcLength::from_px(20f32)))),
Importance::Normal),

(PropertyDeclaration::Height(
DeclaredValue::Value(LengthOrPercentageOrAuto::Length(Length::from_px(20f32)))),
DeclaredValue::Value(LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(20f32)))),
Importance::Important),

(PropertyDeclaration::Display(
Expand Down Expand Up @@ -113,7 +113,7 @@ mod shorthand_serialization {
fn all_equal_properties_should_serialize_to_one_value() {
let mut properties = Vec::new();

let px_70 = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(Length::from_px(70f32)));
let px_70 = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(70f32)));
properties.push(PropertyDeclaration::MarginTop(px_70.clone()));
properties.push(PropertyDeclaration::MarginRight(px_70.clone()));
properties.push(PropertyDeclaration::MarginBottom(px_70.clone()));
Expand All @@ -127,8 +127,8 @@ mod shorthand_serialization {
fn equal_vertical_and_equal_horizontal_properties_should_serialize_to_two_value() {
let mut properties = Vec::new();

let vertical_px = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(Length::from_px(10f32)));
let horizontal_px = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(Length::from_px(5f32)));
let vertical_px = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(10f32)));
let horizontal_px = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(5f32)));

properties.push(PropertyDeclaration::MarginTop(vertical_px.clone()));
properties.push(PropertyDeclaration::MarginRight(horizontal_px.clone()));
Expand All @@ -143,9 +143,9 @@ mod shorthand_serialization {
fn different_vertical_and_equal_horizontal_properties_should_serialize_to_three_values() {
let mut properties = Vec::new();

let top_px = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(Length::from_px(8f32)));
let bottom_px = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(Length::from_px(10f32)));
let horizontal_px = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(Length::from_px(5f32)));
let top_px = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(8f32)));
let bottom_px = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(10f32)));
let horizontal_px = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(5f32)));

properties.push(PropertyDeclaration::MarginTop(top_px));
properties.push(PropertyDeclaration::MarginRight(horizontal_px.clone()));
Expand All @@ -160,10 +160,10 @@ mod shorthand_serialization {
fn different_properties_should_serialize_to_four_values() {
let mut properties = Vec::new();

let top_px = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(Length::from_px(8f32)));
let right_px = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(Length::from_px(12f32)));
let bottom_px = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(Length::from_px(10f32)));
let left_px = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(Length::from_px(14f32)));
let top_px = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(8f32)));
let right_px = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(12f32)));
let bottom_px = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(10f32)));
let left_px = DeclaredValue::Value(LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(14f32)));

properties.push(PropertyDeclaration::MarginTop(top_px));
properties.push(PropertyDeclaration::MarginRight(right_px));
Expand Down Expand Up @@ -738,8 +738,8 @@ mod shorthand_serialization {
let size = single_vec_variant_value!(size,
size::single_value::SpecifiedValue::Explicit(
size::single_value::ExplicitSize {
width: LengthOrPercentageOrAuto::Length(Length::from_px(70f32)),
height: LengthOrPercentageOrAuto::Length(Length::from_px(50f32))
width: LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(70f32)),
height: LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(50f32))
}
)
);
Expand Down Expand Up @@ -798,8 +798,8 @@ mod shorthand_serialization {
let size = single_vec_variant_value!(size,
size::single_value::SpecifiedValue::Explicit(
size::single_value::ExplicitSize {
width: LengthOrPercentageOrAuto::Length(Length::from_px(70f32)),
height: LengthOrPercentageOrAuto::Length(Length::from_px(50f32))
width: LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(70f32)),
height: LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(50f32))
}
)
);
Expand Down Expand Up @@ -934,8 +934,8 @@ mod shorthand_serialization {
let size = single_vec_variant_value!(size,
size::single_value::SpecifiedValue::Explicit(
size::single_value::ExplicitSize {
width: LengthOrPercentageOrAuto::Length(Length::from_px(70f32)),
height: LengthOrPercentageOrAuto::Length(Length::from_px(50f32))
width: LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(70f32)),
height: LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(50f32))
}
)
);
Expand Down Expand Up @@ -988,8 +988,8 @@ mod shorthand_serialization {
let size = single_vec_variant_value!(size,
size::single_value::SpecifiedValue::Explicit(
size::single_value::ExplicitSize {
width: LengthOrPercentageOrAuto::Length(Length::from_px(70f32)),
height: LengthOrPercentageOrAuto::Length(Length::from_px(50f32))
width: LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(70f32)),
height: LengthOrPercentageOrAuto::Length(NoCalcLength::from_px(50f32))
}
)
);
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/style/viewport.rs
Expand Up @@ -12,8 +12,8 @@ use style::error_reporting::ParseErrorReporter;
use style::media_queries::{Device, MediaType};
use style::parser::{ParserContext, ParserContextExtraData};
use style::stylesheets::{Stylesheet, Origin};
use style::values::specified::Length::{self, ViewportPercentage};
use style::values::specified::LengthOrPercentageOrAuto::{self, Auto};
use style::values::specified::NoCalcLength::{self, ViewportPercentage};
use style::values::specified::ViewportPercentageLength::Vw;
use style::viewport::*;
use style_traits::viewport::*;
Expand Down Expand Up @@ -80,7 +80,7 @@ macro_rules! assert_decl_len {

macro_rules! viewport_length {
($value:expr, px) => {
ViewportLength::Specified(LengthOrPercentageOrAuto::Length(Length::from_px($value)))
ViewportLength::Specified(LengthOrPercentageOrAuto::Length(NoCalcLength::from_px($value)))
};
($value:expr, vw) => {
ViewportLength::Specified(LengthOrPercentageOrAuto::Length(ViewportPercentage(Vw($value))))
Expand Down

0 comments on commit bdb08b9

Please sign in to comment.