Implement support for Element.toggleAttribute

This commit is contained in:
Jonathan Kingston 2018-06-21 02:01:02 +01:00
parent 0226a1a4ad
commit a271c615f7
2 changed files with 39 additions and 0 deletions

View file

@ -1680,6 +1680,43 @@ impl ElementMethods for Element {
self.get_attribute(namespace, &LocalName::from(local_name)) self.get_attribute(namespace, &LocalName::from(local_name))
} }
// https://dom.spec.whatwg.org/#dom-element-toggleattribute
fn ToggleAttribute(&self, name: DOMString, force: Option<bool>) -> Fallible<bool> {
// Step 1.
if xml_name_type(&name) == InvalidXMLName {
return Err(Error::InvalidCharacter);
}
// Step 3.
let attribute = self.GetAttribute(name.clone());
// Step 2.
let name = self.parsed_name(name);
match attribute {
// Step 4
None => match force {
// Step 4.1.
None | Some(true) => {
self.set_first_matching_attribute(
name.clone(), AttrValue::String(String::new()), name.clone(), ns!(), None,
|attr| *attr.name() == name);
Ok(true)
},
// Step 4.2.
Some(false) => Ok(false),
},
Some(_index) => match force {
// Step 5.
None | Some(false) => {
self.remove_attribute_by_name(&name);
Ok(false)
},
// Step 6.
Some(true) => Ok(true),
},
}
}
// https://dom.spec.whatwg.org/#dom-element-setattribute // https://dom.spec.whatwg.org/#dom-element-setattribute
fn SetAttribute(&self, name: DOMString, value: DOMString) -> ErrorResult { fn SetAttribute(&self, name: DOMString, value: DOMString) -> ErrorResult {
// Step 1. // Step 1.

View file

@ -41,6 +41,8 @@ interface Element : Node {
[Pure] [Pure]
DOMString? getAttributeNS(DOMString? namespace, DOMString localName); DOMString? getAttributeNS(DOMString? namespace, DOMString localName);
[CEReactions, Throws] [CEReactions, Throws]
boolean toggleAttribute(DOMString name, optional boolean force);
[CEReactions, Throws]
void setAttribute(DOMString name, DOMString value); void setAttribute(DOMString name, DOMString value);
[CEReactions, Throws] [CEReactions, Throws]
void setAttributeNS(DOMString? namespace, DOMString name, DOMString value); void setAttributeNS(DOMString? namespace, DOMString name, DOMString value);