style: Correctly track whether a longhand contains any variable.

This commit is contained in:
Emilio Cobos Álvarez 2017-03-26 17:45:53 +02:00
parent 05b5aabc69
commit 835d95eb41
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
2 changed files with 63 additions and 36 deletions

View file

@ -333,7 +333,9 @@ impl PropertyDeclarationBlock {
} }
let iter = self.declarations.iter().map(get_declaration as fn(_) -> _); let iter = self.declarations.iter().map(get_declaration as fn(_) -> _);
match shorthand.get_shorthand_appendable_value(iter) { match shorthand.get_shorthand_appendable_value(iter) {
Some(AppendableValue::Css(css)) => dest.write_str(css), Some(AppendableValue::Css { css, .. }) => {
dest.write_str(css)
},
Some(AppendableValue::DeclarationsForShorthand(_, decls)) => { Some(AppendableValue::DeclarationsForShorthand(_, decls)) => {
shorthand.longhands_to_css(decls, dest) shorthand.longhands_to_css(decls, dest)
} }
@ -428,15 +430,25 @@ impl ToCss for PropertyDeclarationBlock {
Importance::Normal Importance::Normal
}; };
// Substep 5 - Let value be the result of invoking serialize a CSS // Substep 5 - Let value be the result of invoking serialize
// value of current longhands. // a CSS value of current longhands.
let mut value = String::new(); let mut value = String::new();
let has_variables = {
let appendable_value =
match shorthand.get_shorthand_appendable_value(current_longhands.iter().cloned()) { match shorthand.get_shorthand_appendable_value(current_longhands.iter().cloned()) {
None => continue, None => continue,
Some(appendable_value) => { Some(appendable_value) => appendable_value,
try!(append_declaration_value(&mut value, appendable_value)); };
}
} let has_variables = match appendable_value {
AppendableValue::Css { with_variables, .. } => with_variables,
_ => false,
};
append_declaration_value(&mut value, appendable_value)?;
has_variables
};
// Substep 6 // Substep 6
if value.is_empty() { if value.is_empty() {
@ -444,9 +456,12 @@ impl ToCss for PropertyDeclarationBlock {
} }
// Substeps 7 and 8 // Substeps 7 and 8
try!(append_serialization::<W, Cloned<slice::Iter< _>>, _>( append_serialization::<_, Cloned<slice::Iter< _>>, _>(
dest, &shorthand, AppendableValue::Css(&value), importance, dest,
&mut is_first_serialization)); &shorthand,
AppendableValue::Css { css: &value, with_variables: has_variables },
importance,
&mut is_first_serialization)?;
for current_longhand in current_longhands { for current_longhand in current_longhands {
// Substep 9 // Substep 9
@ -503,7 +518,12 @@ pub enum AppendableValue<'a, I>
DeclarationsForShorthand(ShorthandId, I), DeclarationsForShorthand(ShorthandId, I),
/// A raw CSS string, coming for example from a property with CSS variables, /// A raw CSS string, coming for example from a property with CSS variables,
/// or when storing a serialized shorthand value before appending directly. /// or when storing a serialized shorthand value before appending directly.
Css(&'a str) Css {
/// The raw CSS string.
css: &'a str,
/// Whether the original serialization contained variables or not.
with_variables: bool,
}
} }
/// Potentially appends whitespace after the first (property: value;) pair. /// Potentially appends whitespace after the first (property: value;) pair.
@ -513,12 +533,11 @@ fn handle_first_serialization<W>(dest: &mut W,
where W: fmt::Write, where W: fmt::Write,
{ {
if !*is_first_serialization { if !*is_first_serialization {
try!(write!(dest, " ")); dest.write_str(" ")
} else { } else {
*is_first_serialization = false; *is_first_serialization = false;
}
Ok(()) Ok(())
}
} }
/// Append a given kind of appendable value to a serialization. /// Append a given kind of appendable value to a serialization.
@ -529,18 +548,16 @@ pub fn append_declaration_value<'a, W, I>(dest: &mut W,
I: Iterator<Item=&'a PropertyDeclaration>, I: Iterator<Item=&'a PropertyDeclaration>,
{ {
match appendable_value { match appendable_value {
AppendableValue::Css(css) => { AppendableValue::Css { css, .. } => {
try!(write!(dest, "{}", css)) dest.write_str(css)
}, },
AppendableValue::Declaration(decl) => { AppendableValue::Declaration(decl) => {
try!(decl.to_css(dest)); decl.to_css(dest)
}, },
AppendableValue::DeclarationsForShorthand(shorthand, decls) => { AppendableValue::DeclarationsForShorthand(shorthand, decls) => {
try!(shorthand.longhands_to_css(decls, dest)); shorthand.longhands_to_css(decls, dest)
} }
} }
Ok(())
} }
/// Append a given property and value pair to a serialization. /// Append a given property and value pair to a serialization.
@ -560,17 +577,21 @@ pub fn append_serialization<'a, W, I, N>(dest: &mut W,
try!(dest.write_char(':')); try!(dest.write_char(':'));
// for normal parsed values, add a space between key: and value // for normal parsed values, add a space between key: and value
match &appendable_value { match appendable_value {
&AppendableValue::Declaration(decl) => { AppendableValue::Declaration(decl) => {
if !decl.value_is_unparsed() { if !decl.value_is_unparsed() {
// for normal parsed values, add a space between key: and value // For normal parsed values, add a space between key: and value.
try!(write!(dest, " ")); dest.write_str(" ")?
} }
}, },
AppendableValue::Css { with_variables, .. } => {
if !with_variables {
dest.write_str(" ")?
}
}
// Currently append_serialization is only called with a Css or // Currently append_serialization is only called with a Css or
// a Declaration AppendableValue. // a Declaration AppendableValue.
&AppendableValue::DeclarationsForShorthand(..) => unreachable!(), AppendableValue::DeclarationsForShorthand(..) => unreachable!(),
&AppendableValue::Css(_) => {}
} }
try!(append_declaration_value(dest, appendable_value)); try!(append_declaration_value(dest, appendable_value));

View file

@ -580,7 +580,10 @@ impl ShorthandId {
// https://drafts.csswg.org/css-variables/#variables-in-shorthands // https://drafts.csswg.org/css-variables/#variables-in-shorthands
if let Some(css) = first_declaration.with_variables_from_shorthand(self) { if let Some(css) = first_declaration.with_variables_from_shorthand(self) {
if declarations2.all(|d| d.with_variables_from_shorthand(self) == Some(css)) { if declarations2.all(|d| d.with_variables_from_shorthand(self) == Some(css)) {
return Some(AppendableValue::Css(css)); return Some(AppendableValue::Css {
css: css,
with_variables: true,
});
} }
return None; return None;
} }
@ -588,7 +591,10 @@ impl ShorthandId {
// Check whether they are all the same CSS-wide keyword. // Check whether they are all the same CSS-wide keyword.
if let Some(keyword) = first_declaration.get_css_wide_keyword() { if let Some(keyword) = first_declaration.get_css_wide_keyword() {
if declarations2.all(|d| d.get_css_wide_keyword() == Some(keyword)) { if declarations2.all(|d| d.get_css_wide_keyword() == Some(keyword)) {
return Some(AppendableValue::Css(keyword.to_str())); return Some(AppendableValue::Css {
css: keyword.to_str(),
with_variables: false,
});
} }
return None; return None;
} }