Box DeclaredValue::WithVariables

This commit is contained in:
Nazım Can Altınova 2017-02-09 03:23:50 +03:00 committed by Simon Sapin
parent 43731c5757
commit adb6d20293
6 changed files with 53 additions and 44 deletions

View file

@ -361,7 +361,7 @@ pub fn cascade<'a>(custom_properties: &mut Option<HashMap<&'a Name, BorrowedSpec
references: Some(&specified_value.references), references: Some(&specified_value.references),
}); });
}, },
DeclaredValue::WithVariables { .. } => unreachable!(), DeclaredValue::WithVariables(_) => unreachable!(),
DeclaredValue::Initial => { DeclaredValue::Initial => {
map.remove(&name); map.remove(&name);
} }

View file

@ -196,7 +196,7 @@
% if not property.derived_from: % if not property.derived_from:
use cssparser::Parser; use cssparser::Parser;
use parser::{Parse, ParserContext, ParserContextExtraData}; use parser::{Parse, ParserContext, ParserContextExtraData};
use properties::{CSSWideKeyword, DeclaredValue, ShorthandId}; use properties::{CSSWideKeyword, DeclaredValue, UnparsedValue, ShorthandId};
% endif % endif
use values::{Auto, Either, None_, Normal}; use values::{Auto, Either, None_, Normal};
use cascade_info::CascadeInfo; use cascade_info::CascadeInfo;
@ -259,7 +259,7 @@
.set_${property.ident}(computed ${maybe_wm}); .set_${property.ident}(computed ${maybe_wm});
% endif % endif
} }
DeclaredValue::WithVariables { .. } => unreachable!(), DeclaredValue::WithVariables(_) => unreachable!(),
% if not data.current_style_struct.inherited: % if not data.current_style_struct.inherited:
DeclaredValue::Unset | DeclaredValue::Unset |
% endif % endif
@ -324,12 +324,12 @@
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 { return Ok(DeclaredValue::WithVariables(Box::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(),
from_shorthand: None, from_shorthand: None,
}) })))
} }
specified specified
} }
@ -421,7 +421,8 @@
#[allow(unused_imports)] #[allow(unused_imports)]
use cssparser::Parser; use cssparser::Parser;
use parser::ParserContext; use parser::ParserContext;
use properties::{longhands, PropertyDeclaration, DeclaredValue, ShorthandId}; use properties::{DeclaredValue, PropertyDeclaration, UnparsedValue};
use properties::{ShorthandId, longhands};
use properties::declaration_block::Importance; use properties::declaration_block::Importance;
use std::fmt; use std::fmt;
use style_traits::ToCss; use style_traits::ToCss;
@ -504,7 +505,7 @@
DeclaredValue::Initial => all_flags &= ALL_INITIAL, DeclaredValue::Initial => all_flags &= ALL_INITIAL,
DeclaredValue::Inherit => all_flags &= ALL_INHERIT, DeclaredValue::Inherit => all_flags &= ALL_INHERIT,
DeclaredValue::Unset => all_flags &= ALL_UNSET, DeclaredValue::Unset => all_flags &= ALL_UNSET,
DeclaredValue::WithVariables {..} => with_variables = true, DeclaredValue::WithVariables(_) => with_variables = true,
DeclaredValue::Value(..) => { DeclaredValue::Value(..) => {
all_flags = SerializeFlags::empty(); all_flags = SerializeFlags::empty();
} }
@ -560,12 +561,12 @@
::custom_properties::parse_non_custom_with_var(input)); ::custom_properties::parse_non_custom_with_var(input));
% for sub_property in shorthand.sub_properties: % for sub_property in shorthand.sub_properties:
declarations.push((PropertyDeclaration::${sub_property.camel_case}( declarations.push((PropertyDeclaration::${sub_property.camel_case}(
DeclaredValue::WithVariables { DeclaredValue::WithVariables(Box::new(UnparsedValue {
css: css.clone().into_owned(), css: css.clone().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(),
from_shorthand: Some(ShorthandId::${shorthand.camel_case}), from_shorthand: Some(ShorthandId::${shorthand.camel_case}),
} }))
), Importance::Normal)); ), Importance::Normal));
% endfor % endfor
Ok(()) Ok(())

View file

@ -297,7 +297,7 @@ impl AnimationValue {
PropertyDeclaration::${prop.camel_case}(ref val) => { PropertyDeclaration::${prop.camel_case}(ref val) => {
let computed = match *val { let computed = match *val {
// https://bugzilla.mozilla.org/show_bug.cgi?id=1326131 // https://bugzilla.mozilla.org/show_bug.cgi?id=1326131
DeclaredValue::WithVariables{..} => unimplemented!(), DeclaredValue::WithVariables(_) => unimplemented!(),
DeclaredValue::Value(ref val) => val.to_computed_value(context), DeclaredValue::Value(ref val) => val.to_computed_value(context),
% if not prop.style_struct.inherited: % if not prop.style_struct.inherited:
DeclaredValue::Unset | DeclaredValue::Unset |

View file

@ -260,17 +260,15 @@ mod property_bit_field {
where F: FnOnce(&DeclaredValue<longhands::${property.ident}::SpecifiedValue>) where F: FnOnce(&DeclaredValue<longhands::${property.ident}::SpecifiedValue>)
% endif % endif
{ {
if let DeclaredValue::WithVariables { if let DeclaredValue::WithVariables(ref with_variables) = *value {
ref css, first_token_type, ref base_url, from_shorthand
} = *value {
// FIXME(heycam): A ParserContextExtraData should be built from data // FIXME(heycam): A ParserContextExtraData should be built from data
// stored in the WithVariables, in case variable expansion results in // stored in the WithVariables, in case variable expansion results in
// a url() value. // a url() value.
let extra_data = ParserContextExtraData::default(); let extra_data = ParserContextExtraData::default();
substitute_variables_${property.ident}_slow(css, substitute_variables_${property.ident}_slow(&with_variables.css,
first_token_type, with_variables.first_token_type,
base_url, &with_variables.base_url,
from_shorthand, with_variables.from_shorthand,
custom_properties, custom_properties,
f, f,
error_reporter, error_reporter,
@ -614,17 +612,8 @@ impl ShorthandId {
pub enum DeclaredValue<T> { pub enum DeclaredValue<T> {
/// A known specified value from the stylesheet. /// A known specified value from the stylesheet.
Value(T), Value(T),
/// A value that contained any css variables. /// An unparsed value that contains `var()` functions.
WithVariables { WithVariables(Box<UnparsedValue>),
/// The css serialization for this value.
css: String,
/// The first token type for this serialization.
first_token_type: TokenSerializationType,
/// The base url.
base_url: ServoUrl,
/// The shorthand this came from.
from_shorthand: Option<ShorthandId>,
},
/// The `initial` keyword. /// The `initial` keyword.
Initial, Initial,
/// The `inherit` keyword. /// The `inherit` keyword.
@ -633,11 +622,25 @@ pub enum DeclaredValue<T> {
Unset, Unset,
} }
/// An unparsed property value that contains `var()` functions.
#[derive(Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct UnparsedValue {
/// The css serialization for this value.
css: String,
/// The first token type for this serialization.
first_token_type: TokenSerializationType,
/// The base url.
base_url: ServoUrl,
/// The shorthand this came from.
from_shorthand: Option<ShorthandId>,
}
impl<T: HasViewportPercentage> HasViewportPercentage for DeclaredValue<T> { impl<T: HasViewportPercentage> HasViewportPercentage for DeclaredValue<T> {
fn has_viewport_percentage(&self) -> bool { fn has_viewport_percentage(&self) -> bool {
match *self { match *self {
DeclaredValue::Value(ref v) => v.has_viewport_percentage(), DeclaredValue::Value(ref v) => v.has_viewport_percentage(),
DeclaredValue::WithVariables { .. } => { DeclaredValue::WithVariables(_) => {
panic!("DeclaredValue::has_viewport_percentage without \ panic!("DeclaredValue::has_viewport_percentage without \
resolving variables!") resolving variables!")
}, },
@ -654,11 +657,13 @@ impl<T: ToCss> ToCss for DeclaredValue<T> {
{ {
match *self { match *self {
DeclaredValue::Value(ref inner) => inner.to_css(dest), DeclaredValue::Value(ref inner) => inner.to_css(dest),
DeclaredValue::WithVariables { ref css, from_shorthand: None, .. } => { DeclaredValue::WithVariables(ref with_variables) => {
dest.write_str(css) // https://drafts.csswg.org/css-variables/#variables-in-shorthands
} if with_variables.from_shorthand.is_none() {
// https://drafts.csswg.org/css-variables/#variables-in-shorthands dest.write_str(&*with_variables.css)?
DeclaredValue::WithVariables { .. } => Ok(()), }
Ok(())
},
DeclaredValue::Initial => dest.write_str("initial"), DeclaredValue::Initial => dest.write_str("initial"),
DeclaredValue::Inherit => dest.write_str("inherit"), DeclaredValue::Inherit => dest.write_str("inherit"),
DeclaredValue::Unset => dest.write_str("unset"), DeclaredValue::Unset => dest.write_str("unset"),
@ -959,9 +964,12 @@ impl PropertyDeclaration {
match *self { match *self {
% for property in data.longhands: % for property in data.longhands:
PropertyDeclaration::${property.camel_case}(ref value) => match *value { PropertyDeclaration::${property.camel_case}(ref value) => match *value {
DeclaredValue::WithVariables { ref css, from_shorthand: Some(s), .. } DeclaredValue::WithVariables(ref with_variables) => {
if s == shorthand => { if let Some(s) = with_variables.from_shorthand {
Some(&**css) if s == shorthand {
Some(&*with_variables.css)
} else { None }
} else { None }
} }
_ => None _ => None
}, },
@ -976,12 +984,12 @@ impl PropertyDeclaration {
match *self { match *self {
% for property in data.longhands: % for property in data.longhands:
PropertyDeclaration::${property.camel_case}(ref value) => match *value { PropertyDeclaration::${property.camel_case}(ref value) => match *value {
DeclaredValue::WithVariables { .. } => true, DeclaredValue::WithVariables(_) => true,
_ => false, _ => false,
}, },
% endfor % endfor
PropertyDeclaration::Custom(_, ref value) => match *value { PropertyDeclaration::Custom(_, ref value) => match *value {
DeclaredValue::WithVariables { .. } => true, DeclaredValue::WithVariables(_) => true,
_ => false, _ => false,
} }
} }
@ -997,7 +1005,7 @@ impl PropertyDeclaration {
match *self { match *self {
% for property in data.longhands: % for property in data.longhands:
PropertyDeclaration::${property.camel_case}(ref value) => { PropertyDeclaration::${property.camel_case}(ref value) => {
matches!(*value, DeclaredValue::WithVariables { .. }) matches!(*value, DeclaredValue::WithVariables(_))
}, },
% endfor % endfor
PropertyDeclaration::Custom(..) => true PropertyDeclaration::Custom(..) => true

View file

@ -22,7 +22,7 @@
(&DeclaredValue::Value(ref x_value), &DeclaredValue::Value(ref y_container)) => { (&DeclaredValue::Value(ref x_value), &DeclaredValue::Value(ref y_container)) => {
*x_value == y_container.0 *x_value == y_container.0
}, },
(&DeclaredValue::WithVariables { .. }, &DeclaredValue::WithVariables { .. }) => true, (&DeclaredValue::WithVariables(_), &DeclaredValue::WithVariables(_)) => true,
(&DeclaredValue::Initial, &DeclaredValue::Initial) => true, (&DeclaredValue::Initial, &DeclaredValue::Initial) => true,
(&DeclaredValue::Inherit, &DeclaredValue::Inherit) => true, (&DeclaredValue::Inherit, &DeclaredValue::Inherit) => true,
(&DeclaredValue::Unset, &DeclaredValue::Unset) => true, (&DeclaredValue::Unset, &DeclaredValue::Unset) => true,
@ -43,8 +43,8 @@
(&DeclaredValue::Value(ref x_value), &DeclaredValue::Value(ref y_container)) => { (&DeclaredValue::Value(ref x_value), &DeclaredValue::Value(ref y_container)) => {
*x_value == y_container.0 *x_value == y_container.0
}, },
(_, &DeclaredValue::WithVariables { .. }) | (_, &DeclaredValue::WithVariables(_)) |
(&DeclaredValue::WithVariables { .. }, _) => { (&DeclaredValue::WithVariables(_), _) => {
// We don't serialize shorthands with variables // We don't serialize shorthands with variables
return dest.write_str(""); return dest.write_str("");
}, },

View file

@ -7,7 +7,7 @@ use style::properties::{PropertyDeclaration, specified_value_sizes};
#[test] #[test]
fn size_of_property_declaration() { fn size_of_property_declaration() {
let old = 72; let old = 48;
let new = size_of::<PropertyDeclaration>(); let new = size_of::<PropertyDeclaration>();
if new < old { if new < old {
panic!("Your changes have decreased the stack size of PropertyDeclaration enum from {} to {}. \ panic!("Your changes have decreased the stack size of PropertyDeclaration enum from {} to {}. \