Simplify HTMLCollection predicates

This commit is contained in:
Youngsoo Son 2013-08-09 12:54:06 +09:00
parent 1da9f34082
commit 133cf9caf1
2 changed files with 18 additions and 41 deletions

View file

@ -5,7 +5,8 @@
use dom::bindings::codegen::DocumentBinding; use dom::bindings::codegen::DocumentBinding;
use dom::bindings::utils::{DOMString, WrapperCache, ErrorResult, null_string, str}; use dom::bindings::utils::{DOMString, WrapperCache, ErrorResult, null_string, str};
use dom::bindings::utils::{BindingObject, CacheableWrapper, rust_box, DerivedWrapper}; 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::event::Event;
use dom::htmlcollection::HTMLCollection; use dom::htmlcollection::HTMLCollection;
use dom::htmldocument::HTMLDocument; use dom::htmldocument::HTMLDocument;
@ -216,19 +217,7 @@ impl Document {
} }
pub fn GetElementsByTagName(&self, tag: &DOMString) -> @mut HTMLCollection { pub fn GetElementsByTagName(&self, tag: &DOMString) -> @mut HTMLCollection {
let mut elements = ~[]; self.createHTMLCollection(|elem| eq_slice(elem.tag_name, tag.to_str()))
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)
} }
pub fn GetElementsByTagNameNS(&self, _ns: &DOMString, _tag: &DOMString) -> @mut HTMLCollection { pub fn GetElementsByTagNameNS(&self, _ns: &DOMString, _tag: &DOMString) -> @mut HTMLCollection {
@ -431,14 +420,17 @@ impl Document {
} }
pub fn GetElementsByName(&self, name: &DOMString) -> @mut HTMLCollection { 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 mut elements = ~[];
let name = name.to_str();
let _ = for self.root.traverse_preorder |child| { let _ = for self.root.traverse_preorder |child| {
if child.is_element() { if child.is_element() {
do child.with_imm_element |elem| { do child.with_imm_element |elem| {
match elem.get_attr("name") { if callback(elem) {
Some(val) => if eq_slice(val, name) { elements.push(child) }, elements.push(child);
None() => ()
} }
} }
} }

View file

@ -6,7 +6,7 @@ use dom::bindings::codegen::HTMLDocumentBinding;
use dom::bindings::utils::{DOMString, ErrorResult, null_string}; use dom::bindings::utils::{DOMString, ErrorResult, null_string};
use dom::bindings::utils::{CacheableWrapper, BindingObject, WrapperCache}; use dom::bindings::utils::{CacheableWrapper, BindingObject, WrapperCache};
use dom::document::{AbstractDocument, Document, WrappableDocument, HTML}; use dom::document::{AbstractDocument, Document, WrappableDocument, HTML};
use dom::element::{Element, HTMLHeadElementTypeId}; use dom::element::HTMLHeadElementTypeId;
use dom::htmlcollection::HTMLCollection; use dom::htmlcollection::HTMLCollection;
use dom::node::{AbstractNode, ScriptView, ElementNodeTypeId}; use dom::node::{AbstractNode, ScriptView, ElementNodeTypeId};
use dom::window::Window; use dom::window::Window;
@ -79,11 +79,11 @@ impl HTMLDocument {
} }
pub fn Images(&self) -> @mut HTMLCollection { 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 { 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 { pub fn Plugins(&self) -> @mut HTMLCollection {
@ -91,17 +91,17 @@ impl HTMLDocument {
} }
pub fn Links(&self) -> @mut HTMLCollection { 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")) (eq_slice(elem.tag_name, "a") || eq_slice(elem.tag_name, "area"))
&& elem.get_attr("href").is_some()) && elem.get_attr("href").is_some())
} }
pub fn Forms(&self) -> @mut HTMLCollection { 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 { 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) { pub fn Close(&self, _rv: &mut ErrorResult) {
@ -174,13 +174,13 @@ impl HTMLDocument {
} }
pub fn Anchors(&self) -> @mut HTMLCollection { 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()) eq_slice(elem.tag_name, "a") && elem.get_attr("name").is_some())
} }
pub fn Applets(&self) -> @mut HTMLCollection { pub fn Applets(&self) -> @mut HTMLCollection {
// FIXME: This should be return OBJECT elements containing applets. // 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) { pub fn Clear(&self) {
@ -189,21 +189,6 @@ impl HTMLDocument {
pub fn GetAll(&self, _cx: *JSContext, _rv: &mut ErrorResult) -> *libc::c_void { pub fn GetAll(&self, _cx: *JSContext, _rv: &mut ErrorResult) -> *libc::c_void {
ptr::null() 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 { impl CacheableWrapper for HTMLDocument {