Implement DOMTokenList.supports API

This commit is contained in:
Vincent Ricard 2020-09-20 23:05:41 +02:00
parent 3f7697690a
commit 2a4dd894de
20 changed files with 147 additions and 72 deletions

View file

@ -20,21 +20,35 @@ pub struct DOMTokenList {
reflector_: Reflector,
element: Dom<Element>,
local_name: LocalName,
supported_tokens: Option<Vec<Atom>>,
}
impl DOMTokenList {
pub fn new_inherited(element: &Element, local_name: LocalName) -> DOMTokenList {
pub fn new_inherited(
element: &Element,
local_name: LocalName,
supported_tokens: Option<Vec<Atom>>,
) -> DOMTokenList {
DOMTokenList {
reflector_: Reflector::new(),
element: Dom::from_ref(element),
local_name: local_name,
supported_tokens: supported_tokens,
}
}
pub fn new(element: &Element, local_name: &LocalName) -> DomRoot<DOMTokenList> {
pub fn new(
element: &Element,
local_name: &LocalName,
supported_tokens: Option<Vec<Atom>>,
) -> DomRoot<DOMTokenList> {
let window = window_from_node(element);
reflect_dom_object(
Box::new(DOMTokenList::new_inherited(element, local_name.clone())),
Box::new(DOMTokenList::new_inherited(
element,
local_name.clone(),
supported_tokens,
)),
&*window,
)
}
@ -61,6 +75,25 @@ impl DOMTokenList {
self.element
.set_atomic_tokenlist_attribute(&self.local_name, atoms)
}
// https://dom.spec.whatwg.org/#concept-domtokenlist-validation
fn validation_steps(&self, token: &str) -> Fallible<bool> {
match &self.supported_tokens {
None => Err(Error::Type(
"This attribute has no supported tokens".to_owned(),
)),
Some(supported_tokens) => {
let token = Atom::from(token).to_ascii_lowercase();
if supported_tokens
.iter()
.any(|supported_token| *supported_token == token)
{
return Ok(true);
}
Ok(false)
},
}
}
}
// https://dom.spec.whatwg.org/#domtokenlist
@ -197,6 +230,11 @@ impl DOMTokenListMethods for DOMTokenList {
Ok(result)
}
// https://dom.spec.whatwg.org/#dom-domtokenlist-supports
fn Supports(&self, token: DOMString) -> Fallible<bool> {
self.validation_steps(&token)
}
// check-tidy: no specs after this line
fn IndexedGetter(&self, index: u32) -> Option<DOMString> {
self.Item(index)