From 674fe910c1eab23911fb2fc36bf19ea0504fa1f5 Mon Sep 17 00:00:00 2001 From: Adam Sunderland Date: Tue, 23 Dec 2014 00:28:31 -0600 Subject: [PATCH] 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. --- components/script/dom/cssstyledeclaration.rs | 31 +++++++++++++++++++ components/script/dom/element.rs | 11 +++++++ .../dom/webidls/CSSStyleDeclaration.webidl | 2 +- tests/content/test_getPropertyPriority.html | 25 +++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tests/content/test_getPropertyPriority.html diff --git a/components/script/dom/cssstyledeclaration.rs b/components/script/dom/cssstyledeclaration.rs index df3c492d9da..cacb5afb1a7 100644 --- a/components/script/dom/cssstyledeclaration.rs +++ b/components/script/dom/cssstyledeclaration.rs @@ -69,6 +69,7 @@ impl CSSStyleDeclaration { trait PrivateCSSStyleDeclarationHelpers { fn get_declaration(self, property: &Atom) -> Option; + fn get_important_declaration(self, property: &Atom) -> Option; } impl<'a> PrivateCSSStyleDeclarationHelpers for JSRef<'a, CSSStyleDeclaration> { @@ -77,6 +78,12 @@ impl<'a> PrivateCSSStyleDeclarationHelpers for JSRef<'a, CSSStyleDeclaration> { let element: JSRef = ElementCast::from_ref(*owner); element.get_inline_style_declaration(property).map(|decl| decl.clone()) } + + fn get_important_declaration(self, property: &Atom) -> Option { + let owner = self.owner.root(); + let element: JSRef = 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 { diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index 00f481dcd32..b1d85ef8c74 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -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; + fn get_important_inline_style_declaration(self, property: &Atom) -> Option; } 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 { + 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 { diff --git a/components/script/dom/webidls/CSSStyleDeclaration.webidl b/components/script/dom/webidls/CSSStyleDeclaration.webidl index 7b0e2213956..38f20af8d4e 100644 --- a/components/script/dom/webidls/CSSStyleDeclaration.webidl +++ b/components/script/dom/webidls/CSSStyleDeclaration.webidl @@ -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 = ""); diff --git a/tests/content/test_getPropertyPriority.html b/tests/content/test_getPropertyPriority.html new file mode 100644 index 00000000000..85bcde77238 --- /dev/null +++ b/tests/content/test_getPropertyPriority.html @@ -0,0 +1,25 @@ + + + + + +
+ + +