1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

[quickjs] nodelist.forEach

This commit is contained in:
Witold Filipczyk 2024-05-07 17:24:13 +02:00
parent 3d92071eba
commit 786469da5f
2 changed files with 64 additions and 9 deletions

View File

@ -24,6 +24,8 @@
#define countof(x) (sizeof(x) / sizeof((x)[0]))
static JSClassID js_nodelist_class_id;
void *map_nodelist;
void *map_rev_nodelist;
@ -50,6 +52,30 @@ js_nodeList_SetOpaque(JSValueConst this_val, void *node)
}
}
static JSValue
js_nodeList_get_property_length(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
dom_nodelist *nl = (dom_nodelist *)(js_nodeList_GetOpaque(this_val));
dom_exception err;
uint32_t size;
if (!nl) {
return JS_NewInt32(ctx, 0);
}
err = dom_nodelist_get_length(nl, &size);
if (err != DOM_NO_ERR) {
return JS_NewInt32(ctx, 0);
}
return JS_NewInt32(ctx, size);
}
static JSValue
js_nodeList_item2(JSContext *ctx, JSValueConst this_val, int idx)
{
@ -146,27 +172,36 @@ js_nodeList_toString(JSContext *ctx, JSValueConst this_val, int argc, JSValueCon
}
static const JSCFunctionListEntry js_nodeList_proto_funcs[] = {
// JS_CGETSET_DEF("length", js_nodeList_get_property_length, nullptr),
// JS_CGETSET_DEF("length", js_nodeList_get_property_length, NULL),
JS_CFUNC_DEF("item", 1, js_nodeList_item),
JS_CFUNC_DEF("toString", 0, js_nodeList_toString)
};
static JSClassDef js_nodelist_class = {
"nodelist",
};
JSValue
getNodeList(JSContext *ctx, void *node)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
JSValue nodeList_obj = JS_NewArray(ctx);
JS_SetPropertyFunctionList(ctx, nodeList_obj, js_nodeList_proto_funcs, countof(js_nodeList_proto_funcs));
JSValue proto;
attr_save_in_map(map_nodelist, node, nodeList_obj);
js_nodeList_SetOpaque(nodeList_obj, node);
js_nodeList_set_items(ctx, nodeList_obj, node);
/* nodelist class */
JS_NewClassID(&js_nodelist_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_nodelist_class_id, &js_nodelist_class);
proto = JS_NewArray(ctx);
REF_JS(proto);
return nodeList_obj;
JS_SetPropertyFunctionList(ctx, proto, js_nodeList_proto_funcs, countof(js_nodeList_proto_funcs));
JS_SetClassProto(ctx, js_nodelist_class_id, proto);
// JSValue rr = JS_DupValue(ctx, nodeList_obj);
attr_save_in_map(map_nodelist, node, proto);
js_nodeList_SetOpaque(proto, node);
js_nodeList_set_items(ctx, proto, node);
// RETURN_JS(rr);
JSValue rr = JS_DupValue(ctx, proto);
RETURN_JS(rr);
}

View File

@ -0,0 +1,20 @@
<ul id="birds">
<li>Orange-winged parrot</li>
<li class="endangered">Philippine eagle</li>
<li>Great white pelican</li>
</ul>
<script type="text/javascript">
console.error('nodelist.forEach.html');
var birds = document.querySelectorAll('li');
var counter = 0;
birds.forEach(function(b) {
counter++;
});
console.assert(counter === 3, 'Three');
console.exit(0);
</script>