From dbc9fcc2de0e5ff355dcb7b00ab11aabe45f5857 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Fri, 9 Dec 2016 10:47:35 -1000 Subject: [PATCH] Use PropertyId in per-property CSSStyleDeclaration accessors. --- components/script/dom/cssstyledeclaration.rs | 144 +++++++++--------- .../style/properties/properties.mako.rs | 20 +-- 2 files changed, 85 insertions(+), 79 deletions(-) diff --git a/components/script/dom/cssstyledeclaration.rs b/components/script/dom/cssstyledeclaration.rs index 90df36f2f1dc..36d85ec03227 100644 --- a/components/script/dom/cssstyledeclaration.rs +++ b/components/script/dom/cssstyledeclaration.rs @@ -15,7 +15,7 @@ use parking_lot::RwLock; use std::ascii::AsciiExt; use std::sync::Arc; use style::parser::ParserContextExtraData; -use style::properties::{Importance, PropertyDeclarationBlock, PropertyId}; +use style::properties::{Importance, PropertyDeclarationBlock, PropertyId, LonghandId, ShorthandId}; use style::properties::{parse_one_declaration, parse_style_attribute}; use style::selector_parser::PseudoElement; use style_traits::ToCss; @@ -36,13 +36,13 @@ pub enum CSSModificationAccess { } macro_rules! css_properties( - ( $([$getter:ident, $setter:ident, $cssprop:expr]),* ) => ( + ( $([$getter:ident, $setter:ident, $id:expr],)* ) => ( $( fn $getter(&self) -> DOMString { - self.GetPropertyValue(DOMString::from($cssprop)) + self.get_property_value($id) } fn $setter(&self, value: DOMString) -> ErrorResult { - self.SetPropertyValue(DOMString::from($cssprop), value) + self.set_property($id, value, DOMString::new()) } )* ); @@ -83,33 +83,8 @@ impl CSSStyleDeclaration { let addr = node.to_trusted_node_address(); window_from_node(&*self.owner).resolved_style_query(addr, self.pseudo.clone(), property) } -} - -impl CSSStyleDeclarationMethods for CSSStyleDeclaration { - // https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-length - fn Length(&self) -> u32 { - let elem = self.owner.upcast::(); - let len = match *elem.style_attribute().borrow() { - Some(ref lock) => lock.read().declarations.len(), - None => 0, - }; - len as u32 - } - - // https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-item - fn Item(&self, index: u32) -> DOMString { - self.IndexedGetter(index).unwrap_or_default() - } - - // https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-getpropertyvalue - fn GetPropertyValue(&self, property: DOMString) -> DOMString { - let id = if let Ok(id) = PropertyId::parse(property.into()) { - id - } else { - // Unkwown property - return DOMString::new() - }; + fn get_property_value(&self, id: PropertyId) -> DOMString { if self.readonly { // Readonly style declarations are used for getComputedStyle. return self.get_computed_style(id); @@ -128,50 +103,12 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration { DOMString::from(string) } - // https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-getpropertypriority - fn GetPropertyPriority(&self, property: DOMString) -> DOMString { - let id = if let Ok(id) = PropertyId::parse(property.into()) { - id - } else { - // Unkwown property - return DOMString::new() - }; - - let style_attribute = self.owner.style_attribute().borrow(); - let style_attribute = if let Some(ref lock) = *style_attribute { - lock.read() - } else { - // No style attribute is like an empty style attribute: no matching declaration. - return DOMString::new() - }; - - if style_attribute.property_priority(&id).important() { - DOMString::from("important") - } else { - // Step 4 - DOMString::new() - } - } - - // https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-setproperty - fn SetProperty(&self, - property: DOMString, - value: DOMString, - priority: DOMString) - -> ErrorResult { + fn set_property(&self, id: PropertyId, value: DOMString, priority: DOMString) -> ErrorResult { // Step 1 if self.readonly { return Err(Error::NoModificationAllowed); } - // Step 3 - let id = if let Ok(id) = PropertyId::parse(property.into()) { - id - } else { - // Unkwown property - return Ok(()) - }; - let mut style_attribute = self.owner.style_attribute().borrow_mut(); if value.is_empty() { @@ -242,6 +179,75 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration { node.dirty(NodeDamage::NodeStyleDamaged); Ok(()) } +} + +impl CSSStyleDeclarationMethods for CSSStyleDeclaration { + // https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-length + fn Length(&self) -> u32 { + let elem = self.owner.upcast::(); + let len = match *elem.style_attribute().borrow() { + Some(ref lock) => lock.read().declarations.len(), + None => 0, + }; + len as u32 + } + + // https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-item + fn Item(&self, index: u32) -> DOMString { + self.IndexedGetter(index).unwrap_or_default() + } + + // https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-getpropertyvalue + fn GetPropertyValue(&self, property: DOMString) -> DOMString { + let id = if let Ok(id) = PropertyId::parse(property.into()) { + id + } else { + // Unkwown property + return DOMString::new() + }; + self.get_property_value(id) + } + + // https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-getpropertypriority + fn GetPropertyPriority(&self, property: DOMString) -> DOMString { + let id = if let Ok(id) = PropertyId::parse(property.into()) { + id + } else { + // Unkwown property + return DOMString::new() + }; + + let style_attribute = self.owner.style_attribute().borrow(); + let style_attribute = if let Some(ref lock) = *style_attribute { + lock.read() + } else { + // No style attribute is like an empty style attribute: no matching declaration. + return DOMString::new() + }; + + if style_attribute.property_priority(&id).important() { + DOMString::from("important") + } else { + // Step 4 + DOMString::new() + } + } + + // https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-setproperty + fn SetProperty(&self, + property: DOMString, + value: DOMString, + priority: DOMString) + -> ErrorResult { + // Step 3 + let id = if let Ok(id) = PropertyId::parse(property.into()) { + id + } else { + // Unkwown property + return Ok(()) + }; + self.set_property(id, value, priority) + } // https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-setpropertypriority fn SetPropertyPriority(&self, property: DOMString, priority: DOMString) -> ErrorResult { diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index 5dfb0d018d96..0ad9937acfc6 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -1985,17 +1985,17 @@ pub fn modify_style_for_inline_absolute_hypothetical_fragment(style: &mut Arc { $macro_name! { - % for property in data.shorthands + data.longhands: - % if not property.derived_from and not property.internal: - % if '-' in property.name: - [${property.ident.capitalize()}, Set${property.ident.capitalize()}, "${property.name}"], - % endif - % if property != data.longhands[-1]: - [${property.camel_case}, Set${property.camel_case}, "${property.name}"], - % else: - [${property.camel_case}, Set${property.camel_case}, "${property.name}"] + % for kind, props in [("Longhand", data.longhands), ("Shorthand", data.shorthands)]: + % for property in props: + % if not property.derived_from and not property.internal: + % if '-' in property.name: + [${property.ident.capitalize()}, Set${property.ident.capitalize()}, + PropertyId::${kind}(${kind}Id::${property.camel_case})], + % endif + [${property.camel_case}, Set${property.camel_case}, + PropertyId::${kind}(${kind}Id::${property.camel_case})], % endif - % endif + % endfor % endfor } }