diff --git a/src/components/script/dom/document.rs b/src/components/script/dom/document.rs index e338eee5dae1..af7b5c7ff235 100644 --- a/src/components/script/dom/document.rs +++ b/src/components/script/dom/document.rs @@ -5,7 +5,8 @@ use dom::bindings::codegen::DocumentBinding; use dom::bindings::utils::{DOMString, WrapperCache, ErrorResult, null_string, str}; use dom::bindings::utils::{BindingObject, CacheableWrapper, rust_box, DerivedWrapper}; -use dom::element::{HTMLHtmlElement, HTMLTitleElement, HTMLHtmlElementTypeId, HTMLHeadElementTypeId, HTMLTitleElementTypeId}; +use dom::element::{Element, HTMLHtmlElement, HTMLTitleElement}; +use dom::element::{HTMLHtmlElementTypeId, HTMLHeadElementTypeId, HTMLTitleElementTypeId}; use dom::event::Event; use dom::htmlcollection::HTMLCollection; use dom::htmldocument::HTMLDocument; @@ -216,19 +217,7 @@ impl Document { } pub fn GetElementsByTagName(&self, tag: &DOMString) -> @mut HTMLCollection { - let mut elements = ~[]; - let tag = tag.to_str(); - let _ = for self.root.traverse_preorder |child| { - if child.is_element() { - do child.with_imm_element |elem| { - if elem.tag_name == tag { - elements.push(child); - } - } - } - }; - let (scope, cx) = self.get_scope_and_cx(); - HTMLCollection::new(elements, cx, scope) + self.createHTMLCollection(|elem| eq_slice(elem.tag_name, tag.to_str())) } pub fn GetElementsByTagNameNS(&self, _ns: &DOMString, _tag: &DOMString) -> @mut HTMLCollection { @@ -431,14 +420,17 @@ impl Document { } pub fn GetElementsByName(&self, name: &DOMString) -> @mut HTMLCollection { + self.createHTMLCollection(|elem| + elem.get_attr("name").is_some() && eq_slice(elem.get_attr("name").unwrap(), name.to_str())) + } + + pub fn createHTMLCollection(&self, callback: &fn(elem: &Element) -> bool) -> @mut HTMLCollection { let mut elements = ~[]; - let name = name.to_str(); let _ = for self.root.traverse_preorder |child| { if child.is_element() { do child.with_imm_element |elem| { - match elem.get_attr("name") { - Some(val) => if eq_slice(val, name) { elements.push(child) }, - None() => () + if callback(elem) { + elements.push(child); } } } diff --git a/src/components/script/dom/htmldocument.rs b/src/components/script/dom/htmldocument.rs index a102fd8aa810..52c15fc36cf3 100644 --- a/src/components/script/dom/htmldocument.rs +++ b/src/components/script/dom/htmldocument.rs @@ -6,7 +6,7 @@ use dom::bindings::codegen::HTMLDocumentBinding; use dom::bindings::utils::{DOMString, ErrorResult, null_string}; use dom::bindings::utils::{CacheableWrapper, BindingObject, WrapperCache}; use dom::document::{AbstractDocument, Document, WrappableDocument, HTML}; -use dom::element::{Element, HTMLHeadElementTypeId}; +use dom::element::HTMLHeadElementTypeId; use dom::htmlcollection::HTMLCollection; use dom::node::{AbstractNode, ScriptView, ElementNodeTypeId}; use dom::window::Window; @@ -79,11 +79,11 @@ impl HTMLDocument { } pub fn Images(&self) -> @mut HTMLCollection { - self.createHTMLCollection(|elem| eq_slice(elem.tag_name, "img")) + self.parent.createHTMLCollection(|elem| eq_slice(elem.tag_name, "img")) } pub fn Embeds(&self) -> @mut HTMLCollection { - self.createHTMLCollection(|elem| eq_slice(elem.tag_name, "embed")) + self.parent.createHTMLCollection(|elem| eq_slice(elem.tag_name, "embed")) } pub fn Plugins(&self) -> @mut HTMLCollection { @@ -91,17 +91,17 @@ impl HTMLDocument { } pub fn Links(&self) -> @mut HTMLCollection { - self.createHTMLCollection(|elem| + self.parent.createHTMLCollection(|elem| (eq_slice(elem.tag_name, "a") || eq_slice(elem.tag_name, "area")) && elem.get_attr("href").is_some()) } pub fn Forms(&self) -> @mut HTMLCollection { - self.createHTMLCollection(|elem| eq_slice(elem.tag_name, "form")) + self.parent.createHTMLCollection(|elem| eq_slice(elem.tag_name, "form")) } pub fn Scripts(&self) -> @mut HTMLCollection { - self.createHTMLCollection(|elem| eq_slice(elem.tag_name, "script")) + self.parent.createHTMLCollection(|elem| eq_slice(elem.tag_name, "script")) } pub fn Close(&self, _rv: &mut ErrorResult) { @@ -174,13 +174,13 @@ impl HTMLDocument { } pub fn Anchors(&self) -> @mut HTMLCollection { - self.createHTMLCollection(|elem| + self.parent.createHTMLCollection(|elem| eq_slice(elem.tag_name, "a") && elem.get_attr("name").is_some()) } pub fn Applets(&self) -> @mut HTMLCollection { // FIXME: This should be return OBJECT elements containing applets. - self.createHTMLCollection(|elem| eq_slice(elem.tag_name, "applet")) + self.parent.createHTMLCollection(|elem| eq_slice(elem.tag_name, "applet")) } pub fn Clear(&self) { @@ -189,21 +189,6 @@ impl HTMLDocument { pub fn GetAll(&self, _cx: *JSContext, _rv: &mut ErrorResult) -> *libc::c_void { ptr::null() } - - fn createHTMLCollection(&self, callback: &fn(elem: &Element) -> bool) -> @mut HTMLCollection { - let (scope, cx) = self.get_scope_and_cx(); - let mut elements = ~[]; - let _ = for self.parent.root.traverse_preorder |child| { - if child.is_element() { - do child.with_imm_element |elem| { - if callback(elem) { - elements.push(child); - } - } - } - }; - HTMLCollection::new(elements, cx, scope) - } } impl CacheableWrapper for HTMLDocument {