Make document.getElementsByTagname return a NodeList; consolidate element collection to collectElement; fix #1744

This commit is contained in:
Manish Goregaokar 2014-02-26 10:08:06 +05:30
parent 478c9bfc57
commit 8b425fb8cf
4 changed files with 34 additions and 6 deletions

View file

@ -21,6 +21,7 @@ use dom::element::{HTMLBodyElementTypeId, HTMLFrameSetElementTypeId};
use dom::event::Event; use dom::event::Event;
use dom::eventtarget::{EventTarget, NodeTargetTypeId}; use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlcollection::HTMLCollection; use dom::htmlcollection::HTMLCollection;
use dom::nodelist::NodeList;
use dom::htmlelement::HTMLElement; use dom::htmlelement::HTMLElement;
use dom::htmlheadelement::HTMLHeadElement; use dom::htmlheadelement::HTMLHeadElement;
use dom::htmlhtmlelement::HTMLHtmlElement; use dom::htmlhtmlelement::HTMLHtmlElement;
@ -403,8 +404,8 @@ impl Document {
} }
// http://www.whatwg.org/specs/web-apps/current-work/#dom-document-getelementsbyname // http://www.whatwg.org/specs/web-apps/current-work/#dom-document-getelementsbyname
pub fn GetElementsByName(&self, name: DOMString) -> JS<HTMLCollection> { pub fn GetElementsByName(&self, name: DOMString) -> JS<NodeList> {
self.createHTMLCollection(|elem| { self.createNodeList(|elem| {
elem.get_attribute(Null, "name").map_default(false, |attr| { elem.get_attribute(Null, "name").map_default(false, |attr| {
attr.get().value_ref() == name attr.get().value_ref() == name
}) })
@ -449,7 +450,7 @@ impl Document {
self.createHTMLCollection(|elem| "applet" == elem.tag_name) self.createHTMLCollection(|elem| "applet" == elem.tag_name)
} }
pub fn createHTMLCollection(&self, callback: |elem: &Element| -> bool) -> JS<HTMLCollection> { pub fn create_collection(&self, callback: |elem: &Element| -> bool) -> ~[JS<Element>] {
let mut elements = ~[]; let mut elements = ~[];
match self.GetDocumentElement() { match self.GetDocumentElement() {
None => {}, None => {},
@ -465,7 +466,20 @@ impl Document {
} }
} }
} }
HTMLCollection::new(&self.window, elements) elements
}
pub fn createHTMLCollection(&self, callback: |elem: &Element| -> bool) -> JS<HTMLCollection> {
HTMLCollection::new(&self.window, self.create_collection(callback))
}
pub fn createNodeList(&self, callback: |elem: &Element| -> bool) -> JS<NodeList> {
let elements = self.create_collection(callback);
let nodes = elements.map(|element| {
let node: JS<Node> = NodeCast::from(element);
node
});
NodeList::new_simple_list(&self.window, nodes)
} }
pub fn content_changed(&self) { pub fn content_changed(&self) {

View file

@ -44,7 +44,7 @@ partial interface Document {
attribute DOMString title; attribute DOMString title;
attribute HTMLElement? body; attribute HTMLElement? body;
readonly attribute HTMLHeadElement? head; readonly attribute HTMLHeadElement? head;
/*NodeList*/ HTMLCollection getElementsByName(DOMString elementName); NodeList getElementsByName(DOMString elementName);
readonly attribute HTMLCollection images; readonly attribute HTMLCollection images;
readonly attribute HTMLCollection embeds; readonly attribute HTMLCollection embeds;

View file

@ -38,7 +38,6 @@ check_collection(document.scripts, 2, [HTMLScriptElement], "SCRIPT");
check_collection(document.applets, 1, [HTMLAppletElement], "APPLET"); check_collection(document.applets, 1, [HTMLAppletElement], "APPLET");
check_collection(document.forms, 1, [HTMLFormElement], "FORM"); check_collection(document.forms, 1, [HTMLFormElement], "FORM");
check_collection(document.getElementsByName("test"), 2);
check_collection(document.getElementsByTagName("nosuchtag"), 0); check_collection(document.getElementsByTagName("nosuchtag"), 0);
check_tag("section", 1, []); check_tag("section", 1, []);

View file

@ -0,0 +1,15 @@
<html>
<head >
<title></title>
<script src="harness.js"></script>
</head>
<body>
<div name="foo"></div>
<script>
let nameList = document.getElementsByName("foo");
is_a(nameList, NodeList);
is_not_a(nameList, HTMLCollection);
finish();
</script>
</body>
</html>