Make DeclaredValue store CSSWideKeyword

Rather than having separate variant for each CSS-wide keyword.
This commit is contained in:
Xidorn Quan 2017-02-28 18:53:51 +11:00
parent 29fd30601e
commit 03f6d21cb5
10 changed files with 120 additions and 115 deletions

View file

@ -9,7 +9,7 @@
use Atom;
use cssparser::{Delimiter, Parser, SourcePosition, Token, TokenSerializationType};
use parser::ParserContext;
use properties::DeclaredValue;
use properties::{CSSWideKeyword, DeclaredValue};
use std::ascii::AsciiExt;
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
@ -362,11 +362,13 @@ pub fn cascade<'a>(custom_properties: &mut Option<HashMap<&'a Name, BorrowedSpec
});
},
DeclaredValue::WithVariables(_) => unreachable!(),
DeclaredValue::Initial => {
map.remove(&name);
DeclaredValue::CSSWideKeyword(keyword) => match keyword {
CSSWideKeyword::Initial => {
map.remove(&name);
}
CSSWideKeyword::Unset | // Custom properties are inherited by default.
CSSWideKeyword::Inherit => {} // The inherited value is what we already have.
}
DeclaredValue::Unset | // Custom properties are inherited by default.
DeclaredValue::Inherit => {} // The inherited value is what we already have.
}
}

View file

@ -267,30 +267,32 @@
% 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());
let initial_struct = default_style
.get_${data.current_style_struct.name_lower}();
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.
//
// FIXME: is it still?
*cacheable = false;
let inherited_struct =
inherited_style.get_${data.current_style_struct.name_lower}();
context.mutate_style().mutate_${data.current_style_struct.name_lower}()
.copy_${property.ident}_from(inherited_struct ${maybe_wm});
DeclaredValue::CSSWideKeyword(keyword) => match keyword {
% if not data.current_style_struct.inherited:
CSSWideKeyword::Unset |
% endif
CSSWideKeyword::Initial => {
// We assume that it's faster to use copy_*_from rather than
// set_*(get_initial_value());
let initial_struct = default_style
.get_${data.current_style_struct.name_lower}();
context.mutate_style().mutate_${data.current_style_struct.name_lower}()
.copy_${property.ident}_from(initial_struct ${maybe_wm});
},
% if data.current_style_struct.inherited:
CSSWideKeyword::Unset |
% endif
CSSWideKeyword::Inherit => {
// This is a bit slow, but this is rare so it shouldn't
// matter.
//
// FIXME: is it still?
*cacheable = false;
let inherited_struct =
inherited_style.get_${data.current_style_struct.name_lower}();
context.mutate_style().mutate_${data.current_style_struct.name_lower}()
.copy_${property.ident}_from(inherited_struct ${maybe_wm});
}
}
}
}, error_reporter);
@ -324,9 +326,7 @@
-> Result<DeclaredValue<SpecifiedValue>, ()> {
% endif
match input.try(|i| CSSWideKeyword::parse(context, i)) {
Ok(CSSWideKeyword::Inherit) => Ok(DeclaredValue::Inherit),
Ok(CSSWideKeyword::Initial) => Ok(DeclaredValue::Initial),
Ok(CSSWideKeyword::Unset) => Ok(DeclaredValue::Unset),
Ok(keyword) => Ok(DeclaredValue::CSSWideKeyword(keyword)),
Err(()) => {
input.look_for_var_functions();
let start = input.position();
@ -483,8 +483,8 @@
#[allow(unused_imports)]
use cssparser::Parser;
use parser::ParserContext;
use properties::{DeclaredValue, PropertyDeclaration, UnparsedValue};
use properties::{ShorthandId, longhands};
use properties::{CSSWideKeyword, DeclaredValue, PropertyDeclaration};
use properties::{ShorthandId, UnparsedValue, longhands};
use properties::declaration_block::Importance;
use std::fmt;
use style_traits::ToCss;
@ -563,9 +563,11 @@
let mut with_variables = false;
% for sub_property in shorthand.sub_properties:
match *self.${sub_property.ident} {
DeclaredValue::Initial => all_flags &= ALL_INITIAL,
DeclaredValue::Inherit => all_flags &= ALL_INHERIT,
DeclaredValue::Unset => all_flags &= ALL_UNSET,
DeclaredValue::CSSWideKeyword(keyword) => match keyword {
CSSWideKeyword::Initial => all_flags &= ALL_INITIAL,
CSSWideKeyword::Inherit => all_flags &= ALL_INHERIT,
CSSWideKeyword::Unset => all_flags &= ALL_UNSET,
},
DeclaredValue::WithVariables(_) => with_variables = true,
DeclaredValue::Value(..) => {
all_flags = SerializeFlags::empty();

View file

@ -8,7 +8,7 @@ use app_units::Au;
use cssparser::{Color as CSSParserColor, Parser, RGBA};
use euclid::{Point2D, Size2D};
#[cfg(feature = "gecko")] use gecko_bindings::structs::nsCSSPropertyID;
use properties::{DeclaredValue, PropertyDeclaration};
use properties::{CSSWideKeyword, DeclaredValue, PropertyDeclaration};
use properties::longhands;
use properties::longhands::background_size::computed_value::T as BackgroundSize;
use properties::longhands::font_weight::computed_value::T as FontWeight;
@ -287,21 +287,23 @@ impl AnimationValue {
// https://bugzilla.mozilla.org/show_bug.cgi?id=1326131
DeclaredValue::WithVariables(_) => unimplemented!(),
DeclaredValue::Value(ref val) => val.to_computed_value(context),
% if not prop.style_struct.inherited:
DeclaredValue::Unset |
% endif
DeclaredValue::Initial => {
let initial_struct = initial.get_${prop.style_struct.name_lower}();
initial_struct.clone_${prop.ident}()
},
% if prop.style_struct.inherited:
DeclaredValue::Unset |
% endif
DeclaredValue::Inherit => {
let inherit_struct = context.inherited_style
.get_${prop.style_struct.name_lower}();
inherit_struct.clone_${prop.ident}()
},
DeclaredValue::CSSWideKeyword(keyword) => match keyword {
% if not prop.style_struct.inherited:
CSSWideKeyword::Unset |
% endif
CSSWideKeyword::Initial => {
let initial_struct = initial.get_${prop.style_struct.name_lower}();
initial_struct.clone_${prop.ident}()
},
% if prop.style_struct.inherited:
CSSWideKeyword::Unset |
% endif
CSSWideKeyword::Inherit => {
let inherit_struct = context.inherited_style
.get_${prop.style_struct.name_lower}();
inherit_struct.clone_${prop.ident}()
},
}
};
Some(AnimationValue::${prop.camel_case}(computed))
}

View file

@ -365,7 +365,13 @@ impl PropertyDeclarationIdSet {
})
.unwrap_or(
// Invalid at computed-value time.
DeclaredValue::${"Inherit" if property.style_struct.inherited else "Initial"}
DeclaredValue::CSSWideKeyword(
% if property.style_struct.inherited:
CSSWideKeyword::Inherit
% else:
CSSWideKeyword::Initial
% endif
)
)
);
}
@ -374,6 +380,7 @@ impl PropertyDeclarationIdSet {
/// An enum to represent a CSS Wide keyword.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum CSSWideKeyword {
/// The `initial` keyword.
Initial,
@ -383,6 +390,16 @@ pub enum CSSWideKeyword {
Unset,
}
impl ToCss for CSSWideKeyword {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
dest.write_str(match *self {
CSSWideKeyword::Initial => "initial",
CSSWideKeyword::Inherit => "inherit",
CSSWideKeyword::Unset => "unset",
})
}
}
impl Parse for CSSWideKeyword {
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
let ident = input.expect_ident()?;
@ -545,12 +562,8 @@ pub enum DeclaredValue<T> {
Value(T),
/// An unparsed value that contains `var()` functions.
WithVariables(Box<UnparsedValue>),
/// The `initial` keyword.
Initial,
/// The `inherit` keyword.
Inherit,
/// The `unset` keyword.
Unset,
/// An CSS-wide keyword.
CSSWideKeyword(CSSWideKeyword),
}
/// An unparsed property value that contains `var()` functions.
@ -575,9 +588,7 @@ impl<T: HasViewportPercentage> HasViewportPercentage for DeclaredValue<T> {
panic!("DeclaredValue::has_viewport_percentage without \
resolving variables!")
},
DeclaredValue::Initial |
DeclaredValue::Inherit |
DeclaredValue::Unset => false,
DeclaredValue::CSSWideKeyword(_) => false,
}
}
}
@ -595,9 +606,7 @@ impl<T: ToCss> ToCss for DeclaredValue<T> {
}
Ok(())
},
DeclaredValue::Initial => dest.write_str("initial"),
DeclaredValue::Inherit => dest.write_str("inherit"),
DeclaredValue::Unset => dest.write_str("unset"),
DeclaredValue::CSSWideKeyword(ref keyword) => keyword.to_css(dest),
}
}
}
@ -969,9 +978,7 @@ impl PropertyDeclaration {
match id {
PropertyId::Custom(name) => {
let value = match input.try(|i| CSSWideKeyword::parse(context, i)) {
Ok(CSSWideKeyword::Unset) => DeclaredValue::Unset,
Ok(CSSWideKeyword::Inherit) => DeclaredValue::Inherit,
Ok(CSSWideKeyword::Initial) => DeclaredValue::Initial,
Ok(keyword) => DeclaredValue::CSSWideKeyword(keyword),
Err(()) => match ::custom_properties::SpecifiedValue::parse(context, input) {
Ok(value) => DeclaredValue::Value(value),
Err(()) => return PropertyDeclarationParseResult::InvalidValue,
@ -1029,26 +1036,11 @@ impl PropertyDeclaration {
${property_pref_check(shorthand)}
match input.try(|i| CSSWideKeyword::parse(context, i)) {
Ok(CSSWideKeyword::Inherit) => {
Ok(keyword) => {
% for sub_property in shorthand.sub_properties:
result_list.push((
PropertyDeclaration::${sub_property.camel_case}(
DeclaredValue::Inherit), Importance::Normal));
% endfor
PropertyDeclarationParseResult::ValidOrIgnoredDeclaration
},
Ok(CSSWideKeyword::Initial) => {
% for sub_property in shorthand.sub_properties:
result_list.push((
PropertyDeclaration::${sub_property.camel_case}(
DeclaredValue::Initial), Importance::Normal));
% endfor
PropertyDeclarationParseResult::ValidOrIgnoredDeclaration
},
Ok(CSSWideKeyword::Unset) => {
% for sub_property in shorthand.sub_properties:
result_list.push((PropertyDeclaration::${sub_property.camel_case}(
DeclaredValue::Unset), Importance::Normal));
DeclaredValue::CSSWideKeyword(keyword)), Importance::Normal));
% endfor
PropertyDeclarationParseResult::ValidOrIgnoredDeclaration
},

View file

@ -23,9 +23,8 @@
*x_value == y_container.0
},
(&DeclaredValue::WithVariables(_), &DeclaredValue::WithVariables(_)) => true,
(&DeclaredValue::Initial, &DeclaredValue::Initial) => true,
(&DeclaredValue::Inherit, &DeclaredValue::Inherit) => true,
(&DeclaredValue::Unset, &DeclaredValue::Unset) => true,
(&DeclaredValue::CSSWideKeyword(x_keyword),
&DeclaredValue::CSSWideKeyword(y_keyword)) => x_keyword == y_keyword,
_ => false
};
@ -359,9 +358,8 @@ macro_rules! try_parse_one {
(&DeclaredValue::Value(ref x_value), &DeclaredValue::Value(ref y_value)) => {
*x_value == *y_value
},
(&DeclaredValue::Initial, &DeclaredValue::Initial) => true,
(&DeclaredValue::Inherit, &DeclaredValue::Inherit) => true,
(&DeclaredValue::Unset, &DeclaredValue::Unset) => true,
(&DeclaredValue::CSSWideKeyword(x_keyword),
&DeclaredValue::CSSWideKeyword(y_keyword)) => x_keyword == y_keyword,
(x, y) => { *x == *y },
};

View file

@ -99,21 +99,24 @@
impl<'a> LonghandsToSerialize<'a> {
fn to_css_declared<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self.list_style_position {
DeclaredValue::Initial => try!(write!(dest, "outside")),
DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial) =>
try!(write!(dest, "outside")),
_ => try!(self.list_style_position.to_css(dest))
}
try!(write!(dest, " "));
match *self.list_style_image {
DeclaredValue::Initial => try!(write!(dest, "none")),
DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial) =>
try!(write!(dest, "none")),
_ => try!(self.list_style_image.to_css(dest))
};
try!(write!(dest, " "));
match *self.list_style_type {
DeclaredValue::Initial => write!(dest, "disc"),
DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial) =>
write!(dest, "disc"),
_ => self.list_style_type.to_css(dest)
}
}

View file

@ -57,12 +57,13 @@
try!(write!(dest, " "));
match *self.outline_style {
DeclaredValue::Initial => try!(write!(dest, "none")),
DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial) =>
try!(write!(dest, "none")),
_ => try!(self.outline_style.to_css(dest))
};
match *self.outline_color {
DeclaredValue::Initial => Ok(()),
DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial) => Ok(()),
_ => {
try!(write!(dest, " "));
self.outline_color.to_css(dest)

View file

@ -40,14 +40,16 @@
impl<'a> LonghandsToSerialize<'a> {
fn to_css_declared<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self.flex_direction {
DeclaredValue::Initial => try!(write!(dest, "row")),
DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial) =>
try!(write!(dest, "row")),
_ => try!(self.flex_direction.to_css(dest))
};
try!(write!(dest, " "));
match *self.flex_wrap {
DeclaredValue::Initial => write!(dest, "nowrap"),
DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial) =>
write!(dest, "nowrap"),
_ => self.flex_wrap.to_css(dest)
}
}

View file

@ -7,7 +7,8 @@ use media_queries::CSSErrorReporterTest;
use servo_url::ServoUrl;
use style::computed_values::display::T::inline_block;
use style::parser::ParserContext;
use style::properties::{DeclaredValue, PropertyDeclaration, PropertyDeclarationBlock, Importance, PropertyId};
use style::properties::{CSSWideKeyword, DeclaredValue, PropertyDeclaration};
use style::properties::{PropertyDeclarationBlock, Importance, PropertyId};
use style::properties::longhands::outline_color::computed_value::T as ComputedColor;
use style::properties::parse_property_declaration_list;
use style::stylesheets::Origin;
@ -376,7 +377,7 @@ mod shorthand_serialization {
let mut properties = Vec::new();
let width = DeclaredValue::Value(BorderWidth::from_length(Length::from_px(4f32)));
let style = DeclaredValue::Initial;
let style = DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial);
let color = DeclaredValue::Value(CSSColor {
parsed: ComputedColor::RGBA(RGBA::new(255, 0, 0, 255)),
authored: None
@ -396,7 +397,7 @@ mod shorthand_serialization {
let width = DeclaredValue::Value(BorderWidth::from_length(Length::from_px(4f32)));
let style = DeclaredValue::Value(BorderStyle::solid);
let color = DeclaredValue::Initial;
let color = DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial);
properties.push(PropertyDeclaration::BorderTopWidth(width));
properties.push(PropertyDeclaration::BorderTopStyle(style));
@ -412,7 +413,7 @@ mod shorthand_serialization {
let width = DeclaredValue::Value(BorderWidth::from_length(Length::from_px(4f32)));
let style = DeclaredValue::Value(BorderStyle::solid);
let color = DeclaredValue::Initial;
let color = DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial);
properties.push(PropertyDeclaration::BorderRightWidth(width));
properties.push(PropertyDeclaration::BorderRightStyle(style));
@ -428,7 +429,7 @@ mod shorthand_serialization {
let width = DeclaredValue::Value(BorderWidth::from_length(Length::from_px(4f32)));
let style = DeclaredValue::Value(BorderStyle::solid);
let color = DeclaredValue::Initial;
let color = DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial);
properties.push(PropertyDeclaration::BorderBottomWidth(width));
properties.push(PropertyDeclaration::BorderBottomStyle(style));
@ -444,7 +445,7 @@ mod shorthand_serialization {
let width = DeclaredValue::Value(BorderWidth::from_length(Length::from_px(4f32)));
let style = DeclaredValue::Value(BorderStyle::solid);
let color = DeclaredValue::Initial;
let color = DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial);
properties.push(PropertyDeclaration::BorderLeftWidth(width));
properties.push(PropertyDeclaration::BorderLeftStyle(style));
@ -460,7 +461,7 @@ mod shorthand_serialization {
let top_width = DeclaredValue::Value(BorderWidth::from_length(Length::from_px(4f32)));
let top_style = DeclaredValue::Value(BorderStyle::solid);
let top_color = DeclaredValue::Initial;
let top_color = DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial);
properties.push(PropertyDeclaration::BorderTopWidth(top_width));
properties.push(PropertyDeclaration::BorderTopStyle(top_style));
@ -468,7 +469,7 @@ mod shorthand_serialization {
let right_width = DeclaredValue::Value(BorderWidth::from_length(Length::from_px(4f32)));
let right_style = DeclaredValue::Value(BorderStyle::solid);
let right_color = DeclaredValue::Initial;
let right_color = DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial);
properties.push(PropertyDeclaration::BorderRightWidth(right_width));
properties.push(PropertyDeclaration::BorderRightStyle(right_style));
@ -476,7 +477,7 @@ mod shorthand_serialization {
let bottom_width = DeclaredValue::Value(BorderWidth::from_length(Length::from_px(4f32)));
let bottom_style = DeclaredValue::Value(BorderStyle::solid);
let bottom_color = DeclaredValue::Initial;
let bottom_color = DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial);
properties.push(PropertyDeclaration::BorderBottomWidth(bottom_width));
properties.push(PropertyDeclaration::BorderBottomStyle(bottom_style));
@ -484,7 +485,7 @@ mod shorthand_serialization {
let left_width = DeclaredValue::Value(BorderWidth::from_length(Length::from_px(4f32)));
let left_style = DeclaredValue::Value(BorderStyle::solid);
let left_color = DeclaredValue::Initial;
let left_color = DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial);
properties.push(PropertyDeclaration::BorderLeftWidth(left_width));
properties.push(PropertyDeclaration::BorderLeftStyle(left_style));
@ -522,8 +523,8 @@ mod shorthand_serialization {
fn list_style_should_show_all_properties_even_if_only_one_is_set() {
let mut properties = Vec::new();
let position = DeclaredValue::Initial;
let image = DeclaredValue::Initial;
let position = DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial);
let image = DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial);
let style_type = DeclaredValue::Value(ListStyleType::disc);
properties.push(PropertyDeclaration::ListStylePosition(position));
@ -565,7 +566,7 @@ mod shorthand_serialization {
let width = DeclaredValue::Value(WidthContainer(Length::from_px(4f32)));
let style = DeclaredValue::Value(Either::Second(BorderStyle::solid));
let color = DeclaredValue::Initial;
let color = DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial);
properties.push(PropertyDeclaration::OutlineWidth(width));
properties.push(PropertyDeclaration::OutlineStyle(style));
@ -580,7 +581,7 @@ mod shorthand_serialization {
let mut properties = Vec::new();
let width = DeclaredValue::Value(WidthContainer(Length::from_px(4f32)));
let style = DeclaredValue::Initial;
let style = DeclaredValue::CSSWideKeyword(CSSWideKeyword::Initial);
let color = DeclaredValue::Value(CSSColor {
parsed: ComputedColor::RGBA(RGBA::new(255, 0, 0, 255)),
authored: None

View file

@ -17,7 +17,8 @@ use style::error_reporting::ParseErrorReporter;
use style::keyframes::{Keyframe, KeyframeSelector, KeyframePercentage};
use style::parser::ParserContextExtraData;
use style::properties::Importance;
use style::properties::{PropertyDeclaration, PropertyDeclarationBlock, DeclaredValue, longhands};
use style::properties::{CSSWideKeyword, PropertyDeclaration, PropertyDeclarationBlock};
use style::properties::{DeclaredValue, longhands};
use style::properties::longhands::animation_play_state;
use style::stylesheets::{Origin, Namespaces};
use style::stylesheets::{Stylesheet, NamespaceRule, CssRule, CssRules, StyleRule, KeyframesRule};
@ -102,7 +103,8 @@ fn test_parse_stylesheet() {
(PropertyDeclaration::Display(DeclaredValue::Value(
longhands::display::SpecifiedValue::none)),
Importance::Important),
(PropertyDeclaration::Custom(Atom::from("a"), DeclaredValue::Inherit),
(PropertyDeclaration::Custom(Atom::from("a"),
DeclaredValue::CSSWideKeyword(CSSWideKeyword::Inherit)),
Importance::Important),
],
important_count: 2,