style: Let aspect-ratio (css-sizing-4) support 'auto | <ratio>'.

In order to test its parsing and serialization, we expose it but protect
it behind a pref.

Besides, I would like to drop layout.css.aspect-ratio-number.enabled in
the next patch because the spec has been updated. It seems we don't have
to keep this pref and we should always use Number.

Differential Revision: https://phabricator.services.mozilla.com/D74955
This commit is contained in:
Boris Chiou 2020-05-21 06:45:10 +00:00 committed by Emilio Cobos Álvarez
parent bd23e05c47
commit fc9321bb23
8 changed files with 185 additions and 18 deletions

View file

@ -18,9 +18,8 @@ use crate::context::QuirksMode;
use crate::parser::{Parse, ParserContext};
use crate::values::serialize_atom_identifier;
use crate::values::specified::calc::CalcNode;
use crate::{Atom, Namespace, Prefix, Zero};
use crate::{Atom, Namespace, One, Prefix, Zero};
use cssparser::{Parser, Token};
use num_traits::One;
use std::f32;
use std::fmt::{self, Write};
use std::ops::Add;
@ -72,6 +71,7 @@ pub use self::list::Quotes;
pub use self::motion::{OffsetPath, OffsetRotate};
pub use self::outline::OutlineStyle;
pub use self::percentage::Percentage;
pub use self::position::AspectRatio;
pub use self::position::{GridAutoFlow, GridTemplateAreas, MasonryAutoFlow, Position, PositionOrAuto};
pub use self::position::{PositionComponent, ZIndex};
pub use self::rect::NonNegativeLengthOrNumberRect;
@ -375,6 +375,18 @@ impl Parse for NonNegativeNumber {
}
}
impl One for NonNegativeNumber {
#[inline]
fn one() -> Self {
NonNegativeNumber::new(1.0)
}
#[inline]
fn is_one(&self) -> bool {
self.0.get() == 1.0
}
}
impl NonNegativeNumber {
/// Returns a new non-negative number with the value `val`.
pub fn new(val: CSSFloat) -> Self {
@ -537,7 +549,7 @@ impl Zero for Integer {
}
}
impl One for Integer {
impl num_traits::One for Integer {
#[inline]
fn one() -> Self {
Self::new(1)

View file

@ -12,13 +12,14 @@ use crate::selector_map::PrecomputedHashMap;
use crate::str::HTML_SPACE_CHARACTERS;
use crate::values::computed::LengthPercentage as ComputedLengthPercentage;
use crate::values::computed::{Context, Percentage, ToComputedValue};
use crate::values::generics::position::AspectRatio as GenericAspectRatio;
use crate::values::generics::position::Position as GenericPosition;
use crate::values::generics::position::PositionComponent as GenericPositionComponent;
use crate::values::generics::position::PositionOrAuto as GenericPositionOrAuto;
use crate::values::generics::position::Ratio as GenericRatio;
use crate::values::generics::position::ZIndex as GenericZIndex;
use crate::values::specified::{AllowQuirks, Integer, LengthPercentage};
use crate::Atom;
use crate::Zero;
use crate::values::specified::{AllowQuirks, Integer, LengthPercentage, NonNegativeNumber};
use crate::{Atom, One, Zero};
use cssparser::Parser;
use selectors::parser::SelectorParseErrorKind;
use servo_arc::Arc;
@ -877,3 +878,46 @@ impl GridTemplateAreas {
/// A specified value for the `z-index` property.
pub type ZIndex = GenericZIndex<Integer>;
/// A specified value for the `aspect-ratio` property.
pub type AspectRatio = GenericAspectRatio<NonNegativeNumber>;
// FIXME: Add field_bound for parse custom derive, so we can drop this.
impl Parse for AspectRatio {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
if input
.try(|input| input.expect_ident_matching("auto"))
.is_ok()
{
return Ok(AspectRatio::Auto);
}
GenericRatio::parse(context, input).map(AspectRatio::Ratio)
}
}
// https://drafts.csswg.org/css-values-4/#ratios
impl Parse for GenericRatio<NonNegativeNumber> {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let a = NonNegativeNumber::parse(context, input)?;
let b = match input.try_parse(|input| input.expect_delim('/')) {
Ok(()) => NonNegativeNumber::parse(context, input)?,
_ => One::one(),
};
// The computed value of a <ratio> is the pair of numbers provided, unless
// both numbers are zero, in which case the computed value is the pair (1, 0)
// (same as 1 / 0).
// https://drafts.csswg.org/css-values-4/#ratios
if a.is_zero() && b.is_zero() {
return Ok(GenericRatio(One::one(), Zero::zero()));
}
return Ok(GenericRatio(a, b));
}
}