From 8032b17e364eee08eb7f2f643afa3904048ea857 Mon Sep 17 00:00:00 2001 From: Youngsoo Son Date: Thu, 1 Aug 2013 11:56:17 +0900 Subject: [PATCH 1/2] This implements the DOM tree accessors that return a HTMLCollection --- src/components/script/dom/htmldocument.rs | 54 ++++++++++++++++------- src/test/html/test_bindings.html | 5 +++ src/test/html/test_bindings.js | 35 +++++++++++++++ 3 files changed, 78 insertions(+), 16 deletions(-) diff --git a/src/components/script/dom/htmldocument.rs b/src/components/script/dom/htmldocument.rs index b44a1b400d8..b45e56910f8 100644 --- a/src/components/script/dom/htmldocument.rs +++ b/src/components/script/dom/htmldocument.rs @@ -12,6 +12,8 @@ use dom::window::Window; use js::jsapi::{JSObject, JSContext}; +use servo_util::tree::TreeUtils; + use std::libc; use std::ptr; @@ -64,33 +66,27 @@ impl HTMLDocument { } pub fn Images(&self) -> @mut HTMLCollection { - let (scope, cx) = self.get_scope_and_cx(); - HTMLCollection::new(~[], cx, scope) + self.createHTMLCollection(~"img") } pub fn Embeds(&self) -> @mut HTMLCollection { - let (scope, cx) = self.get_scope_and_cx(); - HTMLCollection::new(~[], cx, scope) + self.createHTMLCollection(~"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(~"link") } pub fn Forms(&self) -> @mut HTMLCollection { - let (scope, cx) = self.get_scope_and_cx(); - HTMLCollection::new(~[], cx, scope) + self.createHTMLCollection(~"form") } pub fn Scripts(&self) -> @mut HTMLCollection { - let (scope, cx) = self.get_scope_and_cx(); - HTMLCollection::new(~[], cx, scope) + self.createHTMLCollection(~"script") } pub fn Close(&self, _rv: &mut ErrorResult) { @@ -163,13 +159,11 @@ impl HTMLDocument { } pub fn Anchors(&self) -> @mut HTMLCollection { - let (scope, cx) = self.get_scope_and_cx(); - HTMLCollection::new(~[], cx, scope) + self.createHTMLCollection(~"a") } pub fn Applets(&self) -> @mut HTMLCollection { - let (scope, cx) = self.get_scope_and_cx(); - HTMLCollection::new(~[], cx, scope) + self.createHTMLCollection(~"applet") } pub fn Clear(&self) { @@ -178,6 +172,33 @@ impl HTMLDocument { pub fn GetAll(&self, _cx: *JSContext, _rv: &mut ErrorResult) -> *libc::c_void { ptr::null() } + + fn createHTMLCollection(&self, elem_name: ~str) -> @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| { + match elem_name { + ~"link" => { + if elem.tag_name == ~"a" || elem.tag_name == ~"area" { + match elem.get_attr("href") { + Some(_val) => elements.push(child), + None() => () + } + } + } + _ => { + if elem.tag_name == elem_name { + elements.push(child); + } + } + } + } + } + }; + HTMLCollection::new(elements, cx, scope) + } } impl CacheableWrapper for HTMLDocument { @@ -196,3 +217,4 @@ impl BindingObject for HTMLDocument { self.parent.GetParentObject(cx) } } + diff --git a/src/test/html/test_bindings.html b/src/test/html/test_bindings.html index e93f363387c..8bf4d28504e 100644 --- a/src/test/html/test_bindings.html +++ b/src/test/html/test_bindings.html @@ -7,5 +7,10 @@
ggg
hhhhhhhh
iiiiiiiiiiiiiiiiiii
+ + + +
+ diff --git a/src/test/html/test_bindings.js b/src/test/html/test_bindings.js index 796e2b8d453..a7cbb9c6c46 100644 --- a/src/test/html/test_bindings.js +++ b/src/test/html/test_bindings.js @@ -60,6 +60,41 @@ window.alert(tags[0].tagName); window.alert(tags[1]); window.alert(tags[1].tagName); window.alert(tags[2]); +let tags = document.links; +window.alert(tags); +window.alert(tags.length); +window.alert(tags[0]); +window.alert(tags[0].tagName); +let tags = document.images; +window.alert(tags); +window.alert(tags.length); +window.alert(tags[0]); +window.alert(tags[0].tagName); +let tags = document.embeds; +window.alert(tags); +window.alert(tags.length); +window.alert(tags[0]); +window.alert(tags[0].tagName); +let tags = document.plugins; +window.alert(tags); +window.alert(tags.length); +window.alert(tags[0]); +window.alert(tags[0].tagName); +let tags = document.forms; +window.alert(tags); +window.alert(tags.length); +window.alert(tags[0]); +window.alert(tags[0].tagName); +let tags = document.scripts; +window.alert(tags); +window.alert(tags.length); +window.alert(tags[0]); +window.alert(tags[0].tagName); +let tags = document.applets; +window.alert(tags); +window.alert(tags.length); +window.alert(tags[0]); +window.alert(tags[0].tagName); window.alert("DOMParser:"); window.alert(DOMParser); From 7b20da05f6467dc7ba748edc1859f08bdcfad06a Mon Sep 17 00:00:00 2001 From: Youngsoo Son Date: Fri, 2 Aug 2013 18:53:38 +0900 Subject: [PATCH 2/2] Fix some implementations for DOM tree accessors --- src/components/script/dom/htmldocument.rs | 53 +++++++++++++---------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/src/components/script/dom/htmldocument.rs b/src/components/script/dom/htmldocument.rs index b45e56910f8..50f2a7a0556 100644 --- a/src/components/script/dom/htmldocument.rs +++ b/src/components/script/dom/htmldocument.rs @@ -2,10 +2,11 @@ * 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; @@ -16,6 +17,7 @@ use servo_util::tree::TreeUtils; use std::libc; use std::ptr; +use std::str::eq_slice; pub struct HTMLDocument { parent: Document @@ -66,11 +68,11 @@ impl HTMLDocument { } pub fn Images(&self) -> @mut HTMLCollection { - self.createHTMLCollection(~"img") + self.createHTMLCollection(|elem| eq_slice(elem.tag_name, "img")) } pub fn Embeds(&self) -> @mut HTMLCollection { - self.createHTMLCollection(~"embed") + self.createHTMLCollection(|elem| eq_slice(elem.tag_name, "embed")) } pub fn Plugins(&self) -> @mut HTMLCollection { @@ -78,15 +80,23 @@ impl HTMLDocument { } pub fn Links(&self) -> @mut HTMLCollection { - self.createHTMLCollection(~"link") + 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 { - self.createHTMLCollection(~"form") + self.createHTMLCollection(|elem| eq_slice(elem.tag_name, "form")) } pub fn Scripts(&self) -> @mut HTMLCollection { - self.createHTMLCollection(~"script") + self.createHTMLCollection(|elem| eq_slice(elem.tag_name, "script")) } pub fn Close(&self, _rv: &mut ErrorResult) { @@ -159,11 +169,20 @@ impl HTMLDocument { } pub fn Anchors(&self) -> @mut HTMLCollection { - self.createHTMLCollection(~"a") + 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 { - self.createHTMLCollection(~"applet") + // FIXME: This should be return OBJECT elements containing applets. + self.createHTMLCollection(|elem| eq_slice(elem.tag_name, "applet")) } pub fn Clear(&self) { @@ -173,26 +192,14 @@ impl HTMLDocument { ptr::null() } - fn createHTMLCollection(&self, elem_name: ~str) -> @mut HTMLCollection { + 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| { - match elem_name { - ~"link" => { - if elem.tag_name == ~"a" || elem.tag_name == ~"area" { - match elem.get_attr("href") { - Some(_val) => elements.push(child), - None() => () - } - } - } - _ => { - if elem.tag_name == elem_name { - elements.push(child); - } - } + if callback(elem) { + elements.push(child); } } }