Skip to content

Commit

Permalink
Bug 1374233 - Part 7: Add NonNegativeLengthOrPercentage.
Browse files Browse the repository at this point in the history
For padding-{*} and grid-{*}-gap.

MozReview-Commit-ID: 81G0b1k6JnD
  • Loading branch information
BorisChiou committed Aug 4, 2017
1 parent 9d69cb2 commit e72a0f1
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 20 deletions.
11 changes: 11 additions & 0 deletions components/style/gecko/values.rs
Expand Up @@ -19,6 +19,7 @@ use values::{Auto, Either, ExtremumLength, None_, Normal};
use values::computed::{Angle, LengthOrPercentage, LengthOrPercentageOrAuto};
use values::computed::{LengthOrPercentageOrNone, Number, NumberOrPercentage, NonNegativeAu};
use values::computed::{MaxLength, MozLength, Percentage};
use values::computed::NonNegativeLengthOrPercentage;
use values::computed::basic_shape::ShapeRadius as ComputedShapeRadius;
use values::generics::{CounterStyleOrNone, NonNegative};
use values::generics::basic_shape::ShapeRadius;
Expand Down Expand Up @@ -121,6 +122,16 @@ impl GeckoStyleCoordConvertible for LengthOrPercentage {
}
}

impl GeckoStyleCoordConvertible for NonNegativeLengthOrPercentage {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
self.0.to_gecko_style_coord(coord);
}

fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
LengthOrPercentage::from_gecko_style_coord(coord).map(NonNegative::<LengthOrPercentage>)
}
}

impl GeckoStyleCoordConvertible for Au {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
coord.set_value(CoordDataValue::Coord(self.0));
Expand Down
1 change: 1 addition & 0 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -1101,6 +1101,7 @@ impl Clone for ${style_struct.gecko_struct_name} {
"LengthOrNormal": impl_style_coord,
"MaxLength": impl_style_coord,
"MozLength": impl_style_coord,
"NonNegativeLengthOrPercentage": impl_style_coord,
"NonNegativeNumber": impl_simple,
"Number": impl_simple,
"Integer": impl_simple,
Expand Down
Expand Up @@ -46,6 +46,7 @@ use values::computed::{CalcLengthOrPercentage, Color, Context, ComputedValueAsSp
use values::computed::{LengthOrPercentage, MaxLength, MozLength, Percentage, ToComputedValue};
use values::computed::{NonNegativeAu, NonNegativeNumber, PositiveIntegerOrAuto};
use values::computed::length::{NonNegativeLengthOrAuto, NonNegativeLengthOrNormal};
use values::computed::length::NonNegativeLengthOrPercentage;
use values::generics::{GreaterThanOrEqualToOne, NonNegative};
use values::generics::border::BorderCornerRadius as GenericBorderCornerRadius;
use values::generics::effects::Filter;
Expand Down
7 changes: 3 additions & 4 deletions components/style/properties/longhand/padding.mako.rs
Expand Up @@ -14,11 +14,10 @@
if side[1]:
spec = "https://drafts.csswg.org/css-logical-props/#propdef-padding-%s" % side[1]
%>
${helpers.predefined_type("padding-%s" % side[0], "LengthOrPercentage",
"computed::LengthOrPercentage::Length(Au(0))",
"parse_non_negative",
${helpers.predefined_type("padding-%s" % side[0], "NonNegativeLengthOrPercentage",
"computed::NonNegativeLengthOrPercentage::zero()",
alias=maybe_moz_logical_alias(product, side, "-moz-padding-%s"),
animation_value_type="ComputedValue",
animation_value_type="NonNegativeLengthOrPercentage",
logical = side[1],
spec = spec,
flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_PLACEHOLDER",
Expand Down
7 changes: 3 additions & 4 deletions components/style/properties/longhand/position.mako.rs
Expand Up @@ -249,11 +249,10 @@ ${helpers.predefined_type("object-position",

% for kind in ["row", "column"]:
${helpers.predefined_type("grid-%s-gap" % kind,
"LengthOrPercentage",
"computed::LengthOrPercentage::Length(Au(0))",
"parse_non_negative",
"NonNegativeLengthOrPercentage",
"computed::NonNegativeLengthOrPercentage::zero()",
spec="https://drafts.csswg.org/css-grid/#propdef-grid-%s-gap" % kind,
animation_value_type="ComputedValue",
animation_value_type="NonNegativeLengthOrPercentage",
products="gecko")}

% for range in ["start", "end"]:
Expand Down
8 changes: 4 additions & 4 deletions components/style/properties/properties.mako.rs
Expand Up @@ -2192,10 +2192,10 @@ impl ComputedValuesInner {
pub fn logical_padding(&self) -> LogicalMargin<computed::LengthOrPercentage> {
let padding_style = self.get_padding();
LogicalMargin::from_physical(self.writing_mode, SideOffsets2D::new(
padding_style.padding_top,
padding_style.padding_right,
padding_style.padding_bottom,
padding_style.padding_left,
padding_style.padding_top.0,
padding_style.padding_right.0,
padding_style.padding_bottom.0,
padding_style.padding_left.0,
))
}

Expand Down
2 changes: 1 addition & 1 deletion components/style/properties/shorthand/padding.mako.rs
Expand Up @@ -4,6 +4,6 @@

<%namespace name="helpers" file="/helpers.mako.rs" />
${helpers.four_sides_shorthand("padding", "padding-%s", "specified::LengthOrPercentage::parse_non_negative",
${helpers.four_sides_shorthand("padding", "padding-%s", "specified::NonNegativeLengthOrPercentage::parse",
spec="https://drafts.csswg.org/css-box-3/#propdef-padding",
allow_quirks=True)}
10 changes: 5 additions & 5 deletions components/style/properties/shorthand/position.mako.rs
Expand Up @@ -459,7 +459,7 @@
use properties::longhands::grid_auto_flow::computed_value::{AutoFlow, T as SpecifiedAutoFlow};
use values::{Either, None_};
use values::generics::grid::{GridTemplateComponent, TrackListType};
use values::specified::{GenericGridTemplateComponent, LengthOrPercentage, TrackSize};
use values::specified::{GenericGridTemplateComponent, NonNegativeLengthOrPercentage, TrackSize};

pub fn parse_value<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Longhands, ParseError<'i>> {
Expand Down Expand Up @@ -520,8 +520,8 @@
grid_auto_columns: auto_cols,
grid_auto_flow: flow,
// This shorthand also resets grid gap
grid_row_gap: LengthOrPercentage::zero(),
grid_column_gap: LengthOrPercentage::zero(),
grid_row_gap: NonNegativeLengthOrPercentage::zero(),
grid_column_gap: NonNegativeLengthOrPercentage::zero(),
})
}

Expand All @@ -539,8 +539,8 @@
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
// `grid` shorthand resets these properties. If they are not zero, that means they
// are changed by longhands and in that case we should fail serializing `grid`.
if *self.grid_row_gap != LengthOrPercentage::zero() ||
*self.grid_column_gap != LengthOrPercentage::zero() {
if *self.grid_row_gap != NonNegativeLengthOrPercentage::zero() ||
*self.grid_column_gap != NonNegativeLengthOrPercentage::zero() {
return Ok(());
}

Expand Down
22 changes: 22 additions & 0 deletions components/style/values/animated/mod.rs
Expand Up @@ -13,6 +13,7 @@ use std::cmp::max;
use values::computed::Angle as ComputedAngle;
use values::computed::GreaterThanOrEqualToOneNumber as ComputedGreaterThanOrEqualToOneNumber;
use values::computed::NonNegativeAu;
use values::computed::NonNegativeLengthOrPercentage as ComputedNonNegativeLengthOrPercentage;
use values::computed::NonNegativeNumber as ComputedNonNegativeNumber;
use values::computed::PositiveInteger as ComputedPositiveInteger;
use values::specified::url::SpecifiedUrl;
Expand Down Expand Up @@ -149,6 +150,27 @@ impl ToAnimatedValue for ComputedPositiveInteger {
}
}

impl ToAnimatedValue for ComputedNonNegativeLengthOrPercentage {
type AnimatedValue = Self;

#[inline]
fn to_animated_value(self) -> Self {
self
}

#[inline]
fn from_animated_value(animated: Self::AnimatedValue) -> Self {
use values::computed::{LengthOrPercentage, Percentage};
match animated.0 {
LengthOrPercentage::Length(au) => LengthOrPercentage::Length(max(au, Au(0))).into(),
LengthOrPercentage::Percentage(percentage) => {
LengthOrPercentage::Percentage(Percentage(percentage.0.max(0.))).into()
},
_ => animated
}
}
}

/// Returns a value similar to `self` that represents zero.
pub trait ToAnimatedZero: Sized {
/// Returns a value that, when added with an underlying value, will produce the underlying
Expand Down
31 changes: 31 additions & 0 deletions components/style/values/computed/length.rs
Expand Up @@ -12,6 +12,7 @@ use style_traits::values::specified::AllowedLengthType;
use super::{Number, ToComputedValue, Context};
use values::{Auto, CSSFloat, Either, ExtremumLength, None_, Normal, specified};
use values::computed::{NonNegativeAu, NonNegativeNumber};
use values::generics::NonNegative;
use values::specified::length::{AbsoluteLength, FontBaseSize, FontRelativeLength};
use values::specified::length::ViewportPercentageLength;

Expand Down Expand Up @@ -551,6 +552,36 @@ impl ToComputedValue for specified::LengthOrPercentageOrNone {
}
}

/// A wrapper of LengthOrPercentage, whose value must be >= 0.
pub type NonNegativeLengthOrPercentage = NonNegative<LengthOrPercentage>;

impl From<LengthOrPercentage> for NonNegativeLengthOrPercentage {
#[inline]
fn from(lop: LengthOrPercentage) -> Self {
NonNegative::<LengthOrPercentage>(lop)
}
}

impl NonNegativeLengthOrPercentage {
/// Get zero value.
#[inline]
pub fn zero() -> Self {
NonNegative::<LengthOrPercentage>(LengthOrPercentage::zero())
}

/// Returns true if the computed value is absolute 0 or 0%.
#[inline]
pub fn is_definitely_zero(&self) -> bool {
self.0.is_definitely_zero()
}

/// Returns the used value.
#[inline]
pub fn to_used_value(&self, containing_length: Au) -> Au {
self.0.to_used_value(containing_length)
}
}

/// A computed `<length>` value.
pub type Length = Au;

Expand Down
1 change: 1 addition & 0 deletions components/style/values/computed/mod.rs
Expand Up @@ -44,6 +44,7 @@ pub use super::generics::grid::GridLine;
pub use super::specified::url::SpecifiedUrl;
pub use self::length::{CalcLengthOrPercentage, Length, LengthOrNone, LengthOrNumber, LengthOrPercentage};
pub use self::length::{LengthOrPercentageOrAuto, LengthOrPercentageOrNone, MaxLength, MozLength, Percentage};
pub use self::length::NonNegativeLengthOrPercentage;
pub use self::position::Position;
pub use self::svg::{SVGLength, SVGOpacity, SVGPaint, SVGPaintKind, SVGStrokeDashArray};
pub use self::text::{InitialLetter, LetterSpacing, LineHeight, WordSpacing};
Expand Down
35 changes: 35 additions & 0 deletions components/style/values/specified/length.rs
Expand Up @@ -1224,6 +1224,41 @@ impl Parse for LengthOrPercentageOrNone {
}
}

/// A wrapper of LengthOrPercentage, whose value must be >= 0.
pub type NonNegativeLengthOrPercentage = NonNegative<LengthOrPercentage>;

impl From<NoCalcLength> for NonNegativeLengthOrPercentage {
#[inline]
fn from(len: NoCalcLength) -> Self {
NonNegative::<LengthOrPercentage>(LengthOrPercentage::from(len))
}
}

impl Parse for NonNegativeLengthOrPercentage {
#[inline]
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
LengthOrPercentage::parse_non_negative(context, input).map(NonNegative::<LengthOrPercentage>)
}
}

impl NonNegativeLengthOrPercentage {
#[inline]
/// Returns a `zero` length.
pub fn zero() -> Self {
NonNegative::<LengthOrPercentage>(LengthOrPercentage::zero())
}

/// Parses a length or a percentage, allowing the unitless length quirk.
/// https://quirks.spec.whatwg.org/#the-unitless-length-quirk
#[inline]
pub fn parse_quirky<'i, 't>(context: &ParserContext,
input: &mut Parser<'i, 't>,
allow_quirks: AllowQuirks) -> Result<Self, ParseError<'i>> {
LengthOrPercentage::parse_non_negative_quirky(context, input, allow_quirks)
.map(NonNegative::<LengthOrPercentage>)
}
}

/// Either a `<length>` or the `none` keyword.
pub type LengthOrNone = Either<Length, None_>;

Expand Down
1 change: 1 addition & 0 deletions components/style/values/specified/mod.rs
Expand Up @@ -42,6 +42,7 @@ pub use self::length::{FontRelativeLength, Length, LengthOrNone, LengthOrNumber}
pub use self::length::{LengthOrPercentage, LengthOrPercentageOrAuto};
pub use self::length::{LengthOrPercentageOrNone, MaxLength, MozLength};
pub use self::length::{NoCalcLength, Percentage, ViewportPercentageLength};
pub use self::length::NonNegativeLengthOrPercentage;
pub use self::rect::LengthOrNumberRect;
pub use self::position::{Position, PositionComponent};
pub use self::svg::{SVGLength, SVGOpacity, SVGPaint, SVGPaintKind, SVGStrokeDashArray};
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/style/properties/serialization.rs
Expand Up @@ -268,10 +268,12 @@ mod shorthand_serialization {

#[test]
fn padding_should_serialize_correctly() {
use style::values::specified::NonNegativeLengthOrPercentage;

let mut properties = Vec::new();

let px_10 = LengthOrPercentage::Length(NoCalcLength::from_px(10f32));
let px_15 = LengthOrPercentage::Length(NoCalcLength::from_px(15f32));
let px_10: NonNegativeLengthOrPercentage = NoCalcLength::from_px(10f32).into();
let px_15: NonNegativeLengthOrPercentage = NoCalcLength::from_px(15f32).into();
properties.push(PropertyDeclaration::PaddingTop(px_10.clone()));
properties.push(PropertyDeclaration::PaddingRight(px_15.clone()));
properties.push(PropertyDeclaration::PaddingBottom(px_10));
Expand Down

0 comments on commit e72a0f1

Please sign in to comment.