Resolve 'unset' to 'initial' or 'inherit' when parsing.

This commit is contained in:
Simon Sapin 2013-09-03 16:56:12 +01:00
parent 96dce99e8f
commit 7a70200250

View file

@ -23,22 +23,23 @@ def to_rust_ident(name):
return name return name
class Longhand(object): class Longhand(object):
def __init__(self, name): def __init__(self, name, is_inherited):
self.name = name self.name = name
self.ident = to_rust_ident(name) self.ident = to_rust_ident(name)
self.is_inherited = is_inherited
class Shorthand(object): class Shorthand(object):
def __init__(self, name, sub_properties): def __init__(self, name, sub_properties):
self.name = name self.name = name
self.ident = to_rust_ident(name) self.ident = to_rust_ident(name)
self.sub_properties = [Longhand(s) for s in sub_properties] self.sub_properties = [LONGHANDS_BY_NAME[s] for s in sub_properties]
LONGHANDS_PER_STYLE_STRUCT = [] LONGHANDS_PER_STYLE_STRUCT = []
THIS_STYLE_STRUCT_LONGHANDS = None THIS_STYLE_STRUCT_LONGHANDS = None
LONGHANDS = [] LONGHANDS = []
LONGHANDS_BY_NAME = {}
SHORTHANDS = [] SHORTHANDS = []
INHERITED = set()
def new_style_struct(name): def new_style_struct(name):
longhands = [] longhands = []
@ -55,32 +56,34 @@ pub mod longhands {
<%def name="raw_longhand(name, inherited=False, no_super=False)"> <%def name="raw_longhand(name, inherited=False, no_super=False)">
<% <%
property = Longhand(name) property = Longhand(name, inherited)
THIS_STYLE_STRUCT_LONGHANDS.append(property) THIS_STYLE_STRUCT_LONGHANDS.append(property)
LONGHANDS.append(property) LONGHANDS.append(property)
if inherited: LONGHANDS_BY_NAME[name] = property
INHERITED.add(name)
%> %>
pub mod ${property.ident} { pub mod ${property.ident} {
% if not no_super: % if not no_super:
use super::*; use super::*;
% endif % endif
${caller.body()} ${caller.body()}
pub fn parse_declared(input: &[ComponentValue])
-> Option<DeclaredValue<SpecifiedValue>> {
match CSSWideKeyword::parse(input) {
Some(Left(keyword)) => Some(CSSWideKeyword(keyword)),
Some(Right(Unset)) => Some(CSSWideKeyword(${
"Inherit" if inherited else "Initial"})),
None => parse_specified(input),
}
}
} }
</%def> </%def>
<%def name="longhand(name, inherited=False, no_super=False)"> <%def name="longhand(name, inherited=False, no_super=False)">
<%self:raw_longhand name="${name}" inherited="${inherited}"> <%self:raw_longhand name="${name}" inherited="${inherited}">
${caller.body()} ${caller.body()}
pub fn parse_declared(input: &[ComponentValue]) pub fn parse_specified(input: &[ComponentValue])
-> Option<DeclaredValue<SpecifiedValue>> { -> Option<DeclaredValue<SpecifiedValue>> {
match CSSWideKeyword::parse(input) { parse(input).map_move(super::SpecifiedValue)
Some(keyword) => Some(CSSWideKeyword(keyword)),
None => match parse(input) {
Some(value) => Some(SpecifiedValue(value)),
None => None,
}
}
} }
</%self:raw_longhand> </%self:raw_longhand>
</%def> </%def>
@ -280,14 +283,11 @@ pub mod longhands {
#[inline] pub fn get_initial_value() -> ComputedValue { #[inline] pub fn get_initial_value() -> ComputedValue {
RGBA { red: 0., green: 0., blue: 0., alpha: 1. } /* black */ RGBA { red: 0., green: 0., blue: 0., alpha: 1. } /* black */
} }
pub fn parse_declared(input: &[ComponentValue]) -> Option<DeclaredValue<SpecifiedValue>> { pub fn parse_specified(input: &[ComponentValue]) -> Option<DeclaredValue<SpecifiedValue>> {
match CSSWideKeyword::parse(input) { match one_component_value(input).chain(Color::parse) {
Some(keyword) => Some(CSSWideKeyword(keyword)), Some(RGBA(rgba)) => Some(SpecifiedValue(rgba)),
None => match one_component_value(input).chain(Color::parse) { Some(CurrentColor) => Some(CSSWideKeyword(Inherit)),
Some(RGBA(rgba)) => Some(SpecifiedValue(rgba)), None => None,
Some(CurrentColor) => Some(CSSWideKeyword(Inherit)),
None => None,
}
} }
} }
</%self:raw_longhand> </%self:raw_longhand>
@ -673,16 +673,17 @@ pub fn parse_property_declaration_list(input: ~[Node]) -> PropertyDeclarationBlo
pub enum CSSWideKeyword { pub enum CSSWideKeyword {
Initial, Initial,
Inherit, Inherit,
Unset,
} }
struct Unset;
impl CSSWideKeyword { impl CSSWideKeyword {
pub fn parse(input: &[ComponentValue]) -> Option<CSSWideKeyword> { pub fn parse(input: &[ComponentValue]) -> Option<Either<CSSWideKeyword, Unset>> {
do one_component_value(input).chain(get_ident_lower).chain |keyword| { do one_component_value(input).chain(get_ident_lower).chain |keyword| {
match keyword.as_slice() { match keyword.as_slice() {
"initial" => Some(Initial), "initial" => Some(Left(Initial)),
"inherit" => Some(Inherit), "inherit" => Some(Left(Inherit)),
"unset" => Some(Unset), "unset" => Some(Right(Unset)),
_ => None _ => None
} }
} }
@ -714,13 +715,21 @@ impl PropertyDeclaration {
% endfor % endfor
% for shorthand in SHORTHANDS: % for shorthand in SHORTHANDS:
"${shorthand.name}" => match CSSWideKeyword::parse(value) { "${shorthand.name}" => match CSSWideKeyword::parse(value) {
Some(keyword) => { Some(Left(keyword)) => {
% for sub_property in shorthand.sub_properties: % for sub_property in shorthand.sub_properties:
result_list.push(${sub_property.ident}_declaration( result_list.push(${sub_property.ident}_declaration(
CSSWideKeyword(keyword) CSSWideKeyword(keyword)
)); ));
% endfor % endfor
}, },
Some(Right(Unset)) => {
% for sub_property in shorthand.sub_properties:
result_list.push(${sub_property.ident}_declaration(
CSSWideKeyword(${
"Inherit" if sub_property.is_inherited else "Initial"})
));
% endfor
},
None => match shorthands::${shorthand.ident}::parse(value) { None => match shorthands::${shorthand.ident}::parse(value) {
Some(result) => { Some(result) => {
% for sub_property in shorthand.sub_properties: % for sub_property in shorthand.sub_properties: