mirror of
https://github.com/rkd77/elinks.git
synced 2024-12-04 14:46:47 -05:00
[mujs] CustomEvent (libdom)
This commit is contained in:
parent
fec5bd561d
commit
d529265f02
@ -10,54 +10,87 @@
|
|||||||
|
|
||||||
#include "elinks.h"
|
#include "elinks.h"
|
||||||
|
|
||||||
|
#include "document/document.h"
|
||||||
|
#include "document/view.h"
|
||||||
#include "ecmascript/ecmascript.h"
|
#include "ecmascript/ecmascript.h"
|
||||||
|
#include "ecmascript/libdom/dom.h"
|
||||||
#include "ecmascript/mujs.h"
|
#include "ecmascript/mujs.h"
|
||||||
#include "ecmascript/mujs/customevent.h"
|
#include "ecmascript/mujs/customevent.h"
|
||||||
#include "intl/charsets.h"
|
#include "intl/charsets.h"
|
||||||
#include "terminal/event.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_bubbles(js_State *J);
|
||||||
static void mjs_customEvent_get_property_cancelable(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_defaultPrevented(js_State *J);
|
||||||
static void mjs_customEvent_get_property_detail(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_get_property_type(js_State *J);
|
||||||
|
|
||||||
static void mjs_customEvent_preventDefault(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
|
static void
|
||||||
mjs_customEvent_finalizer(js_State *J, void *val)
|
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) {
|
||||||
if (event->detail) {
|
const char *detail = NULL;
|
||||||
js_unref(J, event->detail);
|
dom_exception exc = dom_custom_event_get_detail(event, &detail);
|
||||||
|
|
||||||
|
if (detail) {
|
||||||
|
js_unref(J, detail);
|
||||||
}
|
}
|
||||||
mem_free_if(event->type_);
|
dom_event_unref(event);
|
||||||
mem_free(event);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
mjs_push_customEvent(js_State *J, char *type_)
|
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");
|
js_error(J, "out of memory");
|
||||||
return;
|
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);
|
js_newobject(J);
|
||||||
{
|
{
|
||||||
@ -65,7 +98,7 @@ mjs_push_customEvent(js_State *J, char *type_)
|
|||||||
addmethod(J, "preventDefault", mjs_customEvent_preventDefault, 0);
|
addmethod(J, "preventDefault", mjs_customEvent_preventDefault, 0);
|
||||||
addproperty(J, "bubbles", mjs_customEvent_get_property_bubbles, NULL);
|
addproperty(J, "bubbles", mjs_customEvent_get_property_bubbles, NULL);
|
||||||
addproperty(J, "cancelable", mjs_customEvent_get_property_cancelable, 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, "defaultPrevented", mjs_customEvent_get_property_defaultPrevented, NULL);
|
||||||
addproperty(J, "detail", mjs_customEvent_get_property_detail, NULL);
|
addproperty(J, "detail", mjs_customEvent_get_property_detail, NULL);
|
||||||
addproperty(J, "type", mjs_customEvent_get_property_type, 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
|
#ifdef ECMASCRIPT_DEBUG
|
||||||
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
||||||
#endif
|
#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) {
|
if (!event) {
|
||||||
js_pushnull(J);
|
js_pushnull(J);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
js_pushboolean(J, event->bubbles);
|
bool bubbles = false;
|
||||||
|
dom_exception exc = dom_event_get_bubbles(event, &bubbles);
|
||||||
|
js_pushboolean(J, bubbles);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -93,15 +128,18 @@ mjs_customEvent_get_property_cancelable(js_State *J)
|
|||||||
#ifdef ECMASCRIPT_DEBUG
|
#ifdef ECMASCRIPT_DEBUG
|
||||||
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
||||||
#endif
|
#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) {
|
if (!event) {
|
||||||
js_pushnull(J);
|
js_pushnull(J);
|
||||||
return;
|
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
|
static void
|
||||||
mjs_customEvent_get_property_composed(js_State *J)
|
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);
|
js_pushboolean(J, event->composed);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static void
|
static void
|
||||||
mjs_customEvent_get_property_defaultPrevented(js_State *J)
|
mjs_customEvent_get_property_defaultPrevented(js_State *J)
|
||||||
@ -123,13 +162,15 @@ mjs_customEvent_get_property_defaultPrevented(js_State *J)
|
|||||||
#ifdef ECMASCRIPT_DEBUG
|
#ifdef ECMASCRIPT_DEBUG
|
||||||
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
||||||
#endif
|
#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) {
|
if (!event) {
|
||||||
js_pushnull(J);
|
js_pushnull(J);
|
||||||
return;
|
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
|
static void
|
||||||
@ -138,13 +179,20 @@ mjs_customEvent_get_property_detail(js_State *J)
|
|||||||
#ifdef ECMASCRIPT_DEBUG
|
#ifdef ECMASCRIPT_DEBUG
|
||||||
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
||||||
#endif
|
#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);
|
js_pushnull(J);
|
||||||
return;
|
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
|
static void
|
||||||
@ -153,13 +201,21 @@ mjs_customEvent_get_property_type(js_State *J)
|
|||||||
#ifdef ECMASCRIPT_DEBUG
|
#ifdef ECMASCRIPT_DEBUG
|
||||||
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
||||||
#endif
|
#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) {
|
if (!event) {
|
||||||
js_pushnull(J);
|
js_pushnull(J);
|
||||||
return;
|
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
|
static void
|
||||||
@ -168,15 +224,13 @@ mjs_customEvent_preventDefault(js_State *J)
|
|||||||
#ifdef ECMASCRIPT_DEBUG
|
#ifdef ECMASCRIPT_DEBUG
|
||||||
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
||||||
#endif
|
#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) {
|
if (!event) {
|
||||||
js_pushnull(J);
|
js_pushnull(J);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (event->cancelable) {
|
dom_event_prevent_default(event);
|
||||||
event->defaultPrevented = 1;
|
|
||||||
}
|
|
||||||
js_pushundefined(J);
|
js_pushundefined(J);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,33 +249,69 @@ mjs_customEvent_constructor(js_State *J)
|
|||||||
#ifdef ECMASCRIPT_DEBUG
|
#ifdef ECMASCRIPT_DEBUG
|
||||||
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
||||||
#endif
|
#endif
|
||||||
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;
|
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");
|
js_getproperty(J, 2, "bubbles");
|
||||||
event->bubbles = js_toboolean(J, -1);
|
bubbles = js_toboolean(J, -1);
|
||||||
js_pop(J, 1);
|
js_pop(J, 1);
|
||||||
js_getproperty(J, 2, "cancelable");
|
js_getproperty(J, 2, "cancelable");
|
||||||
event->cancelable = js_toboolean(J, -1);
|
cancelable = js_toboolean(J, -1);
|
||||||
js_pop(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_newobject(J);
|
||||||
{
|
{
|
||||||
js_newuserdata(J, "event", event, mjs_customEvent_finalizer);
|
js_newuserdata(J, "event", event, mjs_customEvent_finalizer);
|
||||||
addmethod(J, "preventDefault", mjs_customEvent_preventDefault, 0);
|
addmethod(J, "preventDefault", mjs_customEvent_preventDefault, 0);
|
||||||
addproperty(J, "bubbles", mjs_customEvent_get_property_bubbles, NULL);
|
addproperty(J, "bubbles", mjs_customEvent_get_property_bubbles, NULL);
|
||||||
addproperty(J, "cancelable", mjs_customEvent_get_property_cancelable, 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, "defaultPrevented", mjs_customEvent_get_property_defaultPrevented, NULL);
|
||||||
addproperty(J, "detail", mjs_customEvent_get_property_detail, NULL);
|
addproperty(J, "detail", mjs_customEvent_get_property_detail, NULL);
|
||||||
addproperty(J, "type", mjs_customEvent_get_property_type, NULL);
|
addproperty(J, "type", mjs_customEvent_get_property_type, NULL);
|
||||||
|
@ -273,7 +273,7 @@ js_customEvent_constructor(JSContext *ctx, JSValueConst new_target, int argc, JS
|
|||||||
bubbles = JS_ToBool(ctx, r);
|
bubbles = JS_ToBool(ctx, r);
|
||||||
r = JS_GetPropertyStr(ctx, argv[1], "cancelable");
|
r = JS_GetPropertyStr(ctx, argv[1], "cancelable");
|
||||||
cancelable = JS_ToBool(ctx, r);
|
cancelable = JS_ToBool(ctx, r);
|
||||||
r = JS_GetPropertyStr(ctx, argv[1], "composed");
|
//r = JS_GetPropertyStr(ctx, argv[1], "composed");
|
||||||
//composed = JS_ToBool(ctx, r);
|
//composed = JS_ToBool(ctx, r);
|
||||||
detail = calloc(1, sizeof(*detail));
|
detail = calloc(1, sizeof(*detail));
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user