From b1a3831bf61ed99e676f702b6a9be3f2e41af7ac Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Fri, 24 Jun 2016 16:29:39 +0530 Subject: [PATCH] Add framework for properties which are single elements in servo and arrays in gecko --- components/style/properties/helpers.mako.rs | 75 +++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/components/style/properties/helpers.mako.rs b/components/style/properties/helpers.mako.rs index dae60af0860..92e0ca5f336 100644 --- a/components/style/properties/helpers.mako.rs +++ b/components/style/properties/helpers.mako.rs @@ -33,6 +33,81 @@ +<%doc> + To be used in cases where we have a grammar like + " [ , ]*", but only support a single value + in servo + +<%def name="gecko_autoarray_longhand(name, **kwargs)"> + <%call expr="longhand(name, **kwargs)"> + % if product == "gecko": + use cssparser::ToCss; + use std::fmt; + + pub mod single_value { + use cssparser::Parser; + use parser::{ParserContext, ParserContextExtraData}; + use properties::{CSSWideKeyword, DeclaredValue, Shorthand}; + use values::computed::{TContext, ToComputedValue}; + use values::{computed, specified}; + ${caller.body()} + } + pub mod computed_value { + use super::single_value; + #[derive(Debug, Clone, PartialEq, HeapSizeOf)] + pub struct T(pub Vec); + } + + impl ToCss for computed_value::T { + fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + if self.0.len() >= 1 { + try!(self.0[0].to_css(dest)); + } + for i in self.0.iter().skip(1) { + try!(dest.write_str(", ")); + try!(i.to_css(dest)); + } + Ok(()) + } + } + + #[derive(Debug, Clone, PartialEq, HeapSizeOf)] + pub struct SpecifiedValue(pub Vec); + + impl ToCss for SpecifiedValue { + fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + if self.0.len() >= 1 { + try!(self.0[0].to_css(dest)) + } + for i in self.0.iter().skip(1) { + try!(dest.write_str(", ")); + try!(i.to_css(dest)); + } + Ok(()) + } + } + pub fn get_initial_value() -> computed_value::T { + computed_value::T(vec![single_value::get_initial_value()]) + } + pub fn parse(context: &ParserContext, input: &mut Parser) -> Result { + input.parse_comma_separated(|parser| { + single_value::parse(context, parser) + }).map(|ok| SpecifiedValue(ok)) + } + impl ToComputedValue for SpecifiedValue { + type ComputedValue = computed_value::T; + + #[inline] + fn to_computed_value(&self, context: &Cx) -> computed_value::T { + computed_value::T(self.0.iter().map(|x| x.to_computed_value(context)).collect()) + } + } + % else: + ${caller.body()} + % endif + + + <%def name="raw_longhand(*args, **kwargs)"> <% property = data.declare_longhand(*args, **kwargs)