diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index e7ecee8f0f2..24eea98871b 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -106,32 +106,6 @@ pub mod shorthands { use style_traits::{ParseError, StyleParseErrorKind}; use crate::values::specified; - use style_traits::{CssWriter, ToCss}; - use crate::values::specified::{BorderStyle, Color}; - use std::fmt::{self, Write}; - - fn serialize_directional_border( - dest: &mut CssWriter, - width: &I, - style: &BorderStyle, - color: &Color, - ) -> fmt::Result - where - W: Write, - I: ToCss, - { - width.to_css(dest)?; - // FIXME(emilio): Should we really serialize the border style if it's - // `solid`? - dest.write_char(' ')?; - style.to_css(dest)?; - if *color != Color::CurrentColor { - dest.write_char(' ')?; - color.to_css(dest)?; - } - Ok(()) - } - % for style_struct in data.style_structs: include!("${repr(os.path.join(OUT_DIR, 'shorthands/{}.rs'.format(style_struct.name_lower)))[1:-1]}"); % endfor diff --git a/components/style/properties/shorthands/border.mako.rs b/components/style/properties/shorthands/border.mako.rs index 4bb16ac464e..93c0e93d11c 100644 --- a/components/style/properties/shorthands/border.mako.rs +++ b/components/style/properties/shorthands/border.mako.rs @@ -107,7 +107,7 @@ pub fn parse_border<'i, 't>( engines="gecko servo" sub_properties="${' '.join( 'border-%s-%s' % (side, prop) - for prop in ['color', 'style', 'width'] + for prop in ['width', 'style', 'color'] )}" aliases="${maybe_moz_logical_alias(engine, (side, logical), '-moz-border-%s')}" spec="${spec}"> @@ -126,7 +126,7 @@ pub fn parse_border<'i, 't>( impl<'a> ToCss for LonghandsToSerialize<'a> { fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { - super::serialize_directional_border( + crate::values::specified::border::serialize_directional_border( dest, self.border_${to_rust_ident(side)}_width, self.border_${to_rust_ident(side)}_style, @@ -141,8 +141,8 @@ pub fn parse_border<'i, 't>( <%helpers:shorthand name="border" engines="gecko servo" sub_properties="${' '.join('border-%s-%s' % (side, prop) - for side in PHYSICAL_SIDES - for prop in ['color', 'style', 'width'])} + for side in PHYSICAL_SIDES for prop in ['width', 'style', 'color'] + )} ${' '.join('border-image-%s' % name for name in ['outset', 'repeat', 'slice', 'source', 'width'])}" derive_value_info="False" @@ -205,16 +205,15 @@ pub fn parse_border<'i, 't>( // If all longhands are all present, then all sides should be the same, // so we can just one set of color/style/width - if all_equal { - super::serialize_directional_border( - dest, - self.border_${side}_width, - self.border_${side}_style, - self.border_${side}_color - ) - } else { - Ok(()) + if !all_equal { + return Ok(()) } + crate::values::specified::border::serialize_directional_border( + dest, + self.border_${side}_width, + self.border_${side}_style, + self.border_${side}_color + ) } } @@ -359,14 +358,27 @@ pub fn parse_border<'i, 't>( impl<'a> ToCss for LonghandsToSerialize<'a> { fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { self.border_image_source.to_css(dest)?; - dest.write_char(' ')?; - self.border_image_slice.to_css(dest)?; - dest.write_str(" / ")?; - self.border_image_width.to_css(dest)?; - dest.write_str(" / ")?; - self.border_image_outset.to_css(dest)?; - dest.write_char(' ')?; - self.border_image_repeat.to_css(dest) + % for name in "slice outset width repeat".split(): + let has_${name} = *self.border_image_${name} != border_image_${name}::get_initial_specified_value(); + % endfor + let needs_slice = has_slice || has_width || has_outset; + if needs_slice { + dest.write_char(' ')?; + self.border_image_slice.to_css(dest)?; + if has_width { + dest.write_str(" / ")?; + self.border_image_width.to_css(dest)?; + } + if has_outset { + dest.write_str(" / ")?; + self.border_image_outset.to_css(dest)?; + } + } + if has_repeat { + dest.write_char(' ')?; + self.border_image_repeat.to_css(dest)?; + } + Ok(()) } } @@ -454,7 +466,7 @@ pub fn parse_border<'i, 't>( impl<'a> ToCss for LonghandsToSerialize<'a> { fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { - super::serialize_directional_border( + crate::values::specified::border::serialize_directional_border( dest, self.border_${axis}_start_width, self.border_${axis}_start_style, diff --git a/components/style/values/specified/border.rs b/components/style/values/specified/border.rs index 31da543356b..923da0a5738 100644 --- a/components/style/values/specified/border.rs +++ b/components/style/values/specified/border.rs @@ -16,10 +16,11 @@ use crate::values::generics::rect::Rect; use crate::values::generics::size::Size2D; use crate::values::specified::length::{NonNegativeLength, NonNegativeLengthPercentage}; use crate::values::specified::{AllowQuirks, NonNegativeNumber, NonNegativeNumberOrPercentage}; +use crate::values::specified::Color; use crate::Zero; use cssparser::Parser; use std::fmt::{self, Write}; -use style_traits::{CssWriter, ParseError, ToCss}; +use style_traits::{CssWriter, ParseError, ToCss, values::SequenceWriter}; /// A specified value for a single side of a `border-style` property. /// @@ -316,3 +317,32 @@ impl Parse for BorderImageRepeat { )) } } + +/// Serializes a border shorthand value composed of width/style/color. +pub fn serialize_directional_border( + dest: &mut CssWriter, + width: &BorderSideWidth, + style: &BorderStyle, + color: &Color, +) -> fmt::Result +where + W: Write, +{ + let has_style = *style != BorderStyle::None; + let has_color = *color != Color::CurrentColor; + let has_width = *width != BorderSideWidth::Medium; + if !has_style && !has_color && !has_width { + return width.to_css(dest) + } + let mut writer = SequenceWriter::new(dest, " "); + if has_width { + writer.item(width)?; + } + if has_style { + writer.item(style)?; + } + if has_color { + writer.item(color)?; + } + Ok(()) +}