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:
Boris Chiou 2021-02-25 01:50:55 +00:00 committed by Emilio Cobos Álvarez
parent 35b080e021
commit 52d39fc1bc
12 changed files with 142 additions and 115 deletions

View file

@ -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;

View file

@ -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,

View 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(())
}
}