From ec865c370251a82026c565f3ae9e07671786ff7d Mon Sep 17 00:00:00 2001 From: Karl Date: Mon, 6 Feb 2017 21:35:45 -0500 Subject: [PATCH] Fix border shorthand serialization Fix border shorthand serialization when sides are not identical --- .../style/properties/shorthand/border.mako.rs | 38 +++++++++-- tests/unit/style/properties/serialization.rs | 66 +++++++++++++++++++ 2 files changed, 97 insertions(+), 7 deletions(-) diff --git a/components/style/properties/shorthand/border.mako.rs b/components/style/properties/shorthand/border.mako.rs index f6ae7df638e..ee023d90e16 100644 --- a/components/style/properties/shorthand/border.mako.rs +++ b/components/style/properties/shorthand/border.mako.rs @@ -3,7 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ <%namespace name="helpers" file="/helpers.mako.rs" /> -<% from data import to_rust_ident, ALL_SIDES, maybe_moz_logical_alias %> +<% from data import to_rust_ident, ALL_SIDES, PHYSICAL_SIDES, maybe_moz_logical_alias %> ${helpers.four_sides_shorthand("border-color", "border-%s-color", "specified::CSSColor::parse", spec="https://drafts.csswg.org/css-backgrounds/#border-color")} @@ -141,14 +141,38 @@ pub fn parse_border(context: &ParserContext, input: &mut Parser) impl<'a> LonghandsToSerialize<'a> { fn to_css_declared(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + let all_equal = { + % for side in PHYSICAL_SIDES: + let border_${side}_width = self.border_${side}_width; + let border_${side}_style = self.border_${side}_style; + let border_${side}_color = self.border_${side}_color; + % endfor + + border_top_width == border_right_width && + border_right_width == border_bottom_width && + border_bottom_width == border_left_width && + + border_top_style == border_right_style && + border_right_style == border_bottom_style && + border_bottom_style == border_left_style && + + border_top_color == border_right_color && + border_right_color == border_bottom_color && + border_bottom_color == border_left_color + }; + // If all longhands are all present, then all sides should be the same, // so we can just one set of color/style/width - super::serialize_directional_border( - dest, - self.border_${side}_width, - self.border_${side}_style, - self.border_${side}_color - ) + if all_equal { + super::serialize_directional_border( + dest, + self.border_${side}_width, + self.border_${side}_style, + self.border_${side}_color + ) + } else { + Ok(()) + } } } diff --git a/tests/unit/style/properties/serialization.rs b/tests/unit/style/properties/serialization.rs index 974b94eea8d..fd16fe3f71b 100644 --- a/tests/unit/style/properties/serialization.rs +++ b/tests/unit/style/properties/serialization.rs @@ -186,6 +186,72 @@ mod shorthand_serialization { assert_eq!(serialization, "margin: 8px 12px 10px 14px;"); } + #[test] + fn different_longhands_should_serialize_to_long_form() { + let mut properties = Vec::new(); + + let solid = DeclaredValue::Value(BorderStyle::solid); + + properties.push(PropertyDeclaration::BorderTopStyle(solid.clone())); + properties.push(PropertyDeclaration::BorderRightStyle(solid.clone())); + properties.push(PropertyDeclaration::BorderBottomStyle(solid.clone())); + properties.push(PropertyDeclaration::BorderLeftStyle(solid.clone())); + + let px_30 = DeclaredValue::Value(BorderWidth::from_length(Length::from_px(30f32))); + let px_10 = DeclaredValue::Value(BorderWidth::from_length(Length::from_px(10f32))); + + properties.push(PropertyDeclaration::BorderTopWidth(px_30.clone())); + properties.push(PropertyDeclaration::BorderRightWidth(px_30.clone())); + properties.push(PropertyDeclaration::BorderBottomWidth(px_30.clone())); + properties.push(PropertyDeclaration::BorderLeftWidth(px_10.clone())); + + let blue = DeclaredValue::Value(CSSColor { + parsed: ComputedColor::RGBA(RGBA::new(0, 0, 255, 255)), + authored: None + }); + + properties.push(PropertyDeclaration::BorderTopColor(blue.clone())); + properties.push(PropertyDeclaration::BorderRightColor(blue.clone())); + properties.push(PropertyDeclaration::BorderBottomColor(blue.clone())); + properties.push(PropertyDeclaration::BorderLeftColor(blue.clone())); + + let serialization = shorthand_properties_to_string(properties); + assert_eq!(serialization, + "border-style: solid; border-width: 30px 30px 30px 10px; border-color: rgb(0, 0, 255);"); + } + + #[test] + fn same_longhands_should_serialize_correctly() { + let mut properties = Vec::new(); + + let solid = DeclaredValue::Value(BorderStyle::solid); + + properties.push(PropertyDeclaration::BorderTopStyle(solid.clone())); + properties.push(PropertyDeclaration::BorderRightStyle(solid.clone())); + properties.push(PropertyDeclaration::BorderBottomStyle(solid.clone())); + properties.push(PropertyDeclaration::BorderLeftStyle(solid.clone())); + + let px_30 = DeclaredValue::Value(BorderWidth::from_length(Length::from_px(30f32))); + + properties.push(PropertyDeclaration::BorderTopWidth(px_30.clone())); + properties.push(PropertyDeclaration::BorderRightWidth(px_30.clone())); + properties.push(PropertyDeclaration::BorderBottomWidth(px_30.clone())); + properties.push(PropertyDeclaration::BorderLeftWidth(px_30.clone())); + + let blue = DeclaredValue::Value(CSSColor { + parsed: ComputedColor::RGBA(RGBA::new(0, 0, 255, 255)), + authored: None + }); + + properties.push(PropertyDeclaration::BorderTopColor(blue.clone())); + properties.push(PropertyDeclaration::BorderRightColor(blue.clone())); + properties.push(PropertyDeclaration::BorderBottomColor(blue.clone())); + properties.push(PropertyDeclaration::BorderLeftColor(blue.clone())); + + let serialization = shorthand_properties_to_string(properties); + assert_eq!(serialization, "border: 30px solid rgb(0, 0, 255);"); + } + #[test] fn padding_should_serialize_correctly() { let mut properties = Vec::new();