Derive ToCss for some shorthand properties

This commit is contained in:
Anthony Ramine 2017-06-17 11:32:02 +02:00
parent 3a2e79274a
commit a568f7752f
7 changed files with 33 additions and 93 deletions

View file

@ -764,7 +764,7 @@
% endif
</%def>
<%def name="shorthand(name, sub_properties, experimental=False, **kwargs)">
<%def name="shorthand(name, sub_properties, experimental=False, derive_serialize=False, **kwargs)">
<%
shorthand = data.declare_shorthand(name, sub_properties.split(), experimental=experimental,
**kwargs)
@ -778,9 +778,12 @@
use properties::{ShorthandId, LonghandId, UnparsedValue, longhands};
#[allow(unused_imports)]
use selectors::parser::SelectorParseError;
#[allow(unused_imports)]
use std::fmt;
use stylearc::Arc;
use style_traits::{ToCss, ParseError, StyleParseError};
use style_traits::{ParseError, StyleParseError};
#[allow(unused_imports)]
use style_traits::ToCss;
pub struct Longhands {
% for sub_property in shorthand.sub_properties:
@ -798,6 +801,9 @@
/// Represents a serializable set of all of the longhand properties that
/// correspond to a shorthand.
% if derive_serialize:
#[derive(ToCss)]
% endif
pub struct LonghandsToSerialize<'a> {
% for sub_property in shorthand.sub_properties:
pub ${sub_property.ident}:

View file

@ -334,6 +334,7 @@ macro_rules! try_parse_one {
<%helpers:shorthand name="-moz-transform" products="gecko"
sub_properties="transform"
flags="SHORTHAND_ALIAS_PROPERTY"
derive_serialize="True"
spec="Non-standard: https://developer.mozilla.org/en-US/docs/Web/CSS/transform">
use properties::longhands::transform;
@ -343,10 +344,4 @@ macro_rules! try_parse_one {
transform: transform::parse_prefixed(context, input)?,
})
}
impl<'a> ToCss for LonghandsToSerialize<'a> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.transform.to_css(dest)
}
}
</%helpers:shorthand>

View file

@ -4,7 +4,10 @@
<%namespace name="helpers" file="/helpers.mako.rs" />
<%helpers:shorthand name="columns" sub_properties="column-count column-width" experimental="True"
<%helpers:shorthand name="columns"
sub_properties="column-width column-count"
experimental="True"
derive_serialize="True"
extra_prefixes="moz" spec="https://drafts.csswg.org/css-multicol/#propdef-columns">
use properties::longhands::{column_count, column_width};
@ -49,19 +52,11 @@
})
}
}
impl<'a> ToCss for LonghandsToSerialize<'a> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(self.column_width.to_css(dest));
try!(write!(dest, " "));
self.column_count.to_css(dest)
}
}
</%helpers:shorthand>
<%helpers:shorthand name="column-rule" products="gecko" extra_prefixes="moz"
sub_properties="column-rule-width column-rule-style column-rule-color"
derive_serialize="True"
spec="https://drafts.csswg.org/css-multicol/#propdef-column-rule">
use properties::longhands::{column_rule_width, column_rule_style};
use properties::longhands::column_rule_color;
@ -97,14 +92,4 @@
Err(StyleParseError::UnspecifiedError.into())
}
}
impl<'a> ToCss for LonghandsToSerialize<'a> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.column_rule_width.to_css(dest)?;
dest.write_str(" ")?;
self.column_rule_style.to_css(dest)?;
dest.write_str(" ")?;
self.column_rule_color.to_css(dest)
}
}
</%helpers:shorthand>

View file

@ -4,8 +4,9 @@
<%namespace name="helpers" file="/helpers.mako.rs" />
<%helpers:shorthand name="text-emphasis" products="gecko" sub_properties="text-emphasis-color
text-emphasis-style"
<%helpers:shorthand name="text-emphasis" products="gecko"
sub_properties="text-emphasis-style text-emphasis-color"
derive_serialize="True"
spec="https://drafts.csswg.org/css-text-decor-3/#text-emphasis-property">
use properties::longhands::{text_emphasis_color, text_emphasis_style};
@ -38,22 +39,15 @@
Err(StyleParseError::UnspecifiedError.into())
}
}
impl<'a> ToCss for LonghandsToSerialize<'a> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.text_emphasis_style.to_css(dest)?;
dest.write_str(" ")?;
self.text_emphasis_color.to_css(dest)
}
}
</%helpers:shorthand>
// CSS Compatibility
// https://compat.spec.whatwg.org/
<%helpers:shorthand name="-webkit-text-stroke"
sub_properties="-webkit-text-stroke-color
-webkit-text-stroke-width"
sub_properties="-webkit-text-stroke-width
-webkit-text-stroke-color"
products="gecko"
derive_serialize="True"
spec="https://compat.spec.whatwg.org/#the-webkit-text-stroke">
use properties::longhands::{_webkit_text_stroke_color, _webkit_text_stroke_width};
@ -87,12 +81,4 @@
Err(StyleParseError::UnspecifiedError.into())
}
}
impl<'a> ToCss for LonghandsToSerialize<'a> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self._webkit_text_stroke_width.to_css(dest)?;
dest.write_str(" ")?;
self._webkit_text_stroke_color.to_css(dest)
}
}
</%helpers:shorthand>

View file

@ -5,7 +5,8 @@
<%namespace name="helpers" file="/helpers.mako.rs" />
<%helpers:shorthand name="list-style"
sub_properties="list-style-image list-style-position list-style-type"
sub_properties="list-style-position list-style-image list-style-type"
derive_serialize="True"
spec="https://drafts.csswg.org/css-lists/#propdef-list-style">
use properties::longhands::{list_style_image, list_style_position, list_style_type};
use values::{Either, None_};
@ -108,14 +109,4 @@
_ => Err(StyleParseError::UnspecifiedError.into()),
}
}
impl<'a> ToCss for LonghandsToSerialize<'a> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.list_style_position.to_css(dest)?;
dest.write_str(" ")?;
self.list_style_image.to_css(dest)?;
dest.write_str(" ")?;
self.list_style_type.to_css(dest)
}
}
</%helpers:shorthand>

View file

@ -4,7 +4,9 @@
<%namespace name="helpers" file="/helpers.mako.rs" />
<%helpers:shorthand name="outline" sub_properties="outline-color outline-style outline-width"
<%helpers:shorthand name="outline"
sub_properties="outline-width outline-style outline-color"
derive_serialize="True"
spec="https://drafts.csswg.org/css-ui/#propdef-outline">
use properties::longhands::{outline_color, outline_width, outline_style};
use values::specified;
@ -51,16 +53,6 @@
Err(StyleParseError::UnspecifiedError.into())
}
}
impl<'a> ToCss for LonghandsToSerialize<'a> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(self.outline_width.to_css(dest));
try!(write!(dest, " "));
try!(self.outline_style.to_css(dest));
try!(write!(dest, " "));
self.outline_color.to_css(dest)
}
}
</%helpers:shorthand>
// The -moz-outline-radius shorthand is non-standard and not on a standards track.

View file

@ -4,7 +4,10 @@
<%namespace name="helpers" file="/helpers.mako.rs" />
<%helpers:shorthand name="flex-flow" sub_properties="flex-direction flex-wrap" extra_prefixes="webkit"
<%helpers:shorthand name="flex-flow"
sub_properties="flex-direction flex-wrap"
extra_prefixes="webkit"
derive_serialize="True"
spec="https://drafts.csswg.org/css-flexbox/#flex-flow-property">
use properties::longhands::{flex_direction, flex_wrap};
@ -36,18 +39,12 @@
flex_wrap: unwrap_or_initial!(flex_wrap, wrap),
})
}
impl<'a> ToCss for LonghandsToSerialize<'a> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.flex_direction.to_css(dest)?;
dest.write_str(" ")?;
self.flex_wrap.to_css(dest)
}
}
</%helpers:shorthand>
<%helpers:shorthand name="flex" sub_properties="flex-grow flex-shrink flex-basis" extra_prefixes="webkit"
<%helpers:shorthand name="flex"
sub_properties="flex-grow flex-shrink flex-basis"
extra_prefixes="webkit"
derive_serialize="True"
spec="https://drafts.csswg.org/css-flexbox/#flex-property">
use values::specified::Number;
@ -101,18 +98,6 @@
flex_basis: basis.unwrap_or(longhands::flex_basis::SpecifiedValue::zero_percent()),
})
}
impl<'a> ToCss for LonghandsToSerialize<'a> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(self.flex_grow.to_css(dest));
try!(dest.write_str(" "));
try!(self.flex_shrink.to_css(dest));
try!(dest.write_str(" "));
self.flex_basis.to_css(dest)
}
}
</%helpers:shorthand>
<%helpers:shorthand name="grid-gap" sub_properties="grid-row-gap grid-column-gap"