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

[quickjs] element.dataset

This commit is contained in:
Witold Filipczyk 2024-06-28 20:57:01 +02:00
parent 3756faf0eb
commit 26571aeac2
6 changed files with 281 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 heartbeat.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 heartbeat.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 urlsearchparams.o window.o xhr.o
include $(top_srcdir)/Makefile.lib

View File

@ -0,0 +1,245 @@
/* The QuickJS 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/quickjs/mapa.h"
#include "ecmascript/quickjs.h"
#include "ecmascript/quickjs/dataset.h"
#include "ecmascript/quickjs/element.h"
#define countof(x) (sizeof(x) / sizeof((x)[0]))
JSClassID js_dataset_class_id;
static void
js_dataset_finalizer(JSRuntime *rt, JSValue val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(val);
dom_node *el = (dom_node *)(JS_GetOpaque(val, js_dataset_class_id));
if (el) {
dom_node_unref(el);
}
}
/* return < 0 if exception, or TRUE/FALSE */
static int
js_obj_delete_property(JSContext *ctx, JSValueConst obj, JSAtom prop)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx);
const char *property = JS_AtomToCString(ctx, prop);
if (!property) {
return 0;
}
dom_node *el = (dom_node *)(JS_GetOpaque(obj, js_dataset_class_id));
struct string data;
if (!el ||!init_string(&data)) {
JS_FreeCString(ctx, property);
return 0;
}
add_to_string(&data, "data-");
add_to_string(&data, property);
JS_FreeCString(ctx, 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;
}
/* The following methods can be emulated with the previous ones,
so they are usually not needed */
/* return < 0 if exception or TRUE/FALSE */
static int
js_obj_has_property(JSContext *ctx, JSValueConst obj, JSAtom atom)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
fprintf(stderr, "has_property\n");
return 1;
}
static JSValue
js_obj_get_property(JSContext *ctx, JSValueConst obj, JSAtom atom, JSValueConst receiver)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
const char *property = JS_AtomToCString(ctx, atom);
if (!property) {
return JS_UNDEFINED;
}
dom_node *el = (dom_node *)(JS_GetOpaque(obj, js_dataset_class_id));
struct string data;
if (!el ||!init_string(&data)) {
JS_FreeCString(ctx, property);
return JS_UNDEFINED;
}
add_to_string(&data, "data-");
add_to_string(&data, property);
JS_FreeCString(ctx, 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 JS_UNDEFINED;
}
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 JS_UNDEFINED;
}
JSValue ret = JS_NewStringLen(ctx, dom_string_data(attr_value), dom_string_length(attr_value));
dom_string_unref(attr_value);
RETURN_JS(ret);
}
/* return < 0 if exception or TRUE/FALSE */
static int
js_obj_set_property(JSContext *ctx, JSValueConst obj, JSAtom atom, JSValueConst val, JSValueConst receiver, int flags)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx);
const char *property = JS_AtomToCString(ctx, atom);
if (!property) {
return 0;
}
size_t len;
const char *value = JS_ToCStringLen(ctx, &len, val);
if (!value) {
JS_FreeCString(ctx, property);
return 0;
}
dom_node *el = (dom_node *)(JS_GetOpaque(obj, js_dataset_class_id));
struct string data;
if (!el ||!init_string(&data)) {
JS_FreeCString(ctx, property);
JS_FreeCString(ctx, value);
return 0;
}
add_to_string(&data, "data-");
add_to_string(&data, property);
JS_FreeCString(ctx, 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) {
JS_FreeCString(ctx, value);
return 0;
}
dom_string *attr_value = NULL;
exc = dom_string_create(value, strlen(value), &attr_value);
JS_FreeCString(ctx, 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 JSValue
js_dataset_toString(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
return JS_NewString(ctx, "[dataset object]");
}
static const JSCFunctionListEntry js_dataset_proto_funcs[] = {
JS_CFUNC_DEF("toString", 0, js_dataset_toString)
};
static JSClassExoticMethods exo = {
.delete_property = js_obj_delete_property,
// .has_property = js_obj_has_property,
.get_property = js_obj_get_property,
.set_property = js_obj_set_property
};
static JSClassDef js_dataset_class = {
"dataset",
.finalizer = js_dataset_finalizer,
.exotic = &exo
};
JSValue
getDataset(JSContext *ctx, void *node)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
/* nodelist class */
static int initialized;
if (!initialized) {
JS_NewClassID(&js_dataset_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_dataset_class_id, &js_dataset_class);
initialized = 1;
}
JSValue proto = JS_NewObjectClass(ctx, js_dataset_class_id);
REF_JS(proto);
JS_SetPropertyFunctionList(ctx, proto, js_dataset_proto_funcs, countof(js_dataset_proto_funcs));
JS_SetClassProto(ctx, js_dataset_class_id, proto);
dom_node_ref(node);
JS_SetOpaque(proto, node);
JSValue rr = JS_DupValue(ctx, proto);
RETURN_JS(rr);
}

View File

@ -0,0 +1,16 @@
#ifndef EL__ECMASCRIPT_QUICKJS_DATASET_H
#define EL__ECMASCRIPT_QUICKJS_DATASET_H
#include <quickjs/quickjs.h>
#ifdef __cplusplus
extern "C" {
#endif
JSValue getDataset(JSContext *ctx, void *node);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -29,6 +29,7 @@
#include "ecmascript/quickjs/attr.h"
#include "ecmascript/quickjs/attributes.h"
#include "ecmascript/quickjs/collection.h"
#include "ecmascript/quickjs/dataset.h"
#include "ecmascript/quickjs/domrect.h"
#include "ecmascript/quickjs/element.h"
#include "ecmascript/quickjs/event.h"
@ -533,6 +534,21 @@ js_element_get_property_clientWidth(JSContext *ctx, JSValueConst this_val)
}
#endif
static JSValue
js_element_get_property_dataset(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
dom_element *el = (dom_element *)(js_getopaque(this_val, js_element_class_id));
if (!el) {
return JS_NULL;
}
return getDataset(ctx, el);
}
static JSValue
js_element_get_property_dir(JSContext *ctx, JSValueConst this_val)
{
@ -3664,6 +3680,7 @@ static const JSCFunctionListEntry js_element_proto_funcs[] = {
// JS_CGETSET_DEF("clientLeft", js_element_get_property_clientLeft, NULL),
// JS_CGETSET_DEF("clientTop", js_element_get_property_clientTop, NULL),
// JS_CGETSET_DEF("clientWidth", js_element_get_property_clientWidth, NULL),
JS_CGETSET_DEF("dataset", js_element_get_property_dataset, NULL),
JS_CGETSET_DEF("dir", js_element_get_property_dir, js_element_set_property_dir),
JS_CGETSET_DEF("firstChild", js_element_get_property_firstChild, NULL),
JS_CGETSET_DEF("firstElementChild", js_element_get_property_firstElementChild, NULL),

View File

@ -1,3 +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', 'heartbeat.c', 'history.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', 'heartbeat.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', 'urlsearchparams.c', 'window.c', 'xhr.c')

View File

@ -159,6 +159,7 @@ dataset_obj_setProperty(JSContext* ctx, JS::HandleObject obj, JS::HandleId id, J
done_string(&data);
if (exc != DOM_NO_ERR || !attr_name) {
mem_free(value);
return true;
}
dom_string *attr_value = NULL;