Streamline parsing of NonNegativeLengthOrNumber a bit

This commit is contained in:
Anthony Ramine 2018-02-26 17:08:55 +01:00
parent f097dfad59
commit 4a98fa70bf
5 changed files with 20 additions and 31 deletions

View file

@ -4795,10 +4795,10 @@ fn static_assert() {
use values::Either; use values::Either;
match v { match v {
Either::Second(non_negative_number) => { Either::First(non_negative_number) => {
self.gecko.mTabSize.set_value(CoordDataValue::Factor(non_negative_number.0)); self.gecko.mTabSize.set_value(CoordDataValue::Factor(non_negative_number.0));
} }
Either::First(non_negative_length) => { Either::Second(non_negative_length) => {
self.gecko.mTabSize.set(non_negative_length); self.gecko.mTabSize.set(non_negative_length);
} }
} }
@ -4809,8 +4809,8 @@ fn static_assert() {
use values::Either; use values::Either;
match self.gecko.mTabSize.as_value() { match self.gecko.mTabSize.as_value() {
CoordDataValue::Coord(coord) => Either::First(Au(coord).into()), CoordDataValue::Coord(coord) => Either::Second(Au(coord).into()),
CoordDataValue::Factor(number) => Either::Second(From::from(number)), CoordDataValue::Factor(number) => Either::First(From::from(number)),
_ => unreachable!(), _ => unreachable!(),
} }
} }

View file

@ -511,7 +511,6 @@ ${helpers.single_keyword("resize",
${helpers.predefined_type("perspective", ${helpers.predefined_type("perspective",
"LengthOrNone", "LengthOrNone",
"Either::Second(None_)", "Either::Second(None_)",
"parse_non_negative_length",
gecko_ffi_name="mChildPerspective", gecko_ffi_name="mChildPerspective",
spec="https://drafts.csswg.org/css-transforms/#perspective", spec="https://drafts.csswg.org/css-transforms/#perspective",
extra_prefixes="moz webkit", extra_prefixes="moz webkit",

View file

@ -488,7 +488,7 @@ ${helpers.predefined_type(
${helpers.predefined_type( ${helpers.predefined_type(
"-moz-tab-size", "length::NonNegativeLengthOrNumber", "-moz-tab-size", "length::NonNegativeLengthOrNumber",
"::values::Either::Second(From::from(8.0))", "::values::Either::First(From::from(8.0))",
products="gecko", animation_value_type="::values::computed::length::NonNegativeLengthOrNumber", products="gecko", animation_value_type="::values::computed::length::NonNegativeLengthOrNumber",
spec="https://drafts.csswg.org/css-text-3/#tab-size-property")} spec="https://drafts.csswg.org/css-text-3/#tab-size-property")}

View file

@ -902,7 +902,7 @@ pub type NonNegativeLengthOrAuto = Either<NonNegativeLength, Auto>;
pub type NonNegativeLengthOrNormal = Either<NonNegativeLength, Normal>; pub type NonNegativeLengthOrNormal = Either<NonNegativeLength, Normal>;
/// Either a computed NonNegativeLength or a NonNegativeNumber value. /// Either a computed NonNegativeLength or a NonNegativeNumber value.
pub type NonNegativeLengthOrNumber = Either<NonNegativeLength, NonNegativeNumber>; pub type NonNegativeLengthOrNumber = Either<NonNegativeNumber, NonNegativeLength>;
/// A type for possible values for min- and max- flavors of width, height, /// A type for possible values for min- and max- flavors of width, height,
/// block-size, and inline-size. /// block-size, and inline-size.

View file

@ -640,21 +640,19 @@ impl Length {
} }
} }
impl<T: Parse> Either<Length, T> {
/// Parse a non-negative length
#[inline]
pub fn parse_non_negative_length<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Self, ParseError<'i>> {
if let Ok(v) = input.try(|input| T::parse(context, input)) {
return Ok(Either::Second(v));
}
Length::parse_internal(context, input, AllowedNumericType::NonNegative, AllowQuirks::No).map(Either::First)
}
}
/// A wrapper of Length, whose value must be >= 0. /// A wrapper of Length, whose value must be >= 0.
pub type NonNegativeLength = NonNegative<Length>; pub type NonNegativeLength = NonNegative<Length>;
impl Parse for NonNegativeLength {
#[inline]
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
Ok(NonNegative(Length::parse_non_negative(context, input)?))
}
}
impl From<NoCalcLength> for NonNegativeLength { impl From<NoCalcLength> for NonNegativeLength {
#[inline] #[inline]
fn from(len: NoCalcLength) -> Self { fn from(len: NoCalcLength) -> Self {
@ -669,17 +667,6 @@ impl From<Length> for NonNegativeLength {
} }
} }
impl<T: Parse> Parse for Either<NonNegativeLength, T> {
#[inline]
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
if let Ok(v) = input.try(|input| T::parse(context, input)) {
return Ok(Either::Second(v));
}
Length::parse_internal(context, input, AllowedNumericType::NonNegative, AllowQuirks::No)
.map(NonNegative::<Length>).map(Either::First)
}
}
impl NonNegativeLength { impl NonNegativeLength {
/// Returns a `zero` length. /// Returns a `zero` length.
#[inline] #[inline]
@ -701,7 +688,10 @@ pub type NonNegativeLengthOrNormal = Either<NonNegativeLength, Normal>;
pub type NonNegativeLengthOrAuto = Either<NonNegativeLength, Auto>; pub type NonNegativeLengthOrAuto = Either<NonNegativeLength, Auto>;
/// Either a NonNegativeLength or a NonNegativeNumber value. /// Either a NonNegativeLength or a NonNegativeNumber value.
pub type NonNegativeLengthOrNumber = Either<NonNegativeLength, NonNegativeNumber>; ///
/// The order is important, because `0` must be parsed as the number `0` and not
/// the length `0px`.
pub type NonNegativeLengthOrNumber = Either<NonNegativeNumber, NonNegativeLength>;
/// A length or a percentage value. /// A length or a percentage value.
#[allow(missing_docs)] #[allow(missing_docs)]