Skip to content

Commit

Permalink
style: Simplify border-image-repeat serialization.
Browse files Browse the repository at this point in the history
We're the only ones to preserve explicitly the second keyword, as noticed in:

  web-platform-tests/wpt#10170
  • Loading branch information
emilio committed Apr 10, 2018
1 parent cba8b08 commit 68ce13a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 53 deletions.
48 changes: 3 additions & 45 deletions components/style/values/computed/border.rs
Expand Up @@ -5,10 +5,8 @@
//! Computed types for CSS values related to borders.

use app_units::Au;
use std::fmt::{self, Write};
use style_traits::{ToCss, CssWriter};
use values::animated::ToAnimatedZero;
use values::computed::{Context, Number, NumberOrPercentage, ToComputedValue};
use values::computed::{Number, NumberOrPercentage};
use values::computed::length::{LengthOrPercentage, NonNegativeLength};
use values::generics::border::BorderCornerRadius as GenericBorderCornerRadius;
use values::generics::border::BorderImageSideWidth as GenericBorderImageSideWidth;
Expand All @@ -17,7 +15,8 @@ use values::generics::border::BorderRadius as GenericBorderRadius;
use values::generics::border::BorderSpacing as GenericBorderSpacing;
use values::generics::rect::Rect;
use values::generics::size::Size;
use values::specified::border::{BorderImageRepeat as SpecifiedBorderImageRepeat, BorderImageRepeatKeyword};

pub use values::specified::border::BorderImageRepeat;

/// A computed value for the `border-image-width` property.
pub type BorderImageWidth = Rect<BorderImageSideWidth>;
Expand Down Expand Up @@ -76,44 +75,3 @@ impl ToAnimatedZero for BorderCornerRadius {
Err(())
}
}

/// The computed value of the `border-image-repeat` property:
///
/// https://drafts.csswg.org/css-backgrounds/#the-border-image-repeat
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq)]
pub struct BorderImageRepeat(pub BorderImageRepeatKeyword, pub BorderImageRepeatKeyword);

impl BorderImageRepeat {
/// Returns the `stretch` value.
pub fn stretch() -> Self {
BorderImageRepeat(BorderImageRepeatKeyword::Stretch, BorderImageRepeatKeyword::Stretch)
}
}

impl ToCss for BorderImageRepeat {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
self.0.to_css(dest)?;
if self.0 != self.1 {
dest.write_str(" ")?;
self.1.to_css(dest)?;
}
Ok(())
}
}

impl ToComputedValue for SpecifiedBorderImageRepeat {
type ComputedValue = BorderImageRepeat;

#[inline]
fn to_computed_value(&self, _: &Context) -> Self::ComputedValue {
BorderImageRepeat(self.0, self.1.unwrap_or(self.0))
}

#[inline]
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
SpecifiedBorderImageRepeat(computed.0, Some(computed.1))
}
}
33 changes: 25 additions & 8 deletions components/style/values/specified/border.rs
Expand Up @@ -6,7 +6,8 @@

use cssparser::Parser;
use parser::{Parse, ParserContext};
use style_traits::ParseError;
use std::fmt::{self, Write};
use style_traits::{CssWriter, ParseError, ToCss};
use values::computed::{self, Context, ToComputedValue};
use values::generics::border::BorderCornerRadius as GenericBorderCornerRadius;
use values::generics::border::BorderImageSideWidth as GenericBorderImageSideWidth;
Expand Down Expand Up @@ -54,9 +55,8 @@ impl BorderSideWidth {
pub fn parse_quirky<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
allow_quirks: AllowQuirks)
-> Result<Self, ParseError<'i>>
{
allow_quirks: AllowQuirks,
) -> Result<Self, ParseError<'i>> {
if let Ok(length) = input.try(|i| Length::parse_non_negative_quirky(context, i, allow_quirks)) {
return Ok(BorderSideWidth::Length(length));
}
Expand Down Expand Up @@ -186,14 +186,31 @@ pub enum BorderImageRepeatKeyword {
/// The specified value for the `border-image-repeat` property.
///
/// https://drafts.csswg.org/css-backgrounds/#the-border-image-repeat
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToCss)]
pub struct BorderImageRepeat(pub BorderImageRepeatKeyword, pub Option<BorderImageRepeatKeyword>);
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToComputedValue)]
pub struct BorderImageRepeat(pub BorderImageRepeatKeyword, pub BorderImageRepeatKeyword);

impl ToCss for BorderImageRepeat {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
self.0.to_css(dest)?;
if self.0 != self.1 {
dest.write_str(" ")?;
self.1.to_css(dest)?;
}
Ok(())
}
}

impl BorderImageRepeat {
/// Returns the `stretch` value.
#[inline]
pub fn stretch() -> Self {
BorderImageRepeat(BorderImageRepeatKeyword::Stretch, None)
BorderImageRepeat(
BorderImageRepeatKeyword::Stretch,
BorderImageRepeatKeyword::Stretch,
)
}
}

Expand All @@ -204,6 +221,6 @@ impl Parse for BorderImageRepeat {
) -> Result<Self, ParseError<'i>> {
let horizontal = BorderImageRepeatKeyword::parse(input)?;
let vertical = input.try(BorderImageRepeatKeyword::parse).ok();
Ok(BorderImageRepeat(horizontal, vertical))
Ok(BorderImageRepeat(horizontal, vertical.unwrap_or(horizontal)))
}
}

0 comments on commit 68ce13a

Please sign in to comment.