diff options
Diffstat (limited to 'javascript')
-rw-r--r-- | javascript/js.h | 15 | ||||
-rw-r--r-- | javascript/jsapi.c | 164 | ||||
-rw-r--r-- | javascript/jsapi.h | 21 | ||||
-rw-r--r-- | javascript/jsapi/binding.h | 4 | ||||
-rw-r--r-- | javascript/jsapi/dom.bnd | 10 | ||||
-rw-r--r-- | javascript/jsapi/htmlcollection.bnd | 2 | ||||
-rw-r--r-- | javascript/jsapi/htmldocument.bnd | 289 | ||||
-rw-r--r-- | javascript/jsapi/htmlelement.bnd | 336 | ||||
-rw-r--r-- | javascript/jsapi/location.bnd | 12 | ||||
-rw-r--r-- | javascript/jsapi/nodelist.bnd | 11 | ||||
-rw-r--r-- | javascript/jsapi/window.bnd | 91 | ||||
-rw-r--r-- | javascript/none.c | 2 |
12 files changed, 862 insertions, 95 deletions
diff --git a/javascript/js.h b/javascript/js.h index 4dd8f15d3..44de4fe3d 100644 --- a/javascript/js.h +++ b/javascript/js.h @@ -26,6 +26,10 @@ typedef struct jscontext jscontext; typedef struct jsobject jsobject; +struct dom_document; +struct dom_node; +struct dom_string; + /** Initialise javascript interpreter */ void js_initialise(void); @@ -51,7 +55,16 @@ 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); +bool js_fire_event(jscontext *ctx, const char *type, struct dom_document *doc, struct dom_node *target); + +bool +js_dom_event_add_listener(jscontext *ctx, + struct dom_document *document, + struct dom_node *node, + struct dom_string *event_type_dom, + void *js_funcval); + #endif /* _NETSURF_JAVASCRIPT_JS_H_ */ diff --git a/javascript/jsapi.c b/javascript/jsapi.c index bec565316..ef34371d1 100644 --- a/javascript/jsapi.c +++ b/javascript/jsapi.c @@ -32,14 +32,16 @@ void js_initialise(void) /* Create a JS runtime. */ #if JS_VERSION >= 180 - JS_SetCStringsAreUTF8(); /* we prefer our runtime to be utf-8 */ + JS_SetCStringsAreUTF8(); /* we prefer our runtime to be utf-8 */ #endif rt = JS_NewRuntime(8L * 1024L * 1024L); JSLOG("New runtime handle %p", rt); - /* register script content handler */ - javascript_init(); + if (rt != NULL) { + /* register script content handler */ + javascript_init(); + } } void js_finalise(void) @@ -97,7 +99,7 @@ void js_destroycontext(jscontext *ctx) * * This performs the following actions * 1. constructs a new global object by initialising a window class - * 2. Instantiate the global a window object + * 2. Instantiate the global a window object */ jsobject *js_newcompartment(jscontext *ctx, void *win_priv, void *doc_priv) { @@ -116,7 +118,7 @@ jsobject *js_newcompartment(jscontext *ctx, void *win_priv, void *doc_priv) } window = jsapi_new_Window(cx, window_proto, NULL, win_priv, doc_priv); - + return (jsobject *)window; } @@ -139,9 +141,9 @@ bool js_exec(jscontext *ctx, const char *txt, size_t txtlen) return false; } - if (JS_EvaluateScript(cx, - JS_GetGlobalObject(cx), - txt, txtlen, + if (JS_EvaluateScript(cx, + JS_GetGlobalObject(cx), + txt, txtlen, "<head>", 0, &rval) == JS_TRUE) { return true; @@ -153,7 +155,7 @@ bool js_exec(jscontext *ctx, const char *txt, size_t txtlen) dom_exception _dom_event_create(dom_document *doc, dom_event **evt); #define dom_event_create(d, e) _dom_event_create((dom_document *)(d), (dom_event **) (e)) -bool js_fire_event(jscontext *ctx, const char *type, void *target) +bool js_fire_event(jscontext *ctx, const char *type, dom_document *doc, dom_node *target) { JSContext *cx = (JSContext *)ctx; dom_node *node = target; @@ -165,18 +167,23 @@ bool js_fire_event(jscontext *ctx, const char *type, void *target) dom_event *event; dom_string *type_dom; + if (cx == NULL) { + return false; + } + if (node == NULL) { - /* deliver to window */ - if (cx == NULL) { - return false; - } + /* deliver manufactured event to window */ + JSLOG("Dispatching event %s at window", type); - exc = dom_string_create((unsigned char*)type, strlen(type), &type_dom); + /* create and initialise and event object */ + exc = dom_string_create((unsigned char*)type, + strlen(type), + &type_dom); if (exc != DOM_NO_ERR) { return false; } - exc = dom_event_create(-1, &event); + exc = dom_event_create(doc, &event); if (exc != DOM_NO_ERR) { return false; } @@ -192,18 +199,133 @@ bool js_fire_event(jscontext *ctx, const char *type, void *target) return false; } + /* dispatch event at the window object */ argv[0] = OBJECT_TO_JSVAL(jsevent); - ret = JS_CallFunctionName(cx, - JS_GetGlobalObject(cx), - "dispatchEvent", - 1, - argv, + ret = JS_CallFunctionName(cx, + JS_GetGlobalObject(cx), + "dispatchEvent", + 1, + argv, &rval); - } + } else { + JSLOG("Dispatching event %s at %p", type, node); + + /* create and initialise and event object */ + exc = dom_string_create((unsigned char*)type, + strlen(type), + &type_dom); + if (exc != DOM_NO_ERR) { + return false; + } + + exc = dom_event_create(doc, &event); + if (exc != DOM_NO_ERR) { + return false; + } + + exc = dom_event_init(event, type_dom, true, true); + dom_string_unref(type_dom); + if (exc != DOM_NO_ERR) { + return false; + } + + dom_event_target_dispatch_event(node, event, &ret); + + } if (ret == JS_TRUE) { return true; } return false; } + +struct js_dom_event_private { + JSContext *cx; /* javascript context */ + jsval funcval; /* javascript function to call */ + struct dom_node *node; /* dom node event listening on */ + dom_string *type; /* event type */ + dom_event_listener *listener; /* the listener containing this */ +}; + +static void +js_dom_event_listener(struct dom_event *event, void *pw) +{ + struct js_dom_event_private *private = pw; + jsval event_argv[1]; + jsval event_rval; + JSObject *jsevent; + + JSLOG("WOOT dom event with %p", private); + + if (!JSVAL_IS_VOID(private->funcval)) { + jsevent = jsapi_new_Event(private->cx, NULL, NULL, event); + if (jsevent != NULL) { + + /* dispatch event at the window object */ + event_argv[0] = OBJECT_TO_JSVAL(jsevent); + + JS_CallFunctionValue(private->cx, + NULL, + private->funcval, + 1, + event_argv, + &event_rval); + } + } +} + +/* add a listener to a dom node + * + * 1. Create a dom_event_listener From a handle_event function pointer + * and a private word In a document context + * + * 2. Register for your events on a target (dom nodes are targets) + * dom_event_target_add_event_listener(node, evt_name, listener, + * capture_or_not) + * + */ + +bool +js_dom_event_add_listener(jscontext *ctx, + struct dom_document *document, + struct dom_node *node, + struct dom_string *event_type_dom, + void *js_funcval) +{ + JSContext *cx = (JSContext *)ctx; + dom_exception exc; + struct js_dom_event_private *private; + + private = malloc(sizeof(struct js_dom_event_private)); + if (private == NULL) { + return false; + } + + exc = dom_event_listener_create(document, + js_dom_event_listener, + private, + &private->listener); + if (exc != DOM_NO_ERR) { + return false; + } + + private->cx = cx; + private->funcval = *(jsval *)js_funcval; + private->node = node; + private->type = event_type_dom; + + JSLOG("adding %p to listener", private); + + JSAPI_ADD_VALUE_ROOT(cx, &private->funcval); + exc = dom_event_target_add_event_listener(private->node, + private->type, + private->listener, + true); + if (exc != DOM_NO_ERR) { + JSLOG("failed to add listener"); + JSAPI_REMOVE_VALUE_ROOT(cx, &private->funcval); + } + + return true; +} diff --git a/javascript/jsapi.h b/javascript/jsapi.h index 6b0f1124d..e3295fee9 100644 --- a/javascript/jsapi.h +++ b/javascript/jsapi.h @@ -35,11 +35,13 @@ #include <string.h> -# ifndef JSVERSION_LATEST -# define JSVERSION_LATEST JS_VERSION -# endif - +#ifndef JSVERSION_LATEST +#define JSVERSION_LATEST JS_VERSION +#endif +#ifndef JSOPTION_JIT +#define JSOPTION_JIT 0 +#endif /* *CAUTION* these native function macros introduce and use jsapi_this * and jsapi_rval variables, native function code should not conflict @@ -79,7 +81,7 @@ jsapi_property_##name##_set(cx, obj, jsval jsapi_id, vp) /* native property return value */ -#define JSAPI_PROP_RVAL(cx, vp) (vp) +#define JSAPI_PROP_RVAL(cx, vp) (*(vp)) /* native property getter return value */ #define JSAPI_PROP_SET_RVAL(cx, vp, v) (*(vp) = (v)) @@ -150,6 +152,9 @@ JS_NewCompartmentAndGlobalObject(JSContext *cx, #define JSAPI_ADD_OBJECT_ROOT(cx, obj) JS_AddRoot(cx, obj) #define JSAPI_REMOVE_OBJECT_ROOT(cx, obj) JS_RemoveRoot(cx, obj) +#define JSAPI_ADD_VALUE_ROOT(cx, obj) JS_AddRoot(cx, obj) +#define JSAPI_REMOVE_VALUE_ROOT(cx, obj) JS_RemoveRoot(cx, obj) + #elif JS_VERSION == 180 /************************** Spidermonkey 1.8.0 **************************/ @@ -263,6 +268,9 @@ JS_NewCompartmentAndGlobalObject(JSContext *cx, #define JSAPI_ADD_OBJECT_ROOT(cx, obj) JS_AddRoot(cx, obj) #define JSAPI_REMOVE_OBJECT_ROOT(cx, obj) JS_RemoveRoot(cx, obj) +#define JSAPI_ADD_VALUE_ROOT(cx, obj) JS_AddRoot(cx, obj) +#define JSAPI_REMOVE_VALUE_ROOT(cx, obj) JS_RemoveRoot(cx, obj) + #else /* #if JS_VERSION == 180 */ @@ -365,6 +373,9 @@ JS_NewCompartmentAndGlobalObject(JSContext *cx, #define JSAPI_ADD_OBJECT_ROOT(cx, obj) JS_AddObjectRoot(cx, obj) #define JSAPI_REMOVE_OBJECT_ROOT(cx, obj) JS_RemoveObjectRoot(cx, obj) +#define JSAPI_ADD_VALUE_ROOT(cx, val) JS_AddValueRoot(cx, val) +#define JSAPI_REMOVE_VALUE_ROOT(cx, val) JS_RemoveValueRoot(cx, val) + #endif #define JSLOG(args...) LOG((args)) diff --git a/javascript/jsapi/binding.h b/javascript/jsapi/binding.h index f27493532..7b0f61428 100644 --- a/javascript/jsapi/binding.h +++ b/javascript/jsapi/binding.h @@ -45,8 +45,8 @@ JSObject *jsapi_InitClass_Location(JSContext *cx, JSObject *parent); JSObject *jsapi_new_Location(JSContext *cx, JSObject *window, JSObject *parent, - struct browser_window *bw, - nsurl *url); + nsurl *url, + html_content *htmlc); JSObject *jsapi_InitClass_Document(JSContext *cx, JSObject *parent); diff --git a/javascript/jsapi/dom.bnd b/javascript/jsapi/dom.bnd index 89d0d8449..e781b330c 100644 --- a/javascript/jsapi/dom.bnd +++ b/javascript/jsapi/dom.bnd @@ -1,4 +1,12 @@ -/* DOM bindings entries */ +/* Binding to generate interfaces for the DOM IDL + * + * 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"; diff --git a/javascript/jsapi/htmlcollection.bnd b/javascript/jsapi/htmlcollection.bnd index f4a11549b..5e99e48b8 100644 --- a/javascript/jsapi/htmlcollection.bnd +++ b/javascript/jsapi/htmlcollection.bnd @@ -1,4 +1,4 @@ -/* Binding to generate HTMLcolelction interface +/* Binding to generate HTMLcollection interface * * The js_libdom (javascript to libdom) binding type is currently the * only one implemented and this principly describes that binding. diff --git a/javascript/jsapi/htmldocument.bnd b/javascript/jsapi/htmldocument.bnd index 0ed7ac153..c29e470c2 100644 --- a/javascript/jsapi/htmldocument.bnd +++ b/javascript/jsapi/htmldocument.bnd @@ -28,6 +28,7 @@ preamble %{ #include "content/urldb.h" +#include "javascript/js.h" #include "javascript/jsapi.h" #include "javascript/jsapi/binding.h" @@ -43,6 +44,12 @@ binding document { */ private "dom_document *" node; private "struct html_content *" htmlc; + + /** location instantiated on first use */ + property unshared location; + + /* events through a single interface */ + property unshared type EventHandler; } api finalise %{ @@ -53,6 +60,37 @@ api finalise %{ } %} + +getter location %{ + if (!JSVAL_IS_VOID(JSAPI_PROP_RVAL(cx,vp))) { + /* already created - return it */ + return JS_TRUE; + } + jsret = jsapi_new_Location(cx, + NULL, + NULL, + llcache_handle_get_url(private->htmlc->base.llcache), + private->htmlc); +%} + +getter URL %{ + jsval loc; + jsval jsstr = JSVAL_NULL; + if (JS_GetProperty(cx, obj, "location", &loc) == JS_TRUE) { + JS_GetProperty(cx, JSVAL_TO_OBJECT(loc), "href", &jsstr); + } + jsret = JSVAL_TO_STRING(jsstr); +%} + +getter documentURI %{ + jsval loc; + jsval jsstr = JSVAL_NULL; + if (JS_GetProperty(cx, obj, "location", &loc) == JS_TRUE) { + JS_GetProperty(cx, JSVAL_TO_OBJECT(loc), "href", &jsstr); + } + jsret = JSVAL_TO_STRING(jsstr); +%} + getter cookie %{ char *cookie_str; cookie_str = urldb_get_cookie(llcache_handle_get_url(private->htmlc->base.llcache), false); @@ -238,3 +276,254 @@ operation createElement %{ JSLOG("returning jsobject %p",jsret); %} + +getter EventHandler %{ + JSLOG("propname[%d].name=\"%s\"", + tinyid, + jsclass_properties[tinyid].name); +%} + + +setter EventHandler %{ + dom_string *event_type_dom; + + JSLOG("propname[%d].name=\"%s\"", + tinyid, + jsclass_properties[tinyid].name); + + switch (tinyid) { + case JSAPI_PROP_TINYID_onabort: + event_type_dom = corestring_dom_abort; + break; + + case JSAPI_PROP_TINYID_onblur: + event_type_dom = corestring_dom_blur; + break; + + case JSAPI_PROP_TINYID_oncancel: + event_type_dom = corestring_dom_cancel; + break; + + case JSAPI_PROP_TINYID_oncanplay: + event_type_dom = corestring_dom_canplay; + break; + + case JSAPI_PROP_TINYID_oncanplaythrough: + event_type_dom = corestring_dom_canplaythrough; + break; + + case JSAPI_PROP_TINYID_onchange: + event_type_dom = corestring_dom_change; + break; + + case JSAPI_PROP_TINYID_onclick: + event_type_dom = corestring_dom_click; + break; + + case JSAPI_PROP_TINYID_onclose: + event_type_dom = corestring_dom_close; + break; + + case JSAPI_PROP_TINYID_oncontextmenu: + event_type_dom = corestring_dom_contextmenu; + break; + + case JSAPI_PROP_TINYID_oncuechange: + event_type_dom = corestring_dom_cuechange; + break; + + case JSAPI_PROP_TINYID_ondblclick: + event_type_dom = corestring_dom_dblclick; + break; + + case JSAPI_PROP_TINYID_ondrag: + event_type_dom = corestring_dom_drag; + break; + + case JSAPI_PROP_TINYID_ondragend: + event_type_dom = corestring_dom_dragend; + break; + + case JSAPI_PROP_TINYID_ondragenter: + event_type_dom = corestring_dom_dragenter; + break; + + case JSAPI_PROP_TINYID_ondragleave: + event_type_dom = corestring_dom_dragleave; + break; + + case JSAPI_PROP_TINYID_ondragover: + event_type_dom = corestring_dom_dragover; + break; + + case JSAPI_PROP_TINYID_ondragstart: + event_type_dom = corestring_dom_dragstart; + break; + + case JSAPI_PROP_TINYID_ondrop: + event_type_dom = corestring_dom_drop; + break; + + case JSAPI_PROP_TINYID_ondurationchange: + event_type_dom = corestring_dom_durationchange; + break; + + case JSAPI_PROP_TINYID_onemptied: + event_type_dom = corestring_dom_emptied; + break; + + case JSAPI_PROP_TINYID_onended: + event_type_dom = corestring_dom_ended; + break; + + case JSAPI_PROP_TINYID_onerror: + event_type_dom = corestring_dom_error; + break; + + case JSAPI_PROP_TINYID_onfocus: + event_type_dom = corestring_dom_focus; + break; + + case JSAPI_PROP_TINYID_oninput: + event_type_dom = corestring_dom_input; + break; + + case JSAPI_PROP_TINYID_oninvalid: + event_type_dom = corestring_dom_invalid; + break; + + case JSAPI_PROP_TINYID_onkeydown: + event_type_dom = corestring_dom_keydown; + break; + + case JSAPI_PROP_TINYID_onkeypress: + event_type_dom = corestring_dom_keypress; + break; + + case JSAPI_PROP_TINYID_onkeyup: + event_type_dom = corestring_dom_keyup; + break; + + case JSAPI_PROP_TINYID_onload: + event_type_dom = corestring_dom_load; + break; + + case JSAPI_PROP_TINYID_onloadeddata: + event_type_dom = corestring_dom_loadeddata; + break; + + case JSAPI_PROP_TINYID_onloadedmetadata: + event_type_dom = corestring_dom_loadedmetadata; + break; + + case JSAPI_PROP_TINYID_onloadstart: + event_type_dom = corestring_dom_loadstart; + break; + + case JSAPI_PROP_TINYID_onmousedown: + event_type_dom = corestring_dom_mousedown; + break; + + case JSAPI_PROP_TINYID_onmousemove: + event_type_dom = corestring_dom_mousemove; + break; + + case JSAPI_PROP_TINYID_onmouseout: + event_type_dom = corestring_dom_mouseout; + break; + + case JSAPI_PROP_TINYID_onmouseover: + event_type_dom = corestring_dom_mouseover; + break; + + case JSAPI_PROP_TINYID_onmouseup: + event_type_dom = corestring_dom_mouseup; + break; + + case JSAPI_PROP_TINYID_onmousewheel: + event_type_dom = corestring_dom_mousewheel; + break; + + case JSAPI_PROP_TINYID_onpause: + event_type_dom = corestring_dom_pause; + break; + + case JSAPI_PROP_TINYID_onplay: + event_type_dom = corestring_dom_play; + break; + + case JSAPI_PROP_TINYID_onplaying: + event_type_dom = corestring_dom_playing; + break; + + case JSAPI_PROP_TINYID_onprogress: + event_type_dom = corestring_dom_progress; + break; + + case JSAPI_PROP_TINYID_onratechange: + event_type_dom = corestring_dom_ratechange; + break; + + case JSAPI_PROP_TINYID_onreset: + event_type_dom = corestring_dom_reset; + break; + + case JSAPI_PROP_TINYID_onscroll: + event_type_dom = corestring_dom_scroll; + break; + + case JSAPI_PROP_TINYID_onseeked: + event_type_dom = corestring_dom_seeked; + break; + + case JSAPI_PROP_TINYID_onseeking: + event_type_dom = corestring_dom_seeking; + break; + + case JSAPI_PROP_TINYID_onselect: + event_type_dom = corestring_dom_select; + break; + + case JSAPI_PROP_TINYID_onshow: + event_type_dom = corestring_dom_show; + break; + + case JSAPI_PROP_TINYID_onstalled: + event_type_dom = corestring_dom_stalled; + break; + + case JSAPI_PROP_TINYID_onsubmit: + event_type_dom = corestring_dom_submit; + break; + + case JSAPI_PROP_TINYID_onsuspend: + event_type_dom = corestring_dom_suspend; + break; + + case JSAPI_PROP_TINYID_ontimeupdate: + event_type_dom = corestring_dom_timeupdate; + break; + + case JSAPI_PROP_TINYID_onvolumechange: + event_type_dom = corestring_dom_volumechange; + break; + + case JSAPI_PROP_TINYID_onwaiting: + event_type_dom = corestring_dom_waiting; + break; + + case JSAPI_PROP_TINYID_onreadystatechange: + event_type_dom = corestring_dom_readystatechange; + break; + + default: + JSLOG("called with unknown tinyid"); + return JS_TRUE; + } + + js_dom_event_add_listener((struct jscontext *)cx, + private->node, + (dom_node *)private->node, + event_type_dom, + vp); +%} diff --git a/javascript/jsapi/htmlelement.bnd b/javascript/jsapi/htmlelement.bnd index 71bb31bc1..3ede93bbf 100644 --- a/javascript/jsapi/htmlelement.bnd +++ b/javascript/jsapi/htmlelement.bnd @@ -23,19 +23,96 @@ preamble %{ #include "utils/config.h" #include "utils/log.h" +#include "utils/corestrings.h" +#include "javascript/js.h" #include "javascript/jsapi.h" #include "javascript/jsapi/binding.h" %} binding htmlelement { - type js_libdom; /* the binding type */ - - interface HTMLElement; /* Web IDL interface to generate */ - - private "dom_element *" node; - private "struct html_content *" htmlc; + type js_libdom; /* the binding type */ + + interface HTMLElement; /* Web IDL interface to generate */ + /* superclasses + + interface HTMLAnchorElement + interface HTMLAppletElement + interface HTMLAreaElement + interface HTMLBaseElement + interface HTMLBaseFontElement + interface HTMLBodyElement + interface HTMLBRElement + interface HTMLButtonElement + interface HTMLCanvasElement + interface HTMLCommandElement + interface HTMLDataElement + interface HTMLDataListElement + interface HTMLDetailsElement + interface HTMLDialogElement + interface HTMLDirectoryElement + interface HTMLDivElement + interface HTMLDListElement + interface HTMLEmbedElement + interface HTMLFieldSetElement + interface HTMLFontElement + interface HTMLFormElement + interface HTMLFrameElement + interface HTMLFrameSetElement + interface HTMLHeadElement + interface HTMLHeadingElement + interface HTMLHRElement + interface HTMLHtmlElement + interface HTMLIFrameElement + interface HTMLImageElement + interface HTMLInputElement + interface HTMLKeygenElement + interface HTMLLabelElement + interface HTMLLegendElement + interface HTMLLIElement + interface HTMLLinkElement + interface HTMLMapElement + interface HTMLMarqueeElement + interface HTMLMediaElement + interface HTMLMenuElement + interface HTMLMetaElement + interface HTMLMeterElement + interface HTMLModElement + interface HTMLObjectElement + interface HTMLOListElement + interface HTMLOptGroupElement + interface HTMLOptionElement + interface HTMLOutputElement + interface HTMLParagraphElement + interface HTMLParamElement + interface HTMLPreElement + interface HTMLProgressElement + interface HTMLQuoteElement + interface HTMLScriptElement + interface HTMLSelectElement + interface HTMLSourceElement + interface HTMLSpanElement + interface HTMLStyleElement + interface HTMLTableCaptionElement + interface HTMLTableCellElement + interface HTMLTableColElement + interface HTMLTableElement + interface HTMLTableRowElement + interface HTMLTableSectionElement + interface HTMLTextAreaElement + interface HTMLTimeElement + interface HTMLTitleElement + interface HTMLTrackElement + interface HTMLUListElement + interface HTMLUnknownElement + */ + + private "dom_element *" node; + private "struct html_content *" htmlc; + + /* events through a single interface */ + property unshared type EventHandler; } api finalise %{ @@ -214,3 +291,250 @@ getter childElementCount %{ } } %} + +getter EventHandler %{ + JSLOG("propname[%d].name=\"%s\"", + tinyid, + jsclass_properties[tinyid].name); +%} + + +setter EventHandler %{ + dom_string *event_type_dom; + + JSLOG("propname[%d].name=\"%s\"", + tinyid, + jsclass_properties[tinyid].name); + + switch (tinyid) { + case JSAPI_PROP_TINYID_onabort: + event_type_dom = corestring_dom_abort; + break; + + case JSAPI_PROP_TINYID_onblur: + event_type_dom = corestring_dom_blur; + break; + + case JSAPI_PROP_TINYID_oncancel: + event_type_dom = corestring_dom_cancel; + break; + + case JSAPI_PROP_TINYID_oncanplay: + event_type_dom = corestring_dom_canplay; + break; + + case JSAPI_PROP_TINYID_oncanplaythrough: + event_type_dom = corestring_dom_canplaythrough; + break; + + case JSAPI_PROP_TINYID_onchange: + event_type_dom = corestring_dom_change; + break; + + case JSAPI_PROP_TINYID_onclick: + event_type_dom = corestring_dom_click; + break; + + case JSAPI_PROP_TINYID_onclose: + event_type_dom = corestring_dom_close; + break; + + case JSAPI_PROP_TINYID_oncontextmenu: + event_type_dom = corestring_dom_contextmenu; + break; + + case JSAPI_PROP_TINYID_oncuechange: + event_type_dom = corestring_dom_cuechange; + break; + + case JSAPI_PROP_TINYID_ondblclick: + event_type_dom = corestring_dom_dblclick; + break; + + case JSAPI_PROP_TINYID_ondrag: + event_type_dom = corestring_dom_drag; + break; + + case JSAPI_PROP_TINYID_ondragend: + event_type_dom = corestring_dom_dragend; + break; + + case JSAPI_PROP_TINYID_ondragenter: + event_type_dom = corestring_dom_dragenter; + break; + + case JSAPI_PROP_TINYID_ondragleave: + event_type_dom = corestring_dom_dragleave; + break; + + case JSAPI_PROP_TINYID_ondragover: + event_type_dom = corestring_dom_dragover; + break; + + case JSAPI_PROP_TINYID_ondragstart: + event_type_dom = corestring_dom_dragstart; + break; + + case JSAPI_PROP_TINYID_ondrop: + event_type_dom = corestring_dom_drop; + break; + + case JSAPI_PROP_TINYID_ondurationchange: + event_type_dom = corestring_dom_durationchange; + break; + + case JSAPI_PROP_TINYID_onemptied: + event_type_dom = corestring_dom_emptied; + break; + + case JSAPI_PROP_TINYID_onended: + event_type_dom = corestring_dom_ended; + break; + + case JSAPI_PROP_TINYID_onerror: + event_type_dom = corestring_dom_error; + break; + + case JSAPI_PROP_TINYID_onfocus: + event_type_dom = corestring_dom_focus; + break; + + case JSAPI_PROP_TINYID_oninput: + event_type_dom = corestring_dom_input; + break; + + case JSAPI_PROP_TINYID_oninvalid: + event_type_dom = corestring_dom_invalid; + break; + + case JSAPI_PROP_TINYID_onkeydown: + event_type_dom = corestring_dom_keydown; + break; + + case JSAPI_PROP_TINYID_onkeypress: + event_type_dom = corestring_dom_keypress; + break; + + case JSAPI_PROP_TINYID_onkeyup: + event_type_dom = corestring_dom_keyup; + break; + + case JSAPI_PROP_TINYID_onload: + event_type_dom = corestring_dom_load; + break; + + case JSAPI_PROP_TINYID_onloadeddata: + event_type_dom = corestring_dom_loadeddata; + break; + + case JSAPI_PROP_TINYID_onloadedmetadata: + event_type_dom = corestring_dom_loadedmetadata; + break; + + case JSAPI_PROP_TINYID_onloadstart: + event_type_dom = corestring_dom_loadstart; + break; + + case JSAPI_PROP_TINYID_onmousedown: + event_type_dom = corestring_dom_mousedown; + break; + + case JSAPI_PROP_TINYID_onmousemove: + event_type_dom = corestring_dom_mousemove; + break; + + case JSAPI_PROP_TINYID_onmouseout: + event_type_dom = corestring_dom_mouseout; + break; + + case JSAPI_PROP_TINYID_onmouseover: + event_type_dom = corestring_dom_mouseover; + break; + + case JSAPI_PROP_TINYID_onmouseup: + event_type_dom = corestring_dom_mouseup; + break; + + case JSAPI_PROP_TINYID_onmousewheel: + event_type_dom = corestring_dom_mousewheel; + break; + + case JSAPI_PROP_TINYID_onpause: + event_type_dom = corestring_dom_pause; + break; + + case JSAPI_PROP_TINYID_onplay: + event_type_dom = corestring_dom_play; + break; + + case JSAPI_PROP_TINYID_onplaying: + event_type_dom = corestring_dom_playing; + break; + + case JSAPI_PROP_TINYID_onprogress: + event_type_dom = corestring_dom_progress; + break; + + case JSAPI_PROP_TINYID_onratechange: + event_type_dom = corestring_dom_ratechange; + break; + + case JSAPI_PROP_TINYID_onreset: + event_type_dom = corestring_dom_reset; + break; + + case JSAPI_PROP_TINYID_onscroll: + event_type_dom = corestring_dom_scroll; + break; + + case JSAPI_PROP_TINYID_onseeked: + event_type_dom = corestring_dom_seeked; + break; + + case JSAPI_PROP_TINYID_onseeking: + event_type_dom = corestring_dom_seeking; + break; + + case JSAPI_PROP_TINYID_onselect: + event_type_dom = corestring_dom_select; + break; + + case JSAPI_PROP_TINYID_onshow: + event_type_dom = corestring_dom_show; + break; + + case JSAPI_PROP_TINYID_onstalled: + event_type_dom = corestring_dom_stalled; + break; + + case JSAPI_PROP_TINYID_onsubmit: + event_type_dom = corestring_dom_submit; + break; + + case JSAPI_PROP_TINYID_onsuspend: + event_type_dom = corestring_dom_suspend; + break; + + case JSAPI_PROP_TINYID_ontimeupdate: + event_type_dom = corestring_dom_timeupdate; + break; + + case JSAPI_PROP_TINYID_onvolumechange: + event_type_dom = corestring_dom_volumechange; + break; + + case JSAPI_PROP_TINYID_onwaiting: + event_type_dom = corestring_dom_waiting; + break; + + default: + JSLOG("called with unknown tinyid"); + return JS_TRUE; + } + + js_dom_event_add_listener((struct jscontext *)cx, + private->htmlc->document, + (dom_node *)private->node, + event_type_dom, + vp); +%} diff --git a/javascript/jsapi/location.bnd b/javascript/jsapi/location.bnd index 32e38da93..32677d1b5 100644 --- a/javascript/jsapi/location.bnd +++ b/javascript/jsapi/location.bnd @@ -32,19 +32,27 @@ binding location { interface Location; /* Web IDL interface to generate */ - private "struct browser_window *" bw; private "nsurl *" url; + private "struct html_content *" htmlc; + + property unshared href; } operation reload %{ - browser_window_reload(private->bw, false); + browser_window_reload(private->htmlc->bw, false); %} getter href %{ char *url_s = NULL; size_t url_l; + + if (!JSVAL_IS_VOID(JSAPI_PROP_RVAL(cx,vp))) { + /* already created - return it */ + return JS_TRUE; + } + nsurl_get(private->url, NSURL_COMPLETE, &url_s, &url_l); if (url_s != NULL) { jsret = JS_NewStringCopyN(cx, url_s, url_l); diff --git a/javascript/jsapi/nodelist.bnd b/javascript/jsapi/nodelist.bnd index d6e9fe941..b57dc6e05 100644 --- a/javascript/jsapi/nodelist.bnd +++ b/javascript/jsapi/nodelist.bnd @@ -1,8 +1,5 @@ /* Binding to generate NodeList interface * - * The js_libdom (javascript to libdom) binding type is currently the - * only one implemented and this principly describes that binding. - * * Copyright 2012 Vincent Sanders <vince@netsurf-browser.org> * * This file is part of NetSurf, http://www.netsurf-browser.org/ @@ -32,12 +29,12 @@ preamble %{ webidlfile "dom.idl"; binding nodelist { - type js_libdom; /* the binding type */ + type js_libdom; /* the binding type */ - interface NodeList; /* The WebIDL interface to generate a binding for */ + interface NodeList; /* The WebIDL interface to generate a binding for */ - private "dom_nodelist *" nodelist; - private "struct html_content *" htmlc; + private "dom_nodelist *" nodelist; + private "struct html_content *" htmlc; } getter length %{ diff --git a/javascript/jsapi/window.bnd b/javascript/jsapi/window.bnd index 610aa71ab..6153e90aa 100644 --- a/javascript/jsapi/window.bnd +++ b/javascript/jsapi/window.bnd @@ -42,7 +42,6 @@ binding window { internal "JSObject *" document; internal "JSObject *" navigator; internal "JSObject *" console; - internal "JSObject *" location; property unshared type EventHandler; } @@ -58,9 +57,6 @@ api mark %{ if (private->console != NULL) { JSAPI_GCMARK(private->console); } - if (private->location != NULL) { - JSAPI_GCMARK(private->location); - } } %} @@ -180,13 +176,6 @@ api new %{ return NULL; } - private->location = jsapi_new_Location(cx, NULL, newobject, bw, - llcache_handle_get_url(private->htmlc->base.llcache)); - if (private->location == NULL) { - free(private); - return NULL; - } - private->console = jsapi_new_Console(cx, NULL, newobject); if (private->console == NULL) { free(private); @@ -210,14 +199,6 @@ operation prompt %{ warn_user(message, NULL); %} -getter window %{ - jsret = obj; -%} - -getter self %{ - jsret = obj; -%} - /* boolean dispatchEvent(Event event); */ operation dispatchEvent %{ /* this implementation is unique to the window object as it is @@ -225,49 +206,63 @@ operation dispatchEvent %{ */ /* caution, this must match the struct generated from event.bnd */ - if (event == JSVAL_VOID) { + struct { + dom_event *event; + } *event_private; + dom_string *type_dom = NULL; + dom_exception exc; + jsval eventval = JSVAL_VOID; + jsval event_argv[1]; + jsval event_rval; + + event_private = JS_GetInstancePrivate(cx, event, &JSClass_Event, NULL); + if (event_private->event == NULL) { + /** @todo type error? */ jsret = JS_FALSE; } else { - struct { - dom_event *event; - } *event_private; - dom_string *type_dom = NULL; - dom_exception exc; - jsval eventval = JSVAL_VOID; - jsval event_argv[1]; - jsval event_rval; - - event_private = JS_GetInstancePrivate(cx, event, &JSClass_Event, NULL); - if (event_private->event == NULL) { - /** @todo type error? */ - jsret = JS_FALSE; - } else { - exc = dom_event_get_type(event_private->event, &type_dom); - if (exc == DOM_NO_ERR) { - - 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] = eventval; - jsret = JS_CallFunctionValue(cx, NULL, eventval, 1, event_argv, &event_rval); - } + exc = dom_event_get_type(event_private->event, &type_dom); + if (exc == DOM_NO_ERR) { + + if (dom_string_isequal(type_dom, corestring_dom_load)) { + JS_GetProperty(cx, JSAPI_THIS_OBJECT(cx, vp), "onload", &eventval); + } + + if (!JSVAL_IS_VOID(eventval)) { + event_argv[0] = eventval; + jsret = JS_CallFunctionValue(cx, NULL, eventval, 1, event_argv, &event_rval); } } - } + } +%} + +getter location %{ + jsval loc; + JS_GetProperty(cx, private->document, "location", &loc); + jsret = JSVAL_TO_OBJECT(loc); +%} + +getter window %{ + jsret = obj; +%} + +getter self %{ + jsret = obj; %} 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))); + JSLOG("propname[%d]=\"%s\"", + tinyid, + jsclass_properties[tinyid].name); %} 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))); + JSLOG("propname[%d]=\"%s\"", + tinyid, + jsclass_properties[tinyid].name); %} diff --git a/javascript/none.c b/javascript/none.c index 6354a12ff..3e7b39cb3 100644 --- a/javascript/none.c +++ b/javascript/none.c @@ -54,7 +54,7 @@ bool js_exec(jscontext *ctx, const char *txt, size_t txtlen) return true; } -bool js_fire_event(jscontext *ctx, const char *type, void *target) +bool js_fire_event(jscontext *ctx, const char *type, struct dom_document *doc, struct dom_node *target) { return true; } |