aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/script/dom/htmlcollection.rs
diff options
context:
space:
mode:
authorBruno de Oliveira Abinader <bruno.d@partner.samsung.com>2014-03-20 18:27:30 -0400
committerBruno de Oliveira Abinader <bruno.d@partner.samsung.com>2014-03-26 08:59:59 -0400
commit904e4648241ac3f6a803a7870d65dbfc460ca7c5 (patch)
tree161324ad5fd10c549d9f04cda51daca24d40c70f /src/components/script/dom/htmlcollection.rs
parentf34a64049a8b6dede424d35345db63bc60b5e423 (diff)
downloadservo-904e4648241ac3f6a803a7870d65dbfc460ca7c5.tar.gz
servo-904e4648241ac3f6a803a7870d65dbfc460ca7c5.zip
Added CollectionFilter, CollectionTypeId
Diffstat (limited to 'src/components/script/dom/htmlcollection.rs')
-rw-r--r--src/components/script/dom/htmlcollection.rs66
1 files changed, 52 insertions, 14 deletions
diff --git a/src/components/script/dom/htmlcollection.rs b/src/components/script/dom/htmlcollection.rs
index 77d36e07005..71f4f90a568 100644
--- a/src/components/script/dom/htmlcollection.rs
+++ b/src/components/script/dom/htmlcollection.rs
@@ -12,24 +12,40 @@ use dom::window::Window;
use servo_util::namespace::Namespace;
use servo_util::str::DOMString;
+use serialize::{Encoder, Encodable};
+
+pub trait CollectionFilter {
+ fn filter(&self, elem: &JS<Element>, root: &JS<Node>) -> bool;
+}
+
+impl<S: Encoder> Encodable<S> for ~CollectionFilter {
+ fn encode(&self, _s: &mut S) {}
+}
+
+#[deriving(Encodable)]
+pub enum CollectionTypeId {
+ Static(~[JS<Element>]),
+ Live(JS<Node>, ~CollectionFilter)
+}
+
#[deriving(Encodable)]
pub struct HTMLCollection {
- elements: ~[JS<Element>],
+ collection: CollectionTypeId,
reflector_: Reflector,
window: JS<Window>,
}
impl HTMLCollection {
- pub fn new_inherited(window: JS<Window>, elements: ~[JS<Element>]) -> HTMLCollection {
+ pub fn new_inherited(window: JS<Window>, collection: CollectionTypeId) -> HTMLCollection {
HTMLCollection {
- elements: elements,
+ collection: collection,
reflector_: Reflector::new(),
window: window,
}
}
- pub fn new(window: &JS<Window>, elements: ~[JS<Element>]) -> JS<HTMLCollection> {
- reflect_dom_object(~HTMLCollection::new_inherited(window.clone(), elements),
+ pub fn new(window: &JS<Window>, collection: CollectionTypeId) -> JS<HTMLCollection> {
+ reflect_dom_object(~HTMLCollection::new_inherited(window.clone(), collection),
window, HTMLCollectionBinding::Wrap)
}
}
@@ -45,7 +61,7 @@ impl HTMLCollection {
}
}
}
- HTMLCollection::new(window, elements)
+ HTMLCollection::new(window, Static(elements))
}
pub fn by_tag_name(window: &JS<Window>, root: &JS<Node>, tag_name: DOMString) -> JS<HTMLCollection> {
@@ -66,15 +82,26 @@ impl HTMLCollection {
impl HTMLCollection {
// http://dom.spec.whatwg.org/#dom-htmlcollection-length
pub fn Length(&self) -> u32 {
- self.elements.len() as u32
+ match self.collection {
+ Static(ref elems) => elems.len() as u32,
+ Live(ref root, ref filter) => root.traverse_preorder()
+ .count(|child| {
+ let elem: Option<JS<Element>> = ElementCast::to(&child);
+ elem.map_or(false, |elem| filter.filter(&elem, root))
+ }) as u32
+ }
}
// http://dom.spec.whatwg.org/#dom-htmlcollection-item
pub fn Item(&self, index: u32) -> Option<JS<Element>> {
- if index < self.Length() {
- Some(self.elements[index].clone())
- } else {
- None
+ match self.collection {
+ Static(ref elems) => elems
+ .get(index as uint)
+ .map(|elem| elem.clone()),
+ Live(ref root, ref filter) => root.traverse_preorder()
+ .filter_map(|node| ElementCast::to(&node))
+ .filter(|elem| filter.filter(elem, root))
+ .nth(index as uint).clone()
}
}
@@ -86,9 +113,20 @@ impl HTMLCollection {
}
// Step 2.
- self.elements.iter().find(|elem| {
- elem.get_string_attribute("name") == key || elem.get_string_attribute("id") == key
- }).map(|maybe_elem| maybe_elem.clone())
+ match self.collection {
+ Static(ref elems) => elems.iter()
+ .find(|elem| {
+ elem.get_string_attribute("name") == key ||
+ elem.get_string_attribute("id") == key })
+ .map(|maybe_elem| maybe_elem.clone()),
+ Live(ref root, ref filter) => root.traverse_preorder()
+ .filter_map(|node| ElementCast::to(&node))
+ .filter(|elem| filter.filter(elem, root))
+ .find(|elem| {
+ elem.get_string_attribute("name") == key ||
+ elem.get_string_attribute("id") == key })
+ .map(|maybe_elem| maybe_elem.clone())
+ }
}
}