From 20ab63c1d16fb9a57d13e616fc118a5dcca7d6fd Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Sun, 12 May 2024 13:17:15 +0200 Subject: [PATCH] [spidermonkey] KeyboardEvent.keyCode (Enter only) Only Enter is handled. Strings are not "finalized". --- src/document/libdom/doc.c | 159 ++++++++++++++++++++++ src/document/libdom/doc.h | 1 + src/ecmascript/spidermonkey/keyboard.cpp | 36 +++++ test/ecmascript/assert/keyboardEvent.html | 5 +- 4 files changed, 199 insertions(+), 2 deletions(-) diff --git a/src/document/libdom/doc.c b/src/document/libdom/doc.c index 7e58b1f73..f07ccd5c1 100644 --- a/src/document/libdom/doc.c +++ b/src/document/libdom/doc.c @@ -211,3 +211,162 @@ convert_key_to_dom_string(term_event_key_T key, dom_string **res) *res = dom_key; return true; } + +static const char *__keys_names[] = { + "Enter", + "ArrowLeft", + "ArrowRight", + "ArrowUp", + "ArrowDown", + "PageUp", + "PageDown", + "Home", + "End", + "Escape", + "Backspace", + "Tab", + "Insert", + "Delete", + "F1", + "F1", + "F2", + "F3", + "F4", + "F5", + "F6", + "F7", + "F8", + "F9", + "F10", + "F11", + "F12", + NULL +}; + +static enum { + KEYB_ENTER, + KEYB_ARROW_LEFT, + KEYB_ARROW_RIGHT, + KEYB_ARROW_UP, + KEYB_ARROW_DOWN, + KEYB_PAGE_UP, + KEYB_PAGE_DOWN, + KEYB_HOME, + KEYB_END, + KEYB_ESCAPE, + KEYB_TAB, + KEYB_INSERT, + KEYB_DELETE, + KEYB_F1, + KEYB_F2, + KEYB_F3, + KEYB_F4, + KEYB_F5, + KEYB_F6, + KEYB_F7, + KEYB_F8, + KEYB_F9, + KEYB_F10, + KEYB_F11, + KEYB_F12, + KEYB_COUNT +}; + +static lwc_string *keyb_lwc[KEYB_COUNT]; + +static void +initialize_keyb(void) +{ + int i; + + for (i = 0; i < KEYB_COUNT; i++) { + dom_exception err = lwc_intern_string(__keys_names[i], strlen(__keys_names[i]), &keyb_lwc[i]); + + if (err != lwc_error_ok) { + return; + //return _dom_exception_from_lwc_error(err); + } + } +} + +static void +finalize_keyb(void) +{ + int i; + + for (i = 0; i < KEYB_COUNT; i++) { + if (keyb_lwc[i] != NULL) { + lwc_string_unref(keyb_lwc[i]); + } + } +} + +unicode_val_T +convert_dom_string_to_keycode(dom_string *dom_key) +{ + static int initialized = 0; + + if (!initialized) { + initialize_keyb(); + initialized = 1; + } + if (!dom_key) { + return 0; + } + + int et = -1; + lwc_string *t = NULL; + dom_exception err; + + int i; + err = dom_string_intern(dom_key, &t); + + if (err != DOM_NO_ERR) { + return 0; + } + assert(t != NULL); + + for (i = 0; i < KEYB_COUNT; i++) { + if (keyb_lwc[i] == t) { + et = i; + break; + } + } + lwc_string_unref(t); + + switch (et) { + case KEYB_ENTER: + return 13; + case KEYB_ARROW_LEFT: + case KEYB_ARROW_RIGHT: + case KEYB_ARROW_UP: + case KEYB_ARROW_DOWN: + case KEYB_PAGE_UP: + case KEYB_PAGE_DOWN: + case KEYB_HOME: + case KEYB_END: + case KEYB_ESCAPE: + case KEYB_TAB: + case KEYB_INSERT: + case KEYB_DELETE: + case KEYB_F1: + case KEYB_F2: + case KEYB_F3: + case KEYB_F4: + case KEYB_F5: + case KEYB_F6: + case KEYB_F7: + case KEYB_F8: + case KEYB_F9: + case KEYB_F10: + case KEYB_F11: + case KEYB_F12: + return 0; + default: + { + char *utf8 = dom_string_data(dom_key); + char *end = utf8 + dom_string_length(dom_key); + return utf8_to_unicode(&utf8, end); + } + } +} diff --git a/src/document/libdom/doc.h b/src/document/libdom/doc.h index d56e8b88f..f46cce2c7 100644 --- a/src/document/libdom/doc.h +++ b/src/document/libdom/doc.h @@ -17,6 +17,7 @@ void free_document(void *doc); void *el_match_selector(const char *selector, void *node); void add_lowercase_to_string(struct string *buf, const char *str, int len); bool convert_key_to_dom_string(term_event_key_T key, dom_string **res); +unicode_val_T convert_dom_string_to_keycode(dom_string *dom_key); #ifdef __cplusplus } diff --git a/src/ecmascript/spidermonkey/keyboard.cpp b/src/ecmascript/spidermonkey/keyboard.cpp index de7a6d1f8..bf784e202 100644 --- a/src/ecmascript/spidermonkey/keyboard.cpp +++ b/src/ecmascript/spidermonkey/keyboard.cpp @@ -64,6 +64,7 @@ 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); @@ -199,6 +200,7 @@ JSPropertySpec keyboardEvent_props[] = { // 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 }; @@ -353,6 +355,40 @@ keyboardEvent_get_property_key(JSContext *ctx, unsigned int argc, JS::Value *vp) return true; } +static bool +keyboardEvent_get_property_keyCode(JSContext *ctx, unsigned int argc, JS::Value *vp) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + JS::RootedObject hobj(ctx, &args.thisv().toObject()); + JS::Realm *comp = js::GetContextRealm(ctx); + + if (!comp) { +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__); +#endif + return false; + } + dom_keyboard_event *event = JS::GetMaybePtrFromReservedSlot(hobj, 0); + + if (!event) { + return false; + } + dom_string *key = NULL; + dom_exception exc = dom_keyboard_event_get_key(event, &key); + + if (exc != DOM_NO_ERR) { + return false; + } + unicode_val_T keyCode = convert_dom_string_to_keycode(key); + args.rval().setInt32(keyCode); + if (key) dom_string_unref(key); + + return true; +} + static bool keyboardEvent_get_property_code(JSContext *ctx, unsigned int argc, JS::Value *vp) { diff --git a/test/ecmascript/assert/keyboardEvent.html b/test/ecmascript/assert/keyboardEvent.html index 3b57b16bd..07ff4768f 100644 --- a/test/ecmascript/assert/keyboardEvent.html +++ b/test/ecmascript/assert/keyboardEvent.html @@ -1,5 +1,5 @@