Skip to content

Commit

Permalink
Bug 1496558 - Part 2: Move MozLength and MaxLength into generics r=em…
Browse files Browse the repository at this point in the history
…ilio

Move MozLength and MaxLength into generics, and drop the manual implementation
of ToComputedValue.

Depends on D8290

Differential Revision: https://phabricator.services.mozilla.com/D8291

--HG--
extra : moz-landing-system : lando
  • Loading branch information
BorisChiou committed Oct 16, 2018
1 parent 09a7c9a commit 8ad4fbf
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 121 deletions.
9 changes: 5 additions & 4 deletions servo/components/style/gecko/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ use std::cmp::max;
use values::{Auto, Either, None_, Normal};
use values::computed::{Angle, ExtremumLength, Length, LengthOrPercentage, LengthOrPercentageOrAuto};
use values::computed::{LengthOrPercentageOrNone, Number, NumberOrPercentage};
use values::computed::{MaxLength, MozLength, Percentage};
use values::computed::{MaxLength as ComputedMaxLength, MozLength as ComputedMozLength, Percentage};
use values::computed::{NonNegativeLength, NonNegativeLengthOrPercentage, NonNegativeNumber};
use values::computed::FlexBasis as ComputedFlexBasis;
use values::computed::basic_shape::ShapeRadius as ComputedShapeRadius;
use values::generics::{CounterStyleOrNone, NonNegative};
use values::generics::basic_shape::ShapeRadius;
use values::generics::box_::Perspective;
use values::generics::flex::FlexBasis;
use values::generics::length::{MaxLength, MozLength};
use values::generics::gecko::ScrollSnapPoint;
use values::generics::grid::{TrackBreadth, TrackKeyword};

Expand Down Expand Up @@ -74,7 +75,7 @@ impl GeckoStyleCoordConvertible for ComputedFlexBasis {
}

fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
if let Some(width) = MozLength::from_gecko_style_coord(coord) {
if let Some(width) = ComputedMozLength::from_gecko_style_coord(coord) {
return Some(FlexBasis::Width(width));
}

Expand Down Expand Up @@ -406,7 +407,7 @@ impl GeckoStyleCoordConvertible for ExtremumLength {
}
}

impl GeckoStyleCoordConvertible for MozLength {
impl GeckoStyleCoordConvertible for ComputedMozLength {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
match *self {
MozLength::LengthOrPercentageOrAuto(ref lopoa) => lopoa.to_gecko_style_coord(coord),
Expand All @@ -423,7 +424,7 @@ impl GeckoStyleCoordConvertible for MozLength {
}
}

impl GeckoStyleCoordConvertible for MaxLength {
impl GeckoStyleCoordConvertible for ComputedMaxLength {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
match *self {
MaxLength::LengthOrPercentageOrNone(ref lopon) => lopon.to_gecko_style_coord(coord),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use values::animated::effects::Filter as AnimatedFilter;
use values::computed::{Angle, CalcLengthOrPercentage};
use values::computed::{ClipRect, Context};
use values::computed::{Length, LengthOrPercentage, LengthOrPercentageOrAuto};
use values::computed::{LengthOrPercentageOrNone, MaxLength};
use values::computed::LengthOrPercentageOrNone;
use values::computed::{NonNegativeNumber, Number, NumberOrPercentage, Percentage};
use values::computed::length::NonNegativeLengthOrPercentage;
use values::computed::ToComputedValue;
Expand Down Expand Up @@ -888,11 +888,6 @@ impl ToAnimatedZero for LengthOrPercentageOrNone {
}
}

impl ToAnimatedZero for MaxLength {
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> { Err(()) }
}

impl ToAnimatedZero for FontWeight {
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> {
Expand Down
10 changes: 6 additions & 4 deletions servo/components/style/values/animated/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,9 @@ impl ToAnimatedValue for ComputedMaxLength {
#[inline]
fn from_animated_value(animated: Self::AnimatedValue) -> Self {
use values::computed::{Length, LengthOrPercentageOrNone, Percentage};
use values::generics::length::MaxLength as GenericMaxLength;
match animated {
ComputedMaxLength::LengthOrPercentageOrNone(lopn) => {
GenericMaxLength::LengthOrPercentageOrNone(lopn) => {
let result = match lopn {
LengthOrPercentageOrNone::Length(px) => {
LengthOrPercentageOrNone::Length(Length::new(px.px().max(0.)))
Expand All @@ -303,7 +304,7 @@ impl ToAnimatedValue for ComputedMaxLength {
},
_ => lopn,
};
ComputedMaxLength::LengthOrPercentageOrNone(result)
GenericMaxLength::LengthOrPercentageOrNone(result)
},
_ => animated,
}
Expand All @@ -321,8 +322,9 @@ impl ToAnimatedValue for ComputedMozLength {
#[inline]
fn from_animated_value(animated: Self::AnimatedValue) -> Self {
use values::computed::{Length, LengthOrPercentageOrAuto, Percentage};
use values::generics::length::MozLength as GenericMozLength;
match animated {
ComputedMozLength::LengthOrPercentageOrAuto(lopa) => {
GenericMozLength::LengthOrPercentageOrAuto(lopa) => {
let result = match lopa {
LengthOrPercentageOrAuto::Length(px) => {
LengthOrPercentageOrAuto::Length(Length::new(px.px().max(0.)))
Expand All @@ -332,7 +334,7 @@ impl ToAnimatedValue for ComputedMozLength {
},
_ => lopa,
};
ComputedMozLength::LengthOrPercentageOrAuto(result)
GenericMozLength::LengthOrPercentageOrAuto(result)
},
_ => animated,
}
Expand Down
86 changes: 9 additions & 77 deletions servo/components/style/values/computed/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::{Context, Number, Percentage, ToComputedValue};
use values::{specified, Auto, CSSFloat, Either, Normal};
use values::animated::{Animate, Procedure, ToAnimatedValue, ToAnimatedZero};
use values::distance::{ComputeSquaredDistance, SquaredDistance};
use values::generics::length::{MaxLength as GenericMaxLength, MozLength as GenericMozLength};
use values::generics::NonNegative;
use values::specified::length::{AbsoluteLength, FontBaseSize, FontRelativeLength};
use values::specified::length::ViewportPercentageLength;
Expand Down Expand Up @@ -949,102 +950,33 @@ pub type NonNegativeLengthOrPercentageOrNormal = Either<NonNegativeLengthOrPerce
/// block-size, and inline-size.
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq, SpecifiedValueInfo, ToCss)]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq, SpecifiedValueInfo,
ToComputedValue, ToCss)]
pub enum ExtremumLength {
MozMaxContent,
MozMinContent,
MozFitContent,
MozAvailable,
}

/// A value suitable for a `min-width`, `min-height`, `width` or `height`
/// property.
///
/// See values/specified/length.rs for more details.
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug, PartialEq, ToAnimatedZero, ToCss)]
pub enum MozLength {
LengthOrPercentageOrAuto(LengthOrPercentageOrAuto),
#[animation(error)]
ExtremumLength(ExtremumLength),
}
/// A computed value for `min-width`, `min-height`, `width` or `height` property.
pub type MozLength = GenericMozLength<LengthOrPercentageOrAuto>;

impl MozLength {
/// Returns the `auto` value.
#[inline]
pub fn auto() -> Self {
MozLength::LengthOrPercentageOrAuto(LengthOrPercentageOrAuto::Auto)
}
}

impl ToComputedValue for specified::MozLength {
type ComputedValue = MozLength;

#[inline]
fn to_computed_value(&self, context: &Context) -> MozLength {
match *self {
specified::MozLength::LengthOrPercentageOrAuto(ref lopoa) => {
MozLength::LengthOrPercentageOrAuto(lopoa.to_computed_value(context))
},
specified::MozLength::ExtremumLength(ext) => MozLength::ExtremumLength(ext),
}
}

#[inline]
fn from_computed_value(computed: &MozLength) -> Self {
match *computed {
MozLength::LengthOrPercentageOrAuto(ref lopoa) => {
specified::MozLength::LengthOrPercentageOrAuto(
specified::LengthOrPercentageOrAuto::from_computed_value(lopoa),
)
},
MozLength::ExtremumLength(ext) => specified::MozLength::ExtremumLength(ext),
}
GenericMozLength::LengthOrPercentageOrAuto(LengthOrPercentageOrAuto::Auto)
}
}

/// A value suitable for a `max-width` or `max-height` property.
/// See values/specified/length.rs for more details.
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug, PartialEq, ToCss)]
pub enum MaxLength {
LengthOrPercentageOrNone(LengthOrPercentageOrNone),
#[animation(error)]
ExtremumLength(ExtremumLength),
}
/// A computed value for `max-width` or `min-height` property.
pub type MaxLength = GenericMaxLength<LengthOrPercentageOrNone>;

impl MaxLength {
/// Returns the `none` value.
#[inline]
pub fn none() -> Self {
MaxLength::LengthOrPercentageOrNone(LengthOrPercentageOrNone::None)
}
}

impl ToComputedValue for specified::MaxLength {
type ComputedValue = MaxLength;

#[inline]
fn to_computed_value(&self, context: &Context) -> MaxLength {
match *self {
specified::MaxLength::LengthOrPercentageOrNone(ref lopon) => {
MaxLength::LengthOrPercentageOrNone(lopon.to_computed_value(context))
},
specified::MaxLength::ExtremumLength(ext) => MaxLength::ExtremumLength(ext),
}
}

#[inline]
fn from_computed_value(computed: &MaxLength) -> Self {
match *computed {
MaxLength::LengthOrPercentageOrNone(ref lopon) => {
specified::MaxLength::LengthOrPercentageOrNone(
specified::LengthOrPercentageOrNone::from_computed_value(&lopon),
)
},
MaxLength::ExtremumLength(ext) => specified::MaxLength::ExtremumLength(ext),
}
GenericMaxLength::LengthOrPercentageOrNone(LengthOrPercentageOrNone::None)
}
}
54 changes: 54 additions & 0 deletions servo/components/style/values/generics/length.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! Generic types for CSS values related to length.
use values::computed::ExtremumLength;

/// A generic value for the `width`, `height`, `min-width`, or `min-height` property.
///
/// Unlike `max-width` or `max-height` properties, a MozLength can be `auto`,
/// and cannot be `none`.
///
/// Note that it only accepts non-negative values.
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
PartialEq,
SpecifiedValueInfo,
ToAnimatedZero,
ToComputedValue,
ToCss,
)]
pub enum MozLength<LengthOrPercentageOrAuto> {
LengthOrPercentageOrAuto(LengthOrPercentageOrAuto),
#[animation(error)]
ExtremumLength(ExtremumLength),
}

/// A generic value for the `max-width` or `max-height` property.
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
PartialEq,
SpecifiedValueInfo,
ToAnimatedZero,
ToComputedValue,
ToCss
)]
pub enum MaxLength<LengthOrPercentageOrNone> {
LengthOrPercentageOrNone(LengthOrPercentageOrNone),
#[animation(error)]
ExtremumLength(ExtremumLength),
}
1 change: 1 addition & 0 deletions servo/components/style/values/generics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub mod font;
pub mod gecko;
pub mod grid;
pub mod image;
pub mod length;
pub mod position;
pub mod rect;
pub mod size;
Expand Down
40 changes: 13 additions & 27 deletions servo/components/style/values/specified/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use style_traits::values::specified::AllowedNumericType;
use super::{AllowQuirks, Number, Percentage, ToComputedValue};
use values::{Auto, CSSFloat, Either, Normal};
use values::computed::{self, CSSPixelLength, Context, ExtremumLength};
use values::generics::length::{MaxLength as GenericMaxLength, MozLength as GenericMozLength};
use values::generics::NonNegative;
use values::specified::calc::CalcNode;

Expand Down Expand Up @@ -1188,18 +1189,8 @@ impl LengthOrNumber {
}
}

/// A value suitable for a `min-width` or `min-height` property.
///
/// Unlike `max-width` or `max-height` properties, a MozLength can be `auto`,
/// and cannot be `none`.
///
/// Note that it only accepts non-negative values.
#[allow(missing_docs)]
#[derive(Clone, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToCss)]
pub enum MozLength {
LengthOrPercentageOrAuto(LengthOrPercentageOrAuto),
ExtremumLength(ExtremumLength),
}
/// A specified value for `min-width`, `min-height`, `width` or `height` property.
pub type MozLength = GenericMozLength<LengthOrPercentageOrAuto>;

impl Parse for MozLength {
fn parse<'i, 't>(
Expand All @@ -1219,7 +1210,7 @@ impl MozLength {
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let length = LengthOrPercentageOrAuto::parse_non_negative(context, input)?;
Ok(MozLength::LengthOrPercentageOrAuto(length))
Ok(GenericMozLength::LengthOrPercentageOrAuto(length))
}

/// Parses, with quirks.
Expand All @@ -1229,34 +1220,29 @@ impl MozLength {
allow_quirks: AllowQuirks,
) -> Result<Self, ParseError<'i>> {
if let Ok(l) = input.try(ExtremumLength::parse) {
return Ok(MozLength::ExtremumLength(l));
return Ok(GenericMozLength::ExtremumLength(l));
}

let length =
LengthOrPercentageOrAuto::parse_non_negative_quirky(context, input, allow_quirks)?;
Ok(MozLength::LengthOrPercentageOrAuto(length))
Ok(GenericMozLength::LengthOrPercentageOrAuto(length))
}

/// Returns `auto`.
#[inline]
pub fn auto() -> Self {
MozLength::LengthOrPercentageOrAuto(LengthOrPercentageOrAuto::auto())
GenericMozLength::LengthOrPercentageOrAuto(LengthOrPercentageOrAuto::auto())
}

/// Returns `0%`.
#[inline]
pub fn zero_percent() -> Self {
MozLength::LengthOrPercentageOrAuto(LengthOrPercentageOrAuto::zero_percent())
GenericMozLength::LengthOrPercentageOrAuto(LengthOrPercentageOrAuto::zero_percent())
}
}

/// A value suitable for a `max-width` or `max-height` property.
#[allow(missing_docs)]
#[derive(Clone, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToCss)]
pub enum MaxLength {
LengthOrPercentageOrNone(LengthOrPercentageOrNone),
ExtremumLength(ExtremumLength),
}
/// A specified value for `max-width` or `max-height` property.
pub type MaxLength = GenericMaxLength<LengthOrPercentageOrNone>;

impl Parse for MaxLength {
fn parse<'i, 't>(
Expand All @@ -1276,7 +1262,7 @@ impl MaxLength {
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let length = LengthOrPercentageOrNone::parse_non_negative(context, input)?;
Ok(MaxLength::LengthOrPercentageOrNone(length))
Ok(GenericMaxLength::LengthOrPercentageOrNone(length))
}

/// Parses, with quirks.
Expand All @@ -1286,11 +1272,11 @@ impl MaxLength {
allow_quirks: AllowQuirks,
) -> Result<Self, ParseError<'i>> {
if let Ok(l) = input.try(ExtremumLength::parse) {
return Ok(MaxLength::ExtremumLength(l));
return Ok(GenericMaxLength::ExtremumLength(l));
}

let length =
LengthOrPercentageOrNone::parse_non_negative_quirky(context, input, allow_quirks)?;
Ok(MaxLength::LengthOrPercentageOrNone(length))
Ok(GenericMaxLength::LengthOrPercentageOrNone(length))
}
}
Loading

0 comments on commit 8ad4fbf

Please sign in to comment.