1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

[mujs] Event (libdom)

This commit is contained in:
Witold Filipczyk 2024-05-09 16:36:26 +02:00
parent 1fc9cc8803
commit 5779cd170b

View File

@ -11,6 +11,7 @@
#include "elinks.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/libdom/dom.h"
#include "ecmascript/mujs.h"
#include "ecmascript/mujs/event.h"
#include "intl/charsets.h"
@ -18,49 +19,46 @@
static void mjs_event_get_property_bubbles(js_State *J);
static void mjs_event_get_property_cancelable(js_State *J);
static void mjs_event_get_property_composed(js_State *J);
//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;
unsigned int cancelable:1;
unsigned int composed:1;
unsigned int defaultPrevented:1;
};
static void
mjs_event_finalizer(js_State *J, void *val)
{
struct eljs_event *event = (struct eljs_event *)val;
dom_event *event = (dom_event *)val;
if (event) {
mem_free_if(event->type_);
mem_free(event);
dom_event_unref(event);
}
}
void
mjs_push_event(js_State *J, char *type_)
{
struct eljs_event *event = (struct eljs_event *)mem_calloc(1, sizeof(*event));
dom_event *event = NULL;
dom_exception exc = dom_event_create(&event);
if (!event) {
if (exc != DOM_NO_ERR) {
js_error(J, "out of memory");
return;
}
event->type_ = null_or_stracpy(type_);
if (type_) {
dom_string *typ = NULL;
exc = dom_string_create(type_, strlen(type_), &typ);
dom_event_init(event, typ, false, false);
if (typ) dom_string_unref(typ);
}
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);
// addproperty(J, "composed", mjs_event_get_property_composed, NULL);
addproperty(J, "defaultPrevented", mjs_event_get_property_defaultPrevented, NULL);
addproperty(J, "type", mjs_event_get_property_type, NULL);
}
@ -72,13 +70,15 @@ mjs_event_get_property_bubbles(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");
dom_event *event = (dom_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
@ -87,15 +87,18 @@ mjs_event_get_property_cancelable(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");
dom_event *event = (dom_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_event_get_property_composed(js_State *J)
{
@ -110,6 +113,7 @@ mjs_event_get_property_composed(js_State *J)
}
js_pushboolean(J, event->composed);
}
#endif
static void
mjs_event_get_property_defaultPrevented(js_State *J)
@ -117,13 +121,15 @@ mjs_event_get_property_defaultPrevented(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");
dom_event *event = (dom_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
@ -132,13 +138,21 @@ mjs_event_get_property_type(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");
dom_event *event = (dom_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
@ -147,15 +161,13 @@ 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");
dom_event *event = (dom_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);
}
@ -174,22 +186,28 @@ mjs_event_constructor(js_State *J)
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct eljs_event *event = (struct eljs_event *)mem_calloc(1, sizeof(*event));
dom_event *event = NULL;
dom_exception exc = dom_event_create(&event);
if (!event) {
if (exc != DOM_NO_ERR) {
return;
}
event->type_ = null_or_stracpy(js_tostring(J, 1));
dom_string *typ = NULL;
const char *str = js_tostring(J, 1);
if (str) {
exc = dom_string_create(str, strlen(str), &typ);
}
js_getproperty(J, 2, "bubbles");
event->bubbles = js_toboolean(J, -1);
bool bubbles = js_toboolean(J, -1);
js_pop(J, 1);
js_getproperty(J, 2, "cancelable");
event->cancelable = js_toboolean(J, -1);
js_pop(J, 1);
js_getproperty(J, 2, "composed");
event->composed = js_toboolean(J, -1);
bool cancelable = js_toboolean(J, -1);
js_pop(J, 1);
// js_getproperty(J, 2, "composed");
// event->composed = js_toboolean(J, -1);
// js_pop(J, 1);
exc = dom_event_init(event, typ, bubbles, cancelable);
js_newobject(J);
{
@ -197,7 +215,7 @@ mjs_event_constructor(js_State *J)
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);
// addproperty(J, "composed", mjs_event_get_property_composed, NULL);
addproperty(J, "defaultPrevented", mjs_event_get_property_defaultPrevented, NULL);
addproperty(J, "type", mjs_event_get_property_type, NULL);
}