Skip to content

Commit

Permalink
Implement DOMTokenList.toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoabinader committed Dec 26, 2014
1 parent 2cfb464 commit 5afcf3e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
25 changes: 25 additions & 0 deletions components/script/dom/domtokenlist.rs
Expand Up @@ -128,4 +128,29 @@ impl<'a> DOMTokenListMethods for JSRef<'a, DOMTokenList> {
element.set_atomic_tokenlist_attribute(&self.local_name, atoms);
Ok(())
}

// https://dom.spec.whatwg.org/#dom-domtokenlist-toggle
fn Toggle(self, token: DOMString, force: Option<bool>) -> Fallible<bool> {
let element = self.element.root();
let mut atoms = element.get_tokenlist_attribute(&self.local_name);
let token = try!(self.check_token_exceptions(token.as_slice()));
match atoms.iter().position(|atom| *atom == token) {
Some(index) => match force {
Some(true) => Ok(true),
_ => {
atoms.remove(index);
element.set_atomic_tokenlist_attribute(&self.local_name, atoms);
Ok(false)
}
},
None => match force {
Some(false) => Ok(false),
_ => {
atoms.push(token);
element.set_atomic_tokenlist_attribute(&self.local_name, atoms);
Ok(true)
}
}
}
}
}
3 changes: 2 additions & 1 deletion components/script/dom/webidls/DOMTokenList.webidl
Expand Up @@ -14,7 +14,8 @@ interface DOMTokenList {
void add(DOMString... tokens);
[Throws]
void remove(DOMString... tokens);
[Throws]
boolean toggle(DOMString token, optional boolean force);

//boolean toggle(DOMString token, optional boolean force);
//stringifier;
};

0 comments on commit 5afcf3e

Please sign in to comment.