aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYoungsoo Son <ysoo.son@samsung.com>2013-07-25 13:51:36 +0900
committerYoungsoo Son <ysoo.son@samsung.com>2013-07-25 13:51:36 +0900
commitf8f9d203f5f662945b2c30a50cc4ff74c049fe5c (patch)
tree189736f935f8e5e04fb2ceddb816bd3048bc0872
parentfe91f6e238acd9e423d98c18c906416be3090eb3 (diff)
downloadservo-f8f9d203f5f662945b2c30a50cc4ff74c049fe5c.tar.gz
servo-f8f9d203f5f662945b2c30a50cc4ff74c049fe5c.zip
Add binding for Document (getElementsByName)
-rw-r--r--src/components/script/dom/bindings/document.rs34
-rw-r--r--src/components/script/dom/document.rs18
2 files changed, 52 insertions, 0 deletions
diff --git a/src/components/script/dom/bindings/document.rs b/src/components/script/dom/bindings/document.rs
index 685b7d2599f..db966ce5739 100644
--- a/src/components/script/dom/bindings/document.rs
+++ b/src/components/script/dom/bindings/document.rs
@@ -70,6 +70,35 @@ extern fn getElementsByTagName(cx: *JSContext, _argc: c_uint, vp: *JSVal) -> JSB
}
}
+extern fn getElementsByName(cx: *JSContext, _argc: c_uint, vp: *JSVal) -> JSBool {
+ unsafe {
+ let obj = JS_THIS_OBJECT(cx, vp);
+
+ let argv = JS_ARGV(cx, cast::transmute(vp));
+
+ let arg0: DOMString;
+ let strval = jsval_to_str(cx, (*argv.offset(0)));
+ if strval.is_err() {
+ return 0;
+ }
+ arg0 = str(strval.get());
+
+ let doc = &mut (*unwrap(obj)).payload;
+ let rval: Option<@mut HTMLCollection>;
+ rval = doc.getElementsByName(arg0);
+ if rval.is_none() {
+ JS_SET_RVAL(cx, vp, JSVAL_NULL);
+ } else {
+ let cache = doc.get_wrappercache();
+ let rval = rval.get() as @mut CacheableWrapper;
+ assert!(WrapNewBindingObject(cx, cache.get_wrapper(),
+ rval,
+ cast::transmute(vp)));
+ }
+ return 1;
+ }
+}
+
unsafe fn unwrap(obj: *JSObject) -> *mut rust_box<Document> {
//TODO: some kind of check if this is a Document object
let val = JS_GetReservedSlot(obj, 0);
@@ -112,6 +141,11 @@ pub fn init(compartment: @mut Compartment) {
nargs: 0,
flags: 0,
selfHostedName: null()},
+ JSFunctionSpec {name: compartment.add_name(~"getElementsByName"),
+ call: JSNativeWrapper {op: getElementsByName, info: null()},
+ nargs: 0,
+ flags: 0,
+ selfHostedName: null()},
JSFunctionSpec {name: null(),
call: JSNativeWrapper {op: null(), info: null()},
nargs: 0,
diff --git a/src/components/script/dom/document.rs b/src/components/script/dom/document.rs
index 2a0afc0010e..15e52846aec 100644
--- a/src/components/script/dom/document.rs
+++ b/src/components/script/dom/document.rs
@@ -12,6 +12,8 @@ use script_task::global_script_context;
use js::jsapi::{JS_AddObjectRoot, JS_RemoveObjectRoot};
use servo_util::tree::{TreeNodeRef, TreeUtils};
+use std::str::eq_slice;
+
pub struct Document {
root: AbstractNode<ScriptView>,
wrapper: WrapperCache,
@@ -52,6 +54,22 @@ impl Document {
Some(HTMLCollection::new(elements))
}
+ pub fn getElementsByName(&self, name: DOMString) -> Option<@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() => ()
+ }
+ }
+ }
+ };
+ Some(HTMLCollection::new(elements))
+ }
+
pub fn content_changed(&self) {
for self.window.iter().advance |window| {
window.content_changed()