From b6edae2f5e37c57245b150af349d6f7380a650cc Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Fri, 15 Jul 2016 14:42:08 +0530 Subject: [PATCH] Add allow_empty to vector_longhand --- components/style/properties/helpers.mako.rs | 52 +++++++++++++++------ 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/components/style/properties/helpers.mako.rs b/components/style/properties/helpers.mako.rs index 9db3d076fae..76543de7a6f 100644 --- a/components/style/properties/helpers.mako.rs +++ b/components/style/properties/helpers.mako.rs @@ -41,11 +41,11 @@ to True for cases where Servo takes a single value and Stylo supports vector values. - If the computed value is the same as the specified value, - setting computed_is_specified to True will introduce additional - optimizations + Setting allow_empty to False allows for cases where the vector + is empty. The grammar for these is usually "none | [ , ]*". + We assume that the default/initial value is an empty vector for these. -<%def name="vector_longhand(name, gecko_only=False, **kwargs)"> +<%def name="vector_longhand(name, gecko_only=False, allow_empty=False, **kwargs)"> <%call expr="longhand(name, **kwargs)"> % if product == "gecko" or not gecko_only: use cssparser::ToCss; @@ -68,10 +68,15 @@ impl ToCss for computed_value::T { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - if !self.0.is_empty() { - try!(self.0[0].to_css(dest)); + let mut iter = self.0.iter(); + if let Some(val) = iter.next() { + try!(val.to_css(dest)); + } else { + % if allow_empty: + try!(dest.write_str("none")); + % endif } - for i in self.0.iter().skip(1) { + for i in iter { try!(dest.write_str(", ")); try!(i.to_css(dest)); } @@ -85,10 +90,15 @@ impl ToCss for SpecifiedValue { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - if !self.0.is_empty() { - try!(self.0[0].to_css(dest)) + let mut iter = self.0.iter(); + if let Some(val) = iter.next() { + try!(val.to_css(dest)); + } else { + % if allow_empty: + try!(dest.write_str("none")); + % endif } - for i in self.0.iter().skip(1) { + for i in iter { try!(dest.write_str(", ")); try!(i.to_css(dest)); } @@ -96,12 +106,26 @@ } } pub fn get_initial_value() -> computed_value::T { - computed_value::T(vec![single_value::get_initial_value()]) + % if allow_empty: + computed_value::T(vec![]) + % else: + computed_value::T(vec![single_value::get_initial_value()]) + % endif } pub fn parse(context: &ParserContext, input: &mut Parser) -> Result { - input.parse_comma_separated(|parser| { - single_value::parse(context, parser) - }).map(|ok| SpecifiedValue(ok)) + % if allow_empty: + if input.try(|input| input.expect_ident_matching("none")).is_ok() { + Ok(SpecifiedValue(Vec::new())) + } else { + input.parse_comma_separated(|parser| { + single_value::parse(context, parser) + }).map(SpecifiedValue) + } + % else: + input.parse_comma_separated(|parser| { + single_value::parse(context, parser) + }).map(SpecifiedValue) + % endif } impl ToComputedValue for SpecifiedValue { type ComputedValue = computed_value::T;