Merge common arms in PropertyDeclaration::to_css

This commit is contained in:
Anthony Ramine 2018-02-05 21:50:17 +01:00
parent 42ea822edf
commit 8ebf0f057f

View file

@ -314,6 +314,24 @@ impl Clone for PropertyDeclaration {
}
}
impl PropertyDeclaration {
/// Like the method on ToCss, but without the type parameter to avoid
/// accidentally monomorphizing this large function multiple times for
/// different writers.
pub fn to_css(&self, dest: &mut CssStringWriter) -> fmt::Result {
use self::PropertyDeclaration::*;
let mut dest = CssWriter::new(dest);
match *self {
% for ty, variants in groups.iteritems():
${" | ".join("{}(ref value)".format(v) for v in variants)} => {
value.to_css(&mut dest)
}
% endfor
}
}
}
/// A longhand or shorthand porperty
#[derive(Clone, Copy, Debug)]
pub struct NonCustomPropertyId(usize);
@ -1563,6 +1581,15 @@ pub struct WideKeywordDeclaration {
keyword: CSSWideKeyword,
}
impl ToCss for WideKeywordDeclaration {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: fmt::Write,
{
self.keyword.to_css(dest)
}
}
/// An unparsed declaration that contains `var()` functions.
#[cfg_attr(feature = "gecko", derive(MallocSizeOf))]
#[derive(Clone, PartialEq)]
@ -1572,6 +1599,29 @@ pub struct VariableDeclaration {
value: Arc<UnparsedValue>,
}
impl ToCss for VariableDeclaration {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: fmt::Write,
{
// https://drafts.csswg.org/css-variables/#variables-in-shorthands
match self.value.from_shorthand {
// Normally, we shouldn't be printing variables here if they came from
// shorthands. But we should allow properties that came from shorthand
// aliases. That also matches with the Gecko behavior.
// FIXME(emilio): This is just a hack for `-moz-transform`.
Some(shorthand) if shorthand.flags().contains(PropertyFlags::SHORTHAND_ALIAS_PROPERTY) => {
dest.write_str(&*self.value.css)?
}
None => {
dest.write_str(&*self.value.css)?
}
_ => {},
}
Ok(())
}
}
/// A custom property declaration with the property name and the declared value.
#[cfg_attr(feature = "gecko", derive(MallocSizeOf))]
#[derive(Clone, PartialEq)]
@ -1583,6 +1633,15 @@ pub struct CustomDeclaration {
pub value: DeclaredValueOwned<Arc<::custom_properties::SpecifiedValue>>,
}
impl ToCss for CustomDeclaration {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: fmt::Write,
{
self.value.borrow().to_css(dest)
}
}
impl fmt::Debug for PropertyDeclaration {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.id().to_css(&mut CssWriter::new(f))?;
@ -1597,40 +1656,6 @@ impl fmt::Debug for PropertyDeclaration {
}
}
impl PropertyDeclaration {
/// Like the method on ToCss, but without the type parameter to avoid
/// accidentally monomorphizing this large function multiple times for
/// different writers.
pub fn to_css(&self, dest: &mut CssStringWriter) -> fmt::Result {
match *self {
% for property in data.longhands:
PropertyDeclaration::${property.camel_case}(ref value) => {
value.to_css(&mut CssWriter::new(dest))
}
% endfor
PropertyDeclaration::CSSWideKeyword(ref declaration) => {
declaration.keyword.to_css(&mut CssWriter::new(dest))
},
PropertyDeclaration::WithVariables(ref declaration) => {
// https://drafts.csswg.org/css-variables/#variables-in-shorthands
match declaration.value.from_shorthand {
// Normally, we shouldn't be printing variables here if they came from
// shorthands. But we should allow properties that came from shorthand
// aliases. That also matches with the Gecko behavior.
Some(shorthand) if shorthand.flags().contains(PropertyFlags::SHORTHAND_ALIAS_PROPERTY) =>
dest.write_str(&*declaration.value.css)?,
None => dest.write_str(&*declaration.value.css)?,
_ => {},
}
Ok(())
},
PropertyDeclaration::Custom(ref declaration) => {
declaration.value.borrow().to_css(&mut CssWriter::new(dest))
},
}
}
}
impl PropertyDeclaration {
/// Given a property declaration, return the property declaration id.
pub fn id(&self) -> PropertyDeclarationId {