Store unset keyword as specified value

This commit is contained in:
Xidorn Quan 2016-12-20 12:14:35 +11:00
parent bcf154d8e6
commit 2a502d7377
5 changed files with 35 additions and 17 deletions

View file

@ -345,6 +345,7 @@ pub fn cascade<'a>(custom_properties: &mut Option<HashMap<&'a Name, BorrowedSpec
DeclaredValue::Initial => {
map.remove(&name);
}
DeclaredValue::Unset | // Custom properties are inherited by default.
DeclaredValue::Inherit => {} // The inherited value is what we already have.
}
}

View file

@ -245,6 +245,9 @@
% endif
}
DeclaredValue::WithVariables { .. } => unreachable!(),
% if not data.current_style_struct.inherited:
DeclaredValue::Unset |
% endif
DeclaredValue::Initial => {
// We assume that it's faster to use copy_*_from rather than
// set_*(get_initial_value());
@ -253,6 +256,9 @@
context.mutate_style().mutate_${data.current_style_struct.name_lower}()
.copy_${property.ident}_from(initial_struct ${maybe_wm});
},
% if data.current_style_struct.inherited:
DeclaredValue::Unset |
% endif
DeclaredValue::Inherit => {
// This is a bit slow, but this is rare so it shouldn't
// matter.
@ -286,8 +292,7 @@
match input.try(|i| CSSWideKeyword::parse(context, i)) {
Ok(CSSWideKeyword::InheritKeyword) => Ok(DeclaredValue::Inherit),
Ok(CSSWideKeyword::InitialKeyword) => Ok(DeclaredValue::Initial),
Ok(CSSWideKeyword::UnsetKeyword) => Ok(DeclaredValue::${
"Inherit" if data.current_style_struct.inherited else "Initial"}),
Ok(CSSWideKeyword::UnsetKeyword) => Ok(DeclaredValue::Unset),
Err(()) => {
input.look_for_var_functions();
let start = input.position();
@ -383,6 +388,7 @@
use properties::{longhands, PropertyDeclaration, DeclaredValue, ShorthandId};
use std::fmt;
use style_traits::ToCss;
use super::{SerializeFlags, ALL_INHERIT, ALL_INITIAL, ALL_UNSET};
pub struct Longhands {
% for sub_property in shorthand.sub_properties:
@ -441,17 +447,16 @@
impl<'a> ToCss for LonghandsToSerialize<'a> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
let mut all_inherit = true;
let mut all_initial = true;
let mut all_flags = SerializeFlags::all();
let mut with_variables = false;
% for sub_property in shorthand.sub_properties:
match *self.${sub_property.ident} {
DeclaredValue::Initial => all_inherit = false,
DeclaredValue::Inherit => all_initial = false,
DeclaredValue::Initial => all_flags &= ALL_INITIAL,
DeclaredValue::Inherit => all_flags &= ALL_INHERIT,
DeclaredValue::Unset => all_flags &= ALL_UNSET,
DeclaredValue::WithVariables {..} => with_variables = true,
DeclaredValue::Value(..) => {
all_initial = false;
all_inherit = false;
all_flags = SerializeFlags::empty();
}
}
% endfor
@ -459,10 +464,12 @@
if with_variables {
// We don't serialize shorthands with variables
dest.write_str("")
} else if all_inherit {
} else if all_flags == ALL_INHERIT {
dest.write_str("inherit")
} else if all_initial {
} else if all_flags == ALL_INITIAL {
dest.write_str("initial")
} else if all_flags == ALL_UNSET {
dest.write_str("unset")
} else {
self.to_css_declared(dest)
}

View file

@ -90,6 +90,14 @@ pub mod shorthands {
use parser::{Parse, ParserContext};
use values::specified;
bitflags! {
flags SerializeFlags: u8 {
const ALL_INHERIT = 0b001,
const ALL_INITIAL = 0b010,
const ALL_UNSET = 0b100,
}
}
pub fn parse_four_sides<F, T>(input: &mut Parser, parse_one: F) -> Result<(T, T, T, T), ()>
where F: Fn(&mut Parser) -> Result<T, ()>, T: Clone
{
@ -520,9 +528,7 @@ pub enum DeclaredValue<T> {
},
Initial,
Inherit,
// There is no Unset variant here.
// The 'unset' keyword is represented as either Initial or Inherit,
// depending on whether the property is inherited.
Unset,
}
impl<T: HasViewportPercentage> HasViewportPercentage for DeclaredValue<T> {
@ -533,7 +539,8 @@ impl<T: HasViewportPercentage> HasViewportPercentage for DeclaredValue<T> {
DeclaredValue::WithVariables { .. }
=> panic!("DeclaredValue::has_viewport_percentage without resolving variables!"),
DeclaredValue::Initial |
DeclaredValue::Inherit => false,
DeclaredValue::Inherit |
DeclaredValue::Unset => false,
}
}
}
@ -549,6 +556,7 @@ impl<T: ToCss> ToCss for DeclaredValue<T> {
DeclaredValue::WithVariables { .. } => Ok(()),
DeclaredValue::Initial => dest.write_str("initial"),
DeclaredValue::Inherit => dest.write_str("inherit"),
DeclaredValue::Unset => dest.write_str("unset"),
}
}
}
@ -821,7 +829,7 @@ impl PropertyDeclaration {
match id {
PropertyId::Custom(name) => {
let value = match input.try(|i| CSSWideKeyword::parse(context, i)) {
Ok(CSSWideKeyword::UnsetKeyword) | // Custom properties are alawys inherited
Ok(CSSWideKeyword::UnsetKeyword) => DeclaredValue::Unset,
Ok(CSSWideKeyword::InheritKeyword) => DeclaredValue::Inherit,
Ok(CSSWideKeyword::InitialKeyword) => DeclaredValue::Initial,
Err(()) => match ::custom_properties::SpecifiedValue::parse(context, input) {
@ -904,8 +912,7 @@ impl PropertyDeclaration {
Ok(CSSWideKeyword::UnsetKeyword) => {
% for sub_property in shorthand.sub_properties:
result_list.push(PropertyDeclaration::${sub_property.camel_case}(
DeclaredValue::${"Inherit" if sub_property.style_struct.inherited else "Initial"}
));
DeclaredValue::Unset));
% endfor
PropertyDeclarationParseResult::ValidOrIgnoredDeclaration
},

View file

@ -40,6 +40,7 @@ ${helpers.four_sides_shorthand("border-style", "border-%s-style",
},
&DeclaredValue::Initial => DeclaredValue::Initial,
&DeclaredValue::Inherit => DeclaredValue::Inherit,
&DeclaredValue::Unset => DeclaredValue::Unset,
};
% endfor

View file

@ -28,6 +28,7 @@
(&DeclaredValue::WithVariables { .. }, &DeclaredValue::WithVariables { .. }) => true,
(&DeclaredValue::Initial, &DeclaredValue::Initial) => true,
(&DeclaredValue::Inherit, &DeclaredValue::Inherit) => true,
(&DeclaredValue::Unset, &DeclaredValue::Unset) => true,
_ => false
};
@ -331,6 +332,7 @@ macro_rules! try_parse_one {
},
(&DeclaredValue::Initial, &DeclaredValue::Initial) => true,
(&DeclaredValue::Inherit, &DeclaredValue::Inherit) => true,
(&DeclaredValue::Unset, &DeclaredValue::Unset) => true,
(x, y) => { *x == *y },
};