Skip to content

Commit

Permalink
style: Add AngleOrPercentage to style system.
Browse files Browse the repository at this point in the history
  • Loading branch information
nt1m authored and emilio committed Feb 12, 2020
1 parent 97382a2 commit 5ed8fe8
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
48 changes: 48 additions & 0 deletions components/style/values/computed/mod.rs
Expand Up @@ -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;

Expand Down
5 changes: 3 additions & 2 deletions components/style/values/specified/angle.rs
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
Expand Down
43 changes: 42 additions & 1 deletion components/style/values/specified/mod.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 5ed8fe8

Please sign in to comment.