Bug 1521405 - Cleanup a few shorthand properties. r=heycam
authorEmilio Cobos Álvarez <emilio@crisal.io>
Wed, 23 Jan 2019 17:38:55 +0100
changeset 515147 9820006aa8fbeff17a770cc81b7e3a90621188c0
parent 515146 92f342f036340c2a4c2e4e267197e3ff8c2d3d67
child 515148 830639112fdb75f9fa8deb72d7844e9d7e642f4a
push id1953
push userffxbld-merge
push dateMon, 11 Mar 2019 12:10:20 +0000
treeherdermozilla-release@9c35dcbaa899 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersheycam
bugs1521405
milestone66.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1521405 - Cleanup a few shorthand properties. r=heycam Differential Revision: https://phabricator.services.mozilla.com/D17082
servo/components/style/properties/helpers.mako.rs
servo/components/style/properties/shorthands/box.mako.rs
servo/components/style/properties/shorthands/margin.mako.rs
servo/components/style/properties/shorthands/padding.mako.rs
servo/components/style/properties/shorthands/position.mako.rs
--- a/servo/components/style/properties/helpers.mako.rs
+++ b/servo/components/style/properties/helpers.mako.rs
@@ -788,16 +788,68 @@
             })
         }
 
         ${caller.body()}
     }
     % endif
 </%def>
 
+// A shorthand of kind `<property-1> <property-2>?` where both properties have
+// the same type.
+<%def name="two_properties_shorthand(
+    name,
+    first_property,
+    second_property,
+    parser_function,
+    needs_context=True,
+    **kwargs
+)">
+<%call expr="self.shorthand(name, sub_properties=' '.join([first_property, second_property]), **kwargs)">
+    #[allow(unused_imports)]
+    use crate::parser::Parse;
+    use crate::values::specified;
+
+    pub fn parse_value<'i, 't>(
+        context: &ParserContext,
+        input: &mut Parser<'i, 't>,
+    ) -> Result<Longhands, ParseError<'i>> {
+        let parse_one = |_c: &ParserContext, input: &mut Parser<'i, 't>| {
+            % if needs_context:
+            ${parser_function}(_c, input)
+            % else:
+            ${parser_function}(input)
+            % endif
+        };
+
+        let first = parse_one(context, input)?;
+        let second =
+            input.try(|input| parse_one(context, input)).unwrap_or_else(|_| first.clone());
+        Ok(expanded! {
+            ${to_rust_ident(first_property)}: first,
+            ${to_rust_ident(second_property)}: second,
+        })
+    }
+
+    impl<'a> ToCss for LonghandsToSerialize<'a>  {
+        fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: fmt::Write {
+            let first = &self.${to_rust_ident(first_property)};
+            let second = &self.${to_rust_ident(second_property)};
+
+            first.to_css(dest)?;
+            if first != second {
+                dest.write_str(" ")?;
+                second.to_css(dest)?;
+            }
+            Ok(())
+        }
+    }
+</%call>
+</%def>
+
 <%def name="four_sides_shorthand(name, sub_property_pattern, parser_function,
                                  needs_context=True, allow_quirks=False, **kwargs)">
     <% sub_properties=' '.join(sub_property_pattern % side for side in PHYSICAL_SIDES) %>
     <%call expr="self.shorthand(name, sub_properties=sub_properties, **kwargs)">
         #[allow(unused_imports)]
         use crate::parser::Parse;
         use crate::values::generics::rect::Rect;
         use crate::values::specified;
--- a/servo/components/style/properties/shorthands/box.mako.rs
+++ b/servo/components/style/properties/shorthands/box.mako.rs
@@ -1,84 +1,36 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
 
 <%namespace name="helpers" file="/helpers.mako.rs" />
 
-<%helpers:shorthand
-    name="overflow"
-    flags="SHORTHAND_IN_GETCS"
-    sub_properties="overflow-x overflow-y"
-    spec="https://drafts.csswg.org/css-overflow/#propdef-overflow"
->
-    use crate::properties::longhands::overflow_x::parse as parse_overflow;
-
-    pub fn parse_value<'i, 't>(
-        context: &ParserContext,
-        input: &mut Parser<'i, 't>,
-    ) -> Result<Longhands, ParseError<'i>> {
-        let overflow_x = parse_overflow(context, input)?;
-        let overflow_y =
-            input.try(|i| parse_overflow(context, i)).unwrap_or(overflow_x);
-        Ok(expanded! {
-            overflow_x: overflow_x,
-            overflow_y: overflow_y,
-        })
-    }
-
-    impl<'a> ToCss for LonghandsToSerialize<'a>  {
-        fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: fmt::Write {
-            self.overflow_x.to_css(dest)?;
-            if self.overflow_x != self.overflow_y {
-                dest.write_char(' ')?;
-                self.overflow_y.to_css(dest)?;
-            }
-            Ok(())
-        }
-    }
-</%helpers:shorthand>
+${helpers.two_properties_shorthand(
+    "overflow",
+    "overflow-x",
+    "overflow-y",
+    "specified::Overflow::parse",
+    flags="SHORTHAND_IN_GETCS",
+    needs_context=False,
+    spec="https://drafts.csswg.org/css-overflow/#propdef-overflow",
+)}
 
-<%helpers:shorthand
-    name="overflow-clip-box"
-    sub_properties="overflow-clip-box-block overflow-clip-box-inline"
-    enabled_in="ua"
-    gecko_pref="layout.css.overflow-clip-box.enabled"
+${helpers.two_properties_shorthand(
+    "overflow-clip-box",
+    "overflow-clip-box-block",
+    "overflow-clip-box-inline",
+    "specified::OverflowClipBox::parse",
+    enabled_in="ua",
+    needs_context=False,
+    gecko_pref="layout.css.overflow-clip-box.enabled",
     spec="Internal, may be standardized in the future "
-         "(https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-clip-box)"
-    products="gecko"
->
-    use crate::values::specified::OverflowClipBox;
-    pub fn parse_value<'i, 't>(
-        _: &ParserContext,
-        input: &mut Parser<'i, 't>,
-    ) -> Result<Longhands, ParseError<'i>> {
-        let block_value = OverflowClipBox::parse(input)?;
-        let inline_value =
-            input.try(|input| OverflowClipBox::parse(input)).unwrap_or(block_value);
-
-        Ok(expanded! {
-          overflow_clip_box_block: block_value,
-          overflow_clip_box_inline: inline_value,
-        })
-    }
-
-    impl<'a> ToCss for LonghandsToSerialize<'a>  {
-        fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: fmt::Write {
-            self.overflow_clip_box_block.to_css(dest)?;
-
-            if self.overflow_clip_box_block != self.overflow_clip_box_inline {
-                dest.write_str(" ")?;
-                self.overflow_clip_box_inline.to_css(dest)?;
-            }
-
-            Ok(())
-        }
-    }
-</%helpers:shorthand>
+         "(https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-clip-box)",
+    products="gecko",
+)}
 
 macro_rules! try_parse_one {
     ($context: expr, $input: expr, $var: ident, $prop_module: ident) => {
         if $var.is_none() {
             if let Ok(value) = $input.try(|i| {
                 $prop_module::single_value::parse($context, i)
             }) {
                 $var = Some(value);
@@ -376,46 +328,26 @@ macro_rules! try_parse_one {
                 self.scroll_snap_type_x.to_css(dest)
             } else {
                 Ok(())
             }
         }
     }
 </%helpers:shorthand>
 
-<%helpers:shorthand name="overscroll-behavior" products="gecko"
-                    gecko_pref="layout.css.overscroll-behavior.enabled"
-                    sub_properties="overscroll-behavior-x overscroll-behavior-y"
-                    spec="https://wicg.github.io/overscroll-behavior/#overscroll-behavior-properties">
-    pub fn parse_value<'i, 't>(
-        _: &ParserContext,
-        input: &mut Parser<'i, 't>,
-    ) -> Result<Longhands, ParseError<'i>> {
-        use crate::values::specified::OverscrollBehavior;
-        let behavior_x = OverscrollBehavior::parse(input)?;
-        let behavior_y = input.try(OverscrollBehavior::parse).unwrap_or(behavior_x);
-        Ok(expanded! {
-            overscroll_behavior_x: behavior_x,
-            overscroll_behavior_y: behavior_y,
-        })
-    }
-
-    impl<'a> ToCss for LonghandsToSerialize<'a> {
-        // Serializes into the single keyword value if both overscroll-behavior-x and overscroll-behavior-y are same.
-        // Otherwise into two values separated by a space.
-        fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: fmt::Write {
-            self.overscroll_behavior_x.to_css(dest)?;
-            if self.overscroll_behavior_y != self.overscroll_behavior_x {
-                dest.write_str(" ")?;
-                self.overscroll_behavior_y.to_css(dest)?;
-            }
-            Ok(())
-        }
-    }
-</%helpers:shorthand>
+${helpers.two_properties_shorthand(
+    "overscroll-behavior",
+    "overscroll-behavior-x",
+    "overscroll-behavior-y",
+    "specified::OverscrollBehavior::parse",
+    needs_context=False,
+    products="gecko",
+    gecko_pref="layout.css.overscroll-behavior.enabled",
+    spec="https://wicg.github.io/overscroll-behavior/#overscroll-behavior-properties",
+)}
 
 <%helpers:shorthand
     name="page-break-before"
     products="gecko"
     flags="SHORTHAND_IN_GETCS IS_LEGACY_SHORTHAND"
     sub_properties="break-before"
     spec="https://drafts.csswg.org/css2/page.html#propdef-page-break-before"
 >
--- a/servo/components/style/properties/shorthands/margin.mako.rs
+++ b/servo/components/style/properties/shorthands/margin.mako.rs
@@ -1,54 +1,30 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
 
 <%namespace name="helpers" file="/helpers.mako.rs" />
 
-${helpers.four_sides_shorthand("margin", "margin-%s", "specified::LengthPercentageOrAuto::parse",
-                               spec="https://drafts.csswg.org/css-box/#propdef-margin",
-                               allowed_in_page_rule=True,
-                               allow_quirks=True)}
-
-% for axis in ["block", "inline"]:
-    <%
-        spec = "https://drafts.csswg.org/css-logical/#propdef-margin-%s" % axis
-    %>
-    <%helpers:shorthand
-        name="margin-${axis}"
-        sub_properties="${' '.join(
-            'margin-%s-%s' % (axis, side)
-            for side in ['start', 'end']
-        )}"
-        spec="${spec}">
+${helpers.four_sides_shorthand(
+    "margin",
+    "margin-%s",
+    "specified::LengthPercentageOrAuto::parse",
+    spec="https://drafts.csswg.org/css-box/#propdef-margin",
+    allowed_in_page_rule=True,
+    allow_quirks=True,
+)}
 
-        use crate::parser::Parse;
-        use crate::values::specified::length::LengthPercentageOrAuto;
-        pub fn parse_value<'i, 't>(
-            context: &ParserContext,
-            input: &mut Parser<'i, 't>,
-        ) -> Result<Longhands, ParseError<'i>> {
-            let start_value = LengthPercentageOrAuto::parse(context, input)?;
-            let end_value =
-                input.try(|input| LengthPercentageOrAuto::parse(context, input))
-                    .unwrap_or_else(|_| start_value.clone());
+${helpers.two_properties_shorthand(
+    "margin-block",
+    "margin-block-start",
+    "margin-block-end",
+    "specified::LengthPercentageOrAuto::parse",
+    spec="https://drafts.csswg.org/css-logical/#propdef-margin-block"
+)}
 
-            Ok(expanded! {
-                margin_${axis}_start: start_value,
-                margin_${axis}_end: end_value,
-            })
-        }
-
-        impl<'a> ToCss for LonghandsToSerialize<'a>  {
-            fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: fmt::Write {
-                self.margin_${axis}_start.to_css(dest)?;
-
-                if self.margin_${axis}_end != self.margin_${axis}_start {
-                    dest.write_str(" ")?;
-                    self.margin_${axis}_end.to_css(dest)?;
-                }
-
-                Ok(())
-            }
-        }
-    </%helpers:shorthand>
-% endfor
+${helpers.two_properties_shorthand(
+    "margin-inline",
+    "margin-inline-start",
+    "margin-inline-end",
+    "specified::LengthPercentageOrAuto::parse",
+    spec="https://drafts.csswg.org/css-logical/#propdef-margin-inline"
+)}
--- a/servo/components/style/properties/shorthands/padding.mako.rs
+++ b/servo/components/style/properties/shorthands/padding.mako.rs
@@ -1,53 +1,29 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
 
 <%namespace name="helpers" file="/helpers.mako.rs" />
 
-${helpers.four_sides_shorthand("padding", "padding-%s", "specified::NonNegativeLengthPercentage::parse",
-                               spec="https://drafts.csswg.org/css-box-3/#propdef-padding",
-                               allow_quirks=True)}
-
-% for axis in ["block", "inline"]:
-    <%
-        spec = "https://drafts.csswg.org/css-logical/#propdef-padding-%s" % axis
-    %>
-    <%helpers:shorthand
-        name="padding-${axis}"
-        sub_properties="${' '.join(
-            'padding-%s-%s' % (axis, side)
-            for side in ['start', 'end']
-        )}"
-        spec="${spec}">
+${helpers.four_sides_shorthand(
+    "padding",
+    "padding-%s",
+    "specified::NonNegativeLengthPercentage::parse",
+    spec="https://drafts.csswg.org/css-box-3/#propdef-padding",
+    allow_quirks=True,
+)}
 
-        use crate::parser::Parse;
-        use crate::values::specified::length::NonNegativeLengthPercentage;
-        pub fn parse_value<'i, 't>(
-            context: &ParserContext,
-            input: &mut Parser<'i, 't>,
-        ) -> Result<Longhands, ParseError<'i>> {
-            let start_value = NonNegativeLengthPercentage::parse(context, input)?;
-            let end_value =
-                input.try(|input| NonNegativeLengthPercentage::parse(context, input))
-                    .unwrap_or_else(|_| start_value.clone());
+${helpers.two_properties_shorthand(
+    "padding-block",
+    "padding-block-start",
+    "padding-block-end",
+    "specified::NonNegativeLengthPercentage::parse",
+    spec="https://drafts.csswg.org/css-logical/#propdef-padding-block"
+)}
 
-            Ok(expanded! {
-                padding_${axis}_start: start_value,
-                padding_${axis}_end: end_value,
-            })
-        }
-
-        impl<'a> ToCss for LonghandsToSerialize<'a>  {
-            fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: fmt::Write {
-                self.padding_${axis}_start.to_css(dest)?;
-
-                if self.padding_${axis}_end != self.padding_${axis}_start {
-                    dest.write_str(" ")?;
-                    self.padding_${axis}_end.to_css(dest)?;
-                }
-
-                Ok(())
-            }
-        }
-    </%helpers:shorthand>
-% endfor
+${helpers.two_properties_shorthand(
+    "padding-inline",
+    "padding-inline-start",
+    "padding-inline-end",
+    "specified::NonNegativeLengthPercentage::parse",
+    spec="https://drafts.csswg.org/css-logical/#propdef-padding-inline"
+)}
--- a/servo/components/style/properties/shorthands/position.mako.rs
+++ b/servo/components/style/properties/shorthands/position.mako.rs
@@ -767,51 +767,23 @@
 ${helpers.four_sides_shorthand(
     "inset",
     "%s",
     "specified::LengthPercentageOrAuto::parse",
     spec="https://drafts.csswg.org/css-logical/#propdef-inset",
     allow_quirks=False,
 )}
 
-% for axis in ["block", "inline"]:
-    <%
-        spec = "https://drafts.csswg.org/css-logical/#propdef-inset-%s" % axis
-    %>
-    <%helpers:shorthand
-        name="inset-${axis}"
-        sub_properties="${' '.join(
-            'inset-%s-%s' % (axis, side)
-            for side in ['start', 'end']
-        )}"
-        spec="${spec}">
+${helpers.two_properties_shorthand(
+    "inset-block",
+    "inset-block-start",
+    "inset-block-end",
+    "specified::LengthPercentageOrAuto::parse",
+    spec="https://drafts.csswg.org/css-logical/#propdef-inset-block"
+)}
 
-        use crate::parser::Parse;
-        use crate::values::specified::length::LengthPercentageOrAuto;
-        pub fn parse_value<'i, 't>(
-            context: &ParserContext,
-            input: &mut Parser<'i, 't>,
-        ) -> Result<Longhands, ParseError<'i>> {
-            let start_value = LengthPercentageOrAuto::parse(context, input)?;
-            let end_value =
-                input.try(|input| LengthPercentageOrAuto::parse(context, input))
-                    .unwrap_or_else(|_| start_value.clone());
-
-            Ok(expanded! {
-                inset_${axis}_start: start_value,
-                inset_${axis}_end: end_value,
-            })
-        }
-
-        impl<'a> ToCss for LonghandsToSerialize<'a>  {
-            fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: fmt::Write {
-                self.inset_${axis}_start.to_css(dest)?;
-
-                if self.inset_${axis}_end != self.inset_${axis}_start {
-                    dest.write_str(" ")?;
-                    self.inset_${axis}_end.to_css(dest)?;
-                }
-
-                Ok(())
-            }
-        }
-    </%helpers:shorthand>
-% endfor
+${helpers.two_properties_shorthand(
+    "inset-inline",
+    "inset-inline-start",
+    "inset-inline-end",
+    "specified::LengthPercentageOrAuto::parse",
+    spec="https://drafts.csswg.org/css-logical/#propdef-inset-inline"
+)}