Skip to content

Commit

Permalink
Fix some implementations for DOM tree accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
sonwow committed Aug 2, 2013
1 parent 8032b17 commit 7b20da0
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions src/components/script/dom/htmldocument.rs
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -66,27 +68,35 @@ 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 {
self.Embeds()
}

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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}
}
Expand Down

5 comments on commit 7b20da0

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from jdm
at sonwow@7b20da0

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging sonwow/servo/dom = 7b20da0 into auto

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sonwow/servo/dom = 7b20da0 merged ok, testing candidate = 5f62c95

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 5f62c95

Please sign in to comment.