Auto merge of #15416 - karlding:servo-15395_border_serialization, r=emilio

Fix border shorthand serialization

Fix border shorthand serialization when sides are not identical. I think I managed to get the serialization to work as expected.

I added a check to ensure that the **border-width**, **border-style** and **border-color** were the same, before performing the serialization.

I'm still a Rust newbie, so if there's a more idiomatic way of doing things (or any critiques in general), please let me know!

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #15395

<!-- Either: -->
- [X] There are tests for these changes

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/15416)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-03-01 09:50:49 -08:00 committed by GitHub
commit f0257364c2
2 changed files with 97 additions and 7 deletions

View file

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
<%namespace name="helpers" file="/helpers.mako.rs" /> <%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", ${helpers.four_sides_shorthand("border-color", "border-%s-color", "specified::CSSColor::parse",
spec="https://drafts.csswg.org/css-backgrounds/#border-color")} 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> { impl<'a> LonghandsToSerialize<'a> {
fn to_css_declared<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css_declared<W>(&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, // If all longhands are all present, then all sides should be the same,
// so we can just one set of color/style/width // so we can just one set of color/style/width
super::serialize_directional_border( if all_equal {
dest, super::serialize_directional_border(
self.border_${side}_width, dest,
self.border_${side}_style, self.border_${side}_width,
self.border_${side}_color self.border_${side}_style,
) self.border_${side}_color
)
} else {
Ok(())
}
} }
} }

View file

@ -186,6 +186,72 @@ mod shorthand_serialization {
assert_eq!(serialization, "margin: 8px 12px 10px 14px;"); 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] #[test]
fn padding_should_serialize_correctly() { fn padding_should_serialize_correctly() {
let mut properties = Vec::new(); let mut properties = Vec::new();