style: Update aspect-ratio syntax for HTML IMG mapped ratio.

Differential Revision: https://phabricator.services.mozilla.com/D76942
This commit is contained in:
Boris Chiou 2020-05-27 21:43:00 +00:00 committed by Emilio Cobos Álvarez
parent 964716f72a
commit eff8f0fca0
2 changed files with 71 additions and 15 deletions

View file

@ -191,7 +191,7 @@ where
} }
} }
/// A generic value for the `aspect-ratio` property. /// Ratio or None.
#[derive( #[derive(
Animate, Animate,
Clone, Clone,
@ -208,19 +208,50 @@ where
ToShmem, ToShmem,
)] )]
#[repr(C, u8)] #[repr(C, u8)]
pub enum GenericAspectRatio<N> { pub enum PreferredRatio<N> {
/// The <ratio> value. /// Without specified ratio
#[css(skip)]
None,
/// With specified ratio
Ratio(#[css(field_bound)] Ratio<N>), Ratio(#[css(field_bound)] Ratio<N>),
/// The keyword `auto`. }
Auto,
/// A generic value for the `aspect-ratio` property, the value is `auto || <ratio>`.
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
MallocSizeOf,
PartialEq,
SpecifiedValueInfo,
ToAnimatedZero,
ToComputedValue,
ToCss,
ToResolvedValue,
ToShmem,
)]
#[repr(C)]
pub struct GenericAspectRatio<N> {
/// Specifiy auto or not.
#[animation(constant)]
#[css(represents_keyword)]
pub auto: bool,
/// The preferred aspect-ratio value.
#[css(field_bound)]
pub ratio: PreferredRatio<N>,
} }
pub use self::GenericAspectRatio as AspectRatio; pub use self::GenericAspectRatio as AspectRatio;
impl<R> AspectRatio<R> { impl<N> AspectRatio<N> {
/// Returns `auto` /// Returns `auto`
#[inline] #[inline]
pub fn auto() -> Self { pub fn auto() -> Self {
AspectRatio::Auto AspectRatio {
auto: true,
ratio: PreferredRatio::None,
}
} }
} }

View file

@ -882,24 +882,49 @@ pub type ZIndex = GenericZIndex<Integer>;
/// A specified value for the `aspect-ratio` property. /// A specified value for the `aspect-ratio` property.
pub type AspectRatio = GenericAspectRatio<NonNegativeNumber>; pub type AspectRatio = GenericAspectRatio<NonNegativeNumber>;
// FIXME: Add field_bound for parse custom derive, so we can drop this.
impl Parse for AspectRatio { impl Parse for AspectRatio {
fn parse<'i, 't>( fn parse<'i, 't>(
context: &ParserContext, context: &ParserContext,
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> { ) -> Result<Self, ParseError<'i>> {
if input use crate::values::generics::position::PreferredRatio;
.try(|input| input.expect_ident_matching("auto"))
.is_ok() let location = input.current_source_location();
{ let mut auto = input.try(|i| i.expect_ident_matching("auto"));
return Ok(AspectRatio::Auto); let ratio = input.try(|i| Ratio::parse(context, i));
if auto.is_err() {
auto = input.try(|i| i.expect_ident_matching("auto"));
} }
GenericRatio::parse(context, input).map(AspectRatio::Ratio) if auto.is_err() && ratio.is_err() {
return Err(location.new_custom_error(StyleParseErrorKind::UnspecifiedError));
}
Ok(AspectRatio {
auto: auto.is_ok(),
ratio: match ratio {
Ok(ratio) => PreferredRatio::Ratio(ratio),
Err(..) => PreferredRatio::None,
},
})
} }
} }
/// A specified value for the `aspect-ratio` property. impl AspectRatio {
/// Returns Self by a valid ratio.
pub fn from_mapped_ratio(w: f32, h: f32) -> Self {
use crate::values::generics::position::PreferredRatio;
AspectRatio {
auto: true,
ratio: PreferredRatio::Ratio(GenericRatio(
NonNegativeNumber::new(w),
NonNegativeNumber::new(h),
)),
}
}
}
/// A specified <ratio> value.
pub type Ratio = GenericRatio<NonNegativeNumber>; pub type Ratio = GenericRatio<NonNegativeNumber>;
// https://drafts.csswg.org/css-values-4/#ratios // https://drafts.csswg.org/css-values-4/#ratios