Skip to content

Commit

Permalink
Move CSSStyleDeclaration.SetPropertyPriority logic to style
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Oct 10, 2016
1 parent c740a76 commit 0eed39c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 49 deletions.
22 changes: 12 additions & 10 deletions components/script/dom/cssstyledeclaration.rs
Expand Up @@ -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(())
}

Expand Down
40 changes: 1 addition & 39 deletions components/script/dom/element.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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);

Expand All @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions components/style/properties/declaration_block.rs
Expand Up @@ -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
Expand Down

0 comments on commit 0eed39c

Please sign in to comment.