diff --git a/src/components/script/dom/htmlcollection.rs b/src/components/script/dom/htmlcollection.rs
index ff2cfa7b5a1..bb4cffc227e 100644
--- a/src/components/script/dom/htmlcollection.rs
+++ b/src/components/script/dom/htmlcollection.rs
@@ -5,15 +5,10 @@
use dom::bindings::codegen::HTMLCollectionBinding;
use dom::bindings::js::JS;
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
-use dom::bindings::error::Fallible;
use dom::element::Element;
use dom::window::Window;
use servo_util::str::DOMString;
-use js::jsapi::{JSObject, JSContext};
-
-use std::ptr;
-
#[deriving(Encodable)]
pub struct HTMLCollection {
elements: ~[JS],
@@ -34,11 +29,15 @@ impl HTMLCollection {
reflect_dom_object(~HTMLCollection::new_inherited(window.clone(), elements),
window, HTMLCollectionBinding::Wrap)
}
-
+}
+
+impl HTMLCollection {
+ // http://dom.spec.whatwg.org/#dom-htmlcollection-length
pub fn Length(&self) -> u32 {
self.elements.len() as u32
}
+ // http://dom.spec.whatwg.org/#dom-htmlcollection-item
pub fn Item(&self, index: u32) -> Option> {
if index < self.Length() {
Some(self.elements[index].clone())
@@ -47,17 +46,40 @@ impl HTMLCollection {
}
}
- pub fn NamedItem(&self, _cx: *JSContext, _name: DOMString) -> Fallible<*JSObject> {
- Ok(ptr::null())
- }
+ // http://dom.spec.whatwg.org/#dom-htmlcollection-nameditem
+ pub fn NamedItem(&self, key: DOMString) -> Option> {
+ // 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> {
- *found = true;
- self.Item(index)
+ let maybe_elem = self.Item(index);
+ *found = maybe_elem.is_some();
+ maybe_elem
}
- pub fn NamedGetter(&self, _cx: *JSContext, _name: Option, _found: &mut bool) -> Fallible<*JSObject> {
- Ok(ptr::null())
+ pub fn NamedGetter(&self, maybe_name: Option, found: &mut bool) -> Option> {
+ match maybe_name {
+ Some(name) => {
+ let maybe_elem = self.NamedItem(name);
+ *found = maybe_elem.is_some();
+ maybe_elem
+ },
+ None => {
+ *found = false;
+ None
+ }
+ }
}
}
diff --git a/src/components/script/dom/webidls/HTMLCollection.webidl b/src/components/script/dom/webidls/HTMLCollection.webidl
index 2c1fc60e04a..7389e776834 100644
--- a/src/components/script/dom/webidls/HTMLCollection.webidl
+++ b/src/components/script/dom/webidls/HTMLCollection.webidl
@@ -10,6 +10,5 @@
interface HTMLCollection {
readonly attribute unsigned long length;
getter Element? item(unsigned long index);
- [Throws]
- getter object? namedItem(DOMString name); // only returns Element
+ getter Element? namedItem(DOMString name);
};