From dc7db1e8e612c1fb40a4410b2926b7ce9037da78 Mon Sep 17 00:00:00 2001 From: Glenn Watson Date: Fri, 31 Jul 2015 10:41:18 +1000 Subject: [PATCH] implement cssText --- components/script/dom/cssstyledeclaration.rs | 46 ++++++++++++++++++- .../dom/webidls/CSSStyleDeclaration.webidl | 4 +- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/components/script/dom/cssstyledeclaration.rs b/components/script/dom/cssstyledeclaration.rs index dac16d72bcf..2881044fb22 100644 --- a/components/script/dom/cssstyledeclaration.rs +++ b/components/script/dom/cssstyledeclaration.rs @@ -2,6 +2,7 @@ * 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/. */ +use cssparser::ToCss; use dom::bindings::codegen::Bindings::CSSStyleDeclarationBinding::{self, CSSStyleDeclarationMethods}; use dom::bindings::error::{Error, ErrorResult, Fallible}; use dom::bindings::global::GlobalRef; @@ -15,10 +16,11 @@ use dom::window::Window; use std::ascii::AsciiExt; use std::cell::Ref; use std::slice; +use std::sync::Arc; use string_cache::Atom; use style::parser::ParserContextExtraData; use style::properties::{PropertyDeclaration, Shorthand}; -use style::properties::{is_supported_property, parse_one_declaration}; +use style::properties::{is_supported_property, parse_one_declaration, parse_style_attribute}; use style::selector_impl::PseudoElement; // http://dev.w3.org/csswg/cssom/#the-cssstyledeclaration-interface @@ -340,6 +342,46 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration { rval } - // https://drafts.csswg.org/cssom/#cssstyledeclaration + // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext + fn CssText(&self) -> DOMString { + let elem = self.owner.upcast::(); + let style_attribute = elem.style_attribute().borrow(); + + if let Some(declarations) = style_attribute.as_ref() { + DOMString::from(declarations.to_css_string()) + } else { + DOMString::new() + } + } + + // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext + fn SetCssText(&self, value: DOMString) -> ErrorResult { + let window = window_from_node(self.owner.upcast::()); + let element = self.owner.upcast::(); + + // Step 1 + if self.readonly { + return Err(Error::NoModificationAllowed); + } + + // Step 2 + element.update_inline_style(vec![], StylePriority::Normal); + element.update_inline_style(vec![], StylePriority::Important); + + // Step 3 + let decl_block = parse_style_attribute(&value, &window.get_url(), window.css_error_reporter(), + ParserContextExtraData::default()); + if decl_block.normal.len() == 0 && decl_block.important.len() == 0 { + return Ok(()); + } + let normal_declarations = Arc::try_unwrap(decl_block.normal) + .expect("Unwrapping normal CssText declarations failed."); + let important_declarations = Arc::try_unwrap(decl_block.important) + .expect("Unwrapping important CssText declarations failed."); + element.update_inline_style(normal_declarations, StylePriority::Normal); + element.update_inline_style(important_declarations, StylePriority::Important); + Ok(()) + } + css_properties_accessors!(css_properties); } diff --git a/components/script/dom/webidls/CSSStyleDeclaration.webidl b/components/script/dom/webidls/CSSStyleDeclaration.webidl index 567167024ee..bab5a667f3b 100644 --- a/components/script/dom/webidls/CSSStyleDeclaration.webidl +++ b/components/script/dom/webidls/CSSStyleDeclaration.webidl @@ -9,8 +9,8 @@ */ interface CSSStyleDeclaration { - //[SetterThrows] - // attribute DOMString cssText; + [SetterThrows] + attribute DOMString cssText; readonly attribute unsigned long length; getter DOMString item(unsigned long index); DOMString getPropertyValue(DOMString property);