diff --git a/components/style/custom_properties.rs b/components/style/custom_properties.rs index cb9e3956184..7eca6b6eece 100644 --- a/components/style/custom_properties.rs +++ b/components/style/custom_properties.rs @@ -97,32 +97,35 @@ impl ToCss for ComputedValue { /// DOM. CSSDeclarations expose property names as indexed properties, which /// need to be stable. So we keep an array of property names which order is /// determined on the order that they are added to the name-value map. +pub type CustomPropertiesMap = OrderedMap; + +/// A map that preserves order for the keys, and that is easily indexable. #[derive(Clone, Debug, Eq, PartialEq)] -pub struct CustomPropertiesMap { +pub struct OrderedMap { /// Custom property name index. index: Vec, /// Computed values indexed by custom property name. - values: HashMap, + values: HashMap, } -impl CustomPropertiesMap { +impl OrderedMap { /// Creates a new custom properties map. pub fn new() -> Self { - CustomPropertiesMap { + OrderedMap { index: Vec::new(), values: HashMap::new(), } } /// Insert a computed value if it has not previously been inserted. - pub fn insert(&mut self, name: &Name, value: ComputedValue) { + pub fn insert(&mut self, name: &Name, value: T) { debug_assert!(!self.index.contains(name)); self.index.push(name.clone()); self.values.insert(name.clone(), value); } /// Custom property computed value getter by name. - pub fn get_computed_value(&self, name: &Name) -> Option<&ComputedValue> { + pub fn get(&self, name: &Name) -> Option<&T> { let value = self.values.get(name); debug_assert_eq!(value.is_some(), self.index.contains(name)); value @@ -134,7 +137,7 @@ impl CustomPropertiesMap { } /// Get an iterator for custom properties computed values. - pub fn iter(&self) -> hash_map::Iter { + pub fn iter(&self) -> hash_map::Iter { self.values.iter() } @@ -525,7 +528,7 @@ fn substitute_one(name: &Name, custom_properties_map: &mut CustomPropertiesMap, invalid: &mut HashSet) -> Result { - if let Some(computed_value) = custom_properties_map.get_computed_value(&name) { + if let Some(computed_value) = custom_properties_map.get(&name) { if let Some(partial_computed_value) = partial_computed_value { partial_computed_value.push_variable(computed_value) } @@ -679,7 +682,7 @@ pub fn substitute<'i>(input: &'i str, first_token_type: TokenSerializationType, let mut position = (input.position(), first_token_type); let last_token_type = substitute_block( &mut input, &mut position, &mut substituted, &mut |name, substituted| { - if let Some(value) = computed_values_map.as_ref().and_then(|map| map.get_computed_value(name)) { + if let Some(value) = computed_values_map.as_ref().and_then(|map| map.get(name)) { substituted.push_variable(value); Ok(value.last_token_type) } else { diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index 82b2f4cb73b..dd0049cb75b 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -2314,7 +2314,7 @@ impl ComputedValuesInner { PropertyDeclarationId::Custom(name) => { self.custom_properties .as_ref() - .and_then(|map| map.get_computed_value(name)) + .and_then(|map| map.get(name)) .map(|value| value.to_css_string()) .unwrap_or(String::new()) }