diff --git a/src/ecmascript/mujs/Makefile b/src/ecmascript/mujs/Makefile index 17a6c4507..b46a9c9a5 100644 --- a/src/ecmascript/mujs/Makefile +++ b/src/ecmascript/mujs/Makefile @@ -1,7 +1,7 @@ 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 \ +OBJS = attr.o attributes.o collection.o console.o css.o customevent.o dataset.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 nodelist2.o screen.o style.o tokenlist.o unibar.o url.o window.o xhr.o include $(top_srcdir)/Makefile.lib diff --git a/src/ecmascript/mujs/dataset.c b/src/ecmascript/mujs/dataset.c new file mode 100644 index 000000000..a26105f5c --- /dev/null +++ b/src/ecmascript/mujs/dataset.c @@ -0,0 +1,170 @@ +/* The MuJS dataset object implementation. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#ifdef CONFIG_LIBDOM +#include +#include +#endif + +#include "elinks.h" + +#include "document/libdom/corestrings.h" +#include "ecmascript/ecmascript.h" +#include "ecmascript/mujs/mapa.h" +#include "ecmascript/mujs.h" +#include "ecmascript/mujs/dataset.h" +#include "ecmascript/mujs/element.h" + +static void +mjs_dataset_finalizer(js_State *J, void *node) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + dom_node *el = (dom_node *)(node); + + if (el) { + dom_node_unref(el); + } +} + +static int +mjs_obj_dataset_has(js_State *J, void *p, const char *property) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + if (!property) { + return 0; + } + dom_node *el = (dom_node *)p; + struct string data; + + if (!el ||!init_string(&data)) { + return 0; + } + add_to_string(&data, "data-"); + add_to_string(&data, property); + + dom_string *attr_name = NULL; + dom_exception exc = dom_string_create(data.source, data.length, &attr_name); + done_string(&data); + + if (exc != DOM_NO_ERR || !attr_name) { + return 0; + } + dom_string *attr_value = NULL; + exc = dom_element_get_attribute(el, attr_name, &attr_value); + dom_string_unref(attr_name); + + if (exc != DOM_NO_ERR || !attr_value) { + return 0; + } + js_pushstring(J, dom_string_data(attr_value)); + dom_string_unref(attr_value); + + return 1; +} + +static int +mjs_obj_dataset_put(js_State *J, void *p, const char *property) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_getcontext(J); + + if (!property) { + return 0; + } + const char *value = js_tostring(J, -1); + + if (!value) { + return 0; + } + dom_node *el = (dom_node *)p; + struct string data; + + if (!el ||!init_string(&data)) { + return 0; + } + add_to_string(&data, "data-"); + add_to_string(&data, property); + + dom_string *attr_name = NULL; + dom_exception exc = dom_string_create(data.source, data.length, &attr_name); + done_string(&data); + + if (exc != DOM_NO_ERR || !attr_name) { + return 0; + } + dom_string *attr_value = NULL; + exc = dom_string_create(value, strlen(value), &attr_value); + + if (exc != DOM_NO_ERR || !attr_value) { + dom_string_unref(attr_name); + return 0; + } + exc = dom_element_set_attribute(el, attr_name, attr_value); + dom_string_unref(attr_name); + dom_string_unref(attr_value); + interpreter->changed = true; + + return 1; +} + +static int +mjs_obj_dataset_del(js_State *J, void *p, const char *property) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_getcontext(J); + + if (!property) { + return 0; + } + dom_node *el = (dom_node *)p; + struct string data; + + if (!el ||!init_string(&data)) { + return 0; + } + add_to_string(&data, "data-"); + add_to_string(&data, property); + + dom_string *attr_name = NULL; + dom_exception exc = dom_string_create(data.source, data.length, &attr_name); + done_string(&data); + + if (exc != DOM_NO_ERR || !attr_name) { + return 0; + } + dom_string *attr_value = NULL; + exc = dom_element_remove_attribute(el, attr_name); + dom_string_unref(attr_name); + interpreter->changed = true; + + return 1; +} + +void +mjs_push_dataset(js_State *J, void *node) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + dom_node_ref(node); + + js_newobject(J); + { + js_newuserdatax(J, "dataset", node, mjs_obj_dataset_has, mjs_obj_dataset_put, mjs_obj_dataset_del, mjs_dataset_finalizer); + } +} diff --git a/src/ecmascript/mujs/dataset.h b/src/ecmascript/mujs/dataset.h new file mode 100644 index 000000000..5c657b155 --- /dev/null +++ b/src/ecmascript/mujs/dataset.h @@ -0,0 +1,16 @@ +#ifndef EL__ECMASCRIPT_MUJS_DATASET_H +#define EL__ECMASCRIPT_MUJS_DATASET_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void mjs_push_dataset(js_State *J, void *node); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/ecmascript/mujs/element.c b/src/ecmascript/mujs/element.c index 85daf563c..85ef3d220 100644 --- a/src/ecmascript/mujs/element.c +++ b/src/ecmascript/mujs/element.c @@ -35,6 +35,7 @@ #include "ecmascript/mujs/attr.h" #include "ecmascript/mujs/attributes.h" #include "ecmascript/mujs/collection.h" +#include "ecmascript/mujs/dataset.h" #include "ecmascript/mujs/document.h" #include "ecmascript/mujs/domrect.h" #include "ecmascript/mujs/element.h" @@ -450,6 +451,21 @@ mjs_element_get_property_clientWidth(js_State *J) } #endif +static void +mjs_element_get_property_dataset(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + dom_element *el = (dom_element *)(mjs_getprivate(J, 0)); + + if (!el) { + js_pushnull(J); + return; + } + mjs_push_dataset(J, el); +} + static void mjs_element_get_property_dir(js_State *J) { @@ -3286,6 +3302,7 @@ mjs_push_element(js_State *J, void *node) // addproperty(J, "clientLeft", mjs_element_get_property_clientLeft, NULL); // addproperty(J, "clientTop", mjs_element_get_property_clientTop, NULL); // addproperty(J, "clientWidth", mjs_element_get_property_clientWidth, NULL); + addproperty(J, "dataset", mjs_element_get_property_dataset, NULL); addproperty(J, "dir", mjs_element_get_property_dir, mjs_element_set_property_dir); addproperty(J, "firstChild", mjs_element_get_property_firstChild, NULL); addproperty(J, "firstElementChild", mjs_element_get_property_firstElementChild, NULL); diff --git a/src/ecmascript/mujs/meson.build b/src/ecmascript/mujs/meson.build index 4ee661dc0..b1d0d7926 100644 --- a/src/ecmascript/mujs/meson.build +++ b/src/ecmascript/mujs/meson.build @@ -1,2 +1,3 @@ -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', +srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'css.c', 'customevent.c', 'dataset.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', 'nodelist2.c', 'screen.c', 'style.c', 'tokenlist.c', 'unibar.c', 'url.c', 'window.c', 'xhr.c')