Skip to content

Commit

Permalink
Use PropertyId in per-property CSSStyleDeclaration accessors.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Dec 10, 2016
1 parent 433c33c commit dbc9fcc
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 79 deletions.
144 changes: 75 additions & 69 deletions components/script/dom/cssstyledeclaration.rs
Expand Up @@ -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;
Expand All @@ -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())
}
)*
);
Expand Down Expand Up @@ -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::<Element>();
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);
Expand All @@ -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() {
Expand Down Expand Up @@ -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::<Element>();
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 {
Expand Down
20 changes: 10 additions & 10 deletions components/style/properties/properties.mako.rs
Expand Up @@ -1985,17 +1985,17 @@ pub fn modify_style_for_inline_absolute_hypothetical_fragment(style: &mut Arc<Co
macro_rules! css_properties_accessors {
($macro_name: ident) => {
$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
}
}
Expand Down

0 comments on commit dbc9fcc

Please sign in to comment.