style: Add AngleOrPercentage to style system.

Differential Revision: https://phabricator.services.mozilla.com/D62158
This commit is contained in:
Tim Nguyen 2020-02-09 15:11:43 +00:00 committed by Emilio Cobos Álvarez
parent 97382a2c41
commit 5ed8fe8ee2
No known key found for this signature in database
GPG key ID: E1152D0994E4BF8A
3 changed files with 93 additions and 3 deletions

View file

@ -465,6 +465,54 @@ trivial_to_computed_value!(String);
trivial_to_computed_value!(Box<str>);
trivial_to_computed_value!(crate::OwnedStr);
#[allow(missing_docs)]
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
MallocSizeOf,
PartialEq,
ToAnimatedZero,
ToCss,
ToResolvedValue,
)]
#[repr(C, u8)]
pub enum AngleOrPercentage {
Percentage(Percentage),
Angle(Angle),
}
impl ToComputedValue for specified::AngleOrPercentage {
type ComputedValue = AngleOrPercentage;
#[inline]
fn to_computed_value(&self, context: &Context) -> AngleOrPercentage {
match *self {
specified::AngleOrPercentage::Percentage(percentage) => {
AngleOrPercentage::Percentage(percentage.to_computed_value(context))
},
specified::AngleOrPercentage::Angle(angle) => {
AngleOrPercentage::Angle(angle.to_computed_value(context))
},
}
}
#[inline]
fn from_computed_value(computed: &AngleOrPercentage) -> Self {
match *computed {
AngleOrPercentage::Percentage(percentage) => {
specified::AngleOrPercentage::Percentage(ToComputedValue::from_computed_value(
&percentage,
))
},
AngleOrPercentage::Angle(angle) => {
specified::AngleOrPercentage::Angle(ToComputedValue::from_computed_value(&angle))
},
}
}
}
/// A `<number>` value.
pub type Number = CSSFloat;

View file

@ -163,7 +163,8 @@ impl Angle {
/// https://github.com/w3c/fxtf-drafts/issues/228
///
/// See also: https://github.com/w3c/csswg-drafts/issues/1162.
enum AllowUnitlessZeroAngle {
#[allow(missing_docs)]
pub enum AllowUnitlessZeroAngle {
Yes,
No,
}
@ -203,7 +204,7 @@ impl Angle {
Self::parse_internal(context, input, AllowUnitlessZeroAngle::Yes)
}
fn parse_internal<'i, 't>(
pub(super) fn parse_internal<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
allow_unitless_zero: AllowUnitlessZeroAngle,

View file

@ -31,7 +31,7 @@ use style_traits::{CssWriter, ParseError, SpecifiedValueInfo, StyleParseErrorKin
pub use self::align::{AlignContent, AlignItems, AlignSelf, ContentDistribution};
#[cfg(feature = "gecko")]
pub use self::align::{JustifyContent, JustifyItems, JustifySelf, SelfAlignment};
pub use self::angle::Angle;
pub use self::angle::{Angle, AllowUnitlessZeroAngle};
pub use self::background::{BackgroundRepeat, BackgroundSize};
pub use self::basic_shape::FillRule;
pub use self::border::{BorderCornerRadius, BorderImageSlice, BorderImageWidth};
@ -130,6 +130,47 @@ pub mod transform;
pub mod ui;
pub mod url;
/// <angle> | <percentage>
/// https://drafts.csswg.org/css-values/#typedef-angle-percentage
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToCss, ToShmem)]
pub enum AngleOrPercentage {
Percentage(Percentage),
Angle(Angle),
}
impl AngleOrPercentage {
fn parse_internal<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
allow_unitless_zero: AllowUnitlessZeroAngle,
) -> Result<Self, ParseError<'i>> {
if let Ok(per) = input.try(|i| Percentage::parse(context, i)) {
return Ok(AngleOrPercentage::Percentage(per));
}
Angle::parse_internal(context, input, allow_unitless_zero).map(AngleOrPercentage::Angle)
}
/// Allow unitless angles, used for conic-gradients as specified by the spec.
/// https://drafts.csswg.org/css-images-4/#valdef-conic-gradient-angle
pub fn parse_with_unitless<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
AngleOrPercentage::parse_internal(context, input, AllowUnitlessZeroAngle::Yes)
}
}
impl Parse for AngleOrPercentage {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
AngleOrPercentage::parse_internal(context, input, AllowUnitlessZeroAngle::No)
}
}
/// Parse a `<number>` value, with a given clamping mode.
fn parse_number_with_clamping_mode<'i, 't>(
context: &ParserContext,