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

[mujs] nodelist2 for querySelectorAll

This commit is contained in:
Witold Filipczyk 2024-06-24 17:46:39 +02:00
parent 68ff18eca4
commit 9a68ae3596
6 changed files with 167 additions and 17 deletions

View File

@ -2,6 +2,6 @@ top_builddir=../../..
include $(top_builddir)/Makefile.config
OBJS = attr.o attributes.o collection.o console.o css.o customevent.o document.o domrect.o element.o event.o form.o forms.o history.o implementation.o input.o \
keyboard.o localstorage.o location.o mapa.o message.o navigator.o nodelist.o screen.o style.o unibar.o url.o window.o xhr.o
keyboard.o localstorage.o location.o mapa.o message.o navigator.o nodelist.o nodelist2.o screen.o style.o unibar.o url.o window.o xhr.o
include $(top_srcdir)/Makefile.lib

View File

@ -41,6 +41,7 @@
#include "ecmascript/mujs/document.h"
#include "ecmascript/mujs/element.h"
#include "ecmascript/mujs/nodelist.h"
#include "ecmascript/mujs/nodelist2.h"
#include "ecmascript/mujs/window.h"
#include "intl/libintl.h"
#include "main/select.h"
@ -1435,18 +1436,17 @@ mjs_document_querySelectorAll(js_State *J)
js_pushnull(J);
return;
}
walk_tree_query_append((dom_node *)element, doc_root, selector, 0);
LIST_OF(struct selector_node) *result_list = (LIST_OF(struct selector_node) *)mem_calloc(1, sizeof(*result_list));
if (!result_list) {
dom_node_unref(doc_root);
dom_nodelist *nodes = NULL;
exc = dom_node_get_child_nodes(element, &nodes);
dom_node_unref(element);
if (exc != DOM_NO_ERR || !nodes) {
js_pushnull(J);
return;
}
mjs_push_nodelist(J, nodes);
init_list(*result_list);
walk_tree_query_append((dom_node *)element, doc_root, selector, 0, result_list);
dom_node_unref(doc_root);
mjs_push_nodelist2(J, result_list);
}
static void

View File

@ -41,6 +41,7 @@
#include "ecmascript/mujs/event.h"
#include "ecmascript/mujs/keyboard.h"
#include "ecmascript/mujs/nodelist.h"
#include "ecmascript/mujs/nodelist2.h"
#include "ecmascript/mujs/style.h"
#include "ecmascript/mujs/window.h"
#include "intl/libintl.h"
@ -2987,17 +2988,15 @@ mjs_element_querySelectorAll(js_State *J)
js_pushnull(J);
return;
}
walk_tree_query_append((dom_node *)element, el, selector, 0);
LIST_OF(struct selector_node) *result_list = (LIST_OF(struct selector_node) *)mem_calloc(1, sizeof(*result_list));
dom_nodelist *nodes = NULL;
exc = dom_node_get_child_nodes(element, &nodes);
dom_node_unref(element);
if (exc != DOM_NO_ERR || !nodes) {
if (!result_list) {
js_pushnull(J);
return;
}
mjs_push_nodelist(J, nodes);
init_list(*result_list);
walk_tree_query_append((dom_node *)element, el, selector, 0, result_list);
mjs_push_nodelist2(J, result_list);
}
static void

View File

@ -1,2 +1,2 @@
srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'css.c', 'customevent.c', 'document.c', 'domrect.c', 'element.c', 'event.c', 'form.c', 'forms.c', 'history.c', 'implementation.c', 'input.c', 'keyboard.c',
'localstorage.c', 'location.c', 'mapa.c', 'message.c', 'navigator.c', 'nodelist.c', 'screen.c', 'style.c', 'unibar.c', 'url.c', 'window.c', 'xhr.c')
'localstorage.c', 'location.c', 'mapa.c', 'message.c', 'navigator.c', 'nodelist.c', 'nodelist2.c', 'screen.c', 'style.c', 'unibar.c', 'url.c', 'window.c', 'xhr.c')

View File

@ -0,0 +1,135 @@
/* The MuJS nodeList2 object implementation. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "elinks.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/ecmascript-c.h"
#include "ecmascript/mujs/mapa.h"
#include "ecmascript/mujs.h"
#include "ecmascript/mujs/element.h"
#include "ecmascript/mujs/nodelist2.h"
#include "ecmascript/mujs/window.h"
void *map_nodelist2;
void *map_rev_nodelist2;
static void
mjs_push_nodeList2_item2(js_State *J, int idx)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
LIST_OF(struct selector_node) *sni = (LIST_OF(struct selector_node) *)(js_touserdata(J, 0, "nodelist2"));
dom_node *element = NULL;
dom_exception err;
if (!sni) {
js_pushundefined(J);
return;
}
int counter = 0;
struct selector_node *sn = NULL;
foreach (sn, *sni) {
if (counter == idx) {
break;
}
counter++;
}
if (!sn || !sn->node) {
js_pushnull(J);
return;
}
mjs_push_element(J, sn->node);
}
static void
mjs_nodeList2_item(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
int index = js_toint32(J, 1);
mjs_push_nodeList2_item2(J, index);
}
static void
mjs_nodeList2_set_items(js_State *J, void *nodes)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
LIST_OF(struct selector_node) *sni = (LIST_OF(struct selector_node) *)nodes;
uint32_t i = 0;
if (!sni) {
return;
}
struct selector_node *sn = NULL;
foreach (sn, *sni) {
mjs_push_element(J, sn->node);
js_setindex(J, -2, i);
i++;
}
js_setlength(J, -1, i);
}
static void
mjs_nodeList2_toString(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
js_pushstring(J, "[nodeList2 object]");
}
static void
mjs_nodeList2_finalizer(js_State *J, void *node)
{
attr_erase_from_map(map_nodelist2, node);
}
static void
mjs_nodeList2_get_property_length(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
LIST_OF(struct selector_node) *sni = (LIST_OF(struct selector_node) *)(js_touserdata(J, 0, "nodelist2"));
uint32_t length;
if (!sni) {
js_pushnumber(J, 0);
return;
}
length = list_size(sni);
js_pushnumber(J, length);
}
void
mjs_push_nodelist2(js_State *J, void *nodes)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
js_newarray(J);
{
js_newuserdata(J, "nodelist2", nodes, mjs_nodeList2_finalizer);
// addproperty(J, "length", mjs_nodeList_get_property_length, NULL);
addmethod(J, "item", mjs_nodeList2_item, 1);
addmethod(J, "toString", mjs_nodeList2_toString, 0);
mjs_nodeList2_set_items(J, nodes);
}
attr_save_in_map(map_nodelist2, nodes, nodes);
}

View File

@ -0,0 +1,16 @@
#ifndef EL__ECMASCRIPT_MUJS_NODELIST2_H
#define EL__ECMASCRIPT_MUJS_NODELIST2_H
#include <mujs.h>
#ifdef __cplusplus
extern "C" {
#endif
void mjs_push_nodelist2(js_State *J, void *nodes);
#ifdef __cplusplus
}
#endif
#endif