mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
style: Move Ratio into independent files.
Based on https://drafts.csswg.org/css-values/#ratios, <ratio> should be a general types in css values, and now the media query and the position use this type, so let's move it into the independent files. Differential Revision: https://phabricator.services.mozilla.com/D106218
This commit is contained in:
parent
35b080e021
commit
52d39fc1bc
12 changed files with 142 additions and 115 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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)?)
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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>;
|
||||
|
|
39
components/style/values/computed/ratio.rs
Normal file
39
components/style/values/computed/ratio.rs
Normal file
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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,
|
||||
|
|
53
components/style/values/generics/ratio.rs
Normal file
53
components/style/values/generics/ratio.rs
Normal file
|
@ -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(())
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
32
components/style/values/specified/ratio.rs
Normal file
32
components/style/values/specified/ratio.rs
Normal file
|
@ -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))
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue