From c9efa6e2a281442578d3a2e3c3e238a746e702c9 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Mon, 3 Oct 2016 17:01:00 +0530 Subject: [PATCH] Add to_css_single_value for serializing a single PropertyDeclarationBlock (fixes #13423) --- .../style/properties/properties.mako.rs | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index e55d3d1c06d..fa6c603ba7e 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -313,6 +313,42 @@ impl PropertyDeclarationBlock { } } +impl PropertyDeclarationBlock { + // Take a declaration block known to contain a single property, + // and serialize it + pub fn to_css_single_value(&self, dest: &mut W, name: &str) + -> fmt::Result where W: fmt::Write { + match self.declarations.len() { + 0 => Err(fmt::Error), + 1 if self.declarations[0].0.name().eq_str_ignore_ascii_case(name) => { + self.declarations[0].0.to_css(dest) + } + _ => { + // we use this function because a closure won't be `Clone` + fn get_declaration(dec: &(PropertyDeclaration, Importance)) + -> &PropertyDeclaration { + &dec.0 + } + let shorthand = try!(Shorthand::from_name(name).ok_or(fmt::Error)); + if !self.declarations.iter().all(|decl| decl.0.shorthands().contains(&shorthand)) { + return Err(fmt::Error) + } + let success = try!(shorthand.serialize_shorthand_to_buffer( + dest, + self.declarations.iter() + .map(get_declaration as fn(_) -> _), + &mut true) + ); + if success { + Ok(()) + } else { + Err(fmt::Error) + } + } + } + } +} + impl ToCss for PropertyDeclarationBlock { // https://drafts.csswg.org/cssom/#serialize-a-css-declaration-block fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { @@ -887,6 +923,16 @@ pub enum PropertyDeclarationName { Internal } +impl PropertyDeclarationName { + pub fn eq_str_ignore_ascii_case(&self, other: &str) -> bool { + match *self { + PropertyDeclarationName::Longhand(s) => s.eq_ignore_ascii_case(other), + PropertyDeclarationName::Custom(ref n) => n.eq_str_ignore_ascii_case(other), + PropertyDeclarationName::Internal => false + } + } +} + impl PartialEq for PropertyDeclarationName { fn eq(&self, other: &str) -> bool { match *self {