add HTMLElement interface binding

This commit is contained in:
Vincent Sanders 2012-11-01 21:13:14 +00:00
parent 8f08f820f4
commit 2b8cdf1f29
6 changed files with 92 additions and 14 deletions

View File

@ -11,6 +11,7 @@
S_JSAPI_BINDING:=
JSAPI_BINDING_htmldocument := javascript/jsapi/bindings/htmldocument.bnd
JSAPI_BINDING_htmlelement := javascript/jsapi/bindings/htmlelement.bnd
JSAPI_BINDING_window := javascript/jsapi/bindings/window.bnd
JSAPI_BINDING_navigator := javascript/jsapi/bindings/navigator.bnd
JSAPI_BINDING_console := javascript/jsapi/bindings/console.bnd
@ -31,8 +32,8 @@ endef
# Javascript sources
ifeq ($(NETSURF_USE_JS),YES)
S_JSAPI = htmlelement.c
#htmldocument.c window.c navigator.c console.c
S_JSAPI =
# htmlelement.c htmldocument.c window.c navigator.c console.c
S_JAVASCRIPT += content.c jsapi.c $(addprefix jsapi/,$(S_JSAPI))

View File

@ -76,6 +76,7 @@ JSObject *jsapi_InitClass_Navigator(JSContext *cx, JSObject *parent);
*/
JSObject *jsapi_new_Navigator(JSContext *cx, JSObject *proto, JSObject *parent);
JSObject *jsapi_InitClass_HTMLElement(JSContext *cx, JSObject *parent);
/** Create a new javascript element object
*
* @param cx The javascript context.
@ -83,6 +84,10 @@ JSObject *jsapi_new_Navigator(JSContext *cx, JSObject *proto, JSObject *parent);
* @param doc_priv The private context to set on the object
* @return new javascript object or NULL on error
*/
JSObject *jsapi_new_element(JSContext *cx, JSObject *parent, struct html_content *htmlc, struct dom_element *domelement);
JSObject *jsapi_new_HTMLElement(JSContext *cx,
JSObject *prototype,
JSObject *parent,
dom_element *node,
struct html_content *htmlc);
#endif

View File

@ -1,14 +1,30 @@
/* test binding for document - must be included */
/* DOM bindings entries */
webidlfile "dom.idl";
operation getElementById %{
dom_string *elementId_dom;
dom_element *element;
dom_exception exc;
LOG(("elementId_len %d elementId %s",elementId_len,elementId));
dom_string_create((unsigned char*)elementId, elementId_len, &elementId_dom);
LOG(("dom string %p", elementId_dom));
dom_document_get_element_by_id(private->node, elementId_dom, &element);
jsretval = OBJECT_TO_JSVAL(jsapi_new_element(cx, JS_GetGlobalObject(cx), private->htmlc, element));
exc = dom_document_get_element_by_id(private->node, elementId_dom, &element);
if ((exc == DOM_NO_ERR) && (element != NULL)) {
jsret = jsapi_new_HTMLElement(cx, NULL, JS_GetGlobalObject(cx), element, private->htmlc);
}
%}
getter textContent %{
dom_exception exc;
dom_string *content;
exc = dom_node_get_text_content(private->node, &content);
if ((exc == DOM_NO_ERR) && (content != NULL)) {
jsret = JS_NewStringCopyN(cx, dom_string_data(content), dom_string_length(content));
}
%}

View File

@ -0,0 +1,51 @@
/* Binding to generate HTMLElement 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
*/
#include "dom.bnd"
webidlfile "html.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 htmlelement {
type js_libdom; /* the binding type */
interface HTMLElement; /* Web IDL interface to generate */
/* private members:
* - stored in private context structure.
* - passed as parameters to constructor and stored automatically.
* - are *not* considered for property getters/setters.
*
* internal members:
* - value stored in private context structure
* - not passed to constructor
* - must be instantiated by constructor
* - are considered for property getters/setters.
*/
private "dom_element *" node;
private "struct html_content *" htmlc;
}

View File

@ -76,22 +76,22 @@ binding navigator {
}
getter appName %{
jsretval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, NAVIGATOR_APPNAME));
jsret = JS_NewStringCopyZ(cx, NAVIGATOR_APPNAME);
%}
getter appCodeName %{
jsretval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, NAVIGATOR_APPCODENAME));
jsret = JS_NewStringCopyZ(cx, NAVIGATOR_APPCODENAME);
%}
getter appVersion %{
jsretval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, netsurf_version));
jsret = JS_NewStringCopyZ(cx, netsurf_version);
%}
getter language %{
const char *alang = nsoption_charp(accept_language);
if (alang != NULL) {
jsretval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, alang));
jsret = JS_NewStringCopyZ(cx, alang);
}
%}
@ -109,12 +109,12 @@ getter platform %{
platstr = malloc(platstrlen);
if (platstr != NULL) {
snprintf(platstr, platstrlen, "%s %s", cutsname->sysname, cutsname->machine);
jsretval = STRING_TO_JSVAL(JS_NewStringCopyN(cx, platstr, platstrlen - 1));
jsret = JS_NewStringCopyN(cx, platstr, platstrlen - 1);
free(platstr);
}
}
%}
getter userAgent %{
jsretval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, user_agent_string()));
jsret = JS_NewStringCopyZ(cx, user_agent_string());
%}

View File

@ -31,11 +31,11 @@ operation prompt %{
%}
getter window %{
jsretval = OBJECT_TO_JSVAL(obj);
jsret = obj;
%}
getter self %{
jsretval = OBJECT_TO_JSVAL(obj);
jsret = obj;
%}
api init %{
@ -90,6 +90,11 @@ api init %{
return NULL;
}
user_proto = jsapi_InitClass_HTMLElement(cx, prototype);
if (user_proto == NULL) {
return NULL;
}
%}
api new %{