style: Move the shorthand serialization code to its own function.

This commit is contained in:
Emilio Cobos Álvarez 2018-02-14 20:17:34 +01:00
parent 5c2ac8cf8b
commit f3e38aca1b
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C

View file

@ -304,17 +304,14 @@ impl PropertyDeclarationBlock {
}) })
} }
/// Find the value of the given property in this block and serialize it fn shorthand_to_css(
/// &self,
/// <https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertyvalue> shorthand: ShorthandId,
pub fn property_value_to_css(&self, property: &PropertyId, dest: &mut CssStringWriter) -> fmt::Result { dest: &mut CssStringWriter,
// Step 1.1: done when parsing a string to PropertyId ) -> fmt::Result {
// Step 1.2.1 of
// Step 1.2 // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertyvalue
match property.as_shorthand() { let mut list = SmallVec::<[&_; 10]>::new();
Ok(shorthand) => {
// Step 1.2.1
let mut list = Vec::new();
let mut important_count = 0; let mut important_count = 0;
// Step 1.2.2 // Step 1.2.2
@ -343,13 +340,26 @@ impl PropertyDeclarationBlock {
// Step 1.2.3 // Step 1.2.3
// We don't print !important when serializing individual properties, // We don't print !important when serializing individual properties,
// so we treat this as a normal-importance property // so we treat this as a normal-importance property
match shorthand.get_shorthand_appendable_value(list) { match shorthand.get_shorthand_appendable_value(list.iter().cloned()) {
Some(appendable_value) => Some(appendable_value) => {
append_declaration_value(dest, appendable_value), append_declaration_value(dest, appendable_value)
}
None => return Ok(()), None => return Ok(()),
} }
} }
Err(longhand_or_custom) => {
/// Find the value of the given property in this block and serialize it
///
/// <https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertyvalue>
pub fn property_value_to_css(&self, property: &PropertyId, dest: &mut CssStringWriter) -> fmt::Result {
// Step 1.1: done when parsing a string to PropertyId
// Step 1.2
let longhand_or_custom = match property.as_shorthand() {
Ok(shorthand) => return self.shorthand_to_css(shorthand, dest),
Err(longhand_or_custom) => longhand_or_custom,
};
if let Some((value, _importance)) = self.get(longhand_or_custom) { if let Some((value, _importance)) = self.get(longhand_or_custom) {
// Step 2 // Step 2
value.to_css(dest) value.to_css(dest)
@ -358,8 +368,6 @@ impl PropertyDeclarationBlock {
Ok(()) Ok(())
} }
} }
}
}
/// <https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-getpropertypriority> /// <https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-getpropertypriority>
pub fn property_priority(&self, property: &PropertyId) -> Importance { pub fn property_priority(&self, property: &PropertyId) -> Importance {