Skip to content

Commit

Permalink
Added readonly flag for CSSStyleDeclaration
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-daniels committed Dec 28, 2014
1 parent c4b93d3 commit 18d8ee6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
3 changes: 2 additions & 1 deletion components/script/dom/bindings/error.rs
Expand Up @@ -35,7 +35,8 @@ pub enum Error {
Network,
Abort,
Timeout,
DataClone
DataClone,
NoModificationAllowedError
}

/// The return type for IDL operations that can throw DOM exceptions.
Expand Down
40 changes: 29 additions & 11 deletions components/script/dom/cssstyledeclaration.rs
Expand Up @@ -4,7 +4,9 @@

use dom::bindings::codegen::Bindings::CSSStyleDeclarationBinding::{mod, CSSStyleDeclarationMethods};
use dom::bindings::codegen::InheritTypes::{NodeCast, ElementCast};
use dom::bindings::error::Error;
use dom::bindings::error::ErrorResult;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, JSRef, OptionalRootedRootable, Temporary};
use dom::bindings::utils::{Reflector, reflect_dom_object};
Expand All @@ -24,6 +26,13 @@ use std::ascii::AsciiExt;
pub struct CSSStyleDeclaration {
reflector_: Reflector,
owner: JS<HTMLElement>,
readonly: bool,
}

#[deriving(PartialEq)]
pub enum CSSModificationAccess {
ReadWrite,
Readonly
}

macro_rules! css_properties(
Expand Down Expand Up @@ -53,15 +62,16 @@ fn serialize_value(declaration: &PropertyDeclaration) -> DOMString {
}

impl CSSStyleDeclaration {
pub fn new_inherited(owner: JSRef<HTMLElement>) -> CSSStyleDeclaration {
pub fn new_inherited(owner: JSRef<HTMLElement>, modification_access: CSSModificationAccess) -> CSSStyleDeclaration {
CSSStyleDeclaration {
reflector_: Reflector::new(),
owner: JS::from_rooted(owner),
readonly: modification_access == CSSModificationAccess::Readonly,
}
}

pub fn new(global: JSRef<Window>, owner: JSRef<HTMLElement>) -> Temporary<CSSStyleDeclaration> {
reflect_dom_object(box CSSStyleDeclaration::new_inherited(owner),
pub fn new(global: JSRef<Window>, owner: JSRef<HTMLElement>, modification_access: CSSModificationAccess) -> Temporary<CSSStyleDeclaration> {
reflect_dom_object(box CSSStyleDeclaration::new_inherited(owner, modification_access),
GlobalRef::Window(global),
CSSStyleDeclarationBinding::Wrap)
}
Expand Down Expand Up @@ -178,7 +188,10 @@ impl<'a> CSSStyleDeclarationMethods for JSRef<'a, CSSStyleDeclaration> {
// http://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-setproperty
fn SetProperty(self, property: DOMString, value: DOMString,
priority: DOMString) -> ErrorResult {
//TODO: disallow modifications if readonly flag is set
// Step 1
if self.readonly {
return Err(Error::NoModificationAllowedError);
}

// Step 2
let property = property.as_slice().to_ascii_lower();
Expand All @@ -190,8 +203,7 @@ impl<'a> CSSStyleDeclarationMethods for JSRef<'a, CSSStyleDeclaration> {

// Step 4
if value.is_empty() {
self.RemoveProperty(property);
return Ok(());
return self.RemoveProperty(property).map(|_| ());
}

// Step 5
Expand Down Expand Up @@ -234,7 +246,10 @@ impl<'a> CSSStyleDeclarationMethods for JSRef<'a, CSSStyleDeclaration> {

// http://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-setpropertypriority
fn SetPropertyPriority(self, property: DOMString, priority: DOMString) -> ErrorResult {
//TODO: disallow modifications if readonly flag is set
// Step 1
if self.readonly {
return Err(Error::NoModificationAllowedError);
}

// Step 2
let property = property.as_slice().to_ascii_lower();
Expand Down Expand Up @@ -276,8 +291,11 @@ impl<'a> CSSStyleDeclarationMethods for JSRef<'a, CSSStyleDeclaration> {
}

// http://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-removeproperty
fn RemoveProperty(self, property: DOMString) -> DOMString {
//TODO: disallow modifications if readonly flag is set
fn RemoveProperty(self, property: DOMString) -> Fallible<DOMString> {
// Step 1
if self.readonly {
return Err(Error::NoModificationAllowedError);
}

// Step 2
let property = property.as_slice().to_ascii_lower();
Expand All @@ -290,7 +308,7 @@ impl<'a> CSSStyleDeclarationMethods for JSRef<'a, CSSStyleDeclaration> {
Some(longhands) => {
// Step 4
for longhand in longhands.iter() {
self.RemoveProperty(longhand.clone());
try!(self.RemoveProperty(longhand.clone()));
}
}

Expand All @@ -303,7 +321,7 @@ impl<'a> CSSStyleDeclarationMethods for JSRef<'a, CSSStyleDeclaration> {
}

// Step 6
value
Ok(value)
}

// http://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-cssfloat
Expand Down
1 change: 1 addition & 0 deletions components/script/dom/domexception.rs
Expand Up @@ -55,6 +55,7 @@ impl DOMErrorName {
Error::Abort => DOMErrorName::AbortError,
Error::Timeout => DOMErrorName::TimeoutError,
Error::DataClone => DOMErrorName::DataCloneError,
Error::NoModificationAllowedError => DOMErrorName::NoModificationAllowedError,
Error::FailureUnknown => panic!(),
}
}
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/htmlelement.rs
Expand Up @@ -16,7 +16,7 @@ use dom::bindings::js::{JSRef, Temporary, MutNullableJS};
use dom::bindings::error::ErrorResult;
use dom::bindings::error::Error::Syntax;
use dom::bindings::utils::Reflectable;
use dom::cssstyledeclaration::CSSStyleDeclaration;
use dom::cssstyledeclaration::{CSSStyleDeclaration, CSSModificationAccess};
use dom::document::Document;
use dom::domstringmap::DOMStringMap;
use dom::element::{Element, ElementTypeId, ActivationElementHelpers, AttributeHandlers};
Expand Down Expand Up @@ -78,7 +78,7 @@ impl<'a> HTMLElementMethods for JSRef<'a, HTMLElement> {
fn Style(self) -> Temporary<CSSStyleDeclaration> {
self.style_decl.or_init(|| {
let global = window_from_node(self).root();
CSSStyleDeclaration::new(*global, self)
CSSStyleDeclaration::new(*global, self, CSSModificationAccess::ReadWrite)
})
}

Expand Down
1 change: 1 addition & 0 deletions components/script/dom/webidls/CSSStyleDeclaration.webidl
Expand Up @@ -24,6 +24,7 @@ interface CSSStyleDeclaration {
[Throws]
void setPropertyPriority(DOMString property, [TreatNullAs=EmptyString] DOMString priority);

[Throws]
DOMString removeProperty(DOMString property);
//readonly attribute CSSRule? parentRule;
[SetterThrows]
Expand Down

5 comments on commit 18d8ee6

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from jdm
at thomas-daniels@18d8ee6

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging ProgramFOX/servo/readonly-csssd = 18d8ee6 into auto

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ProgramFOX/servo/readonly-csssd = 18d8ee6 merged ok, testing candidate = 366ea4f

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 366ea4f

Please sign in to comment.