Skip to content

Commit

Permalink
var() functions in shorthands: use a single code path with IDs
Browse files Browse the repository at this point in the history
… rather than generating similar code for each shorthand.
  • Loading branch information
SimonSapin committed Jul 13, 2017
1 parent 8a8614e commit 252e52e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 44 deletions.
41 changes: 7 additions & 34 deletions components/style/properties/helpers.mako.rs
Expand Up @@ -760,13 +760,12 @@
pub mod ${shorthand.ident} {
use cssparser::Parser;
use parser::ParserContext;
use properties::{PropertyDeclaration, SourcePropertyDeclaration, MaybeBoxed};
use properties::{ShorthandId, LonghandId, UnparsedValue, longhands};
use properties::{PropertyDeclaration, SourcePropertyDeclaration, MaybeBoxed, longhands};
#[allow(unused_imports)]
use selectors::parser::SelectorParseError;
#[allow(unused_imports)]
use std::fmt;
use stylearc::Arc;
#[allow(unused_imports)]
use style_traits::{ParseError, StyleParseError};
#[allow(unused_imports)]
use style_traits::ToCss;
Expand Down Expand Up @@ -845,41 +844,15 @@
/// Parse the given shorthand and fill the result into the
/// `declarations` vector.
pub fn parse_into<'i, 't>(declarations: &mut SourcePropertyDeclaration,
context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<(), ParseError<'i>> {
input.look_for_var_functions();
let start = input.position();
let value = input.parse_entirely(|input| parse_value(context, input));
if value.is_err() {
while let Ok(_) = input.next() {} // Look for var() after the error.
}
let var = input.seen_var_functions();
if let Ok(value) = value {
context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<(), ParseError<'i>> {
parse_value(context, input).map(|longhands| {
% for sub_property in shorthand.sub_properties:
declarations.push(PropertyDeclaration::${sub_property.camel_case}(
value.${sub_property.ident}
));
% endfor
Ok(())
} else if var {
input.reset(start);
let (first_token_type, css) =
::custom_properties::parse_non_custom_with_var(input)?;
let unparsed = Arc::new(UnparsedValue {
css: css.into_owned(),
first_token_type: first_token_type,
url_data: context.url_data.clone(),
from_shorthand: Some(ShorthandId::${shorthand.camel_case}),
});
% for sub_property in shorthand.sub_properties:
declarations.push(PropertyDeclaration::WithVariables(
LonghandId::${sub_property.camel_case},
unparsed.clone()
longhands.${sub_property.ident}
));
% endfor
Ok(())
} else {
Err(StyleParseError::UnspecifiedError.into())
}
})
}

${caller.body()}
Expand Down
44 changes: 34 additions & 10 deletions components/style/properties/properties.mako.rs
Expand Up @@ -1522,7 +1522,7 @@ impl PropertyDeclaration {
}
PropertyId::Longhand(id) => {
if let Ok(keyword) = input.try(|i| CSSWideKeyword::parse(context, i)) {
declarations.push(PropertyDeclaration::CSSWideKeyword(id, keyword))
declarations.push(PropertyDeclaration::CSSWideKeyword(id, keyword));
Ok(())
} else {
match id {
Expand Down Expand Up @@ -1559,16 +1559,40 @@ impl PropertyDeclaration {
}
Ok(())
} else {
match id {
% for shorthand in data.shorthands:
ShorthandId::${shorthand.camel_case} => {
shorthands::${shorthand.ident}::parse_into(declarations, context, input)
.map_err(|_| PropertyDeclarationParseError::InvalidValue(
"${shorthand.ident}".into()
))
input.look_for_var_functions();
let start = input.position();
input.parse_entirely(|input| {
match id {
% for shorthand in data.shorthands:
ShorthandId::${shorthand.camel_case} => {
shorthands::${shorthand.ident}::parse_into(declarations, context, input)
}
% endfor
}
}).or_else(|_| {
while let Ok(_) = input.next() {} // Look for var() after the error.
if input.seen_var_functions() {
input.reset(start);
let (first_token_type, css) =
::custom_properties::parse_non_custom_with_var(input).map_err(|_| {
PropertyDeclarationParseError::InvalidValue(id.name().into())
})?;
let unparsed = Arc::new(UnparsedValue {
css: css.into_owned(),
first_token_type: first_token_type,
url_data: context.url_data.clone(),
from_shorthand: Some(id),
});
for &longhand in id.longhands() {
declarations.push(
PropertyDeclaration::WithVariables(longhand, unparsed.clone())
)
}
% endfor
}
Ok(())
} else {
Err(PropertyDeclarationParseError::InvalidValue(id.name().into()))
}
})
}
}
}
Expand Down

0 comments on commit 252e52e

Please sign in to comment.