From f8f9d203f5f662945b2c30a50cc4ff74c049fe5c Mon Sep 17 00:00:00 2001 From: Youngsoo Son Date: Thu, 25 Jul 2013 13:51:36 +0900 Subject: [PATCH 1/2] Add binding for Document (getElementsByName) --- .../script/dom/bindings/document.rs | 34 +++++++++++++++++++ src/components/script/dom/document.rs | 18 ++++++++++ 2 files changed, 52 insertions(+) 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 { //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, 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() From 65d2ea2ffdb07a29729ccca2cb9adb2b7671c02d Mon Sep 17 00:00:00 2001 From: Youngsoo Son Date: Thu, 25 Jul 2013 13:57:21 +0900 Subject: [PATCH 2/2] Add a test for getElementsByName --- src/test/html/test_bindings.html | 4 ++-- src/test/html/test_bindings.js | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/test/html/test_bindings.html b/src/test/html/test_bindings.html index 1d9fd6de5a0..e93f363387c 100644 --- a/src/test/html/test_bindings.html +++ b/src/test/html/test_bindings.html @@ -3,9 +3,9 @@ -
fffff



fffffffffffffffff
+
fffff



fffffffffffffffff
ggg
- hhhhhhhh + hhhhhhhh
iiiiiiiiiiiiiiiiiii
diff --git a/src/test/html/test_bindings.js b/src/test/html/test_bindings.js index 35bbfd4c772..7fe369536b9 100644 --- a/src/test/html/test_bindings.js +++ b/src/test/html/test_bindings.js @@ -43,6 +43,14 @@ window.alert(tags[0].getClientRects()); window.alert(tags[1]); window.alert(tags[2]); window.alert(tags[3]); +let tags = document.getElementsByName("test"); +window.alert(tags); +window.alert(tags.length); +window.alert(tags[0]); +window.alert(tags[0].tagName); +window.alert(tags[1]); +window.alert(tags[1].tagName); +window.alert(tags[2]); window.alert("DOMParser:"); window.alert(DOMParser);