style: Hoist cascade() to CustomPropertyBuilder.

This commit is contained in:
Emilio Cobos Álvarez 2017-10-08 19:12:47 +02:00
parent 715fc9cea6
commit 4914351f2b
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C

View file

@ -481,51 +481,16 @@ impl<'a> CustomPropertiesBuilder<'a> {
name: &'a Name, name: &'a Name,
specified_value: DeclaredValue<'a, Box<SpecifiedValue>>, specified_value: DeclaredValue<'a, Box<SpecifiedValue>>,
) { ) {
cascade( let was_already_present = !self.seen.insert(name);
&mut self.custom_properties,
self.inherited,
&mut self.seen,
name,
specified_value,
)
}
/// Returns the final map of applicable custom properties.
///
/// If there was any specified property, we've created a new map and now we need
/// to remove any potential cycles, and wrap it in an arc.
///
/// Otherwise, just use the inherited custom properties map.
pub fn build(mut self) -> Option<Arc<CustomPropertiesMap>> {
let mut map = match self.custom_properties.take() {
Some(map) => map,
None => return self.inherited.cloned(),
};
remove_cycles(&mut map);
Some(Arc::new(substitute_all(map)))
}
}
/// Add one custom property declaration to a map, unless another with the same
/// name was already there.
fn cascade<'a>(
custom_properties: &mut Option<OrderedMap<&'a Name, BorrowedSpecifiedValue<'a>>>,
inherited: Option<&'a Arc<CustomPropertiesMap>>,
seen: &mut PrecomputedHashSet<&'a Name>,
name: &'a Name,
specified_value: DeclaredValue<'a, Box<SpecifiedValue>>
) {
let was_already_present = !seen.insert(name);
if was_already_present { if was_already_present {
return; return;
} }
let map = match *custom_properties { let map = match self.custom_properties {
Some(ref mut map) => map, Some(ref mut map) => map,
None => { None => {
let mut map = OrderedMap::new(); let mut map = OrderedMap::new();
if let Some(inherited) = inherited { if let Some(inherited) = self.inherited {
for (name, inherited_value) in inherited.iter() { for (name, inherited_value) in inherited.iter() {
map.insert(name, BorrowedSpecifiedValue { map.insert(name, BorrowedSpecifiedValue {
css: &inherited_value.css, css: &inherited_value.css,
@ -535,10 +500,11 @@ fn cascade<'a>(
}) })
} }
} }
*custom_properties = Some(map); self.custom_properties = Some(map);
custom_properties.as_mut().unwrap() self.custom_properties.as_mut().unwrap()
} }
}; };
match specified_value { match specified_value {
DeclaredValue::Value(ref specified_value) => { DeclaredValue::Value(ref specified_value) => {
map.insert(name, BorrowedSpecifiedValue { map.insert(name, BorrowedSpecifiedValue {
@ -557,6 +523,23 @@ fn cascade<'a>(
CSSWideKeyword::Inherit => {} // The inherited value is what we already have. CSSWideKeyword::Inherit => {} // The inherited value is what we already have.
} }
} }
}
/// Returns the final map of applicable custom properties.
///
/// If there was any specified property, we've created a new map and now we need
/// to remove any potential cycles, and wrap it in an arc.
///
/// Otherwise, just use the inherited custom properties map.
pub fn build(mut self) -> Option<Arc<CustomPropertiesMap>> {
let mut map = match self.custom_properties.take() {
Some(map) => map,
None => return self.inherited.cloned(),
};
remove_cycles(&mut map);
Some(Arc::new(substitute_all(map)))
}
} }
/// https://drafts.csswg.org/css-variables/#cycles /// https://drafts.csswg.org/css-variables/#cycles