From c768097adc6366dd60319aeaccc4f7e2f883e9f6 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Tue, 11 Feb 2014 13:39:57 -0400 Subject: [PATCH] Added HTMLCollection::create This is a sub-task for #1662. --- src/components/script/dom/htmlcollection.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/components/script/dom/htmlcollection.rs b/src/components/script/dom/htmlcollection.rs index bb4cffc227e..3f176f4bc31 100644 --- a/src/components/script/dom/htmlcollection.rs +++ b/src/components/script/dom/htmlcollection.rs @@ -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, root: &JS, predicate: |elem: &JS| -> bool) -> JS { + let mut elements = ~[]; + for child in root.traverse_preorder() { + if child.is_element() { + let elem: JS = 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 {