Added HTMLCollection::create

This is a sub-task for #1662.
This commit is contained in:
Bruno de Oliveira Abinader 2014-02-11 13:39:57 -04:00
parent 6cf0eb1115
commit c768097adc

View file

@ -2,10 +2,12 @@
* 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::bindings::codegen::InheritTypes::{ElementCast};
use dom::bindings::codegen::HTMLCollectionBinding;
use dom::bindings::js::JS;
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
use dom::element::Element;
use dom::node::{Node, NodeHelpers};
use dom::window::Window;
use servo_util::str::DOMString;
@ -31,6 +33,21 @@ impl HTMLCollection {
}
}
impl HTMLCollection {
pub fn create(window: &JS<Window>, root: &JS<Node>, predicate: |elem: &JS<Element>| -> bool) -> JS<HTMLCollection> {
let mut elements = ~[];
for child in root.traverse_preorder() {
if child.is_element() {
let elem: JS<Element> = ElementCast::to(&child);
if predicate(&elem) {
elements.push(elem);
}
}
}
HTMLCollection::new(window, elements)
}
}
impl HTMLCollection {
// http://dom.spec.whatwg.org/#dom-htmlcollection-length
pub fn Length(&self) -> u32 {