Implement something like CSS value serialization. Fetch actual inline style declarations from owning elements.

This commit is contained in:
Josh Matthews 2014-09-19 13:06:19 -04:00
parent 2e14b653bf
commit 505e1855a3
8 changed files with 375 additions and 55 deletions

View file

@ -10,6 +10,7 @@ use dom::bindings::global;
use dom::bindings::js::{JSRef, Temporary};
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
use dom::cssstyledeclaration::CSSStyleDeclaration;
use dom::htmlelement::HTMLElement;
use dom::window::Window;
use servo_util::str::DOMString;
@ -37,15 +38,15 @@ macro_rules! css_setter(
)
impl CSS2Properties {
fn new_inherited() -> CSS2Properties {
fn new_inherited(owner: JSRef<HTMLElement>) -> CSS2Properties {
CSS2Properties {
cssstyledeclaration: CSSStyleDeclaration::new_inherited(),
cssstyledeclaration: CSSStyleDeclaration::new_inherited(Some(owner)),
}
}
pub fn new(global: &JSRef<Window>) -> Temporary<CSS2Properties> {
reflect_dom_object(box CSS2Properties::new_inherited(),
global::Window(*global),
pub fn new(global: JSRef<Window>, owner: JSRef<HTMLElement>) -> Temporary<CSS2Properties> {
reflect_dom_object(box CSS2Properties::new_inherited(owner),
global::Window(global),
CSS2PropertiesBinding::Wrap)
}
}

View file

@ -3,55 +3,67 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::codegen::Bindings::CSSStyleDeclarationBinding::CSSStyleDeclarationMethods;
use dom::bindings::codegen::InheritTypes::ElementCast;
use dom::bindings::error::{ErrorResult, Fallible};
use dom::bindings::js::JSRef;
use dom::bindings::js::{JS, JSRef, OptionalRootedRootable};
use dom::bindings::utils::{Reflectable, Reflector};
use dom::element::{Element, ElementHelpers};
use dom::htmlelement::HTMLElement;
use servo_util::str::DOMString;
use string_cache::atom::Atom;
use style::longhands_from_shorthand;
use style::PropertyDeclaration;
use std::ascii::AsciiExt;
#[dom_struct]
pub struct CSSStyleDeclaration {
reflector_: Reflector,
owner: Option<JS<HTMLElement>>,
}
fn get_longhands_from_shorthand(shorthand: &Atom) -> Vec<Atom> {
match shorthand.as_slice() {
"background" =>
vec!(Atom::from_slice("background-color"), Atom::from_slice("background-position"),
Atom::from_slice("background-attachment"), Atom::from_slice("background-image"),
Atom::from_slice("background-repeat")),
_ => vec!(),
}
}
type Declaration = int;
fn serialize_list(property: String, list: Vec<Declaration>) -> DOMString {
let mut result = property;
result.push_str(": ");
fn serialize_list(list: &Vec<PropertyDeclaration>) -> DOMString {
let mut result = String::new();
for declaration in list.iter() {
result.push_str(serialize_declaration(declaration).as_slice());
result.push_str(serialize_value(declaration).as_slice());
result.push_str(" ");
}
result
}
fn serialize_declaration(_declaration: &Declaration) -> DOMString {
"".to_string()
}
fn get_declaration(_property: &Atom) -> Option<Declaration> {
None
fn serialize_value(declaration: &PropertyDeclaration) -> DOMString {
declaration.value().unwrap()
}
impl CSSStyleDeclaration {
pub fn new_inherited() -> CSSStyleDeclaration {
pub fn new_inherited(owner: Option<JSRef<HTMLElement>>) -> CSSStyleDeclaration {
CSSStyleDeclaration {
reflector_: Reflector::new()
reflector_: Reflector::new(),
owner: owner.map(|owner| JS::from_rooted(owner)),
}
}
}
trait PrivateCSSStyleDeclarationHelpers {
fn get_declaration(self, property: &Atom) -> Option<PropertyDeclaration>;
}
impl<'a> PrivateCSSStyleDeclarationHelpers for JSRef<'a, CSSStyleDeclaration> {
fn get_declaration(self, property: &Atom) -> Option<PropertyDeclaration> {
self.owner.root().and_then(|owner| {
let element: JSRef<Element> = ElementCast::from_ref(*owner);
let inline_declarations = element.style_attribute().borrow();
inline_declarations.as_ref().and_then(|declarations| {
for declaration in declarations.normal.iter().chain(declarations.important.iter()) {
if declaration.matches(property.as_slice()) {
return Some(declaration.clone());
}
}
None
})
})
}
}
impl<'a> CSSStyleDeclarationMethods for JSRef<'a, CSSStyleDeclaration> {
fn CssText(self) -> DOMString {
"".to_string()
@ -75,39 +87,38 @@ impl<'a> CSSStyleDeclarationMethods for JSRef<'a, CSSStyleDeclaration> {
let property = Atom::from_slice(property.as_slice().to_ascii_lower().as_slice());
// 2. If property is a shorthand property, then follow these substeps:
let longhand_properties = get_longhands_from_shorthand(&property);
if !longhand_properties.is_empty() {
let longhand_properties = longhands_from_shorthand(property.as_slice());
if longhand_properties.is_some() {
// 1. Let list be a new empty array.
let mut list = vec!();
// 2. For each longhand property longhand that property maps to, in canonical order,
// follow these substeps:
for longhand in longhand_properties.iter() {
for longhand in longhand_properties.unwrap().iter() {
// 1. If longhand is a case-sensitive match for a property name of a
// CSS declaration in the declarations, let declaration be that CSS
// declaration, or null otherwise.
let declaration = get_declaration(longhand);
let declaration = self.get_declaration(&Atom::from_slice(longhand.as_slice()));
// 2. If declaration is null, return the empty string and terminate these
// steps.
if declaration.is_none() {
return "".to_string();
//XXXjdm ambiguous? this suggests that if we're missing a longhand we return nothing at all.
if declaration.is_some() {
// 3. Append the declaration to list.
list.push(declaration.unwrap());
}
// 3. Append the declaration to list.
list.push(declaration.unwrap());
}
// 3. Return the serialization of list and terminate these steps.
return serialize_list(property.as_slice().to_string(), list);
return serialize_list(&list);
}
// 3. If property is a case-sensitive match for a property name of a CSS declaration
// in the declarations, return the result of invoking serialize a CSS value of that
// declaration and terminate these steps.
// 4. Return the empty string.
let declaration = get_declaration(&property);
declaration.as_ref().map(|declaration| serialize_declaration(declaration))
let declaration = self.get_declaration(&property);
declaration.as_ref().map(|declaration| serialize_value(declaration))
.unwrap_or("".to_string())
}

View file

@ -73,8 +73,8 @@ impl<'a> PrivateHTMLElementHelpers for JSRef<'a, HTMLElement> {
impl<'a> HTMLElementMethods for JSRef<'a, HTMLElement> {
fn Style(self) -> Temporary<CSSStyleDeclaration> {
if self.style_decl.get().is_none() {
let global = window_from_node(self);
let style_props = CSS2Properties::new(&*global.root()).root();
let global = window_from_node(self).root();
let style_props = CSS2Properties::new(*global, self).root();
let style_decl: JSRef<CSSStyleDeclaration> = CSSStyleDeclarationCast::from_ref(*style_props);
self.style_decl.assign(Some(style_decl));
}

View file

@ -194,7 +194,7 @@ pub struct SharedLayoutData {
/// Encapsulates the abstract layout data.
pub struct LayoutData {
chan: Option<LayoutChan>,
_shared_data: SharedLayoutData,
pub shared_data: SharedLayoutData,
_data: *const (),
}