diff options
Diffstat (limited to 'src/components/script/dom/document.rs')
-rw-r--r-- | src/components/script/dom/document.rs | 72 |
1 files changed, 44 insertions, 28 deletions
diff --git a/src/components/script/dom/document.rs b/src/components/script/dom/document.rs index 471d1439fec..2a81d08f4a5 100644 --- a/src/components/script/dom/document.rs +++ b/src/components/script/dom/document.rs @@ -213,7 +213,7 @@ impl Document { // http://dom.spec.whatwg.org/#dom-document-getelementsbytagname pub fn GetElementsByTagName(&self, tag: DOMString) -> JS<HTMLCollection> { - self.createHTMLCollection(|elem| elem.tag_name == tag) + self.createHTMLCollection(|elem| elem.get().tag_name == tag) } // http://dom.spec.whatwg.org/#dom-nonelementparentnode-getelementbyid @@ -405,19 +405,24 @@ impl Document { // http://www.whatwg.org/specs/web-apps/current-work/#dom-document-getelementsbyname pub fn GetElementsByName(&self, name: DOMString) -> JS<NodeList> { - self.createNodeList(|elem| { - elem.get_attribute(Null, "name").map_default(false, |attr| { + self.createNodeList(|node| { + if !node.is_element() { + return false; + } + + let element: JS<Element> = ElementCast::to(node); + element.get().get_attribute(Null, "name").map_default(false, |attr| { attr.get().value_ref() == name }) }) } pub fn Images(&self) -> JS<HTMLCollection> { - self.createHTMLCollection(|elem| "img" == elem.tag_name) + self.createHTMLCollection(|elem| "img" == elem.get().tag_name) } pub fn Embeds(&self) -> JS<HTMLCollection> { - self.createHTMLCollection(|elem| "embed" == elem.tag_name) + self.createHTMLCollection(|elem| "embed" == elem.get().tag_name) } pub fn Plugins(&self) -> JS<HTMLCollection> { @@ -426,60 +431,71 @@ impl Document { pub fn Links(&self) -> JS<HTMLCollection> { self.createHTMLCollection(|elem| { - ("a" == elem.tag_name || "area" == elem.tag_name) && - elem.get_attribute(Null, "href").is_some() + ("a" == elem.get().tag_name || "area" == elem.get().tag_name) && + elem.get().get_attribute(Null, "href").is_some() }) } pub fn Forms(&self) -> JS<HTMLCollection> { - self.createHTMLCollection(|elem| "form" == elem.tag_name) + self.createHTMLCollection(|elem| "form" == elem.get().tag_name) } pub fn Scripts(&self) -> JS<HTMLCollection> { - self.createHTMLCollection(|elem| "script" == elem.tag_name) + self.createHTMLCollection(|elem| "script" == elem.get().tag_name) } pub fn Anchors(&self) -> JS<HTMLCollection> { self.createHTMLCollection(|elem| { - "a" == elem.tag_name && elem.get_attribute(Null, "name").is_some() + "a" == elem.get().tag_name && + elem.get().get_attribute(Null, "name").is_some() }) } pub fn Applets(&self) -> JS<HTMLCollection> { // FIXME: This should be return OBJECT elements containing applets. - self.createHTMLCollection(|elem| "applet" == elem.tag_name) + self.createHTMLCollection(|elem| "applet" == elem.get().tag_name) } - pub fn create_collection(&self, callback: |elem: &Element| -> bool) -> ~[JS<Element>] { - let mut elements = ~[]; + pub fn create_collection<T>(&self, callback: |elem: &JS<Node>| -> Option<JS<T>>) -> ~[JS<T>] { + let mut nodes = ~[]; match self.GetDocumentElement() { None => {}, Some(root) => { let root: JS<Node> = NodeCast::from(&root); for child in root.traverse_preorder() { - if child.is_element() { - let elem: JS<Element> = ElementCast::to(&child); - if callback(elem.get()) { - elements.push(elem); - } + match callback(&child) { + Some(node) => nodes.push(node), + None => (), } } } } - elements + nodes } - pub fn createHTMLCollection(&self, callback: |elem: &Element| -> bool) -> JS<HTMLCollection> { - HTMLCollection::new(&self.window, self.create_collection(callback)) + pub fn createHTMLCollection(&self, callback: |elem: &JS<Element>| -> bool) -> JS<HTMLCollection> { + HTMLCollection::new(&self.window, self.create_collection(|node| { + if !node.is_element() { + return None; + } + + let element: JS<Element> = ElementCast::to(node); + if !callback(&element) { + return None; + } + + Some(element) + })) } - 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 createNodeList(&self, callback: |node: &JS<Node>| -> bool) -> JS<NodeList> { + NodeList::new_simple_list(&self.window, self.create_collection(|node| { + if !callback(node) { + return None; + } + + Some(node.clone()) + })) } pub fn content_changed(&self) { |