diff options
Diffstat (limited to 'src/components/script/dom/domtokenlist.rs')
-rw-r--r-- | src/components/script/dom/domtokenlist.rs | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/components/script/dom/domtokenlist.rs b/src/components/script/dom/domtokenlist.rs index cf44d5f7abc..3d252d36731 100644 --- a/src/components/script/dom/domtokenlist.rs +++ b/src/components/script/dom/domtokenlist.rs @@ -5,14 +5,16 @@ use dom::attr::{Attr, TokenListAttrValue}; use dom::bindings::codegen::Bindings::DOMTokenListBinding; use dom::bindings::codegen::Bindings::DOMTokenListBinding::DOMTokenListMethods; +use dom::bindings::error::{Fallible, InvalidCharacter, Syntax}; use dom::bindings::global::Window; use dom::bindings::js::{JS, JSRef, Temporary, OptionalRootable}; use dom::bindings::utils::{Reflector, Reflectable, reflect_dom_object}; use dom::element::{Element, AttributeHandlers}; use dom::node::window_from_node; +use servo_util::atom::Atom; use servo_util::namespace::Null; -use servo_util::str::DOMString; +use servo_util::str::{DOMString, HTML_SPACE_CHARACTERS}; #[deriving(Encodable)] pub struct DOMTokenList { @@ -47,6 +49,7 @@ impl Reflectable for DOMTokenList { trait PrivateDOMTokenListHelpers { fn attribute(&self) -> Option<Temporary<Attr>>; + fn check_token_exceptions<'a>(&self, token: &'a str) -> Fallible<&'a str>; } impl<'a> PrivateDOMTokenListHelpers for JSRef<'a, DOMTokenList> { @@ -54,6 +57,14 @@ impl<'a> PrivateDOMTokenListHelpers for JSRef<'a, DOMTokenList> { let element = self.element.root(); element.deref().get_attribute(Null, self.local_name) } + + fn check_token_exceptions<'a>(&self, token: &'a str) -> Fallible<&'a str> { + match token { + "" => Err(Syntax), + token if token.find(HTML_SPACE_CHARACTERS).is_some() => Err(InvalidCharacter), + token => Ok(token) + } + } } // http://dom.spec.whatwg.org/#domtokenlist @@ -92,4 +103,14 @@ impl<'a> DOMTokenListMethods for JSRef<'a, DOMTokenList> { *found = item.is_some(); item } + + // http://dom.spec.whatwg.org/#dom-domtokenlist-contains + fn Contains(&self, token: DOMString) -> Fallible<bool> { + self.check_token_exceptions(token.as_slice()).map(|slice| { + self.attribute().root().and_then(|attr| attr.value().tokens().map(|mut tokens| { + let atom = Atom::from_slice(slice); + tokens.any(|token| *token == atom) + })).unwrap_or(false) + }) + } } |