1
0
Fork 0

Compare commits

...

4 Commits

Author SHA1 Message Date
Witold Filipczyk c45fa86605 [mujs] KeyboardEvent (libdom) 2024-05-11 14:24:27 +02:00
Witold Filipczyk 3e416135f1 [quickjs] KeyboardEvent (libdom) 2024-05-11 13:55:46 +02:00
Witold Filipczyk 078edf0966 [spideronkey] keyboardEvent.keyCode -> keyboardEvent.code 2024-05-11 13:45:18 +02:00
Witold Filipczyk 348bc937c0 [spidermonkey] KeyboardEvent (libdom)
It passes test, but there is a regression.
It will be addressed later.
2024-05-11 13:04:36 +02:00
5 changed files with 344 additions and 195 deletions

View File

@ -12,6 +12,7 @@ extern "C" {
#include <dom/bindings/hubbub/parser.h>
#include <dom/events/custom_event.h>
#include <dom/events/event.h>
#include <dom/events/keyboard_event.h>
#endif
#undef namespace

View File

@ -11,13 +11,14 @@
#include "elinks.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/libdom/dom.h"
#include "ecmascript/mujs.h"
#include "ecmascript/mujs/keyboard.h"
#include "intl/charsets.h"
#include "terminal/event.h"
static void mjs_keyboardEvent_get_property_code(js_State *J);
static void mjs_keyboardEvent_get_property_key(js_State *J);
static void mjs_keyboardEvent_get_property_keyCode(js_State *J);
static void mjs_keyboardEvent_get_property_bubbles(js_State *J);
static void mjs_keyboardEvent_get_property_cancelable(js_State *J);
@ -29,50 +30,52 @@ static void mjs_keyboardEvent_preventDefault(js_State *J);
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;
};
extern struct term_event last_event;
static void
mjs_keyboardEvent_finalizer(js_State *J, void *val)
{
struct keyboard *keyb = (struct keyboard *)val;
dom_keyboard_event *event = (dom_keyboard_event *)val;
if (keyb) {
mem_free_if(keyb->type_);
mem_free(keyb);
if (event) {
dom_event_unref(event);
}
}
void
mjs_push_keyboardEvent(js_State *J, struct term_event *ev, const char *type_)
{
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) {
js_error(J, "out of memory");
if (exc != DOM_NO_ERR) {
js_error(J, "error");
return;
}
keyCode = keyb->keyCode = ev ? get_kbd_key(ev) : 0;
keyb->type_ = null_or_stracpy(type_);
keyCode = ev ? get_kbd_key(ev) : 0;
dom_string *typ = NULL;
const char *t = js_tostring(J, 1);
if (t) {
exc = dom_string_create(t, strlen(t), &typ);
}
exc = dom_keyboard_event_init(event, typ, false, false,
NULL, NULL, NULL, DOM_KEY_LOCATION_STANDARD,
false, false, false, false,
false, false);
if (typ) dom_string_unref(typ);
js_newobject(J);
{
js_newuserdata(J, "event", keyb, mjs_keyboardEvent_finalizer);
js_newuserdata(J, "event", event, mjs_keyboardEvent_finalizer);
addmethod(J, "preventDefault", mjs_keyboardEvent_preventDefault, 0);
addproperty(J, "bubbles", mjs_keyboardEvent_get_property_bubbles, NULL);
addproperty(J, "cancelable", mjs_keyboardEvent_get_property_cancelable, NULL);
addproperty(J, "composed", mjs_keyboardEvent_get_property_composed, NULL);
addproperty(J, "code", mjs_keyboardEvent_get_property_code, NULL);
// addproperty(J, "composed", mjs_keyboardEvent_get_property_composed, NULL);
addproperty(J, "defaultPrevented", mjs_keyboardEvent_get_property_defaultPrevented, NULL);
addproperty(J, "key", mjs_keyboardEvent_get_property_key, NULL);
addproperty(J, "keyCode", mjs_keyboardEvent_get_property_keyCode, NULL);
addproperty(J, "type", mjs_keyboardEvent_get_property_type, NULL);
}
}
@ -83,13 +86,15 @@ mjs_keyboardEvent_get_property_bubbles(js_State *J)
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct keyboard *keyb = (struct keyboard *)js_touserdata(J, 0, "event");
dom_keyboard_event *event = (dom_keyboard_event *)js_touserdata(J, 0, "event");
if (!keyb) {
if (!event) {
js_pushnull(J);
return;
}
js_pushboolean(J, keyb->bubbles);
bool bubbles = false;
dom_exception exc = dom_event_get_bubbles(event, &bubbles);
js_pushboolean(J, bubbles);
}
static void
@ -98,15 +103,18 @@ mjs_keyboardEvent_get_property_cancelable(js_State *J)
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct keyboard *keyb = (struct keyboard *)js_touserdata(J, 0, "event");
dom_keyboard_event *event = (dom_keyboard_event *)js_touserdata(J, 0, "event");
if (!keyb) {
if (!event) {
js_pushnull(J);
return;
}
js_pushboolean(J, keyb->cancelable);
bool cancelable = false;
dom_exception exc = dom_event_get_cancelable(event, &cancelable);
js_pushboolean(J, cancelable);
}
#if 0
static void
mjs_keyboardEvent_get_property_composed(js_State *J)
{
@ -121,6 +129,7 @@ mjs_keyboardEvent_get_property_composed(js_State *J)
}
js_pushboolean(J, keyb->composed);
}
#endif
static void
mjs_keyboardEvent_get_property_defaultPrevented(js_State *J)
@ -128,13 +137,15 @@ mjs_keyboardEvent_get_property_defaultPrevented(js_State *J)
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct keyboard *keyb = (struct keyboard *)js_touserdata(J, 0, "event");
dom_keyboard_event *event = (dom_keyboard_event *)js_touserdata(J, 0, "event");
if (!keyb) {
if (!event) {
js_pushnull(J);
return;
}
js_pushboolean(J, keyb->defaultPrevented);
bool prevented = false;
dom_exception exc = dom_event_is_default_prevented(event, &prevented);
js_pushboolean(J, prevented);
}
static void
@ -143,37 +154,44 @@ mjs_keyboardEvent_get_property_key(js_State *J)
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct keyboard *keyb = (struct keyboard *)js_touserdata(J, 0, "event");
dom_keyboard_event *event = (dom_keyboard_event *)js_touserdata(J, 0, "event");
if (!keyb) {
if (!event) {
js_pushnull(J);
return;
}
char text[8] = {0};
dom_string *key = NULL;
dom_exception exc = dom_keyboard_event_get_key(event, &key);
*text = keyb->keyCode;
js_pushstring(J, text);
if (exc != DOM_NO_ERR || !key) {
js_pushnull(J);
return;
}
js_pushstring(J, dom_string_data(key));
dom_string_unref(key);
}
static void
mjs_keyboardEvent_get_property_keyCode(js_State *J)
mjs_keyboardEvent_get_property_code(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct keyboard *keyb = (struct keyboard *)js_touserdata(J, 0, "event");
unicode_val_T code;
dom_keyboard_event *event = (dom_keyboard_event *)js_touserdata(J, 0, "event");
if (!keyb) {
if (!event) {
js_pushnull(J);
return;
}
code = keyb->keyCode ?: get_kbd_key(&last_event);
dom_string *code = NULL;
dom_exception exc = dom_keyboard_event_get_code(event, &code);
if (code == KBD_ENTER) {
code = 13;
if (exc != DOM_NO_ERR || !code) {
js_pushnull(J);
return;
}
js_pushnumber(J, code);
js_pushstring(J, dom_string_data(code));
dom_string_unref(code);
}
static void
@ -182,13 +200,21 @@ mjs_keyboardEvent_get_property_type(js_State *J)
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct keyboard *keyb = (struct keyboard *)js_touserdata(J, 0, "event");
dom_keyboard_event *event = (dom_keyboard_event *)js_touserdata(J, 0, "event");
if (!keyb) {
if (!event) {
js_pushnull(J);
return;
}
js_pushstring(J, keyb->type_ ?: "");
dom_string *typ = NULL;
dom_exception exc = dom_event_get_type(event, &typ);
if (exc != DOM_NO_ERR || !typ) {
js_pushstring(J, "");
return;
}
js_pushstring(J, dom_string_data(typ));
dom_string_unref(typ);
}
static void
@ -197,15 +223,13 @@ mjs_keyboardEvent_preventDefault(js_State *J)
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct keyboard *keyb = (struct keyboard *)js_touserdata(J, 0, "event");
dom_keyboard_event *event = (dom_keyboard_event *)js_touserdata(J, 0, "event");
if (!keyb) {
if (!event) {
js_pushnull(J);
return;
}
if (keyb->cancelable) {
keyb->defaultPrevented = 1;
}
dom_event_prevent_default(event);
js_pushundefined(J);
}
@ -224,36 +248,65 @@ mjs_keyboardEvent_constructor(js_State *J)
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
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) {
js_error(J, "error");
return;
}
keyb->type_ = null_or_stracpy(js_tostring(J, 1));
dom_string *typ = NULL;
const char *t = js_tostring(J, 1);
if (t) {
exc = dom_string_create(t, strlen(t), &typ);
}
bool bubbles = false;
bool cancelable = false;
dom_string *key = NULL;
dom_string *code = NULL;
js_getproperty(J, 2, "bubbles");
keyb->bubbles = js_toboolean(J, -1);
bubbles = js_toboolean(J, -1);
js_pop(J, 1);
js_getproperty(J, 2, "cancelable");
keyb->cancelable = js_toboolean(J, -1);
cancelable = js_toboolean(J, -1);
js_pop(J, 1);
js_getproperty(J, 2, "composed");
keyb->composed = js_toboolean(J, -1);
js_getproperty(J, 2, "code");
const char *c = js_tostring(J, -1);
js_pop(J, 1);
js_getproperty(J, 2, "keyCode");
keyb->keyCode = js_toint32(J, -1);
if (c) {
exc = dom_string_create(c, strlen(c), &code);
}
js_getproperty(J, 2, "key");
const char *k = js_tostring(J, -1);
js_pop(J, 1);
if (k) {
exc = dom_string_create(k, strlen(k), &key);
}
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);
if (key) dom_string_unref(key);
if (code) dom_string_unref(code);
js_newobject(J);
{
js_newuserdata(J, "event", keyb, mjs_keyboardEvent_finalizer);
js_newuserdata(J, "event", event, mjs_keyboardEvent_finalizer);
addmethod(J, "preventDefault", mjs_keyboardEvent_preventDefault, 0);
addproperty(J, "bubbles", mjs_keyboardEvent_get_property_bubbles, NULL);
addproperty(J, "cancelable", mjs_keyboardEvent_get_property_cancelable, NULL);
addproperty(J, "composed", mjs_keyboardEvent_get_property_composed, NULL);
addproperty(J, "code", mjs_keyboardEvent_get_property_code, NULL);
// addproperty(J, "composed", mjs_keyboardEvent_get_property_composed, NULL);
addproperty(J, "defaultPrevented", mjs_keyboardEvent_get_property_defaultPrevented, NULL);
addproperty(J, "key", mjs_keyboardEvent_get_property_key, NULL);
addproperty(J, "keyCode", mjs_keyboardEvent_get_property_keyCode, NULL);
addproperty(J, "type", mjs_keyboardEvent_get_property_type, NULL);
}
}

View File

@ -11,6 +11,7 @@
#include "elinks.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/libdom/dom.h"
#include "ecmascript/quickjs.h"
#include "ecmascript/quickjs/keyboard.h"
#include "intl/charsets.h"
@ -20,8 +21,8 @@
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_keyCode(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);
@ -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 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
void js_keyboardEvent_finalizer(JSRuntime *rt, JSValue 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) {
mem_free_if(keyb->type_);
mem_free(keyb);
if (event) {
dom_event_unref(event);
}
}
@ -64,10 +54,10 @@ static JSClassDef js_keyboardEvent_class = {
static const JSCFunctionListEntry js_keyboardEvent_proto_funcs[] = {
JS_CGETSET_DEF("bubbles", js_keyboardEvent_get_property_bubbles, 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("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_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__);
#endif
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 (!keyb) {
if (!event) {
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);
}
@ -97,17 +88,18 @@ js_keyboardEvent_get_property_cancelable(JSContext *ctx, JSValueConst this_val)
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
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 (!keyb) {
if (!event) {
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);
}
#if 0
static JSValue
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);
}
#endif
static JSValue
js_keyboardEvent_get_property_defaultPrevented(JSContext *ctx, JSValueConst this_val)
@ -134,12 +127,14 @@ js_keyboardEvent_get_property_defaultPrevented(JSContext *ctx, JSValueConst this
#endif
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;
}
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);
}
@ -151,34 +146,45 @@ js_keyboardEvent_get_property_key(JSContext *ctx, JSValueConst this_val)
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
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 (!keyb) {
if (!event) {
return JS_NULL;
}
char text[8] = {0};
dom_string *key = NULL;
dom_exception exc = dom_keyboard_event_get_key(event, &key);
*text = keyb->keyCode;
JSValue r = JS_NewString(ctx, text);
if (exc != DOM_NO_ERR || !key) {
return JS_NULL;
}
JSValue r = JS_NewString(ctx, dom_string_data(key));
dom_string_unref(key);
RETURN_JS(r);
}
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
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
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 (!keyb) {
if (!event) {
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
@ -188,17 +194,20 @@ js_keyboardEvent_get_property_type(JSContext *ctx, JSValueConst this_val)
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
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 (!keyb) {
if (!event) {
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, "");
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);
}
@ -211,14 +220,13 @@ js_keyboardEvent_preventDefault(JSContext *ctx, JSValueConst this_val, int argc,
#endif
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;
}
if (keyb->cancelable) {
keyb->defaultPrevented = 1;
}
dom_event_prevent_default(event);
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);
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;
}
keyCode = get_kbd_key(ev);
@ -245,12 +254,17 @@ get_keyboardEvent(JSContext *ctx, struct term_event *ev)
if (keyCode == KBD_ENTER) {
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);
JS_SetPropertyFunctionList(ctx, keyb_obj, js_keyboardEvent_proto_funcs, countof(js_keyboardEvent_proto_funcs));
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);
RETURN_JS(rr);
@ -270,38 +284,60 @@ js_keyboardEvent_constructor(JSContext *ctx, JSValueConst new_target, int argc,
if (JS_IsException(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) {
return JS_NULL;
if (exc != DOM_NO_ERR) {
return JS_EXCEPTION;
}
dom_string *typ = NULL;
size_t len;
if (argc > 0) {
const char *str;
size_t len;
char *t = JS_ToCStringLen(ctx, &len, argv[0]);
str = JS_ToCStringLen(ctx, &len, argv[0]);
if (str) {
keyb->type_ = memacpy(str, len);
JS_FreeCString(ctx, str);
if (t) {
exc = dom_string_create(t, strlen(t), &typ);
JS_FreeCString(ctx, t);
}
}
bool bubbles = false;
bool cancelable = false;
dom_string *key = NULL;
dom_string *code = NULL;
if (argc > 1) {
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");
keyb->cancelable = JS_ToBool(ctx, r);
r = JS_GetPropertyStr(ctx, argv[1], "composed");
keyb->composed = JS_ToBool(ctx, r);
r = JS_GetPropertyStr(ctx, argv[1], "keyCode");
int keyCode;
if (JS_ToInt32(ctx, &keyCode, argv[1])) {
keyb->keyCode = keyCode;
cancelable = JS_ToBool(ctx, r);
r = JS_GetPropertyStr(ctx, argv[1], "key");
const char *k = JS_ToCStringLen(ctx, &len, r);
if (k) {
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);
if (key) dom_string_unref(key);
if (code) dom_string_unref(code);
JS_SetOpaque(obj, event);
return obj;
}

View File

@ -24,6 +24,7 @@
#include "document/forms.h"
#include "document/view.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/libdom/dom.h"
#include "ecmascript/spidermonkey.h"
#include "ecmascript/spidermonkey/heartbeat.h"
#include "ecmascript/spidermonkey/keyboard.h"
@ -59,12 +60,12 @@
#include <vector>
static bool keyboardEvent_get_property_code(JSContext *cx, unsigned int argc, JS::Value *vp);
static bool keyboardEvent_get_property_key(JSContext *cx, unsigned int argc, JS::Value *vp);
static bool keyboardEvent_get_property_keyCode(JSContext *cx, unsigned int argc, JS::Value *vp);
static bool keyboardEvent_get_property_bubbles(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool keyboardEvent_get_property_cancelable(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool keyboardEvent_get_property_composed(JSContext *ctx, unsigned int argc, JS::Value *vp);
//static bool keyboardEvent_get_property_composed(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool keyboardEvent_get_property_defaultPrevented(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool keyboardEvent_get_property_type(JSContext *ctx, unsigned int argc, JS::Value *vp);
@ -73,26 +74,16 @@ static bool keyboardEvent_preventDefault(JSContext *ctx, unsigned int argc, JS::
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 void
keyboardEvent_finalize(JS::GCContext *op, JSObject *keyb_obj)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct keyboard *keyb = JS::GetMaybePtrFromReservedSlot<struct keyboard>(keyb_obj, 0);
dom_keyboard_event *event = JS::GetMaybePtrFromReservedSlot<dom_keyboard_event>(keyb_obj, 0);
if (keyb) {
mem_free_if(keyb->type_);
mem_free(keyb);
if (event) {
dom_event_unref(&event);
}
}
@ -134,36 +125,66 @@ keyboardEvent_constructor(JSContext* ctx, unsigned argc, JS::Value* vp)
if (!newObj) {
return false;
}
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 false;
}
dom_string *typ = NULL;
if (argc > 0) {
keyb->type_ = jsval_to_string(ctx, args[0]);
char *t = jsval_to_string(ctx, args[0]);
if (t) {
exc = dom_string_create(t, strlen(t), &typ);
mem_free(t);
}
}
bool bubbles = false;
bool cancelable = false;
dom_string *key = NULL;
dom_string *code = NULL;
if (argc > 1) {
JS::RootedValue v(ctx);
JS::RootedObject v_obj(ctx, &args[1].toObject());
if (JS_GetProperty(ctx, v_obj, "bubbles", &v)) {
keyb->bubbles = (unsigned int)v.toBoolean();
bubbles = v.toBoolean();
}
if (JS_GetProperty(ctx, v_obj, "cancelable", &v)) {
keyb->cancelable = (unsigned int)v.toBoolean();
cancelable = v.toBoolean();
}
if (JS_GetProperty(ctx, v_obj, "composed", &v)) {
keyb->composed = (unsigned int)v.toBoolean();
// if (JS_GetProperty(ctx, v_obj, "composed", &v)) {
// keyb->composed = (unsigned int)v.toBoolean();
// }
if (JS_GetProperty(ctx, v_obj, "key", &v)) {
char *k = jsval_to_string(ctx, v);
if (k) {
exc = dom_string_create(k, strlen(k), &key);
mem_free(k);
}
}
if (JS_GetProperty(ctx, v_obj, "keyCode", &v)) {
keyb->keyCode = v.toInt32();
} else {
keyb->keyCode = 0;
if (JS_GetProperty(ctx, v_obj, "code", &v)) {
char *c = jsval_to_string(ctx, v);
if (c) {
exc = dom_string_create(c, strlen(c), &code);
mem_free(c);
}
}
}
JS::SetReservedSlot(newObj, 0, JS::PrivateValue(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);
if (key) dom_string_unref(key);
if (code) dom_string_unref(code);
JS::SetReservedSlot(newObj, 0, JS::PrivateValue(event));
args.rval().setObject(*newObj);
return true;
@ -172,10 +193,10 @@ keyboardEvent_constructor(JSContext* ctx, unsigned argc, JS::Value* vp)
JSPropertySpec keyboardEvent_props[] = {
JS_PSG("bubbles", keyboardEvent_get_property_bubbles, JSPROP_ENUMERATE),
JS_PSG("cancelable", keyboardEvent_get_property_cancelable, JSPROP_ENUMERATE),
JS_PSG("composed", keyboardEvent_get_property_composed, JSPROP_ENUMERATE),
JS_PSG("code", keyboardEvent_get_property_code, JSPROP_ENUMERATE),
// JS_PSG("composed", keyboardEvent_get_property_composed, JSPROP_ENUMERATE),
JS_PSG("defaultPrevented", keyboardEvent_get_property_defaultPrevented, JSPROP_ENUMERATE),
JS_PSG("key", keyboardEvent_get_property_key, JSPROP_ENUMERATE),
JS_PSG("keyCode", keyboardEvent_get_property_keyCode, JSPROP_ENUMERATE),
JS_PSG("type", keyboardEvent_get_property_type, JSPROP_ENUMERATE),
JS_PS_END
};
@ -201,12 +222,14 @@ keyboardEvent_get_property_bubbles(JSContext *ctx, unsigned int argc, JS::Value
#endif
return false;
}
struct keyboard *keyb = JS::GetMaybePtrFromReservedSlot<struct keyboard>(hobj, 0);
dom_keyboard_event *event = JS::GetMaybePtrFromReservedSlot<dom_keyboard_event>(hobj, 0);
if (!keyb) {
if (!event) {
return false;
}
args.rval().setBoolean(keyb->bubbles);
bool bubbles = false;
dom_exception exc = dom_event_get_bubbles(event, &bubbles);
args.rval().setBoolean(bubbles);
return true;
}
@ -227,16 +250,19 @@ keyboardEvent_get_property_cancelable(JSContext *ctx, unsigned int argc, JS::Val
#endif
return false;
}
struct keyboard *keyb = JS::GetMaybePtrFromReservedSlot<struct keyboard>(hobj, 0);
dom_keyboard_event *event = JS::GetMaybePtrFromReservedSlot<dom_keyboard_event>(hobj, 0);
if (!keyb) {
if (!event) {
return false;
}
args.rval().setBoolean(keyb->cancelable);
bool cancelable = false;
dom_exception exc = dom_event_get_cancelable(event, &cancelable);
args.rval().setBoolean(cancelable);
return true;
}
#if 0
static bool
keyboardEvent_get_property_composed(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
@ -262,6 +288,7 @@ keyboardEvent_get_property_composed(JSContext *ctx, unsigned int argc, JS::Value
return true;
}
#endif
static bool
keyboardEvent_get_property_defaultPrevented(JSContext *ctx, unsigned int argc, JS::Value *vp)
@ -279,12 +306,14 @@ keyboardEvent_get_property_defaultPrevented(JSContext *ctx, unsigned int argc, J
#endif
return false;
}
struct keyboard *keyb = JS::GetMaybePtrFromReservedSlot<struct keyboard>(hobj, 0);
dom_keyboard_event *event = JS::GetMaybePtrFromReservedSlot<dom_keyboard_event>(hobj, 0);
if (!keyb) {
if (!event) {
return false;
}
args.rval().setBoolean(keyb->defaultPrevented);
bool prevented = false;
dom_exception exc = dom_event_is_default_prevented(event, &prevented);
args.rval().setBoolean(prevented);
return true;
}
@ -305,21 +334,25 @@ keyboardEvent_get_property_key(JSContext *ctx, unsigned int argc, JS::Value *vp)
#endif
return false;
}
struct keyboard *keyb = JS::GetMaybePtrFromReservedSlot<struct keyboard>(hobj, 0);
dom_keyboard_event *event = JS::GetMaybePtrFromReservedSlot<dom_keyboard_event>(hobj, 0);
if (!keyb) {
if (!event) {
return false;
}
char text[8] = {0};
dom_string *key = NULL;
dom_exception exc = dom_keyboard_event_get_key(event, &key);
*text = keyb->keyCode;
args.rval().setString(JS_NewStringCopyZ(ctx, text));
if (exc != DOM_NO_ERR || !key) {
return false;
}
args.rval().setString(JS_NewStringCopyZ(ctx, dom_string_data(key)));
dom_string_unref(key);
return true;
}
static bool
keyboardEvent_get_property_keyCode(JSContext *ctx, unsigned int argc, JS::Value *vp)
keyboardEvent_get_property_code(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
@ -334,12 +367,19 @@ keyboardEvent_get_property_keyCode(JSContext *ctx, unsigned int argc, JS::Value
#endif
return false;
}
struct keyboard *keyb = JS::GetMaybePtrFromReservedSlot<struct keyboard>(hobj, 0);
dom_keyboard_event *event = JS::GetMaybePtrFromReservedSlot<dom_keyboard_event>(hobj, 0);
if (!keyb) {
if (!event) {
return false;
}
args.rval().setInt32(keyb->keyCode);
dom_string *code = NULL;
dom_exception exc = dom_keyboard_event_get_code(event, &code);
if (exc != DOM_NO_ERR || !code) {
return false;
}
args.rval().setString(JS_NewStringCopyZ(ctx, dom_string_data(code)));
dom_string_unref(code);
return true;
}
@ -360,17 +400,20 @@ keyboardEvent_get_property_type(JSContext *ctx, unsigned int argc, JS::Value *vp
#endif
return false;
}
struct keyboard *keyb = JS::GetMaybePtrFromReservedSlot<struct keyboard>(hobj, 0);
dom_keyboard_event *keyb = JS::GetMaybePtrFromReservedSlot<dom_keyboard_event>(hobj, 0);
if (!keyb) {
return false;
}
dom_string *typ = NULL;
dom_exception exc = dom_event_get_type(keyb, &typ);
if (!keyb->type_) {
if (exc != DOM_NO_ERR || !typ) {
args.rval().setString(JS_NewStringCopyZ(ctx, ""));
return true;
}
args.rval().setString(JS_NewStringCopyZ(ctx, keyb->type_));
args.rval().setString(JS_NewStringCopyZ(ctx, dom_string_data(typ)));
dom_string_unref(typ);
return true;
}
@ -391,14 +434,12 @@ keyboardEvent_preventDefault(JSContext *ctx, unsigned int argc, JS::Value *vp)
#endif
return false;
}
struct keyboard *keyb = JS::GetMaybePtrFromReservedSlot<struct keyboard>(hobj, 0);
dom_keyboard_event *event = JS::GetMaybePtrFromReservedSlot<dom_keyboard_event>(hobj, 0);
if (!keyb) {
if (!event) {
return false;
}
if (keyb->cancelable) {
keyb->defaultPrevented = 1;
}
dom_event_prevent_default(event);
args.rval().setUndefined();
return true;
@ -416,9 +457,10 @@ get_keyboardEvent(JSContext *ctx, struct term_event *ev)
JS::RootedObject r_keyb(ctx, k);
JS_DefineProperties(ctx, r_keyb, (JSPropertySpec *) keyboardEvent_props);
struct keyboard *keyb = (struct keyboard *)mem_calloc(1, sizeof(*keyb));
dom_keyboard_event *keyb = NULL;
dom_exception exc = dom_keyboard_event_create(&keyb);
if (!keyb) {
if (exc != DOM_NO_ERR) {
return NULL;
}
keyCode = get_kbd_key(ev);
@ -426,7 +468,12 @@ get_keyboardEvent(JSContext *ctx, struct term_event *ev)
if (keyCode == KBD_ENTER) {
keyCode = 13;
}
keyb->keyCode = keyCode;
exc = dom_keyboard_event_init(keyb, NULL, false, false,
NULL, NULL, NULL, DOM_KEY_LOCATION_STANDARD,
false, false, false, false,
false, false);
// keyb->keyCode = keyCode;
JS::SetReservedSlot(k, 0, JS::PrivateValue(keyb));
return k;

View File

@ -0,0 +1,12 @@
<script>
var e = new KeyboardEvent('message', { cancelable: true, key: "e" });
console.error('keyboardEvent.html');
console.assert(e.cancelable, 'cancelable true');
console.assert(!e.defaultPrevented, 'false');
e.preventDefault();
console.assert(e.defaultPrevented, 'true');
console.assert(e.type === 'message', 'message');
console.assert(e.key === 'e', 'key e');
console.exit(0);
</script>