Skip to content

Commit

Permalink
Move CSSStyleDeclaration.GetPropertyPriority logic to style
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Oct 10, 2016
1 parent bd37f4e commit fc6a536
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 36 deletions.
36 changes: 11 additions & 25 deletions components/script/dom/cssstyledeclaration.rs
Expand Up @@ -126,34 +126,20 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {

// https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-getpropertypriority
fn GetPropertyPriority(&self, mut property: DOMString) -> DOMString {
// Step 1
property.make_ascii_lowercase();
let property = Atom::from(property);

// Step 2
if let Some(shorthand) = Shorthand::from_name(&property) {
// Step 2.1 & 2.2 & 2.3
if shorthand.longhands().iter()
.map(|&longhand| self.GetPropertyPriority(DOMString::from(longhand)))
.all(|priority| priority == "important") {
return DOMString::from("important");
}
let style_attribute = self.owner.style_attribute().borrow();
let style_attribute = if let Some(ref style_attribute) = *style_attribute {
style_attribute.read()
} else {
// Step 3
return self.owner.get_inline_style_declaration(&property, |d| {
if let Some(decl) = d {
if decl.1.important() {
return DOMString::from("important");
}
}
// No style attribute is like an empty style attribute: no matching declaration.
return DOMString::new()
};

// Step 4
DOMString::new()
})
if style_attribute.property_priority(&property).important() {
DOMString::from("important")
} else {
// Step 4
DOMString::new()
}

// Step 4
DOMString::new()
}

// https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-setproperty
Expand Down
11 changes: 0 additions & 11 deletions components/script/dom/element.rs
Expand Up @@ -872,17 +872,6 @@ impl Element {
self.sync_property_with_attrs_style();
}

pub fn get_inline_style_declaration<F, R>(&self, property: &str, f: F) -> R
where F: FnOnce(Option<&(PropertyDeclaration, Importance)>) -> R {
let style_attr = self.style_attribute.borrow();
if let Some(ref block) = *style_attr {
let block = block.read();
f(block.get(property))
} else {
f(None)
}
}

pub fn serialize(&self, traversal_scope: TraversalScope) -> Fallible<DOMString> {
let mut writer = vec![];
match serialize(&mut writer,
Expand Down
21 changes: 21 additions & 0 deletions components/style/properties/declaration_block.rs
Expand Up @@ -109,6 +109,27 @@ impl PropertyDeclarationBlock {
}
}

/// https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-getpropertypriority
pub fn property_priority(&self, property_name: &str) -> Importance {
// Step 1
let property = property_name.to_ascii_lowercase();

// Step 2
if let Some(shorthand) = Shorthand::from_name(&property) {
// Step 2.1 & 2.2 & 2.3
if shorthand.longhands().iter().all(|l| {
self.get(l).map_or(false, |&(_, importance)| importance.important())
}) {
Importance::Important
} else {
Importance::Normal
}
} else {
// Step 3
self.get(&property).map_or(Importance::Normal, |&(_, importance)| importance)
}
}

/// Take a declaration block known to contain a single property and serialize it.
pub fn single_value_to_css<W>(&self, property_name: &str, dest: &mut W) -> fmt::Result
where W: fmt::Write {
Expand Down

0 comments on commit fc6a536

Please sign in to comment.