Bug 1355402 - Factor out implemantations for {min,max} size properties as a macro. r?manishearth
MozReview-Commit-ID: HlftlOMUJON
--- a/servo/components/style/properties/helpers.mako.rs
+++ b/servo/components/style/properties/helpers.mako.rs
@@ -1027,8 +1027,94 @@
},
(&T(None), &T(None)) => {
Ok(0.0)
},
}
}
}
</%def>
+
+// Define property that supports prefixed intrinsic size keyword values for gecko.
+// E.g. -moz-max-content, -moz-min-content, etc.
+<%def name="gecko_length_type(name, length_type, initial_value, logical, **kwargs)">
+ <%call expr="longhand(name,
+ predefined_type=length_type,
+ logical=logical,
+ **kwargs)">
+ use std::fmt;
+ use style_traits::ToCss;
+ use values::HasViewportPercentage;
+ use values::specified::{AllowQuirks, ${length_type}};
+
+ impl HasViewportPercentage for SpecifiedValue {
+ fn has_viewport_percentage(&self) -> bool {
+ self.0.has_viewport_percentage()
+ }
+ }
+
+ pub mod computed_value {
+ pub type T = ::values::computed::${length_type};
+ }
+
+ #[derive(PartialEq, Clone, Debug)]
+ #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
+ pub struct SpecifiedValue(${length_type});
+
+ #[inline]
+ pub fn get_initial_value() -> computed_value::T {
+ use values::computed::${length_type};
+ ${length_type}::${initial_value}
+ }
+ fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
+ % if logical:
+ let ret = ${length_type}::parse(context, input);
+ % else:
+ let ret = ${length_type}::parse_quirky(context, input, AllowQuirks::Yes);
+ % endif
+ // Keyword values don't make sense in the block direction; don't parse them
+ % if "block" in name:
+ if let Ok(${length_type}::ExtremumLength(..)) = ret {
+ return Err(())
+ }
+ % endif
+ ret.map(SpecifiedValue)
+ }
+
+ impl ToCss for SpecifiedValue {
+ fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
+ self.0.to_css(dest)
+ }
+ }
+
+ impl ToComputedValue for SpecifiedValue {
+ type ComputedValue = computed_value::T;
+ #[inline]
+ fn to_computed_value(&self, context: &Context) -> computed_value::T {
+ use values::computed::${length_type};
+ let computed = self.0.to_computed_value(context);
+
+ // filter out keyword values in the block direction
+ % if logical:
+ % if "block" in name:
+ if let ${length_type}::ExtremumLength(..) = computed {
+ return get_initial_value()
+ }
+ % endif
+ % else:
+ if let ${length_type}::ExtremumLength(..) = computed {
+ <% is_height = "true" if "height" in name else "false" %>
+ if ${is_height} != context.style().writing_mode.is_vertical() {
+ return get_initial_value()
+ }
+ }
+ % endif
+ computed
+ }
+
+ #[inline]
+ fn from_computed_value(computed: &computed_value::T) -> Self {
+ SpecifiedValue(ToComputedValue::from_computed_value(computed))
+ }
+ }
+ </%call>
+</%def>
+
--- a/servo/components/style/properties/longhand/position.mako.rs
+++ b/servo/components/style/properties/longhand/position.mako.rs
@@ -156,108 +156,26 @@
${helpers.predefined_type("%s" % size,
"LengthOrPercentageOrAuto",
"computed::LengthOrPercentageOrAuto::Auto",
"parse_non_negative",
spec=spec % size,
allow_quirks=not logical,
animation_value_type="ComputedValue", logical = logical)}
% if product == "gecko":
- % for min_max in ["min", "max"]:
- <%
- LengthType = "MaxLength" if "max" == min_max else "MozLength"
- initial = "none()" if "max" == min_max else "auto()"
- %>
-
- // min-width, min-height, min-block-size, min-inline-size,
- // max-width, max-height, max-block-size, max-inline-size
- //
- // Keyword values are only valid in the inline direction; they must
- // be replaced with auto/none in block.
- <%helpers:longhand name="${min_max}-${size}" spec="${spec % ('%s-%s' % (min_max, size))}"
- animation_value_type="ComputedValue"
- logical="${logical}" predefined_type="${LengthType}">
-
- use std::fmt;
- use style_traits::ToCss;
- use values::HasViewportPercentage;
- use values::specified::{AllowQuirks, ${LengthType}};
-
- impl HasViewportPercentage for SpecifiedValue {
- fn has_viewport_percentage(&self) -> bool {
- self.0.has_viewport_percentage()
- }
- }
-
- pub mod computed_value {
- pub type T = ::values::computed::${LengthType};
- }
-
- #[derive(PartialEq, Clone, Debug)]
- #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
- pub struct SpecifiedValue(${LengthType});
-
- #[inline]
- pub fn get_initial_value() -> computed_value::T {
- use values::computed::${LengthType};
- ${LengthType}::${initial}
- }
- fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
- % if logical:
- let ret = ${LengthType}::parse(context, input);
- % else:
- let ret = ${LengthType}::parse_quirky(context, input, AllowQuirks::Yes);
- % endif
- // Keyword values don't make sense in the block direction; don't parse them
- % if "block" in size:
- if let Ok(${LengthType}::ExtremumLength(..)) = ret {
- return Err(())
- }
- % endif
- ret.map(SpecifiedValue)
- }
-
- impl ToCss for SpecifiedValue {
- fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
- self.0.to_css(dest)
- }
- }
-
- impl ToComputedValue for SpecifiedValue {
- type ComputedValue = computed_value::T;
- #[inline]
- fn to_computed_value(&self, context: &Context) -> computed_value::T {
- use values::computed::${LengthType};
- let computed = self.0.to_computed_value(context);
-
- // filter out keyword values in the block direction
- % if logical:
- % if "block" in size:
- if let ${LengthType}::ExtremumLength(..) = computed {
- return get_initial_value()
- }
- % endif
- % else:
- if let ${LengthType}::ExtremumLength(..) = computed {
- <% is_height = "true" if "height" in size else "false" %>
- if ${is_height} != context.style().writing_mode.is_vertical() {
- return get_initial_value()
- }
- }
- % endif
- computed
- }
-
- #[inline]
- fn from_computed_value(computed: &computed_value::T) -> Self {
- SpecifiedValue(ToComputedValue::from_computed_value(computed))
- }
- }
- </%helpers:longhand>
- % endfor
+ // min-width, min-height, min-block-size, min-inline-size,
+ // max-width, max-height, max-block-size, max-inline-size
+ ${helpers.gecko_length_type("min-%s" % size, "MozLength", "auto()",
+ logical,
+ spec=spec % size,
+ animation_value_type="ComputedValue")}
+ ${helpers.gecko_length_type("max-%s" % size, "MaxLength", "none()",
+ logical,
+ spec=spec % size,
+ animation_value_type="ComputedValue")}
% else:
// servo versions (no keyword support)
${helpers.predefined_type("min-%s" % size,
"LengthOrPercentage",
"computed::LengthOrPercentage::Length(Au(0))",
"parse_non_negative",
spec=spec % ("min-%s" % size),
animation_value_type="ComputedValue",