diff options
-rw-r--r-- | components/style/gecko/media_features.rs | 2 | ||||
-rw-r--r-- | components/style/media_queries/media_feature.rs | 2 | ||||
-rw-r--r-- | components/style/media_queries/media_feature_expression.rs | 15 | ||||
-rw-r--r-- | components/style/values/computed/mod.rs | 2 | ||||
-rw-r--r-- | components/style/values/computed/position.rs | 33 | ||||
-rw-r--r-- | components/style/values/computed/ratio.rs | 39 | ||||
-rw-r--r-- | components/style/values/generics/mod.rs | 4 | ||||
-rw-r--r-- | components/style/values/generics/position.rs | 47 | ||||
-rw-r--r-- | components/style/values/generics/ratio.rs | 53 | ||||
-rw-r--r-- | components/style/values/specified/mod.rs | 2 | ||||
-rw-r--r-- | components/style/values/specified/position.rs | 26 | ||||
-rw-r--r-- | components/style/values/specified/ratio.rs | 32 |
12 files changed, 142 insertions, 115 deletions
diff --git a/components/style/gecko/media_features.rs b/components/style/gecko/media_features.rs index 03fea6e51e4..0e010f7a0cb 100644 --- a/components/style/gecko/media_features.rs +++ b/components/style/gecko/media_features.rs @@ -10,8 +10,8 @@ use crate::media_queries::media_feature::{AllowsRanges, ParsingRequirements}; use crate::media_queries::media_feature::{Evaluator, MediaFeatureDescription}; use crate::media_queries::media_feature_expression::RangeOrOperator; use crate::media_queries::{Device, MediaType}; -use crate::values::computed::position::Ratio; use crate::values::computed::CSSPixelLength; +use crate::values::computed::Ratio; use crate::values::computed::Resolution; use crate::Atom; use app_units::Au; diff --git a/components/style/media_queries/media_feature.rs b/components/style/media_queries/media_feature.rs index cad695413c3..e6edff3f68a 100644 --- a/components/style/media_queries/media_feature.rs +++ b/components/style/media_queries/media_feature.rs @@ -7,7 +7,7 @@ use super::media_feature_expression::RangeOrOperator; use super::Device; use crate::parser::ParserContext; -use crate::values::computed::position::Ratio; +use crate::values::computed::Ratio; use crate::values::computed::{CSSPixelLength, Resolution}; use crate::Atom; use cssparser::Parser; diff --git a/components/style/media_queries/media_feature_expression.rs b/components/style/media_queries/media_feature_expression.rs index 08b13136ef9..41be485d5c0 100644 --- a/components/style/media_queries/media_feature_expression.rs +++ b/components/style/media_queries/media_feature_expression.rs @@ -15,8 +15,7 @@ use crate::parser::{Parse, ParserContext}; #[cfg(feature = "servo")] use crate::servo::media_queries::MEDIA_FEATURES; use crate::str::{starts_with_ignore_ascii_case, string_as_ascii_lowercase}; -use crate::values::computed::position::Ratio; -use crate::values::computed::{self, ToComputedValue}; +use crate::values::computed::{self, Ratio, ToComputedValue}; use crate::values::specified::{Integer, Length, Number, Resolution}; use crate::values::{serialize_atom_identifier, CSSFloat}; use crate::{Atom, Zero}; @@ -498,15 +497,9 @@ impl MediaExpressionValue { MediaExpressionValue::Float(number.get()) }, Evaluator::NumberRatio(..) => { - use crate::values::generics::position::Ratio as GenericRatio; - use crate::values::generics::NonNegative; - use crate::values::specified::position::Ratio; - - let ratio = Ratio::parse(context, input)?; - MediaExpressionValue::NumberRatio(GenericRatio( - NonNegative(ratio.0.get()), - NonNegative(ratio.1.get()), - )) + use crate::values::specified::Ratio as SpecifiedRatio; + let ratio = SpecifiedRatio::parse(context, input)?; + MediaExpressionValue::NumberRatio(Ratio::new(ratio.0.get(), ratio.1.get())) }, Evaluator::Resolution(..) => { MediaExpressionValue::Resolution(Resolution::parse(context, input)?) diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index 9ac147c8279..38ed9cbb90a 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -77,6 +77,7 @@ pub use self::position::AspectRatio; pub use self::position::{ GridAutoFlow, GridTemplateAreas, MasonryAutoFlow, Position, PositionOrAuto, ZIndex, }; +pub use self::ratio::Ratio; pub use self::rect::NonNegativeLengthOrNumberRect; pub use self::resolution::Resolution; pub use self::svg::MozContextProperties; @@ -122,6 +123,7 @@ pub mod outline; pub mod page; pub mod percentage; pub mod position; +pub mod ratio; pub mod rect; pub mod resolution; pub mod svg; diff --git a/components/style/values/computed/position.rs b/components/style/values/computed/position.rs index f5e586fdfa4..6d09e327e63 100644 --- a/components/style/values/computed/position.rs +++ b/components/style/values/computed/position.rs @@ -12,11 +12,9 @@ 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; pub use crate::values::specified::position::{GridAutoFlow, GridTemplateAreas, MasonryAutoFlow}; -use crate::{One, Zero}; -use std::cmp::{Ordering, PartialOrd}; +use crate::Zero; use std::fmt::{self, Write}; use style_traits::{CssWriter, ToCss}; @@ -72,34 +70,5 @@ impl GenericPositionComponent for LengthPercentage { /// A computed value for the `z-index` property. pub type ZIndex = GenericZIndex<Integer>; -/// A computed <ratio> value. -pub type Ratio = GenericRatio<NonNegativeNumber>; - -impl PartialOrd for Ratio { - fn partial_cmp(&self, other: &Self) -> Option<Ordering> { - f64::partial_cmp( - &((self.0).0 as f64 * (other.1).0 as f64), - &((self.1).0 as f64 * (other.0).0 as f64), - ) - } -} - -impl Ratio { - /// Returns a new Ratio. - pub fn new(a: f32, b: f32) -> Self { - GenericRatio(a.into(), b.into()) - } - - /// Returns the used value. A ratio of 0/0 behaves as the ratio 1/0. - /// https://drafts.csswg.org/css-values-4/#ratios - pub fn used_value(self) -> Self { - if self.0.is_zero() && self.1.is_zero() { - Ratio::new(One::one(), Zero::zero()) - } else { - self - } - } -} - /// A computed value for the `aspect-ratio` property. pub type AspectRatio = GenericAspectRatio<NonNegativeNumber>; diff --git a/components/style/values/computed/ratio.rs b/components/style/values/computed/ratio.rs new file mode 100644 index 00000000000..b1786f3983f --- /dev/null +++ b/components/style/values/computed/ratio.rs @@ -0,0 +1,39 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +//! `<ratio>` computed values. + +use crate::values::computed::NonNegativeNumber; +use crate::values::generics::ratio::Ratio as GenericRatio; +use crate::{One, Zero}; +use std::cmp::{Ordering, PartialOrd}; + +/// A computed <ratio> value. +pub type Ratio = GenericRatio<NonNegativeNumber>; + +impl PartialOrd for Ratio { + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { + f64::partial_cmp( + &((self.0).0 as f64 * (other.1).0 as f64), + &((self.1).0 as f64 * (other.0).0 as f64), + ) + } +} + +impl Ratio { + /// Returns a new Ratio. + pub fn new(a: f32, b: f32) -> Self { + GenericRatio(a.into(), b.into()) + } + + /// Returns the used value. A ratio of 0/0 behaves as the ratio 1/0. + /// https://drafts.csswg.org/css-values-4/#ratios + pub fn used_value(self) -> Self { + if self.0.is_zero() && self.1.is_zero() { + Ratio::new(One::one(), Zero::zero()) + } else { + self + } + } +} diff --git a/components/style/values/generics/mod.rs b/components/style/values/generics/mod.rs index 1bcfa23352d..b1536bb7f33 100644 --- a/components/style/values/generics/mod.rs +++ b/components/style/values/generics/mod.rs @@ -11,8 +11,7 @@ use crate::parser::{Parse, ParserContext}; use crate::Zero; use cssparser::Parser; use std::ops::Add; -use style_traits::{KeywordsCollectFn, ParseError}; -use style_traits::{SpecifiedValueInfo, StyleParseErrorKind}; +use style_traits::{KeywordsCollectFn, ParseError, SpecifiedValueInfo, StyleParseErrorKind}; pub mod background; pub mod basic_shape; @@ -33,6 +32,7 @@ pub mod length; pub mod motion; pub mod page; pub mod position; +pub mod ratio; pub mod rect; pub mod size; pub mod svg; diff --git a/components/style/values/generics/position.rs b/components/style/values/generics/position.rs index 76e5c94e056..7a909ccfc90 100644 --- a/components/style/values/generics/position.rs +++ b/components/style/values/generics/position.rs @@ -5,8 +5,7 @@ //! Generic types for CSS handling of specified and computed values of //! [`position`](https://drafts.csswg.org/css-backgrounds-3/#position) -use std::fmt::{self, Write}; -use style_traits::{CssWriter, ToCss}; +use crate::values::generics::ratio::Ratio; /// A generic type for representing a CSS [position](https://drafts.csswg.org/css-values/#position). #[derive( @@ -154,50 +153,6 @@ impl<Integer> ZIndex<Integer> { } } -/// A generic value for the `<ratio>` value. -#[derive( - Animate, - Clone, - ComputeSquaredDistance, - Copy, - Debug, - MallocSizeOf, - PartialEq, - SpecifiedValueInfo, - ToAnimatedZero, - ToComputedValue, - ToResolvedValue, - ToShmem, -)] -#[repr(C)] -pub struct Ratio<N>(pub N, pub N); - -impl<N> ToCss for Ratio<N> -where - N: ToCss -{ - fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result - where - W: Write, - { - self.0.to_css(dest)?; - // Even though 1 could be omitted, we don't per - // https://drafts.csswg.org/css-values-4/#ratio-value: - // - // The second <number> is optional, defaulting to 1. However, - // <ratio> is always serialized with both components. - // - // And for compat reasons, see bug 1669742. - // - // We serialize with spaces for consistency with all other - // slash-delimited things, see - // https://github.com/w3c/csswg-drafts/issues/4282 - dest.write_str(" / ")?; - self.1.to_css(dest)?; - Ok(()) - } -} - /// Ratio or None. #[derive( Animate, diff --git a/components/style/values/generics/ratio.rs b/components/style/values/generics/ratio.rs new file mode 100644 index 00000000000..ea3354f9828 --- /dev/null +++ b/components/style/values/generics/ratio.rs @@ -0,0 +1,53 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +//! Generic types for CSS values related to <ratio>. +//! https://drafts.csswg.org/css-values/#ratios + +use std::fmt::{self, Write}; +use style_traits::{CssWriter, ToCss}; + +/// A generic value for the `<ratio>` value. +#[derive( + Animate, + Clone, + ComputeSquaredDistance, + Copy, + Debug, + MallocSizeOf, + PartialEq, + SpecifiedValueInfo, + ToAnimatedZero, + ToComputedValue, + ToResolvedValue, + ToShmem, +)] +#[repr(C)] +pub struct Ratio<N>(pub N, pub N); + +impl<N> ToCss for Ratio<N> +where + N: ToCss, +{ + fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result + where + W: Write, + { + self.0.to_css(dest)?; + // Even though 1 could be omitted, we don't per + // https://drafts.csswg.org/css-values-4/#ratio-value: + // + // The second <number> is optional, defaulting to 1. However, + // <ratio> is always serialized with both components. + // + // And for compat reasons, see bug 1669742. + // + // We serialize with spaces for consistency with all other + // slash-delimited things, see + // https://github.com/w3c/csswg-drafts/issues/4282 + dest.write_str(" / ")?; + self.1.to_css(dest)?; + Ok(()) + } +} diff --git a/components/style/values/specified/mod.rs b/components/style/values/specified/mod.rs index f5183993f64..d60c5d87f1f 100644 --- a/components/style/values/specified/mod.rs +++ b/components/style/values/specified/mod.rs @@ -77,6 +77,7 @@ pub use self::position::{ GridAutoFlow, GridTemplateAreas, MasonryAutoFlow, Position, PositionOrAuto, }; pub use self::position::{PositionComponent, ZIndex}; +pub use self::ratio::Ratio; pub use self::rect::NonNegativeLengthOrNumberRect; pub use self::resolution::Resolution; pub use self::svg::MozContextProperties; @@ -124,6 +125,7 @@ pub mod outline; pub mod percentage; pub mod page; pub mod position; +pub mod ratio; pub mod rect; pub mod resolution; pub mod source_size_list; diff --git a/components/style/values/specified/position.rs b/components/style/values/specified/position.rs index 9810959f3a6..26359c155c6 100644 --- a/components/style/values/specified/position.rs +++ b/components/style/values/specified/position.rs @@ -16,10 +16,9 @@ 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, NonNegativeNumber}; -use crate::{Atom, One, Zero}; +use crate::{Atom, Zero}; use cssparser::Parser; use selectors::parser::SelectorParseErrorKind; use servo_arc::Arc; @@ -900,6 +899,7 @@ impl Parse for AspectRatio { input: &mut Parser<'i, 't>, ) -> Result<Self, ParseError<'i>> { use crate::values::generics::position::PreferredRatio; + use crate::values::specified::Ratio; let location = input.current_source_location(); let mut auto = input.try_parse(|i| i.expect_ident_matching("auto")); @@ -926,31 +926,13 @@ impl AspectRatio { /// Returns Self by a valid ratio. pub fn from_mapped_ratio(w: f32, h: f32) -> Self { use crate::values::generics::position::PreferredRatio; + use crate::values::generics::ratio::Ratio; AspectRatio { auto: true, - ratio: PreferredRatio::Ratio(GenericRatio( + ratio: PreferredRatio::Ratio(Ratio( NonNegativeNumber::new(w), NonNegativeNumber::new(h), )), } } } - -/// A specified <ratio> value. -pub type Ratio = GenericRatio<NonNegativeNumber>; - -// https://drafts.csswg.org/css-values-4/#ratios -impl Parse for Ratio { - 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(), - }; - - Ok(GenericRatio(a, b)) - } -} diff --git a/components/style/values/specified/ratio.rs b/components/style/values/specified/ratio.rs new file mode 100644 index 00000000000..4cdddd452ed --- /dev/null +++ b/components/style/values/specified/ratio.rs @@ -0,0 +1,32 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +//! Specified types for <ratio>. +//! +//! [ratio]: https://drafts.csswg.org/css-values/#ratios + +use crate::parser::{Parse, ParserContext}; +use crate::values::generics::ratio::Ratio as GenericRatio; +use crate::values::specified::NonNegativeNumber; +use crate::One; +use cssparser::Parser; +use style_traits::ParseError; + +/// A specified <ratio> value. +pub type Ratio = GenericRatio<NonNegativeNumber>; + +impl Parse for Ratio { + 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(), + }; + + Ok(GenericRatio(a, b)) + } +} |