Skip to content

Commit

Permalink
Introduce enums for identifying CSS properties.
Browse files Browse the repository at this point in the history
* `LonghandId` and `ShorthandId` are C-like enums
* `Atom` is used for the name of custom properties.
* `PropertyDeclarationId` is the identifier for `PropertyDeclaration`,
  after parsing and shorthand expansion. (Longhand or custom property.)
* `PropertyId` represents any CSS property, e.g. in CSSOM.
  (Longhand, shorthand, or custom.)

Using these instead of strings avoids some memory allocations and copies.
  • Loading branch information
SimonSapin committed Dec 9, 2016
1 parent 97344b1 commit 137e30b
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 279 deletions.
6 changes: 6 additions & 0 deletions components/script/dom/bindings/str.rs
Expand Up @@ -286,6 +286,12 @@ impl Into<Vec<u8>> for DOMString {
}
}

impl<'a> Into<Cow<'a, str>> for DOMString {
fn into(self) -> Cow<'a, str> {
self.0.into()
}
}

impl Extend<char> for DOMString {
fn extend<I>(&mut self, iterable: I) where I: IntoIterator<Item=char> {
self.0.extend(iterable)
Expand Down
59 changes: 42 additions & 17 deletions components/script/dom/cssstyledeclaration.rs
Expand Up @@ -16,8 +16,8 @@ use servo_atoms::Atom;
use std::ascii::AsciiExt;
use std::sync::Arc;
use style::parser::ParserContextExtraData;
use style::properties::{Shorthand, Importance, PropertyDeclarationBlock};
use style::properties::{is_supported_property, parse_one_declaration, parse_style_attribute};
use style::properties::{Importance, PropertyDeclarationBlock, PropertyId};
use style::properties::{parse_one_declaration, parse_style_attribute};
use style::selector_parser::PseudoElement;
use style_traits::ToCss;

Expand Down Expand Up @@ -111,6 +111,13 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
return self.get_computed_style(&property).unwrap_or(DOMString::new());
}

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()
Expand All @@ -120,12 +127,19 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
};

let mut string = String::new();
style_attribute.property_value_to_css(&property, &mut string).unwrap();
style_attribute.property_value_to_css(&id, &mut string).unwrap();
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()
Expand All @@ -134,7 +148,7 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
return DOMString::new()
};

if style_attribute.property_priority(&property).important() {
if style_attribute.property_priority(&id).important() {
DOMString::from("important")
} else {
// Step 4
Expand All @@ -154,9 +168,13 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
}

// Step 3
if !is_supported_property(&property) {
return Ok(());
}
// FIXME: give ownership on `property` here when parse_one_declaration can take &PropertyId
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();

Expand All @@ -171,7 +189,7 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
return Ok(())
};

style_attribute.remove_property(&property);
style_attribute.remove_property(&id);
empty = style_attribute.declarations.is_empty()
}
if empty {
Expand Down Expand Up @@ -237,9 +255,12 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
}

// Step 2 & 3
if !is_supported_property(&property) {
return Ok(());
}
let id = if let Ok(id) = PropertyId::parse(property.into()) {
id
} else {
// Unkwown property
return Ok(())
};

// Step 4
let importance = match &*priority {
Expand All @@ -253,10 +274,7 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
let mut style_attribute = lock.write();

// Step 5 & 6
match Shorthand::from_name(&property) {
Some(shorthand) => style_attribute.set_importance(shorthand.longhands(), importance),
None => style_attribute.set_importance(&[&*property], importance),
}
style_attribute.set_importance(&id, importance);

self.owner.set_style_attr(style_attribute.to_css_string());
let node = self.owner.upcast::<Node>();
Expand All @@ -277,6 +295,13 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
return Err(Error::NoModificationAllowed);
}

let id = if let Ok(id) = PropertyId::parse(property.into()) {
id
} else {
// Unkwown property, cannot be there to remove.
return Ok(DOMString::new())
};

let mut style_attribute = self.owner.style_attribute().borrow_mut();
let mut string = String::new();
let empty;
Expand All @@ -289,10 +314,10 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
};

// Step 3
style_attribute.property_value_to_css(&property, &mut string).unwrap();
style_attribute.property_value_to_css(&id, &mut string).unwrap();

// Step 4 & 5
style_attribute.remove_property(&property);
style_attribute.remove_property(&id);
self.owner.set_style_attr(style_attribute.to_css_string());
empty = style_attribute.declarations.is_empty()
}
Expand Down
13 changes: 2 additions & 11 deletions components/style/gecko_string_cache/mod.rs
Expand Up @@ -9,10 +9,9 @@ use gecko_bindings::bindings::Gecko_Atomize;
use gecko_bindings::bindings::Gecko_ReleaseAtom;
use gecko_bindings::structs::nsIAtom;
use heapsize::HeapSizeOf;
use std::ascii::AsciiExt;
use std::borrow::{Cow, Borrow};
use std::char::{self, DecodeUtf16};
use std::fmt;
use std::fmt::{self, Write};
use std::hash::{Hash, Hasher};
use std::iter::Cloned;
use std::mem;
Expand Down Expand Up @@ -111,14 +110,6 @@ impl WeakAtom {
cb(&owned)
}

#[inline]
pub fn eq_str_ignore_ascii_case(&self, s: &str) -> bool {
self.chars().map(|r| match r {
Ok(c) => c.to_ascii_lowercase() as u32,
Err(e) => e.unpaired_surrogate() as u32,
}).eq(s.chars().map(|c| c.to_ascii_lowercase() as u32))
}

#[inline]
pub fn to_string(&self) -> String {
String::from_utf16(self.as_slice()).unwrap()
Expand Down Expand Up @@ -154,7 +145,7 @@ impl fmt::Debug for WeakAtom {
impl fmt::Display for WeakAtom {
fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
for c in self.chars() {
try!(write!(w, "{}", c.unwrap_or(char::REPLACEMENT_CHARACTER)))
try!(w.write_char(c.unwrap_or(char::REPLACEMENT_CHARACTER)))
}
Ok(())
}
Expand Down

0 comments on commit 137e30b

Please sign in to comment.