Add a style property to HTMLElement.

This commit is contained in:
Josh Matthews 2014-09-18 15:34:55 -04:00
parent 2cfa8e85a6
commit 2e14b653bf
5 changed files with 62 additions and 9 deletions

View file

@ -2,17 +2,20 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::codegen::Bindings::CSS2PropertiesBinding;
use dom::bindings::codegen::Bindings::CSS2PropertiesBinding::CSS2PropertiesMethods; use dom::bindings::codegen::Bindings::CSS2PropertiesBinding::CSS2PropertiesMethods;
use dom::bindings::codegen::Bindings::CSSStyleDeclarationBinding::CSSStyleDeclarationMethods; use dom::bindings::codegen::Bindings::CSSStyleDeclarationBinding::CSSStyleDeclarationMethods;
use dom::bindings::codegen::InheritTypes::CSSStyleDeclarationCast; use dom::bindings::codegen::InheritTypes::CSSStyleDeclarationCast;
use dom::bindings::utils::{Reflectable, Reflector}; use dom::bindings::global;
use dom::bindings::js::JSRef; use dom::bindings::js::{JSRef, Temporary};
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
use dom::cssstyledeclaration::CSSStyleDeclaration; use dom::cssstyledeclaration::CSSStyleDeclaration;
use dom::window::Window;
use servo_util::str::DOMString; use servo_util::str::DOMString;
#[dom_struct] #[dom_struct]
pub struct CSS2Properties { pub struct CSS2Properties {
declaration: CSSStyleDeclaration, cssstyledeclaration: CSSStyleDeclaration,
} }
macro_rules! css_getter( macro_rules! css_getter(
@ -33,6 +36,20 @@ macro_rules! css_setter(
); );
) )
impl CSS2Properties {
fn new_inherited() -> CSS2Properties {
CSS2Properties {
cssstyledeclaration: CSSStyleDeclaration::new_inherited(),
}
}
pub fn new(global: &JSRef<Window>) -> Temporary<CSS2Properties> {
reflect_dom_object(box CSS2Properties::new_inherited(),
global::Window(*global),
CSS2PropertiesBinding::Wrap)
}
}
impl<'a> CSS2PropertiesMethods for JSRef<'a, CSS2Properties> { impl<'a> CSS2PropertiesMethods for JSRef<'a, CSS2Properties> {
css_getter!(Color, "color") css_getter!(Color, "color")
css_setter!(SetColor, "color") css_setter!(SetColor, "color")
@ -72,6 +89,6 @@ impl<'a> CSS2PropertiesMethods for JSRef<'a, CSS2Properties> {
impl Reflectable for CSS2Properties { impl Reflectable for CSS2Properties {
fn reflector<'a>(&'a self) -> &'a Reflector { fn reflector<'a>(&'a self) -> &'a Reflector {
self.declaration.reflector() self.cssstyledeclaration.reflector()
} }
} }

View file

@ -4,8 +4,8 @@
use dom::bindings::codegen::Bindings::CSSStyleDeclarationBinding::CSSStyleDeclarationMethods; use dom::bindings::codegen::Bindings::CSSStyleDeclarationBinding::CSSStyleDeclarationMethods;
use dom::bindings::error::{ErrorResult, Fallible}; use dom::bindings::error::{ErrorResult, Fallible};
use dom::bindings::utils::{Reflectable, Reflector};
use dom::bindings::js::JSRef; use dom::bindings::js::JSRef;
use dom::bindings::utils::{Reflectable, Reflector};
use servo_util::str::DOMString; use servo_util::str::DOMString;
use string_cache::atom::Atom; use string_cache::atom::Atom;
use std::ascii::AsciiExt; use std::ascii::AsciiExt;
@ -44,6 +44,14 @@ fn get_declaration(_property: &Atom) -> Option<Declaration> {
None None
} }
impl CSSStyleDeclaration {
pub fn new_inherited() -> CSSStyleDeclaration {
CSSStyleDeclaration {
reflector_: Reflector::new()
}
}
}
impl<'a> CSSStyleDeclarationMethods for JSRef<'a, CSSStyleDeclaration> { impl<'a> CSSStyleDeclarationMethods for JSRef<'a, CSSStyleDeclaration> {
fn CssText(self) -> DOMString { fn CssText(self) -> DOMString {
"".to_string() "".to_string()

View file

@ -10,10 +10,12 @@ use dom::bindings::codegen::Bindings::HTMLElementBinding::HTMLElementMethods;
use dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods; use dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods;
use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods; use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLFrameSetElementDerived}; use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLFrameSetElementDerived};
use dom::bindings::codegen::InheritTypes::{EventTargetCast, HTMLInputElementCast}; use dom::bindings::codegen::InheritTypes::{EventTargetCast, HTMLInputElementCast, CSSStyleDeclarationCast};
use dom::bindings::codegen::InheritTypes::{HTMLElementDerived, HTMLBodyElementDerived}; use dom::bindings::codegen::InheritTypes::{HTMLElementDerived, HTMLBodyElementDerived};
use dom::bindings::js::{JSRef, Temporary}; use dom::bindings::js::{JSRef, Temporary, MutNullableJS};
use dom::bindings::utils::{Reflectable, Reflector}; use dom::bindings::utils::{Reflectable, Reflector};
use dom::cssstyledeclaration::CSSStyleDeclaration;
use dom::css2properties::CSS2Properties;
use dom::document::Document; use dom::document::Document;
use dom::element::{Element, ElementTypeId, ActivationElementHelpers}; use dom::element::{Element, ElementTypeId, ActivationElementHelpers};
use dom::eventtarget::{EventTarget, EventTargetHelpers, EventTargetTypeId}; use dom::eventtarget::{EventTarget, EventTargetHelpers, EventTargetTypeId};
@ -24,9 +26,12 @@ use servo_util::str::DOMString;
use string_cache::Atom; use string_cache::Atom;
use std::default::Default;
#[dom_struct] #[dom_struct]
pub struct HTMLElement { pub struct HTMLElement {
element: Element element: Element,
style_decl: MutNullableJS<CSSStyleDeclaration>,
} }
impl HTMLElementDerived for EventTarget { impl HTMLElementDerived for EventTarget {
@ -42,7 +47,8 @@ impl HTMLElementDerived for EventTarget {
impl HTMLElement { impl HTMLElement {
pub fn new_inherited(type_id: ElementTypeId, tag_name: DOMString, prefix: Option<DOMString>, document: JSRef<Document>) -> HTMLElement { pub fn new_inherited(type_id: ElementTypeId, tag_name: DOMString, prefix: Option<DOMString>, document: JSRef<Document>) -> HTMLElement {
HTMLElement { HTMLElement {
element: Element::new_inherited(type_id, tag_name, ns!(HTML), prefix, document) element: Element::new_inherited(type_id, tag_name, ns!(HTML), prefix, document),
style_decl: Default::default(),
} }
} }
@ -65,6 +71,16 @@ impl<'a> PrivateHTMLElementHelpers for JSRef<'a, HTMLElement> {
} }
impl<'a> HTMLElementMethods 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 style_decl: JSRef<CSSStyleDeclaration> = CSSStyleDeclarationCast::from_ref(*style_props);
self.style_decl.assign(Some(style_decl));
}
self.style_decl.get().unwrap()
}
make_getter!(Title) make_getter!(Title)
make_setter!(SetTitle, "title") make_setter!(SetTitle, "title")

View file

@ -0,0 +1,11 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//http://dev.w3.org/csswg/cssom/#elementcssinlinestyle
[NoInterfaceObject]
interface ElementCSSInlineStyle {
[SameObject, PutForwards=cssText] readonly attribute CSSStyleDeclaration style;
};

View file

@ -46,3 +46,4 @@ interface HTMLElement : Element {
//readonly attribute boolean? commandChecked; //readonly attribute boolean? commandChecked;
}; };
HTMLElement implements GlobalEventHandlers; HTMLElement implements GlobalEventHandlers;
HTMLElement implements ElementCSSInlineStyle;