From 0d28694b455e8dfc6906e641e0406bcb250001c3 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Sun, 10 Mar 2024 18:11:44 +0100 Subject: [PATCH] [mujs] event.preventDefault --- src/ecmascript/mujs/event.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/ecmascript/mujs/event.c b/src/ecmascript/mujs/event.c index f67da38de..2950d0a9a 100644 --- a/src/ecmascript/mujs/event.c +++ b/src/ecmascript/mujs/event.c @@ -22,6 +22,8 @@ static void mjs_event_get_property_composed(js_State *J); static void mjs_event_get_property_defaultPrevented(js_State *J); static void mjs_event_get_property_type(js_State *J); +static void mjs_event_preventDefault(js_State *J); + struct eljs_event { char *type_; unsigned int bubbles:1; @@ -55,6 +57,7 @@ mjs_push_event(js_State *J, char *type_) js_newobject(J); { js_newuserdata(J, "event", event, mjs_event_finalizer); + addmethod(J, "preventDefault", mjs_event_preventDefault, 0); addproperty(J, "bubbles", mjs_event_get_property_bubbles, NULL); addproperty(J, "cancelable", mjs_event_get_property_cancelable, NULL); addproperty(J, "composed", mjs_event_get_property_composed, NULL); @@ -138,6 +141,24 @@ mjs_event_get_property_type(js_State *J) js_pushstring(J, event->type_ ?: ""); } +static void +mjs_event_preventDefault(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct eljs_event *event = (struct eljs_event *)js_touserdata(J, 0, "event"); + + if (!event) { + js_pushnull(J); + return; + } + if (event->cancelable) { + event->defaultPrevented = 1; + } + js_pushundefined(J); +} + static void mjs_event_fun(js_State *J) { @@ -163,6 +184,7 @@ mjs_event_constructor(js_State *J) js_newobject(J); { js_newuserdata(J, "event", event, mjs_event_finalizer); + addmethod(J, "preventDefault", mjs_event_preventDefault, 0); addproperty(J, "bubbles", mjs_event_get_property_bubbles, NULL); addproperty(J, "cancelable", mjs_event_get_property_cancelable, NULL); addproperty(J, "composed", mjs_event_get_property_composed, NULL);