mirror of
https://github.com/servo/servo.git
synced 2025-08-04 13:10:20 +01:00
auto merge of #620 : sonwow/servo/bindings, r=jdm
It's just a part of bingings for Document.
This commit is contained in:
commit
07267c634a
4 changed files with 62 additions and 2 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()
|
||||
|
|
|
@ -3,9 +3,9 @@
|
|||
<script src="test_bindings.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="first">fffff<br><br><br><br>fffffffffffffffff</div>
|
||||
<div id="first" name="test">fffff<br><br><br><br>fffffffffffffffff</div>
|
||||
<div id="second">ggg</div>
|
||||
<span id="third">hhhhhhhh</span>
|
||||
<span id="third" name="test">hhhhhhhh</span>
|
||||
<div id="fourth">iiiiiiiiiiiiiiiiiii</div>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue