initial event fireing implementation

This commit is contained in:
Vincent Sanders 2012-11-28 18:07:36 +00:00
parent fb5d763d75
commit 68f359d1ec
8 changed files with 161 additions and 0 deletions

View File

@ -21,6 +21,7 @@ JSAPI_BINDING_htmlcollection := javascript/jsapi/htmlcollection.bnd
JSAPI_BINDING_nodelist := javascript/jsapi/nodelist.bnd
JSAPI_BINDING_text := javascript/jsapi/text.bnd
JSAPI_BINDING_node := javascript/jsapi/node.bnd
JSAPI_BINDING_event := javascript/jsapi/event.bnd
# 1: input file
# 2: output file

View File

@ -51,4 +51,7 @@ jsobject *js_newcompartment(jscontext *ctx, void *win_priv, void *doc_priv);
/* execute some javascript in a context */
bool js_exec(jscontext *ctx, const char *txt, size_t txtlen);
/* fire an event at a dom node */
bool js_fire_event(jscontext *ctx, const char *type, void *target);
#endif /* _NETSURF_JAVASCRIPT_JS_H_ */

View File

@ -149,3 +149,59 @@ bool js_exec(jscontext *ctx, const char *txt, size_t txtlen)
return false;
}
bool js_fire_event(jscontext *ctx, const char *type, void *target)
{
JSContext *cx = (JSContext *)ctx;
dom_node *node = target;
JSObject *jsevent;
jsval rval;
jsval argv[1];
JSBool ret = JS_TRUE;
dom_exception exc;
dom_event *event;
dom_string *type_dom;
if (node == NULL) {
/* deliver to window */
if (cx == NULL) {
return false;
}
exc = dom_string_create((unsigned char*)type, strlen(type), &type_dom);
if (exc != DOM_NO_ERR) {
return false;
}
/* exc = dom_event_create(document, &event);*/
exc = -1;
if (exc != DOM_NO_ERR) {
return false;
}
exc = dom_event_init(event, type_dom, false, false);
dom_string_unref(type_dom);
if (exc != DOM_NO_ERR) {
return false;
}
jsevent = jsapi_new_Event(cx, NULL, NULL, event);
if (jsevent == NULL) {
return false;
}
argv[0] = OBJECT_TO_JSVAL(jsevent);
ret = JS_CallFunctionName(cx,
JS_GetGlobalObject(cx),
"dispatchEvent",
1,
argv,
&rval);
}
if (ret == JS_TRUE) {
return true;
}
return false;
}

View File

@ -149,4 +149,11 @@ JSObject *jsapi_new_Node(JSContext *cx,
JSObject *prototype,
JSObject *parent);
extern JSClass JSClass_Event;
JSObject *jsapi_InitClass_Event(JSContext *cx, JSObject *parent);
JSObject *jsapi_new_Event(JSContext *cx,
JSObject *prototype,
JSObject *parent,
dom_event *event);
#endif

View File

@ -0,0 +1,36 @@
/* Binding to generate event interface
*
* Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
*
* This file is part of NetSurf, http://www.netsurf-browser.org/
*
* Released under the terms of the MIT License,
* http://www.opensource.org/licenses/mit-license
*/
webidlfile "dom.idl";
hdrcomment "Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>";
hdrcomment "This file is part of NetSurf, http://www.netsurf-browser.org/";
hdrcomment "Released under the terms of the MIT License,";
hdrcomment " http://www.opensource.org/licenses/mit-license";
preamble %{
#include <dom/dom.h>
#include "utils/config.h"
#include "utils/log.h"
#include "javascript/jsapi.h"
#include "javascript/jsapi/binding.h"
%}
binding node {
type js_libdom; /* the binding type */
interface Event; /* Web IDL interface to generate */
private "dom_event *" event;
}

View File

@ -23,6 +23,7 @@ preamble %{
#include "utils/config.h"
#include "utils/log.h"
#include "utils/corestrings.h"
#include "javascript/jsapi.h"
#include "javascript/jsapi/binding.h"
@ -147,6 +148,11 @@ api init %{
return NULL;
}
user_proto = jsapi_InitClass_Event(cx, prototype);
if (user_proto == NULL) {
return NULL;
}
%}
api new %{
@ -211,10 +217,51 @@ getter self %{
jsret = obj;
%}
/* boolean dispatchEvent(Event event); */
operation dispatchEvent %{
/* this implementation is unique to the window object as it is
* not a "real" dom node.
*/
/* caution, this must match the struct generated from event.bnd */
if (event == JSVAL_VOID) {
jsret = JS_FALSE;
} else {
dom_event *domevent;
dom_string *type_dom;
dom_exception exc;
jsval eventval = JSVAL_VOID;
jsval event_argv[1];
jsval event_rval;
domevent = JS_GetInstancePrivate(cx, event, &JSClass_Event, NULL);
if (domevent == NULL) {
/** @todo type error? */
jsret = JS_FALSE;
} else {
exc = dom_event_get_type(domevent, &type_dom);
if (dom_string_isequal(type_dom, corestring_dom_load)) {
JS_GetProperty(cx, JSAPI_THIS_OBJECT(cx, vp), "onload", &eventval);
}
if (eventval != JSVAL_VOID) {
event_argv[0] = event;
jsret = JS_CallFunctionValue(cx, NULL, eventval, 1, event_argv, &event_rval);
}
}
}
%}
getter EventHandler %{
/* this implementation is unique to the window object as it is
* not a dom node.
*/
JSLOG("propname[%d] %s %s", tinyid , jsclass_properties[tinyid].name, JS_GetTypeName(cx, JS_TypeOfValue(cx, tinyid_jsval)));
%}
setter EventHandler %{
/* this implementation is unique to the window object as it is
* not a dom node.
*/
JSLOG("propname[%d] %s %s", tinyid, jsclass_properties[tinyid].name, JS_GetTypeName(cx, JS_TypeOfValue(cx, tinyid_jsval)));
%}

View File

@ -53,3 +53,8 @@ bool js_exec(jscontext *ctx, const char *txt, size_t txtlen)
{
return true;
}
bool js_fire_event(jscontext *ctx, const char *type, void *target)
{
return true;
}

View File

@ -331,6 +331,12 @@ void html_finish_conversion(html_content *c)
}
}
/* fire a simple event named load at the Document's Window
* object, but with its target set to the Document object (and
* the currentTarget set to the Window object)
*/
js_fire_event(c->jscontext, "load", NULL);
/* convert dom tree to box tree */
LOG(("DOM to box (%p)", c));
content_set_status(&c->base, messages_get("Processing"));