mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
auto merge of #659 : sonwow/servo/dom, r=jdm
Basic implementation about DOM tree accessors in HTMLDocument. (image, embeds, plugins, links, forms, scripts, anchors, applets)
This commit is contained in:
commit
5f62c95437
3 changed files with 86 additions and 17 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,5 +7,10 @@
|
|||
<div id="second">ggg</div>
|
||||
<span id="third" name="test">hhhhhhhh</span>
|
||||
<div id="fourth">iiiiiiiiiiiiiiiiiii</div>
|
||||
<a href="http://www.mozilla.org"></a>
|
||||
<img src="test.jpg"/>
|
||||
<embed></embed>
|
||||
<form></form>
|
||||
<applet></applet>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue