Move CSSStyleDeclaration.SetPropertyPriority logic to style

This commit is contained in:
Simon Sapin 2016-10-07 19:20:05 +02:00
parent c740a76410
commit 0eed39c198
3 changed files with 30 additions and 49 deletions

View file

@ -242,24 +242,26 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
}
// Step 4
let priority = match &*priority {
let importance = match &*priority {
"" => Importance::Normal,
p if p.eq_ignore_ascii_case("important") => Importance::Important,
_ => return Ok(()),
};
let element = self.owner.upcast::<Element>();
let style_attribute = self.owner.style_attribute().borrow();
if let Some(ref lock) = *style_attribute {
let mut style_attribute = lock.write();
// Step 5 & 6
match Shorthand::from_name(&property) {
Some(shorthand) => {
element.set_inline_style_property_priority(shorthand.longhands(), priority)
// Step 5 & 6
match Shorthand::from_name(&property) {
Some(shorthand) => style_attribute.set_importance(shorthand.longhands(), importance),
None => style_attribute.set_importance(&[&*property], importance),
}
None => element.set_inline_style_property_priority(&[&*property], priority),
}
let node = element.upcast::<Node>();
node.dirty(NodeDamage::NodeStyleDamaged);
self.owner.set_style_attr(style_attribute.to_css_string());
let node = self.owner.upcast::<Node>();
node.dirty(NodeDamage::NodeStyleDamaged);
}
Ok(())
}

View file

@ -5,7 +5,7 @@
//! Element nodes.
use app_units::Au;
use cssparser::{Color, ToCss};
use cssparser::Color;
use devtools_traits::AttrInfo;
use dom::activation::Activatable;
use dom::attr::{Attr, AttrHelpersForLayout};
@ -735,15 +735,6 @@ impl Element {
// this sync method is called upon modification of the style_attribute property,
// therefore, it should not trigger subsequent mutation events
pub fn sync_property_with_attrs_style(&self) {
let style_str = if let &Some(ref declarations) = &*self.style_attribute().borrow() {
declarations.read().to_css_string()
} else {
String::new()
};
self.set_style_attr(style_str)
}
pub fn set_style_attr(&self, new_value: String) {
let mut new_style = AttrValue::String(new_value);
@ -767,35 +758,6 @@ impl Element {
self.attrs.borrow_mut().push(JS::from_ref(&attr));
}
pub fn set_inline_style_property_priority(&self,
properties: &[&str],
new_importance: Importance) {
{
let mut inline_declarations = self.style_attribute().borrow_mut();
if let &mut Some(ref mut block) = &mut *inline_declarations {
let mut block = block.write();
let block = &mut *block;
let declarations = &mut block.declarations;
for &mut (ref declaration, ref mut importance) in declarations {
if properties.iter().any(|p| declaration.name() == **p) {
match (*importance, new_importance) {
(Importance::Normal, Importance::Important) => {
block.important_count += 1;
}
(Importance::Important, Importance::Normal) => {
block.important_count -= 1;
}
_ => {}
}
*importance = new_importance;
}
}
}
}
self.sync_property_with_attrs_style();
}
pub fn serialize(&self, traversal_scope: TraversalScope) -> Fallible<DOMString> {
let mut writer = vec![];
match serialize(&mut writer,

View file

@ -154,6 +154,23 @@ impl PropertyDeclarationBlock {
}
}
pub fn set_importance(&mut self, property_names: &[&str], new_importance: Importance) {
for &mut (ref declaration, ref mut importance) in &mut self.declarations {
if property_names.iter().any(|p| declaration.matches(p)) {
match (*importance, new_importance) {
(Importance::Normal, Importance::Important) => {
self.important_count += 1;
}
(Importance::Important, Importance::Normal) => {
self.important_count -= 1;
}
_ => {}
}
*importance = new_importance;
}
}
}
/// https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-removeproperty
pub fn remove_property(&mut self, property_name: &str) {
// Step 2