style: Use Rust sizes for flex-basis, width, height, and their min/max properties.

Really sorry for the size of the patch :(

Only intentional behavior change is in the uses of HasLengthAndPercentage(),
where it's easier to do the right thing. The checks that used to check for
(IsCalcUnit() && CalcHasPercentage()) are wrong since bug 957915.

Differential Revision: https://phabricator.services.mozilla.com/D19553
This commit is contained in:
Emilio Cobos Álvarez 2019-02-10 13:55:16 +01:00
parent e12c76982d
commit f7a59bf0ee
9 changed files with 70 additions and 106 deletions

View file

@ -79,6 +79,9 @@ include = [
"LengthPercentageOrAuto", "LengthPercentageOrAuto",
"Rect", "Rect",
"IntersectionObserverRootMargin", "IntersectionObserverRootMargin",
"Size",
"MaxSize",
"FlexBasis",
] ]
item_types = ["enums", "structs", "typedefs"] item_types = ["enums", "structs", "typedefs"]
@ -86,10 +89,12 @@ item_types = ["enums", "structs", "typedefs"]
"LengthPercentage" = """ "LengthPercentage" = """
// Defined in nsStyleCoord.h // Defined in nsStyleCoord.h
static constexpr inline StyleLengthPercentage Zero(); static constexpr inline StyleLengthPercentage Zero();
static inline StyleLengthPercentage FromAppUnits(nscoord);
inline bool HasPercent() const; inline bool HasPercent() const;
inline bool ConvertsToLength() const; inline bool ConvertsToLength() const;
inline nscoord ToLength() const; inline nscoord ToLength() const;
inline bool ConvertsToPercentage() const; inline bool ConvertsToPercentage() const;
inline bool HasLengthAndPercentage() const;
inline float ToPercentage() const; inline float ToPercentage() const;
inline CSSCoord LengthInCSSPixels() const; inline CSSCoord LengthInCSSPixels() const;
inline float Percentage() const; inline float Percentage() const;
@ -101,8 +106,41 @@ item_types = ["enums", "structs", "typedefs"]
"GenericLengthPercentageOrAuto" = """ "GenericLengthPercentageOrAuto" = """
inline const StyleLengthPercentage& AsLengthPercentage() const; inline const StyleLengthPercentage& AsLengthPercentage() const;
inline bool HasPercent() const;
inline bool ConvertsToLength() const; inline bool ConvertsToLength() const;
inline nscoord ToLength() const;
inline bool ConvertsToPercentage() const;
inline float ToPercentage() const;
inline bool HasPercent() const;
inline bool HasLengthAndPercentage() const;
"""
"GenericSize" = """
inline const StyleLengthPercentage& AsLengthPercentage() const;
inline StyleExtremumLength AsExtremumLength() const;
inline bool ConvertsToLength() const;
inline nscoord ToLength() const;
inline bool ConvertsToPercentage() const;
inline float ToPercentage() const;
inline bool HasPercent() const;
inline bool HasLengthAndPercentage() const;
inline bool BehavesLikeInitialValueOnBlockAxis() const;
"""
"GenericFlexBasis" = """
inline bool IsAuto() const;
inline const StyleSize& AsSize() const;
"""
"GenericMaxSize" = """
inline const StyleLengthPercentage& AsLengthPercentage() const;
inline StyleExtremumLength AsExtremumLength() const;
inline bool ConvertsToLength() const;
inline nscoord ToLength() const;
inline bool ConvertsToPercentage() const;
inline float ToPercentage() const;
inline bool HasPercent() const;
inline bool HasLengthAndPercentage() const;
inline bool BehavesLikeInitialValueOnBlockAxis() const;
""" """
"Rect" = """ "Rect" = """

View file

@ -7,22 +7,18 @@
//! Different kind of helpers to interact with Gecko values. //! Different kind of helpers to interact with Gecko values.
use crate::counter_style::{Symbol, Symbols}; use crate::counter_style::{Symbol, Symbols};
use crate::gecko_bindings::structs::{self, nsStyleCoord, CounterStylePtr}; use crate::gecko_bindings::structs::{nsStyleCoord, CounterStylePtr};
use crate::gecko_bindings::structs::{StyleGridTrackBreadth, StyleShapeRadius}; use crate::gecko_bindings::structs::{StyleGridTrackBreadth, StyleShapeRadius};
use crate::gecko_bindings::sugar::ns_style_coord::{CoordData, CoordDataMut, CoordDataValue}; use crate::gecko_bindings::sugar::ns_style_coord::{CoordData, CoordDataMut, CoordDataValue};
use crate::media_queries::Device; use crate::media_queries::Device;
use crate::values::computed::basic_shape::ShapeRadius as ComputedShapeRadius; use crate::values::computed::basic_shape::ShapeRadius as ComputedShapeRadius;
use crate::values::computed::FlexBasis as ComputedFlexBasis; use crate::values::computed::{Angle, Length, LengthPercentage};
use crate::values::computed::{Angle, ExtremumLength, Length, LengthPercentage}; use crate::values::computed::{Number, NumberOrPercentage, Percentage};
use crate::values::computed::{MaxSize as ComputedMaxSize, Size as ComputedSize};
use crate::values::computed::{NonNegativeLengthPercentage, Percentage};
use crate::values::computed::{Number, NumberOrPercentage};
use crate::values::generics::basic_shape::ShapeRadius; use crate::values::generics::basic_shape::ShapeRadius;
use crate::values::generics::box_::Perspective; use crate::values::generics::box_::Perspective;
use crate::values::generics::flex::FlexBasis;
use crate::values::generics::gecko::ScrollSnapPoint; use crate::values::generics::gecko::ScrollSnapPoint;
use crate::values::generics::grid::{TrackBreadth, TrackKeyword}; use crate::values::generics::grid::{TrackBreadth, TrackKeyword};
use crate::values::generics::length::{LengthPercentageOrAuto, MaxSize, Size}; use crate::values::generics::length::LengthPercentageOrAuto;
use crate::values::generics::{CounterStyleOrNone, NonNegative}; use crate::values::generics::{CounterStyleOrNone, NonNegative};
use crate::values::{Auto, Either, None_, Normal}; use crate::values::{Auto, Either, None_, Normal};
use crate::Atom; use crate::Atom;
@ -80,29 +76,6 @@ where
} }
} }
impl GeckoStyleCoordConvertible for ComputedFlexBasis {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
match *self {
FlexBasis::Content => coord.set_value(CoordDataValue::Enumerated(
structs::NS_STYLE_FLEX_BASIS_CONTENT,
)),
FlexBasis::Width(ref w) => w.to_gecko_style_coord(coord),
}
}
fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
if let Some(width) = ComputedSize::from_gecko_style_coord(coord) {
return Some(FlexBasis::Width(width));
}
if let CoordDataValue::Enumerated(structs::NS_STYLE_FLEX_BASIS_CONTENT) = coord.as_value() {
return Some(FlexBasis::Content);
}
None
}
}
impl GeckoStyleCoordConvertible for Number { impl GeckoStyleCoordConvertible for Number {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) { fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
coord.set_value(CoordDataValue::Factor(*self)); coord.set_value(CoordDataValue::Factor(*self));
@ -336,60 +309,6 @@ impl GeckoStyleCoordConvertible for Normal {
} }
} }
impl GeckoStyleCoordConvertible for ExtremumLength {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
coord.set_value(CoordDataValue::Enumerated(*self as u32));
}
fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
use num_traits::FromPrimitive;
match coord.as_value() {
CoordDataValue::Enumerated(v) => ExtremumLength::from_u32(v),
_ => None,
}
}
}
impl GeckoStyleCoordConvertible for ComputedSize {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
match *self {
Size::LengthPercentage(ref lpoa) => lpoa.to_gecko_style_coord(coord),
Size::Auto => coord.set_value(CoordDataValue::Auto),
Size::ExtremumLength(ref e) => e.to_gecko_style_coord(coord),
}
}
fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
if let CoordDataValue::Auto = coord.as_value() {
return Some(Size::Auto);
}
if let Some(lp) = NonNegativeLengthPercentage::from_gecko_style_coord(coord) {
return Some(Size::LengthPercentage(lp));
}
ExtremumLength::from_gecko_style_coord(coord).map(Size::ExtremumLength)
}
}
impl GeckoStyleCoordConvertible for ComputedMaxSize {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
match *self {
MaxSize::LengthPercentage(ref lpon) => lpon.to_gecko_style_coord(coord),
MaxSize::None => coord.set_value(CoordDataValue::None),
MaxSize::ExtremumLength(ref e) => e.to_gecko_style_coord(coord),
}
}
fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
if let CoordDataValue::None = coord.as_value() {
return Some(MaxSize::None);
}
if let Some(lp) = NonNegativeLengthPercentage::from_gecko_style_coord(coord) {
return Some(MaxSize::LengthPercentage(lp));
}
ExtremumLength::from_gecko_style_coord(coord).map(MaxSize::ExtremumLength)
}
}
impl GeckoStyleCoordConvertible for ScrollSnapPoint<LengthPercentage> { impl GeckoStyleCoordConvertible for ScrollSnapPoint<LengthPercentage> {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) { fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
match self.repeated() { match self.repeated() {

View file

@ -1385,13 +1385,13 @@ impl Clone for ${style_struct.gecko_struct_name} {
"length::NonNegativeLengthOrAuto": impl_style_coord, "length::NonNegativeLengthOrAuto": impl_style_coord,
"length::NonNegativeLengthPercentageOrNormal": impl_style_coord, "length::NonNegativeLengthPercentageOrNormal": impl_style_coord,
"FillRule": impl_simple, "FillRule": impl_simple,
"FlexBasis": impl_style_coord, "FlexBasis": impl_simple,
"Length": impl_absolute_length, "Length": impl_absolute_length,
"LengthOrNormal": impl_style_coord, "LengthOrNormal": impl_style_coord,
"LengthPercentage": impl_simple, "LengthPercentage": impl_simple,
"LengthPercentageOrAuto": impl_style_coord, "LengthPercentageOrAuto": impl_style_coord,
"MaxSize": impl_style_coord, "MaxSize": impl_simple,
"Size": impl_style_coord, "Size": impl_simple,
"MozScriptMinSize": impl_absolute_length, "MozScriptMinSize": impl_absolute_length,
"MozScriptSizeMultiplier": impl_simple, "MozScriptSizeMultiplier": impl_simple,
"NonNegativeLengthPercentage": impl_simple, "NonNegativeLengthPercentage": impl_simple,

View file

@ -225,7 +225,7 @@ ${helpers.predefined_type(
extra_prefixes="webkit", extra_prefixes="webkit",
animation_value_type="ComputedValue", animation_value_type="ComputedValue",
spec="https://drafts.csswg.org/css-flexbox/#order-property", spec="https://drafts.csswg.org/css-flexbox/#order-property",
servo_restyle_damage = "reflow", servo_restyle_damage="reflow",
)} )}
${helpers.predefined_type( ${helpers.predefined_type(
@ -235,7 +235,8 @@ ${helpers.predefined_type(
spec="https://drafts.csswg.org/css-flexbox/#flex-basis-property", spec="https://drafts.csswg.org/css-flexbox/#flex-basis-property",
extra_prefixes="webkit", extra_prefixes="webkit",
animation_value_type="FlexBasis", animation_value_type="FlexBasis",
servo_restyle_damage = "reflow", servo_restyle_damage="reflow",
boxed=True,
)} )}
% for (size, logical) in ALL_SIZES: % for (size, logical) in ALL_SIZES:

View file

@ -14,6 +14,6 @@ impl FlexBasis {
/// `auto` /// `auto`
#[inline] #[inline]
pub fn auto() -> Self { pub fn auto() -> Self {
GenericFlexBasis::Width(Size::auto()) GenericFlexBasis::Size(Size::auto())
} }
} }

View file

@ -19,9 +19,12 @@
ToComputedValue, ToComputedValue,
ToCss, ToCss,
)] )]
pub enum FlexBasis<Width> { #[repr(C)]
pub enum GenericFlexBasis<S> {
/// `content` /// `content`
Content, Content,
/// `<width>` /// `<width>`
Width(Width), Size(S),
} }
pub use self::GenericFlexBasis as FlexBasis;

View file

@ -192,10 +192,7 @@ impl<L> TrackBreadth<L> {
/// <https://drafts.csswg.org/css-grid/#typedef-fixed-breadth> /// <https://drafts.csswg.org/css-grid/#typedef-fixed-breadth>
#[inline] #[inline]
pub fn is_fixed(&self) -> bool { pub fn is_fixed(&self) -> bool {
match *self { matches!(*self, TrackBreadth::Breadth(..))
TrackBreadth::Breadth(ref _lp) => true,
_ => false,
}
} }
} }

View file

@ -96,14 +96,17 @@ impl<LengthPercentage: Parse> Parse for LengthPercentageOrAuto<LengthPercentage>
ToComputedValue, ToComputedValue,
ToCss, ToCss,
)] )]
pub enum Size<LengthPercentage> { #[repr(C, u8)]
LengthPercentage(LengthPercentage), pub enum GenericSize<LengthPercent> {
LengthPercentage(LengthPercent),
Auto, Auto,
#[cfg(feature = "gecko")] #[cfg(feature = "gecko")]
#[animation(error)] #[animation(error)]
ExtremumLength(ExtremumLength), ExtremumLength(ExtremumLength),
} }
pub use self::GenericSize as Size;
impl<LengthPercentage> Size<LengthPercentage> { impl<LengthPercentage> Size<LengthPercentage> {
/// `auto` value. /// `auto` value.
#[inline] #[inline]
@ -134,14 +137,17 @@ impl<LengthPercentage> Size<LengthPercentage> {
ToComputedValue, ToComputedValue,
ToCss, ToCss,
)] )]
pub enum MaxSize<LengthPercentage> { #[repr(C, u8)]
LengthPercentage(LengthPercentage), pub enum GenericMaxSize<LengthPercent> {
LengthPercentage(LengthPercent),
None, None,
#[cfg(feature = "gecko")] #[cfg(feature = "gecko")]
#[animation(error)] #[animation(error)]
ExtremumLength(ExtremumLength), ExtremumLength(ExtremumLength),
} }
pub use self::GenericMaxSize as MaxSize;
impl<LengthPercentage> MaxSize<LengthPercentage> { impl<LengthPercentage> MaxSize<LengthPercentage> {
/// `none` value. /// `none` value.
#[inline] #[inline]

View file

@ -18,8 +18,8 @@ impl Parse for FlexBasis {
context: &ParserContext, context: &ParserContext,
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> { ) -> Result<Self, ParseError<'i>> {
if let Ok(width) = input.try(|i| Size::parse(context, i)) { if let Ok(size) = input.try(|i| Size::parse(context, i)) {
return Ok(GenericFlexBasis::Width(width)); return Ok(GenericFlexBasis::Size(size));
} }
try_match_ident_ignore_ascii_case! { input, try_match_ident_ignore_ascii_case! { input,
"content" => Ok(GenericFlexBasis::Content), "content" => Ok(GenericFlexBasis::Content),
@ -31,12 +31,12 @@ impl FlexBasis {
/// `auto` /// `auto`
#[inline] #[inline]
pub fn auto() -> Self { pub fn auto() -> Self {
GenericFlexBasis::Width(Size::auto()) GenericFlexBasis::Size(Size::auto())
} }
/// `0%` /// `0%`
#[inline] #[inline]
pub fn zero_percent() -> Self { pub fn zero_percent() -> Self {
GenericFlexBasis::Width(Size::zero_percent()) GenericFlexBasis::Size(Size::zero_percent())
} }
} }