mirror of
https://github.com/servo/servo.git
synced 2025-06-21 15:49:04 +01:00
Add binding for Document (getElementsByName)
This commit is contained in:
parent
fe91f6e238
commit
f8f9d203f5
2 changed files with 52 additions and 0 deletions
|
@ -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,
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue