1
0
mirror of https://github.com/rkd77/elinks.git synced 2025-02-02 15:09:23 -05:00

[quickjs] KeyboardEvent (libdom)

This commit is contained in:
Witold Filipczyk 2024-05-11 13:55:46 +02:00
parent 078edf0966
commit 3e416135f1
2 changed files with 110 additions and 73 deletions

View File

@ -11,6 +11,7 @@
#include "elinks.h" #include "elinks.h"
#include "ecmascript/ecmascript.h" #include "ecmascript/ecmascript.h"
#include "ecmascript/libdom/dom.h"
#include "ecmascript/quickjs.h" #include "ecmascript/quickjs.h"
#include "ecmascript/quickjs/keyboard.h" #include "ecmascript/quickjs/keyboard.h"
#include "intl/charsets.h" #include "intl/charsets.h"
@ -20,8 +21,8 @@
static JSClassID js_keyboardEvent_class_id; static JSClassID js_keyboardEvent_class_id;
static JSValue js_keyboardEvent_get_property_code(JSContext *ctx, JSValueConst this_val);
static JSValue js_keyboardEvent_get_property_key(JSContext *ctx, JSValueConst this_val); static JSValue js_keyboardEvent_get_property_key(JSContext *ctx, JSValueConst this_val);
static JSValue js_keyboardEvent_get_property_keyCode(JSContext *ctx, JSValueConst this_val);
static JSValue js_keyboardEvent_get_property_bubbles(JSContext *ctx, JSValueConst this_val); static JSValue js_keyboardEvent_get_property_bubbles(JSContext *ctx, JSValueConst this_val);
static JSValue js_keyboardEvent_get_property_cancelable(JSContext *ctx, JSValueConst this_val); static JSValue js_keyboardEvent_get_property_cancelable(JSContext *ctx, JSValueConst this_val);
@ -31,28 +32,17 @@ static JSValue js_keyboardEvent_get_property_type(JSContext *ctx, JSValueConst t
static JSValue js_keyboardEvent_preventDefault(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv); static JSValue js_keyboardEvent_preventDefault(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
static unicode_val_T keyCode; static unicode_val_T keyCode;
struct keyboard {
unicode_val_T keyCode;
char *type_;
unsigned int bubbles:1;
unsigned int cancelable:1;
unsigned int composed:1;
unsigned int defaultPrevented:1;
};
static static
void js_keyboardEvent_finalizer(JSRuntime *rt, JSValue val) void js_keyboardEvent_finalizer(JSRuntime *rt, JSValue val)
{ {
REF_JS(val); REF_JS(val);
struct keyboard *keyb = (struct keyboard *)JS_GetOpaque(val, js_keyboardEvent_class_id); dom_keyboard_event *event = (dom_keyboard_event *)JS_GetOpaque(val, js_keyboardEvent_class_id);
if (keyb) { if (event) {
mem_free_if(keyb->type_); dom_event_unref(event);
mem_free(keyb);
} }
} }
@ -64,10 +54,10 @@ static JSClassDef js_keyboardEvent_class = {
static const JSCFunctionListEntry js_keyboardEvent_proto_funcs[] = { static const JSCFunctionListEntry js_keyboardEvent_proto_funcs[] = {
JS_CGETSET_DEF("bubbles", js_keyboardEvent_get_property_bubbles, NULL), JS_CGETSET_DEF("bubbles", js_keyboardEvent_get_property_bubbles, NULL),
JS_CGETSET_DEF("cancelable", js_keyboardEvent_get_property_cancelable, NULL), JS_CGETSET_DEF("cancelable", js_keyboardEvent_get_property_cancelable, NULL),
JS_CGETSET_DEF("composed", js_keyboardEvent_get_property_composed, NULL), JS_CGETSET_DEF("code", js_keyboardEvent_get_property_code, NULL),
// JS_CGETSET_DEF("composed", js_keyboardEvent_get_property_composed, NULL),
JS_CGETSET_DEF("defaultPrevented", js_keyboardEvent_get_property_defaultPrevented, NULL), JS_CGETSET_DEF("defaultPrevented", js_keyboardEvent_get_property_defaultPrevented, NULL),
JS_CGETSET_DEF("key", js_keyboardEvent_get_property_key, NULL), JS_CGETSET_DEF("key", js_keyboardEvent_get_property_key, NULL),
JS_CGETSET_DEF("keyCode", js_keyboardEvent_get_property_keyCode, NULL),
JS_CGETSET_DEF("type", js_keyboardEvent_get_property_type, NULL), JS_CGETSET_DEF("type", js_keyboardEvent_get_property_type, NULL),
JS_CFUNC_DEF("preventDefault", 0, js_keyboardEvent_preventDefault) JS_CFUNC_DEF("preventDefault", 0, js_keyboardEvent_preventDefault)
}; };
@ -79,13 +69,14 @@ js_keyboardEvent_get_property_bubbles(JSContext *ctx, JSValueConst this_val)
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif #endif
REF_JS(this_val); REF_JS(this_val);
dom_keyboard_event *event = (dom_keyboard_event *)(JS_GetOpaque(this_val, js_keyboardEvent_class_id));
struct keyboard *keyb = (struct keyboard *)(JS_GetOpaque(this_val, js_keyboardEvent_class_id)); if (!event) {
if (!keyb) {
return JS_NULL; return JS_NULL;
} }
JSValue r = JS_NewBool(ctx, keyb->bubbles); bool bubbles = false;
dom_exception exc = dom_event_get_bubbles(event, &bubbles);
JSValue r = JS_NewBool(ctx, bubbles);
RETURN_JS(r); RETURN_JS(r);
} }
@ -97,17 +88,18 @@ js_keyboardEvent_get_property_cancelable(JSContext *ctx, JSValueConst this_val)
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif #endif
REF_JS(this_val); REF_JS(this_val);
dom_keyboard_event *event = (dom_keyboard_event *)(JS_GetOpaque(this_val, js_keyboardEvent_class_id));
struct keyboard *keyb = (struct keyboard *)(JS_GetOpaque(this_val, js_keyboardEvent_class_id)); if (!event) {
if (!keyb) {
return JS_NULL; return JS_NULL;
} }
JSValue r = JS_NewBool(ctx, keyb->cancelable); bool cancelable = false;
dom_exception exc = dom_event_get_cancelable(event, &cancelable);
JSValue r = JS_NewBool(ctx, cancelable);
RETURN_JS(r); RETURN_JS(r);
} }
#if 0
static JSValue static JSValue
js_keyboardEvent_get_property_composed(JSContext *ctx, JSValueConst this_val) js_keyboardEvent_get_property_composed(JSContext *ctx, JSValueConst this_val)
{ {
@ -125,6 +117,7 @@ js_keyboardEvent_get_property_composed(JSContext *ctx, JSValueConst this_val)
RETURN_JS(r); RETURN_JS(r);
} }
#endif
static JSValue static JSValue
js_keyboardEvent_get_property_defaultPrevented(JSContext *ctx, JSValueConst this_val) js_keyboardEvent_get_property_defaultPrevented(JSContext *ctx, JSValueConst this_val)
@ -134,12 +127,14 @@ js_keyboardEvent_get_property_defaultPrevented(JSContext *ctx, JSValueConst this
#endif #endif
REF_JS(this_val); REF_JS(this_val);
struct keyboard *keyb = (struct keyboard *)(JS_GetOpaque(this_val, js_keyboardEvent_class_id)); dom_keyboard_event *event = (dom_keyboard_event *)(JS_GetOpaque(this_val, js_keyboardEvent_class_id));
if (!keyb) { if (!event) {
return JS_NULL; return JS_NULL;
} }
JSValue r = JS_NewBool(ctx, keyb->defaultPrevented); bool prevented = false;
dom_exception exc = dom_event_is_default_prevented(event, &prevented);
JSValue r = JS_NewBool(ctx, prevented);
RETURN_JS(r); RETURN_JS(r);
} }
@ -151,34 +146,45 @@ js_keyboardEvent_get_property_key(JSContext *ctx, JSValueConst this_val)
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif #endif
REF_JS(this_val); REF_JS(this_val);
struct dom_keyboard_event *event = (dom_keyboard_event *)(JS_GetOpaque(this_val, js_keyboardEvent_class_id));
struct keyboard *keyb = (struct keyboard *)(JS_GetOpaque(this_val, js_keyboardEvent_class_id)); if (!event) {
if (!keyb) {
return JS_NULL; return JS_NULL;
} }
char text[8] = {0}; dom_string *key = NULL;
dom_exception exc = dom_keyboard_event_get_key(event, &key);
*text = keyb->keyCode; if (exc != DOM_NO_ERR || !key) {
JSValue r = JS_NewString(ctx, text); return JS_NULL;
}
JSValue r = JS_NewString(ctx, dom_string_data(key));
dom_string_unref(key);
RETURN_JS(r); RETURN_JS(r);
} }
static JSValue static JSValue
js_keyboardEvent_get_property_keyCode(JSContext *ctx, JSValueConst this_val) js_keyboardEvent_get_property_code(JSContext *ctx, JSValueConst this_val)
{ {
#ifdef ECMASCRIPT_DEBUG #ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif #endif
REF_JS(this_val); REF_JS(this_val);
struct dom_keyboard_event *event = (dom_keyboard_event *)(JS_GetOpaque(this_val, js_keyboardEvent_class_id));
struct keyboard *keyb = (struct keyboard *)(JS_GetOpaque(this_val, js_keyboardEvent_class_id)); if (!event) {
if (!keyb) {
return JS_NULL; return JS_NULL;
} }
return JS_NewUint32(ctx, keyb->keyCode); dom_string *code = NULL;
dom_exception exc = dom_keyboard_event_get_code(event, &code);
if (exc != DOM_NO_ERR || !code) {
return JS_NULL;
}
JSValue r = JS_NewString(ctx, dom_string_data(code));
dom_string_unref(code);
RETURN_JS(r);
} }
static JSValue static JSValue
@ -188,17 +194,20 @@ js_keyboardEvent_get_property_type(JSContext *ctx, JSValueConst this_val)
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif #endif
REF_JS(this_val); REF_JS(this_val);
struct dom_keyboard_event *event = (dom_keyboard_event *)(JS_GetOpaque(this_val, js_keyboardEvent_class_id));
struct keyboard *keyb = (struct keyboard *)(JS_GetOpaque(this_val, js_keyboardEvent_class_id)); if (!event) {
if (!keyb) {
return JS_NULL; return JS_NULL;
} }
if (!keyb->type_) { dom_string *typ = NULL;
dom_exception exc = dom_event_get_type(event, &typ);
if (exc != DOM_NO_ERR || !typ) {
JSValue r = JS_NewString(ctx, ""); JSValue r = JS_NewString(ctx, "");
RETURN_JS(r); RETURN_JS(r);
} }
JSValue r = JS_NewString(ctx, keyb->type_); JSValue r = JS_NewString(ctx, dom_string_data(typ));
dom_string_unref(typ);
RETURN_JS(r); RETURN_JS(r);
} }
@ -211,14 +220,13 @@ js_keyboardEvent_preventDefault(JSContext *ctx, JSValueConst this_val, int argc,
#endif #endif
REF_JS(this_val); REF_JS(this_val);
struct keyboard *keyb = (struct keyboard *)(JS_GetOpaque(this_val, js_keyboardEvent_class_id)); dom_keyboard_event *event = (dom_keyboard_event *)(JS_GetOpaque(this_val, js_keyboardEvent_class_id));
if (!keyb) { if (!event) {
return JS_NULL; return JS_NULL;
} }
if (keyb->cancelable) { dom_event_prevent_default(event);
keyb->defaultPrevented = 1;
}
return JS_UNDEFINED; return JS_UNDEFINED;
} }
@ -235,9 +243,10 @@ get_keyboardEvent(JSContext *ctx, struct term_event *ev)
JS_NewClass(JS_GetRuntime(ctx), js_keyboardEvent_class_id, &js_keyboardEvent_class); JS_NewClass(JS_GetRuntime(ctx), js_keyboardEvent_class_id, &js_keyboardEvent_class);
initialized = 1; initialized = 1;
} }
struct keyboard *keyb = (struct keyboard *)mem_calloc(1, sizeof(*keyb)); dom_keyboard_event *event = NULL;
dom_exception exc = dom_keyboard_event_create(&event);
if (!keyb) { if (exc != DOM_NO_ERR) {
return JS_NULL; return JS_NULL;
} }
keyCode = get_kbd_key(ev); keyCode = get_kbd_key(ev);
@ -245,12 +254,17 @@ get_keyboardEvent(JSContext *ctx, struct term_event *ev)
if (keyCode == KBD_ENTER) { if (keyCode == KBD_ENTER) {
keyCode = 13; keyCode = 13;
} }
keyb->keyCode = keyCode; // keyb->keyCode = keyCode;
exc = dom_keyboard_event_init(event, NULL, false, false,
NULL, NULL, NULL, DOM_KEY_LOCATION_STANDARD,
false, false, false, false,
false, false);
JSValue keyb_obj = JS_NewObjectClass(ctx, js_keyboardEvent_class_id); JSValue keyb_obj = JS_NewObjectClass(ctx, js_keyboardEvent_class_id);
JS_SetPropertyFunctionList(ctx, keyb_obj, js_keyboardEvent_proto_funcs, countof(js_keyboardEvent_proto_funcs)); JS_SetPropertyFunctionList(ctx, keyb_obj, js_keyboardEvent_proto_funcs, countof(js_keyboardEvent_proto_funcs));
JS_SetClassProto(ctx, js_keyboardEvent_class_id, keyb_obj); JS_SetClassProto(ctx, js_keyboardEvent_class_id, keyb_obj);
JS_SetOpaque(keyb_obj, keyb); JS_SetOpaque(keyb_obj, event);
JSValue rr = JS_DupValue(ctx, keyb_obj); JSValue rr = JS_DupValue(ctx, keyb_obj);
RETURN_JS(rr); RETURN_JS(rr);
@ -270,38 +284,58 @@ js_keyboardEvent_constructor(JSContext *ctx, JSValueConst new_target, int argc,
if (JS_IsException(obj)) { if (JS_IsException(obj)) {
return obj; return obj;
} }
struct keyboard *keyb = (struct keyboard *)mem_calloc(1, sizeof(*keyb)); dom_keyboard_event *event = NULL;
dom_exception exc = dom_keyboard_event_create(&event);
if (!keyb) { if (exc != DOM_NO_ERR) {
return JS_NULL; return JS_EXCEPTION;
} }
dom_string *typ = NULL;
size_t len;
if (argc > 0) { if (argc > 0) {
const char *str; char *t = JS_ToCStringLen(ctx, &len, argv[0]);
size_t len;
str = JS_ToCStringLen(ctx, &len, argv[0]); if (t) {
exc = dom_string_create(t, strlen(t), &typ);
if (str) { JS_FreeCString(ctx, t);
keyb->type_ = memacpy(str, len);
JS_FreeCString(ctx, str);
} }
} }
bool bubbles = false;
bool cancelable = false;
dom_string *key = NULL;
dom_string *code = NULL;
if (argc > 1) { if (argc > 1) {
JSValue r = JS_GetPropertyStr(ctx, argv[1], "bubbles"); JSValue r = JS_GetPropertyStr(ctx, argv[1], "bubbles");
keyb->bubbles = JS_ToBool(ctx, r); bubbles = JS_ToBool(ctx, r);
r = JS_GetPropertyStr(ctx, argv[1], "cancelable"); r = JS_GetPropertyStr(ctx, argv[1], "cancelable");
keyb->cancelable = JS_ToBool(ctx, r); cancelable = JS_ToBool(ctx, r);
r = JS_GetPropertyStr(ctx, argv[1], "composed");
keyb->composed = JS_ToBool(ctx, r); r = JS_GetPropertyStr(ctx, argv[1], "key");
r = JS_GetPropertyStr(ctx, argv[1], "keyCode"); const char *k = JS_ToCStringLen(ctx, &len, r);
int keyCode;
if (JS_ToInt32(ctx, &keyCode, argv[1])) { if (k) {
keyb->keyCode = keyCode; exc = dom_string_create(k, strlen(k), &key);
JS_FreeCString(ctx, k);
JS_FreeValue(ctx, r);
}
r = JS_GetPropertyStr(ctx, argv[1], "code");
const char *c = JS_ToCStringLen(ctx, &len, r);
if (c) {
exc = dom_string_create(c, strlen(c), &code);
JS_FreeCString(ctx, c);
JS_FreeValue(ctx, r);
} }
} }
JS_SetOpaque(obj, keyb); exc = dom_keyboard_event_init(event, typ, bubbles, cancelable, NULL/*view*/,
key, code, DOM_KEY_LOCATION_STANDARD,
false, false, false,
false, false, false);
if (typ) dom_string_unref(typ);
JS_SetOpaque(obj, event);
return obj; return obj;
} }

View File

@ -180,6 +180,9 @@ keyboardEvent_constructor(JSContext* ctx, unsigned argc, JS::Value* vp)
key, code, DOM_KEY_LOCATION_STANDARD, key, code, DOM_KEY_LOCATION_STANDARD,
false, false, false, false, false, false,
false, false, false); false, false, false);
if (typ) {
dom_string_unref(typ);
}
JS::SetReservedSlot(newObj, 0, JS::PrivateValue(event)); JS::SetReservedSlot(newObj, 0, JS::PrivateValue(event));
args.rval().setObject(*newObj); args.rval().setObject(*newObj);