mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
style: Move the shorthand serialization code to its own function.
This commit is contained in:
parent
5c2ac8cf8b
commit
f3e38aca1b
1 changed files with 54 additions and 46 deletions
|
@ -304,6 +304,50 @@ impl PropertyDeclarationBlock {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn shorthand_to_css(
|
||||||
|
&self,
|
||||||
|
shorthand: ShorthandId,
|
||||||
|
dest: &mut CssStringWriter,
|
||||||
|
) -> fmt::Result {
|
||||||
|
// Step 1.2.1 of
|
||||||
|
// https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertyvalue
|
||||||
|
let mut list = SmallVec::<[&_; 10]>::new();
|
||||||
|
let mut important_count = 0;
|
||||||
|
|
||||||
|
// Step 1.2.2
|
||||||
|
for &longhand in shorthand.longhands() {
|
||||||
|
// Step 1.2.2.1
|
||||||
|
let declaration = self.get(PropertyDeclarationId::Longhand(longhand));
|
||||||
|
|
||||||
|
// Step 1.2.2.2 & 1.2.2.3
|
||||||
|
match declaration {
|
||||||
|
Some((declaration, importance)) => {
|
||||||
|
list.push(declaration);
|
||||||
|
if importance.important() {
|
||||||
|
important_count += 1;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => return Ok(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there is one or more longhand with important, and one or more
|
||||||
|
// without important, we don't serialize it as a shorthand.
|
||||||
|
if important_count > 0 && important_count != list.len() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 1.2.3
|
||||||
|
// We don't print !important when serializing individual properties,
|
||||||
|
// so we treat this as a normal-importance property
|
||||||
|
match shorthand.get_shorthand_appendable_value(list.iter().cloned()) {
|
||||||
|
Some(appendable_value) => {
|
||||||
|
append_declaration_value(dest, appendable_value)
|
||||||
|
}
|
||||||
|
None => return Ok(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Find the value of the given property in this block and serialize it
|
/// Find the value of the given property in this block and serialize it
|
||||||
///
|
///
|
||||||
/// <https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertyvalue>
|
/// <https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertyvalue>
|
||||||
|
@ -311,53 +355,17 @@ impl PropertyDeclarationBlock {
|
||||||
// Step 1.1: done when parsing a string to PropertyId
|
// Step 1.1: done when parsing a string to PropertyId
|
||||||
|
|
||||||
// Step 1.2
|
// Step 1.2
|
||||||
match property.as_shorthand() {
|
let longhand_or_custom = match property.as_shorthand() {
|
||||||
Ok(shorthand) => {
|
Ok(shorthand) => return self.shorthand_to_css(shorthand, dest),
|
||||||
// Step 1.2.1
|
Err(longhand_or_custom) => longhand_or_custom,
|
||||||
let mut list = Vec::new();
|
};
|
||||||
let mut important_count = 0;
|
|
||||||
|
|
||||||
// Step 1.2.2
|
if let Some((value, _importance)) = self.get(longhand_or_custom) {
|
||||||
for &longhand in shorthand.longhands() {
|
// Step 2
|
||||||
// Step 1.2.2.1
|
value.to_css(dest)
|
||||||
let declaration = self.get(PropertyDeclarationId::Longhand(longhand));
|
} else {
|
||||||
|
// Step 3
|
||||||
// Step 1.2.2.2 & 1.2.2.3
|
Ok(())
|
||||||
match declaration {
|
|
||||||
Some((declaration, importance)) => {
|
|
||||||
list.push(declaration);
|
|
||||||
if importance.important() {
|
|
||||||
important_count += 1;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None => return Ok(()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If there is one or more longhand with important, and one or more
|
|
||||||
// without important, we don't serialize it as a shorthand.
|
|
||||||
if important_count > 0 && important_count != list.len() {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 1.2.3
|
|
||||||
// We don't print !important when serializing individual properties,
|
|
||||||
// so we treat this as a normal-importance property
|
|
||||||
match shorthand.get_shorthand_appendable_value(list) {
|
|
||||||
Some(appendable_value) =>
|
|
||||||
append_declaration_value(dest, appendable_value),
|
|
||||||
None => return Ok(()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(longhand_or_custom) => {
|
|
||||||
if let Some((value, _importance)) = self.get(longhand_or_custom) {
|
|
||||||
// Step 2
|
|
||||||
value.to_css(dest)
|
|
||||||
} else {
|
|
||||||
// Step 3
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue