diff --git a/src/ecmascript/mujs/Makefile b/src/ecmascript/mujs/Makefile index d253328ea..0b730c8cf 100644 --- a/src/ecmascript/mujs/Makefile +++ b/src/ecmascript/mujs/Makefile @@ -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 diff --git a/src/ecmascript/mujs/document.c b/src/ecmascript/mujs/document.c index 31d1a1f1c..e233a6317 100644 --- a/src/ecmascript/mujs/document.c +++ b/src/ecmascript/mujs/document.c @@ -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); - dom_node_unref(doc_root); + 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) { + dom_node_unref(doc_root); 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 diff --git a/src/ecmascript/mujs/element.c b/src/ecmascript/mujs/element.c index 97e862f6f..f250bc126 100644 --- a/src/ecmascript/mujs/element.c +++ b/src/ecmascript/mujs/element.c @@ -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 diff --git a/src/ecmascript/mujs/meson.build b/src/ecmascript/mujs/meson.build index 44f33984d..d8a6c1514 100644 --- a/src/ecmascript/mujs/meson.build +++ b/src/ecmascript/mujs/meson.build @@ -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') diff --git a/src/ecmascript/mujs/nodelist2.c b/src/ecmascript/mujs/nodelist2.c new file mode 100644 index 000000000..3d7c46a2c --- /dev/null +++ b/src/ecmascript/mujs/nodelist2.c @@ -0,0 +1,135 @@ +/* The MuJS nodeList2 object implementation. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#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); +} diff --git a/src/ecmascript/mujs/nodelist2.h b/src/ecmascript/mujs/nodelist2.h new file mode 100644 index 000000000..9f1fa4288 --- /dev/null +++ b/src/ecmascript/mujs/nodelist2.h @@ -0,0 +1,16 @@ +#ifndef EL__ECMASCRIPT_MUJS_NODELIST2_H +#define EL__ECMASCRIPT_MUJS_NODELIST2_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void mjs_push_nodelist2(js_State *J, void *nodes); + +#ifdef __cplusplus +} +#endif + +#endif