Skip to content

Commit

Permalink
Add binding for Document (getElementsByName)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonwow committed Jul 25, 2013
1 parent fe91f6e commit f8f9d20
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/components/script/dom/bindings/document.rs
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions src/components/script/dom/document.rs
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit f8f9d20

Please sign in to comment.