Add GetPropertyPriority to CSSStyleDeclaration

Tweak getPropertyPriority to match recommendations

Adding Tests for Style Priority

Use else if

Adding Content Test for GetPropertyPriority

Revert "Adding Tests for Style Priority"

This reverts commit 8666a37f833b06c3e43f27acd8a9678e94425e55.
This commit is contained in:
Adam Sunderland 2014-12-23 00:28:31 -06:00
parent 49f2c6974d
commit 674fe910c1
4 changed files with 68 additions and 1 deletions

View file

@ -69,6 +69,7 @@ impl CSSStyleDeclaration {
trait PrivateCSSStyleDeclarationHelpers {
fn get_declaration(self, property: &Atom) -> Option<PropertyDeclaration>;
fn get_important_declaration(self, property: &Atom) -> Option<PropertyDeclaration>;
}
impl<'a> PrivateCSSStyleDeclarationHelpers for JSRef<'a, CSSStyleDeclaration> {
@ -77,6 +78,12 @@ impl<'a> PrivateCSSStyleDeclarationHelpers for JSRef<'a, CSSStyleDeclaration> {
let element: JSRef<Element> = ElementCast::from_ref(*owner);
element.get_inline_style_declaration(property).map(|decl| decl.clone())
}
fn get_important_declaration(self, property: &Atom) -> Option<PropertyDeclaration> {
let owner = self.owner.root();
let element: JSRef<Element> = ElementCast::from_ref(*owner);
element.get_important_inline_style_declaration(property).map(|decl| decl.clone())
}
}
impl<'a> CSSStyleDeclarationMethods for JSRef<'a, CSSStyleDeclaration> {
@ -144,6 +151,30 @@ impl<'a> CSSStyleDeclarationMethods for JSRef<'a, CSSStyleDeclaration> {
}
}
// http://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-getpropertypriority
fn GetPropertyPriority(self, property: DOMString) -> DOMString {
// Step 1
let property = Atom::from_slice(property.as_slice().to_ascii_lower().as_slice());
// Step 2
let longhand_properties = longhands_from_shorthand(property.as_slice());
if let Some(longhand_properties) = longhand_properties {
// Step 2.1 & 2.2 & 2.3
if longhand_properties.iter()
.map(|longhand| self.GetPropertyPriority(longhand.clone()))
.all(|priority| priority.as_slice() == "important") {
return "important".to_string();
}
// Step 3
} else if self.get_important_declaration(&property).is_some() {
return "important".to_string();
}
// Step 4
"".to_string()
}
// http://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-setproperty
fn SetProperty(self, property: DOMString, value: DOMString,
priority: DOMString) -> ErrorResult {

View file

@ -469,6 +469,7 @@ pub trait ElementHelpers<'a> {
fn remove_inline_style_property(self, property: DOMString);
fn update_inline_style(self, property_decl: style::PropertyDeclaration, important: bool);
fn get_inline_style_declaration(self, property: &Atom) -> Option<style::PropertyDeclaration>;
fn get_important_inline_style_declaration(self, property: &Atom) -> Option<style::PropertyDeclaration>;
}
impl<'a> ElementHelpers<'a> for JSRef<'a, Element> {
@ -595,6 +596,16 @@ impl<'a> ElementHelpers<'a> for JSRef<'a, Element> {
.map(|decl| decl.clone())
})
}
fn get_important_inline_style_declaration(self, property: &Atom) -> Option<style::PropertyDeclaration> {
let inline_declarations = self.style_attribute.borrow();
inline_declarations.as_ref().and_then(|declarations| {
declarations.important
.iter()
.find(|decl| decl.matches(property.as_slice()))
.map(|decl| decl.clone())
})
}
}
pub trait AttributeHandlers {

View file

@ -14,7 +14,7 @@ interface CSSStyleDeclaration {
readonly attribute unsigned long length;
getter DOMString item(unsigned long index);
DOMString getPropertyValue(DOMString property);
//DOMString getPropertyPriority(DOMString property);
DOMString getPropertyPriority(DOMString property);
[Throws]
void setProperty(DOMString property, [TreatNullAs=EmptyString] DOMString value,
[TreatNullAs=EmptyString] optional DOMString priority = "");

View file

@ -0,0 +1,25 @@
<html>
<head>
<script src="harness.js"></script>
</head>
<body>
<div id="important" style='outline-color: #111 !important; outline-style: solid !important; outline-width: 1px !important; background-color: #222 !important; color: #FFF'></div>
<script>
var properties = {
'outline-color': 'important',
'outline-style': 'important',
'outline-width': 'important',
'outline': 'important',
'background-color': 'important',
'background': '',
'color': '',
};
var elem = document.getElementById('important')
for (var property in Object.keys(properties)) {
var name = Object.keys(properties)[property];
var value = properties[name];
is(elem.style.getPropertyPriority(name), value, name + ' priority');
}
</script>
</body>
</html>