diff --git a/servo/src/components/script/dom/document.rs b/servo/src/components/script/dom/document.rs index 5994f653f186..5c1fea26596d 100644 --- a/servo/src/components/script/dom/document.rs +++ b/servo/src/components/script/dom/document.rs @@ -21,6 +21,7 @@ use dom::element::{HTMLBodyElementTypeId, HTMLFrameSetElementTypeId}; use dom::event::Event; use dom::eventtarget::{EventTarget, NodeTargetTypeId}; use dom::htmlcollection::HTMLCollection; +use dom::nodelist::NodeList; use dom::htmlelement::HTMLElement; use dom::htmlheadelement::HTMLHeadElement; use dom::htmlhtmlelement::HTMLHtmlElement; @@ -403,8 +404,8 @@ impl Document { } // http://www.whatwg.org/specs/web-apps/current-work/#dom-document-getelementsbyname - pub fn GetElementsByName(&self, name: DOMString) -> JS { - self.createHTMLCollection(|elem| { + pub fn GetElementsByName(&self, name: DOMString) -> JS { + self.createNodeList(|elem| { elem.get_attribute(Null, "name").map_default(false, |attr| { attr.get().value_ref() == name }) @@ -449,7 +450,7 @@ impl Document { self.createHTMLCollection(|elem| "applet" == elem.tag_name) } - pub fn createHTMLCollection(&self, callback: |elem: &Element| -> bool) -> JS { + pub fn create_collection(&self, callback: |elem: &Element| -> bool) -> ~[JS] { let mut elements = ~[]; match self.GetDocumentElement() { None => {}, @@ -465,7 +466,20 @@ impl Document { } } } - HTMLCollection::new(&self.window, elements) + elements + } + + pub fn createHTMLCollection(&self, callback: |elem: &Element| -> bool) -> JS { + HTMLCollection::new(&self.window, self.create_collection(callback)) + } + + pub fn createNodeList(&self, callback: |elem: &Element| -> bool) -> JS { + let elements = self.create_collection(callback); + let nodes = elements.map(|element| { + let node: JS = NodeCast::from(element); + node + }); + NodeList::new_simple_list(&self.window, nodes) } pub fn content_changed(&self) { diff --git a/servo/src/components/script/dom/webidls/Document.webidl b/servo/src/components/script/dom/webidls/Document.webidl index daa91f540fb1..11e92a0930e1 100644 --- a/servo/src/components/script/dom/webidls/Document.webidl +++ b/servo/src/components/script/dom/webidls/Document.webidl @@ -44,7 +44,7 @@ partial interface Document { attribute DOMString title; attribute HTMLElement? body; readonly attribute HTMLHeadElement? head; - /*NodeList*/ HTMLCollection getElementsByName(DOMString elementName); + NodeList getElementsByName(DOMString elementName); readonly attribute HTMLCollection images; readonly attribute HTMLCollection embeds; diff --git a/servo/src/test/html/content/test_collections.html b/servo/src/test/html/content/test_collections.html index f3dc6d33e59f..323ee4509382 100644 --- a/servo/src/test/html/content/test_collections.html +++ b/servo/src/test/html/content/test_collections.html @@ -38,7 +38,6 @@ check_collection(document.scripts, 2, [HTMLScriptElement], "SCRIPT"); check_collection(document.applets, 1, [HTMLAppletElement], "APPLET"); check_collection(document.forms, 1, [HTMLFormElement], "FORM"); -check_collection(document.getElementsByName("test"), 2); check_collection(document.getElementsByTagName("nosuchtag"), 0); check_tag("section", 1, []); diff --git a/servo/src/test/html/content/test_document_getElementsByName.html b/servo/src/test/html/content/test_document_getElementsByName.html new file mode 100644 index 000000000000..ab69292eced4 --- /dev/null +++ b/servo/src/test/html/content/test_document_getElementsByName.html @@ -0,0 +1,15 @@ + + + + + + +
+ + +