mirror of
https://github.com/servo/servo.git
synced 2025-08-07 06:25:32 +01:00
Return shorthand decarations as a new enum, don’t push them to a Vec.
This commit is contained in:
parent
b11847d86c
commit
da6316fbe3
2 changed files with 74 additions and 31 deletions
|
@ -339,7 +339,7 @@
|
||||||
input.reset(start);
|
input.reset(start);
|
||||||
let (first_token_type, css) = try!(
|
let (first_token_type, css) = try!(
|
||||||
::custom_properties::parse_non_custom_with_var(input));
|
::custom_properties::parse_non_custom_with_var(input));
|
||||||
return Ok(DeclaredValue::WithVariables(Box::new(UnparsedValue {
|
return Ok(DeclaredValue::WithVariables(Arc::new(UnparsedValue {
|
||||||
css: css.into_owned(),
|
css: css.into_owned(),
|
||||||
first_token_type: first_token_type,
|
first_token_type: first_token_type,
|
||||||
base_url: context.base_url.clone(),
|
base_url: context.base_url.clone(),
|
||||||
|
@ -483,10 +483,10 @@
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
use cssparser::Parser;
|
use cssparser::Parser;
|
||||||
use parser::ParserContext;
|
use parser::ParserContext;
|
||||||
use properties::{DeclaredValue, PropertyDeclaration};
|
use properties::{DeclaredValue, PropertyDeclaration, ParsedDeclaration};
|
||||||
use properties::{ShorthandId, UnparsedValue, longhands};
|
use properties::{ShorthandId, UnparsedValue, longhands};
|
||||||
use properties::declaration_block::Importance;
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::sync::Arc;
|
||||||
use style_traits::ToCss;
|
use style_traits::ToCss;
|
||||||
|
|
||||||
pub struct Longhands {
|
pub struct Longhands {
|
||||||
|
@ -551,10 +551,7 @@
|
||||||
|
|
||||||
/// Parse the given shorthand and fill the result into the
|
/// Parse the given shorthand and fill the result into the
|
||||||
/// `declarations` vector.
|
/// `declarations` vector.
|
||||||
pub fn parse(context: &ParserContext,
|
pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<ParsedDeclaration, ()> {
|
||||||
input: &mut Parser,
|
|
||||||
declarations: &mut Vec<(PropertyDeclaration, Importance)>)
|
|
||||||
-> Result<(), ()> {
|
|
||||||
input.look_for_var_functions();
|
input.look_for_var_functions();
|
||||||
let start = input.position();
|
let start = input.position();
|
||||||
let value = input.parse_entirely(|input| parse_value(context, input));
|
let value = input.parse_entirely(|input| parse_value(context, input));
|
||||||
|
@ -563,31 +560,17 @@
|
||||||
}
|
}
|
||||||
let var = input.seen_var_functions();
|
let var = input.seen_var_functions();
|
||||||
if let Ok(value) = value {
|
if let Ok(value) = value {
|
||||||
% for sub_property in shorthand.sub_properties:
|
Ok(ParsedDeclaration::${shorthand.camel_case}(value))
|
||||||
declarations.push((PropertyDeclaration::${sub_property.camel_case}(
|
|
||||||
% if sub_property.boxed:
|
|
||||||
DeclaredValue::Value(Box::new(value.${sub_property.ident}))
|
|
||||||
% else:
|
|
||||||
DeclaredValue::Value(value.${sub_property.ident})
|
|
||||||
% endif
|
|
||||||
), Importance::Normal));
|
|
||||||
% endfor
|
|
||||||
Ok(())
|
|
||||||
} else if var {
|
} else if var {
|
||||||
input.reset(start);
|
input.reset(start);
|
||||||
let (first_token_type, css) = try!(
|
let (first_token_type, css) = try!(
|
||||||
::custom_properties::parse_non_custom_with_var(input));
|
::custom_properties::parse_non_custom_with_var(input));
|
||||||
% for sub_property in shorthand.sub_properties:
|
Ok(ParsedDeclaration::${shorthand.camel_case}WithVariables(Arc::new(UnparsedValue {
|
||||||
declarations.push((PropertyDeclaration::${sub_property.camel_case}(
|
css: css.into_owned(),
|
||||||
DeclaredValue::WithVariables(Box::new(UnparsedValue {
|
first_token_type: first_token_type,
|
||||||
css: css.clone().into_owned(),
|
base_url: context.base_url.clone(),
|
||||||
first_token_type: first_token_type,
|
from_shorthand: Some(ShorthandId::${shorthand.camel_case}),
|
||||||
base_url: context.base_url.clone(),
|
})))
|
||||||
from_shorthand: Some(ShorthandId::${shorthand.camel_case}),
|
|
||||||
}))
|
|
||||||
), Importance::Normal));
|
|
||||||
% endfor
|
|
||||||
Ok(())
|
|
||||||
} else {
|
} else {
|
||||||
Err(())
|
Err(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -568,7 +568,7 @@ pub enum DeclaredValue<T> {
|
||||||
/// A known specified value from the stylesheet.
|
/// A known specified value from the stylesheet.
|
||||||
Value(T),
|
Value(T),
|
||||||
/// An unparsed value that contains `var()` functions.
|
/// An unparsed value that contains `var()` functions.
|
||||||
WithVariables(Box<UnparsedValue>),
|
WithVariables(Arc<UnparsedValue>),
|
||||||
/// An CSS-wide keyword.
|
/// An CSS-wide keyword.
|
||||||
CSSWideKeyword(CSSWideKeyword),
|
CSSWideKeyword(CSSWideKeyword),
|
||||||
}
|
}
|
||||||
|
@ -797,6 +797,61 @@ impl PropertyId {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Includes shorthands before expansion
|
||||||
|
pub enum ParsedDeclaration {
|
||||||
|
% for shorthand in data.shorthands:
|
||||||
|
/// ${shorthand.name}
|
||||||
|
${shorthand.camel_case}(shorthands::${shorthand.ident}::Longhands),
|
||||||
|
% endfor
|
||||||
|
% for shorthand in data.shorthands:
|
||||||
|
/// ${shorthand.name} with var() functions
|
||||||
|
${shorthand.camel_case}WithVariables(Arc<UnparsedValue>),
|
||||||
|
% endfor
|
||||||
|
/// Not a shorthand
|
||||||
|
LonghandOrCustom(PropertyDeclaration),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ParsedDeclaration {
|
||||||
|
/// Transform this ParsedDeclaration into a sequence of PropertyDeclaration
|
||||||
|
/// by expanding shorthand declarations into their corresponding longhands
|
||||||
|
pub fn expand<F>(self, mut f: F) where F: FnMut(PropertyDeclaration) {
|
||||||
|
match self {
|
||||||
|
% for shorthand in data.shorthands:
|
||||||
|
ParsedDeclaration::${shorthand.camel_case}(
|
||||||
|
shorthands::${shorthand.ident}::Longhands {
|
||||||
|
% for sub_property in shorthand.sub_properties:
|
||||||
|
${sub_property.ident},
|
||||||
|
% endfor
|
||||||
|
}
|
||||||
|
) => {
|
||||||
|
% for sub_property in shorthand.sub_properties:
|
||||||
|
f(PropertyDeclaration::${sub_property.camel_case}(
|
||||||
|
% if sub_property.boxed:
|
||||||
|
DeclaredValue::Value(Box::new(${sub_property.ident}))
|
||||||
|
% else:
|
||||||
|
DeclaredValue::Value(${sub_property.ident})
|
||||||
|
% endif
|
||||||
|
));
|
||||||
|
% endfor
|
||||||
|
}
|
||||||
|
% endfor
|
||||||
|
% for shorthand in data.shorthands:
|
||||||
|
ParsedDeclaration::${shorthand.camel_case}WithVariables(value) => {
|
||||||
|
debug_assert_eq!(
|
||||||
|
value.from_shorthand,
|
||||||
|
Some(ShorthandId::${shorthand.camel_case})
|
||||||
|
);
|
||||||
|
% for sub_property in shorthand.sub_properties:
|
||||||
|
f(PropertyDeclaration::${sub_property.camel_case}(
|
||||||
|
DeclaredValue::WithVariables(value.clone())));
|
||||||
|
% endfor
|
||||||
|
}
|
||||||
|
% endfor
|
||||||
|
ParsedDeclaration::LonghandOrCustom(declaration) => f(declaration),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Servo's representation for a property declaration.
|
/// Servo's representation for a property declaration.
|
||||||
#[derive(PartialEq, Clone)]
|
#[derive(PartialEq, Clone)]
|
||||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||||
|
@ -1075,8 +1130,13 @@ impl PropertyDeclaration {
|
||||||
% endfor
|
% endfor
|
||||||
PropertyDeclarationParseResult::ValidOrIgnoredDeclaration
|
PropertyDeclarationParseResult::ValidOrIgnoredDeclaration
|
||||||
},
|
},
|
||||||
Err(()) => match shorthands::${shorthand.ident}::parse(context, input, result_list) {
|
Err(()) => match shorthands::${shorthand.ident}::parse(context, input) {
|
||||||
Ok(()) => PropertyDeclarationParseResult::ValidOrIgnoredDeclaration,
|
Ok(parsed) => {
|
||||||
|
parsed.expand(|declaration| {
|
||||||
|
result_list.push((declaration, Importance::Normal))
|
||||||
|
});
|
||||||
|
PropertyDeclarationParseResult::ValidOrIgnoredDeclaration
|
||||||
|
}
|
||||||
Err(()) => PropertyDeclarationParseResult::InvalidValue,
|
Err(()) => PropertyDeclarationParseResult::InvalidValue,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue