Bumped HTMLCollection interface to latest

Spec:
http://dom.spec.whatwg.org/#interface-htmlcollection

This is a sub-task for #1662.
This commit is contained in:
Bruno de Oliveira Abinader 2014-02-11 12:11:28 -04:00
parent cdec81ea4f
commit 6cf0eb1115
2 changed files with 36 additions and 15 deletions

View file

@ -5,15 +5,10 @@
use dom::bindings::codegen::HTMLCollectionBinding; use dom::bindings::codegen::HTMLCollectionBinding;
use dom::bindings::js::JS; use dom::bindings::js::JS;
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object}; use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::error::Fallible;
use dom::element::Element; use dom::element::Element;
use dom::window::Window; use dom::window::Window;
use servo_util::str::DOMString; use servo_util::str::DOMString;
use js::jsapi::{JSObject, JSContext};
use std::ptr;
#[deriving(Encodable)] #[deriving(Encodable)]
pub struct HTMLCollection { pub struct HTMLCollection {
elements: ~[JS<Element>], elements: ~[JS<Element>],
@ -34,11 +29,15 @@ impl HTMLCollection {
reflect_dom_object(~HTMLCollection::new_inherited(window.clone(), elements), reflect_dom_object(~HTMLCollection::new_inherited(window.clone(), elements),
window, HTMLCollectionBinding::Wrap) window, HTMLCollectionBinding::Wrap)
} }
}
impl HTMLCollection {
// http://dom.spec.whatwg.org/#dom-htmlcollection-length
pub fn Length(&self) -> u32 { pub fn Length(&self) -> u32 {
self.elements.len() as u32 self.elements.len() as u32
} }
// http://dom.spec.whatwg.org/#dom-htmlcollection-item
pub fn Item(&self, index: u32) -> Option<JS<Element>> { pub fn Item(&self, index: u32) -> Option<JS<Element>> {
if index < self.Length() { if index < self.Length() {
Some(self.elements[index].clone()) Some(self.elements[index].clone())
@ -47,17 +46,40 @@ impl HTMLCollection {
} }
} }
pub fn NamedItem(&self, _cx: *JSContext, _name: DOMString) -> Fallible<*JSObject> { // http://dom.spec.whatwg.org/#dom-htmlcollection-nameditem
Ok(ptr::null()) pub fn NamedItem(&self, key: DOMString) -> Option<JS<Element>> {
// Step 1.
if key.is_empty() {
return None;
} }
// Step 2.
self.elements.iter().find(|elem| {
let elem = elem.get();
elem.get_string_attribute("name") == key || elem.get_string_attribute("id") == key
}).map(|maybe_elem| maybe_elem.clone())
}
}
impl HTMLCollection {
pub fn IndexedGetter(&self, index: u32, found: &mut bool) -> Option<JS<Element>> { pub fn IndexedGetter(&self, index: u32, found: &mut bool) -> Option<JS<Element>> {
*found = true; let maybe_elem = self.Item(index);
self.Item(index) *found = maybe_elem.is_some();
maybe_elem
} }
pub fn NamedGetter(&self, _cx: *JSContext, _name: Option<DOMString>, _found: &mut bool) -> Fallible<*JSObject> { pub fn NamedGetter(&self, maybe_name: Option<DOMString>, found: &mut bool) -> Option<JS<Element>> {
Ok(ptr::null()) match maybe_name {
Some(name) => {
let maybe_elem = self.NamedItem(name);
*found = maybe_elem.is_some();
maybe_elem
},
None => {
*found = false;
None
}
}
} }
} }

View file

@ -10,6 +10,5 @@
interface HTMLCollection { interface HTMLCollection {
readonly attribute unsigned long length; readonly attribute unsigned long length;
getter Element? item(unsigned long index); getter Element? item(unsigned long index);
[Throws] getter Element? namedItem(DOMString name);
getter object? namedItem(DOMString name); // only returns Element
}; };