Skip to content

Commit

Permalink
Simplify HTMLCollection predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
sonwow committed Aug 9, 2013
1 parent 1da9f34 commit 133cf9c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 41 deletions.
28 changes: 10 additions & 18 deletions src/components/script/dom/document.rs
Expand Up @@ -5,7 +5,8 @@
use dom::bindings::codegen::DocumentBinding;
use dom::bindings::utils::{DOMString, WrapperCache, ErrorResult, null_string, str};
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::htmlcollection::HTMLCollection;
use dom::htmldocument::HTMLDocument;
Expand Down Expand Up @@ -216,19 +217,7 @@ impl Document {
}

pub fn GetElementsByTagName(&self, tag: &DOMString) -> @mut HTMLCollection {
let mut elements = ~[];
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)
self.createHTMLCollection(|elem| eq_slice(elem.tag_name, tag.to_str()))
}

pub fn GetElementsByTagNameNS(&self, _ns: &DOMString, _tag: &DOMString) -> @mut HTMLCollection {
Expand Down Expand Up @@ -431,14 +420,17 @@ impl Document {
}

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 name = name.to_str();
let _ = for self.root.traverse_preorder |child| {
if child.is_element() {
do child.with_imm_element |elem| {
match elem.get_attr("name") {
Some(val) => if eq_slice(val, name) { elements.push(child) },
None() => ()
if callback(elem) {
elements.push(child);
}
}
}
Expand Down
31 changes: 8 additions & 23 deletions src/components/script/dom/htmldocument.rs
Expand Up @@ -6,7 +6,7 @@ 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, HTMLHeadElementTypeId};
use dom::element::HTMLHeadElementTypeId;
use dom::htmlcollection::HTMLCollection;
use dom::node::{AbstractNode, ScriptView, ElementNodeTypeId};
use dom::window::Window;
Expand Down Expand Up @@ -79,29 +79,29 @@ impl HTMLDocument {
}

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 {
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 {
self.Embeds()
}

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"))
&& elem.get_attr("href").is_some())
}

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 {
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) {
Expand Down Expand Up @@ -174,13 +174,13 @@ impl HTMLDocument {
}

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())
}

pub fn Applets(&self) -> @mut HTMLCollection {
// 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) {
Expand All @@ -189,21 +189,6 @@ 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 {
Expand Down

5 comments on commit 133cf9c

@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@133cf9c

@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/collection = 133cf9c 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/collection = 133cf9c merged ok, testing candidate = 73e7b65

@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 = 73e7b65

Please sign in to comment.