aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/script/dom/nodelist.rs
diff options
context:
space:
mode:
authorbors-servo <release+servo@mozilla.com>2013-10-11 07:51:59 -0700
committerbors-servo <release+servo@mozilla.com>2013-10-11 07:51:59 -0700
commitfc9fdf30a6b4b4437cfe7a624c52c9a8b5e5a645 (patch)
tree81e44e4c34830ca94d5dd7d21f4e5e19f85f43ad /src/components/script/dom/nodelist.rs
parentbc3eeb6f1c1b643df72b787ef772f20bcc094856 (diff)
parent9fe9145be4386ae38facc029946678fb0a54c2f7 (diff)
downloadservo-fc9fdf30a6b4b4437cfe7a624c52c9a8b5e5a645.tar.gz
servo-fc9fdf30a6b4b4437cfe7a624c52c9a8b5e5a645.zip
auto merge of #1018 : ttaubert/servo/nodelist, r=jdm
This should fix #652 and #775. I'm not sure if that's all that is needed to properly implement NodeList? Should we add tests somewhere? Sorry for any stupid stuff I might have done :) r? @jdm
Diffstat (limited to 'src/components/script/dom/nodelist.rs')
-rw-r--r--src/components/script/dom/nodelist.rs91
1 files changed, 91 insertions, 0 deletions
diff --git a/src/components/script/dom/nodelist.rs b/src/components/script/dom/nodelist.rs
new file mode 100644
index 00000000000..8781dc4b72a
--- /dev/null
+++ b/src/components/script/dom/nodelist.rs
@@ -0,0 +1,91 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * 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::NodeListBinding;
+use dom::bindings::utils::{Reflectable, BindingObject, Reflector};
+use dom::node::{AbstractNode, ScriptView};
+use script_task::page_from_context;
+
+use js::jsapi::{JSObject, JSContext};
+
+enum NodeListType {
+ Simple(~[AbstractNode<ScriptView>]),
+ Children(AbstractNode<ScriptView>)
+}
+
+pub struct NodeList {
+ list_type: NodeListType,
+ reflector_: Reflector
+}
+
+impl NodeList {
+ pub fn new_simple_list(elements: ~[AbstractNode<ScriptView>], cx: *JSContext, scope: *JSObject) -> @mut NodeList {
+ let list = @mut NodeList {
+ list_type: Simple(elements),
+ reflector_: Reflector::new()
+ };
+
+ list.init_wrapper(cx, scope);
+ list
+ }
+
+ pub fn new_child_list(node: AbstractNode<ScriptView>, cx: *JSContext, scope: *JSObject) -> @mut NodeList {
+ let list = @mut NodeList {
+ list_type: Children(node),
+ reflector_: Reflector::new()
+ };
+
+ list.init_wrapper(cx, scope);
+ list
+ }
+
+ fn init_wrapper(@mut self, cx: *JSContext, scope: *JSObject) {
+ self.wrap_object_shared(cx, scope);
+ }
+
+ pub fn Length(&self) -> u32 {
+ match self.list_type {
+ Simple(ref elems) => elems.len() as u32,
+ Children(ref node) => node.children().len() as u32
+ }
+ }
+
+ pub fn Item(&self, index: u32) -> Option<AbstractNode<ScriptView>> {
+ match self.list_type {
+ _ if index >= self.Length() => None,
+ Simple(ref elems) => Some(elems[index]),
+ Children(ref node) => node.children().nth(index as uint)
+ }
+ }
+
+ pub fn IndexedGetter(&self, index: u32, found: &mut bool) -> Option<AbstractNode<ScriptView>> {
+ let item = self.Item(index);
+ *found = item.is_some();
+ item
+ }
+}
+
+impl BindingObject for NodeList {
+ fn GetParentObject(&self, cx: *JSContext) -> Option<@mut Reflectable> {
+ let page = page_from_context(cx);
+ unsafe {
+ Some((*page).frame.get_ref().window as @mut Reflectable)
+ }
+ }
+}
+
+impl Reflectable for NodeList {
+ fn reflector<'a>(&'a self) -> &'a Reflector {
+ &self.reflector_
+ }
+
+ fn mut_reflector<'a>(&'a mut self) -> &'a mut Reflector {
+ &mut self.reflector_
+ }
+
+ fn wrap_object_shared(@mut self, cx: *JSContext, scope: *JSObject) -> *JSObject {
+ let mut unused = false;
+ NodeListBinding::Wrap(cx, scope, self, &mut unused)
+ }
+}