servo: Merge #1756 - #1744 make getElementsByName return a NodeList (from Manishearth:nodelist-1744); r=Ms2ger

I haven't been able to test it yet (need to run a full build), but it looks like it should work.

Source-Repo: https://github.com/servo/servo
Source-Revision: ee549af3ae6c85e1ff8c99f42d4cd5f8e3ec327e
This commit is contained in:
Manish Goregaokar 2014-03-05 05:22:39 -05:00
Родитель abe5497980
Коммит ca4dde6062
4 изменённых файлов: 34 добавлений и 6 удалений

Просмотреть файл

@ -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<HTMLCollection> {
self.createHTMLCollection(|elem| {
pub fn GetElementsByName(&self, name: DOMString) -> JS<NodeList> {
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<HTMLCollection> {
pub fn create_collection(&self, callback: |elem: &Element| -> bool) -> ~[JS<Element>] {
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> {
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) {

Просмотреть файл

@ -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;

Просмотреть файл

@ -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, []);

Просмотреть файл

@ -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>