mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
style: Derive more stuff for clip rects.
I feel a bit weird for using LenghtPercentageOrAuto to implement LengthOrAuto, but I don't think much other code will use it so it seemed a bit better to me. Differential Revision: https://phabricator.services.mozilla.com/D21863
This commit is contained in:
parent
e723a5b7d6
commit
e0b3e5f691
8 changed files with 143 additions and 254 deletions
|
@ -17,7 +17,7 @@ use crate::values::generics::length::{
|
|||
use crate::values::generics::NonNegative;
|
||||
use crate::values::specified::calc::CalcNode;
|
||||
use crate::values::specified::NonNegativeNumber;
|
||||
use crate::values::{Auto, CSSFloat, Either, Normal};
|
||||
use crate::values::{CSSFloat, Either, Normal};
|
||||
use crate::Zero;
|
||||
use app_units::Au;
|
||||
use cssparser::{Parser, Token};
|
||||
|
@ -729,9 +729,6 @@ impl NonNegativeLength {
|
|||
}
|
||||
}
|
||||
|
||||
/// Either a NonNegativeLength or the `auto` keyword.
|
||||
pub type NonNegativeLengthOrAuto = Either<NonNegativeLength, Auto>;
|
||||
|
||||
/// A `<length-percentage>` value. This can be either a `<length>`, a
|
||||
/// `<percentage>`, or a combination of both via `calc()`.
|
||||
///
|
||||
|
@ -997,7 +994,25 @@ impl NonNegativeLengthPercentage {
|
|||
pub type LengthOrNormal = Either<Length, Normal>;
|
||||
|
||||
/// Either a `<length>` or the `auto` keyword.
|
||||
pub type LengthOrAuto = Either<Length, Auto>;
|
||||
pub type LengthOrAuto = generics::LengthPercentageOrAuto<Length>;
|
||||
|
||||
impl LengthOrAuto {
|
||||
/// Parses a length, allowing the unitless length quirk.
|
||||
/// <https://quirks.spec.whatwg.org/#the-unitless-length-quirk>
|
||||
#[inline]
|
||||
pub fn parse_quirky<'i, 't>(
|
||||
context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>,
|
||||
allow_quirks: AllowQuirks,
|
||||
) -> Result<Self, ParseError<'i>> {
|
||||
Self::parse_with(context, input, |context, input| {
|
||||
Length::parse_quirky(context, input, allow_quirks)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Either a non-negative `<length>` or the `auto` keyword.
|
||||
pub type NonNegativeLengthOrAuto = generics::LengthPercentageOrAuto<NonNegativeLength>;
|
||||
|
||||
/// Either a `<length>` or a `<number>`.
|
||||
pub type LengthOrNumber = GenericLengthOrNumber<Length, Number>;
|
||||
|
|
|
@ -11,7 +11,7 @@ use super::computed::{Context, ToComputedValue};
|
|||
use super::generics::grid::{GridLine as GenericGridLine, TrackBreadth as GenericTrackBreadth};
|
||||
use super::generics::grid::{TrackList as GenericTrackList, TrackSize as GenericTrackSize};
|
||||
use super::generics::transform::IsParallelTo;
|
||||
use super::generics::{GreaterThanOrEqualToOne, NonNegative};
|
||||
use super::generics::{self, GreaterThanOrEqualToOne, NonNegative};
|
||||
use super::{Auto, CSSFloat, CSSInteger, Either};
|
||||
use crate::context::QuirksMode;
|
||||
use crate::parser::{Parse, ParserContext};
|
||||
|
@ -60,7 +60,7 @@ pub use self::image::{ColorStop, EndingShape as GradientEndingShape, Gradient};
|
|||
pub use self::image::{GradientItem, GradientKind, Image, ImageLayer, MozImageRect};
|
||||
pub use self::length::{AbsoluteLength, CalcLengthPercentage, CharacterWidth};
|
||||
pub use self::length::{FontRelativeLength, Length, LengthOrNumber, NonNegativeLengthOrNumber};
|
||||
pub use self::length::{LengthPercentage, LengthPercentageOrAuto};
|
||||
pub use self::length::{LengthOrAuto, LengthPercentage, LengthPercentageOrAuto};
|
||||
pub use self::length::{MaxSize, Size};
|
||||
pub use self::length::{NoCalcLength, ViewportPercentageLength};
|
||||
pub use self::length::{NonNegativeLengthPercentage, NonNegativeLengthPercentageOrAuto};
|
||||
|
@ -605,99 +605,8 @@ pub type GridLine = GenericGridLine<Integer>;
|
|||
/// `<grid-template-rows> | <grid-template-columns>`
|
||||
pub type GridTemplateComponent = GenericGridTemplateComponent<LengthPercentage, Integer>;
|
||||
|
||||
#[derive(Clone, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo)]
|
||||
/// rect(<top>, <left>, <bottom>, <right>) used by clip and image-region
|
||||
#[css(function = "rect")]
|
||||
pub struct ClipRect {
|
||||
/// <top> (<length> | <auto>)
|
||||
pub top: Option<Length>,
|
||||
/// <right> (<length> | <auto>)
|
||||
pub right: Option<Length>,
|
||||
/// <bottom> (<length> | <auto>)
|
||||
pub bottom: Option<Length>,
|
||||
/// <left> (<length> | <auto>)
|
||||
pub left: Option<Length>,
|
||||
}
|
||||
|
||||
impl ToCss for ClipRect {
|
||||
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
|
||||
where
|
||||
W: Write,
|
||||
{
|
||||
dest.write_str("rect(")?;
|
||||
|
||||
if let Some(ref top) = self.top {
|
||||
top.to_css(dest)?;
|
||||
dest.write_str(", ")?;
|
||||
} else {
|
||||
dest.write_str("auto, ")?;
|
||||
}
|
||||
|
||||
if let Some(ref right) = self.right {
|
||||
right.to_css(dest)?;
|
||||
dest.write_str(", ")?;
|
||||
} else {
|
||||
dest.write_str("auto, ")?;
|
||||
}
|
||||
|
||||
if let Some(ref bottom) = self.bottom {
|
||||
bottom.to_css(dest)?;
|
||||
dest.write_str(", ")?;
|
||||
} else {
|
||||
dest.write_str("auto, ")?;
|
||||
}
|
||||
|
||||
if let Some(ref left) = self.left {
|
||||
left.to_css(dest)?;
|
||||
} else {
|
||||
dest.write_str("auto")?;
|
||||
}
|
||||
|
||||
dest.write_str(")")?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl ToComputedValue for ClipRect {
|
||||
type ComputedValue = super::computed::ClipRect;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, context: &Context) -> super::computed::ClipRect {
|
||||
super::computed::ClipRect {
|
||||
top: self.top.as_ref().map(|top| top.to_computed_value(context)),
|
||||
right: self
|
||||
.right
|
||||
.as_ref()
|
||||
.map(|right| right.to_computed_value(context)),
|
||||
bottom: self
|
||||
.bottom
|
||||
.as_ref()
|
||||
.map(|bottom| bottom.to_computed_value(context)),
|
||||
left: self
|
||||
.left
|
||||
.as_ref()
|
||||
.map(|left| left.to_computed_value(context)),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &super::computed::ClipRect) -> Self {
|
||||
ClipRect {
|
||||
top: computed
|
||||
.top
|
||||
.map(|top| ToComputedValue::from_computed_value(&top)),
|
||||
right: computed
|
||||
.right
|
||||
.map(|right| ToComputedValue::from_computed_value(&right)),
|
||||
bottom: computed
|
||||
.bottom
|
||||
.map(|bottom| ToComputedValue::from_computed_value(&bottom)),
|
||||
left: computed
|
||||
.left
|
||||
.map(|left| ToComputedValue::from_computed_value(&left)),
|
||||
}
|
||||
}
|
||||
}
|
||||
/// rect(...)
|
||||
pub type ClipRect = generics::ClipRect<LengthOrAuto>;
|
||||
|
||||
impl Parse for ClipRect {
|
||||
fn parse<'i, 't>(
|
||||
|
@ -715,25 +624,16 @@ impl ClipRect {
|
|||
input: &mut Parser<'i, 't>,
|
||||
allow_quirks: AllowQuirks,
|
||||
) -> Result<Self, ParseError<'i>> {
|
||||
use crate::values::specified::Length;
|
||||
input.expect_function_matching("rect")?;
|
||||
|
||||
fn parse_argument<'i, 't>(
|
||||
context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>,
|
||||
allow_quirks: AllowQuirks,
|
||||
) -> Result<Option<Length>, ParseError<'i>> {
|
||||
if input
|
||||
.try(|input| input.expect_ident_matching("auto"))
|
||||
.is_ok()
|
||||
{
|
||||
Ok(None)
|
||||
} else {
|
||||
Length::parse_quirky(context, input, allow_quirks).map(Some)
|
||||
}
|
||||
) -> Result<LengthOrAuto, ParseError<'i>> {
|
||||
LengthOrAuto::parse_quirky(context, input, allow_quirks)
|
||||
}
|
||||
|
||||
input.expect_function_matching("rect")?;
|
||||
|
||||
input.parse_nested_block(|input| {
|
||||
let top = parse_argument(context, input, allow_quirks)?;
|
||||
let right;
|
||||
|
@ -751,12 +651,8 @@ impl ClipRect {
|
|||
bottom = parse_argument(context, input, allow_quirks)?;
|
||||
left = parse_argument(context, input, allow_quirks)?;
|
||||
}
|
||||
Ok(ClipRect {
|
||||
top: top,
|
||||
right: right,
|
||||
bottom: bottom,
|
||||
left: left,
|
||||
})
|
||||
|
||||
Ok(ClipRect { top, right, bottom, left })
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue