diff --git a/src/ecmascript/mujs/customevent.c b/src/ecmascript/mujs/customevent.c index 1e2450ff..14be10d1 100644 --- a/src/ecmascript/mujs/customevent.c +++ b/src/ecmascript/mujs/customevent.c @@ -10,54 +10,87 @@ #include "elinks.h" +#include "document/document.h" +#include "document/view.h" #include "ecmascript/ecmascript.h" +#include "ecmascript/libdom/dom.h" #include "ecmascript/mujs.h" #include "ecmascript/mujs/customevent.h" #include "intl/charsets.h" #include "terminal/event.h" +#include "viewer/text/vs.h" static void mjs_customEvent_get_property_bubbles(js_State *J); static void mjs_customEvent_get_property_cancelable(js_State *J); -static void mjs_customEvent_get_property_composed(js_State *J); +//static void mjs_customEvent_get_property_composed(js_State *J); static void mjs_customEvent_get_property_defaultPrevented(js_State *J); static void mjs_customEvent_get_property_detail(js_State *J); static void mjs_customEvent_get_property_type(js_State *J); static void mjs_customEvent_preventDefault(js_State *J); -struct eljscustom_event { - const char *detail; - char *type_; - unsigned int bubbles:1; - unsigned int cancelable:1; - unsigned int composed:1; - unsigned int defaultPrevented:1; -}; - static void mjs_customEvent_finalizer(js_State *J, void *val) { - struct eljscustom_event *event = (struct eljscustom_event *)val; + dom_custom_event *event = (dom_custom_event *)val; if (event) { - if (event->detail) { - js_unref(J, event->detail); + const char *detail = NULL; + dom_exception exc = dom_custom_event_get_detail(event, &detail); + + if (detail) { + js_unref(J, detail); } - mem_free_if(event->type_); - mem_free(event); + dom_event_unref(event); } } void mjs_push_customEvent(js_State *J, char *type_) { - struct eljscustom_event *event = (struct eljscustom_event *)mem_calloc(1, sizeof(*event)); + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_getcontext(J); + struct view_state *vs = interpreter->vs; - if (!event) { + if (!vs) { + js_error(J, "error"); + return; + } + struct document_view *doc_view = vs->doc_view; + struct document *document = doc_view->document; + + if (!document->dom) { + js_error(J, "error"); + return; + } + dom_custom_event *event = NULL; + dom_string *CustomEventStr = NULL; + dom_exception exc = dom_string_create("CustomEvent", sizeof("CustomEvent") - 1, &CustomEventStr); + + if (exc != DOM_NO_ERR || !CustomEventStr) { + js_error(J, "error"); + return; + } + exc = dom_document_event_create_event(document->dom, CustomEventStr, &event); + dom_string_unref(CustomEventStr); + + if (exc != DOM_NO_ERR) { + js_error(J, "error"); + return; + } + dom_string *typ = NULL; + + if (type_) { + exc = dom_string_create((const uint8_t *)type_, strlen(type_), &typ); + } + if (exc != DOM_NO_ERR) { js_error(J, "out of memory"); return; } - event->type_ = null_or_stracpy(type_); + dom_custom_event_init_ns(event, NULL, typ, false, false, NULL); + + if (typ) { + dom_string_unref(typ); + } js_newobject(J); { @@ -65,7 +98,7 @@ mjs_push_customEvent(js_State *J, char *type_) addmethod(J, "preventDefault", mjs_customEvent_preventDefault, 0); addproperty(J, "bubbles", mjs_customEvent_get_property_bubbles, NULL); addproperty(J, "cancelable", mjs_customEvent_get_property_cancelable, NULL); - addproperty(J, "composed", mjs_customEvent_get_property_composed, NULL); +// addproperty(J, "composed", mjs_customEvent_get_property_composed, NULL); addproperty(J, "defaultPrevented", mjs_customEvent_get_property_defaultPrevented, NULL); addproperty(J, "detail", mjs_customEvent_get_property_detail, NULL); addproperty(J, "type", mjs_customEvent_get_property_type, NULL); @@ -78,13 +111,15 @@ mjs_customEvent_get_property_bubbles(js_State *J) #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif - struct eljscustom_event *event = (struct eljscustom_event *)js_touserdata(J, 0, "event"); + dom_custom_event *event = (dom_custom_event *)js_touserdata(J, 0, "event"); if (!event) { js_pushnull(J); return; } - js_pushboolean(J, event->bubbles); + bool bubbles = false; + dom_exception exc = dom_event_get_bubbles(event, &bubbles); + js_pushboolean(J, bubbles); } static void @@ -93,15 +128,18 @@ mjs_customEvent_get_property_cancelable(js_State *J) #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif - struct eljscustom_event *event = (struct eljscustom_event *)js_touserdata(J, 0, "event"); + dom_custom_event *event = (dom_custom_event *)js_touserdata(J, 0, "event"); if (!event) { js_pushnull(J); return; } - js_pushboolean(J, event->cancelable); + bool cancelable = false; + dom_exception exc = dom_event_get_cancelable(event, &cancelable); + js_pushboolean(J, cancelable); } +#if 0 static void mjs_customEvent_get_property_composed(js_State *J) { @@ -116,6 +154,7 @@ mjs_customEvent_get_property_composed(js_State *J) } js_pushboolean(J, event->composed); } +#endif static void mjs_customEvent_get_property_defaultPrevented(js_State *J) @@ -123,13 +162,15 @@ mjs_customEvent_get_property_defaultPrevented(js_State *J) #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif - struct eljscustom_event *event = (struct eljscustom_event *)js_touserdata(J, 0, "event"); + dom_custom_event *event = (dom_custom_event *)js_touserdata(J, 0, "event"); if (!event) { js_pushnull(J); return; } - js_pushboolean(J, event->defaultPrevented); + bool prevented = false; + dom_exception exc = dom_event_is_default_prevented(event, &prevented); + js_pushboolean(J, prevented); } static void @@ -138,13 +179,20 @@ mjs_customEvent_get_property_detail(js_State *J) #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif - struct eljscustom_event *event = (struct eljscustom_event *)js_touserdata(J, 0, "event"); + dom_custom_event *event = (dom_custom_event *)js_touserdata(J, 0, "event"); - if (!event || !event->detail) { + if (!event) { js_pushnull(J); return; } - js_getregistry(J, event->detail); + const char *detail = NULL; + dom_exception exc = dom_custom_event_get_detail(event, &detail); + + if (exc != DOM_NO_ERR || !detail) { + js_pushnull(J); + return; + } + js_getregistry(J, detail); } static void @@ -153,13 +201,21 @@ mjs_customEvent_get_property_type(js_State *J) #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif - struct eljscustom_event *event = (struct eljscustom_event *)js_touserdata(J, 0, "event"); + dom_custom_event *event = (dom_custom_event *)js_touserdata(J, 0, "event"); if (!event) { js_pushnull(J); return; } - js_pushstring(J, event->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 @@ -168,15 +224,13 @@ mjs_customEvent_preventDefault(js_State *J) #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif - struct eljscustom_event *event = (struct eljscustom_event *)js_touserdata(J, 0, "event"); + dom_custom_event *event = (dom_custom_event *)js_touserdata(J, 0, "event"); if (!event) { js_pushnull(J); return; } - if (event->cancelable) { - event->defaultPrevented = 1; - } + dom_event_prevent_default(event); js_pushundefined(J); } @@ -195,33 +249,69 @@ mjs_customEvent_constructor(js_State *J) #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif - struct eljscustom_event *event = (struct eljscustom_event *)mem_calloc(1, sizeof(*event)); - - if (!event) { + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_getcontext(J); + struct view_state *vs = interpreter->vs; + if (!vs) { + js_error(J, "error"); return; } - event->type_ = null_or_stracpy(js_tostring(J, 1)); + struct document_view *doc_view = vs->doc_view; + struct document *document = doc_view->document; + + if (!document->dom) { + js_error(J, "error"); + return; + } + dom_custom_event *event = NULL; + dom_string *CustomEventStr = NULL; + dom_exception exc = dom_string_create("CustomEvent", sizeof("CustomEvent") - 1, &CustomEventStr); + + if (exc != DOM_NO_ERR || !CustomEventStr) { + js_error(J, "error"); + return; + } + exc = dom_document_event_create_event(document->dom, CustomEventStr, &event); + dom_string_unref(CustomEventStr); + + if (exc != DOM_NO_ERR) { + js_error(J, "error"); + return; + } + dom_string *typ = NULL; + const char *tt = js_tostring(J, 1); + + if (!tt) { + js_error(J, "error"); + return; + } + exc = dom_string_create((const uint8_t *)tt, strlen(tt), &typ); + bool bubbles = false; + bool cancelable = false; + + const char *detail = NULL; js_getproperty(J, 2, "bubbles"); - event->bubbles = js_toboolean(J, -1); + bubbles = js_toboolean(J, -1); js_pop(J, 1); js_getproperty(J, 2, "cancelable"); - event->cancelable = js_toboolean(J, -1); + cancelable = js_toboolean(J, -1); js_pop(J, 1); - js_getproperty(J, 2, "composed"); - event->composed = js_toboolean(J, -1); - js_pop(J, 1); - if (js_hasproperty(J, 2, "detail")) { - event->detail = js_ref(J); - } + if (js_hasproperty(J, 2, "detail")) { + detail = js_ref(J); + } + exc = dom_custom_event_init_ns(event, NULL, typ, bubbles, cancelable, detail); + + if (typ) { + dom_string_unref(typ); + } js_newobject(J); { js_newuserdata(J, "event", event, mjs_customEvent_finalizer); addmethod(J, "preventDefault", mjs_customEvent_preventDefault, 0); addproperty(J, "bubbles", mjs_customEvent_get_property_bubbles, NULL); addproperty(J, "cancelable", mjs_customEvent_get_property_cancelable, NULL); - addproperty(J, "composed", mjs_customEvent_get_property_composed, NULL); +// addproperty(J, "composed", mjs_customEvent_get_property_composed, NULL); addproperty(J, "defaultPrevented", mjs_customEvent_get_property_defaultPrevented, NULL); addproperty(J, "detail", mjs_customEvent_get_property_detail, NULL); addproperty(J, "type", mjs_customEvent_get_property_type, NULL); diff --git a/src/ecmascript/quickjs/customevent.c b/src/ecmascript/quickjs/customevent.c index fa0cff98..489a35cd 100644 --- a/src/ecmascript/quickjs/customevent.c +++ b/src/ecmascript/quickjs/customevent.c @@ -273,7 +273,7 @@ js_customEvent_constructor(JSContext *ctx, JSValueConst new_target, int argc, JS bubbles = JS_ToBool(ctx, r); r = JS_GetPropertyStr(ctx, argv[1], "cancelable"); cancelable = JS_ToBool(ctx, r); - r = JS_GetPropertyStr(ctx, argv[1], "composed"); + //r = JS_GetPropertyStr(ctx, argv[1], "composed"); //composed = JS_ToBool(ctx, r); detail = calloc(1, sizeof(*detail));