Changing update_inline_style to process multiple declarations at once to handle shorthand serialization better

This commit is contained in:
David Raifaizen 2016-04-24 01:43:59 -04:00 committed by Simon Sapin
parent f38607967e
commit 3dafd558c5
3 changed files with 131 additions and 81 deletions

View file

@ -157,7 +157,9 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
self.0.next().map(|r| &**r)
}
}
let serialized_value = shorthand.serialize_shorthand_to_string(Map(list.iter()));
// TODO: important is hardcoded to false because method does not implement it yet
let serialized_value = shorthand.serialize_shorthand_value_to_string(Map(list.iter()), false);
return DOMString::from(serialized_value);
}
@ -241,10 +243,8 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
let element = self.owner.upcast::<Element>();
// Step 8
for decl in declarations {
// Step 9
element.update_inline_style(decl, priority);
}
// Step 9
element.update_inline_style(declarations, priority);
Ok(())
}

View file

@ -748,37 +748,45 @@ impl Element {
}
pub fn update_inline_style(&self,
property_decl: PropertyDeclaration,
declarations: Vec<PropertyDeclaration>,
style_priority: StylePriority) {
fn update(element: &Element, property_decl: PropertyDeclaration, style_priority: StylePriority) {
fn update(element: &Element, mut declarations: Vec<PropertyDeclaration>, style_priority: StylePriority) {
let mut inline_declarations = element.style_attribute().borrow_mut();
if let &mut Some(ref mut declarations) = &mut *inline_declarations {
if let &mut Some(ref mut existing_declarations) = &mut *inline_declarations {
let existing_declarations = if style_priority == StylePriority::Important {
&mut declarations.important
&mut existing_declarations.important
} else {
&mut declarations.normal
&mut existing_declarations.normal
};
// Usually, the reference count will be 1 here. But transitions could make it greater
// than that.
let existing_declarations = Arc::make_mut(existing_declarations);
for declaration in &mut *existing_declarations {
if declaration.name() == property_decl.name() {
*declaration = property_decl;
return;
while let Some(mut incoming_declaration) = declarations.pop() {
let mut replaced = false;
for existing_declaration in &mut *existing_declarations {
if existing_declaration.name() == incoming_declaration.name() {
mem::swap(existing_declaration, &mut incoming_declaration);
replaced = true;
break;
}
}
if !replaced {
// inserting instead of pushing since the declarations are in reverse order
existing_declarations.insert(0, incoming_declaration);
}
}
// inserting instead of pushing since the declarations are in reverse order
existing_declarations.insert(0, property_decl);
return;
}
let (important, normal) = if style_priority == StylePriority::Important {
(vec![property_decl], vec![])
(declarations, vec![])
} else {
(vec![], vec![property_decl])
(vec![], declarations)
};
*inline_declarations = Some(PropertyDeclarationBlock {
@ -787,7 +795,7 @@ impl Element {
});
}
update(self, property_decl, style_priority);
update(self, declarations, style_priority);
self.sync_property_with_attrs_style();
}