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

[mujs] element.dataset

This commit is contained in:
Witold Filipczyk 2024-06-29 08:51:59 +02:00
parent 26571aeac2
commit e1ab592abf
5 changed files with 206 additions and 2 deletions

View File

@ -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

View File

@ -0,0 +1,170 @@
/* The MuJS dataset object implementation. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef CONFIG_LIBDOM
#include <dom/dom.h>
#include <dom/bindings/hubbub/parser.h>
#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);
}
}

View File

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

View File

@ -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);

View File

@ -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')