diff options
Diffstat (limited to 'src/components/script/dom/htmldocument.rs')
-rw-r--r-- | src/components/script/dom/htmldocument.rs | 63 |
1 files changed, 46 insertions, 17 deletions
diff --git a/src/components/script/dom/htmldocument.rs b/src/components/script/dom/htmldocument.rs index b44a1b400d8..50f2a7a0556 100644 --- a/src/components/script/dom/htmldocument.rs +++ b/src/components/script/dom/htmldocument.rs @@ -2,18 +2,22 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -use dom::document::{AbstractDocument, Document, WrappableDocument, HTML}; 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; use dom::htmlcollection::HTMLCollection; use dom::node::{AbstractNode, ScriptView}; use dom::window::Window; use js::jsapi::{JSObject, JSContext}; +use servo_util::tree::TreeUtils; + use std::libc; use std::ptr; +use std::str::eq_slice; pub struct HTMLDocument { parent: Document @@ -64,33 +68,35 @@ impl HTMLDocument { } pub fn Images(&self) -> @mut HTMLCollection { - let (scope, cx) = self.get_scope_and_cx(); - HTMLCollection::new(~[], cx, scope) + self.createHTMLCollection(|elem| eq_slice(elem.tag_name, "img")) } pub fn Embeds(&self) -> @mut HTMLCollection { - let (scope, cx) = self.get_scope_and_cx(); - HTMLCollection::new(~[], cx, scope) + self.createHTMLCollection(|elem| eq_slice(elem.tag_name, "embed")) } pub fn Plugins(&self) -> @mut HTMLCollection { - let (scope, cx) = self.get_scope_and_cx(); - HTMLCollection::new(~[], cx, scope) + self.Embeds() } pub fn Links(&self) -> @mut HTMLCollection { - let (scope, cx) = self.get_scope_and_cx(); - HTMLCollection::new(~[], cx, scope) + self.createHTMLCollection(|elem| { + if eq_slice(elem.tag_name, "a") || eq_slice(elem.tag_name, "area") { + match elem.get_attr("href") { + Some(_val) => true, + None() => false + } + } + else { false } + }) } pub fn Forms(&self) -> @mut HTMLCollection { - let (scope, cx) = self.get_scope_and_cx(); - HTMLCollection::new(~[], cx, scope) + self.createHTMLCollection(|elem| eq_slice(elem.tag_name, "form")) } pub fn Scripts(&self) -> @mut HTMLCollection { - let (scope, cx) = self.get_scope_and_cx(); - HTMLCollection::new(~[], cx, scope) + self.createHTMLCollection(|elem| eq_slice(elem.tag_name, "script")) } pub fn Close(&self, _rv: &mut ErrorResult) { @@ -163,13 +169,20 @@ impl HTMLDocument { } pub fn Anchors(&self) -> @mut HTMLCollection { - let (scope, cx) = self.get_scope_and_cx(); - HTMLCollection::new(~[], cx, scope) + self.createHTMLCollection(|elem| { + if eq_slice(elem.tag_name, "a") { + match elem.get_attr("name") { + Some(_val) => true, + None() => false + } + } + else { false } + }) } pub fn Applets(&self) -> @mut HTMLCollection { - let (scope, cx) = self.get_scope_and_cx(); - HTMLCollection::new(~[], cx, scope) + // FIXME: This should be return OBJECT elements containing applets. + self.createHTMLCollection(|elem| eq_slice(elem.tag_name, "applet")) } pub fn Clear(&self) { @@ -178,6 +191,21 @@ 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 { @@ -196,3 +224,4 @@ impl BindingObject for HTMLDocument { self.parent.GetParentObject(cx) } } + |