From 2b8cdf1f295cc65c5f6935f067289fdb35e3349d Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Thu, 1 Nov 2012 21:13:14 +0000 Subject: add HTMLElement interface binding --- javascript/jsapi/binding.h | 7 ++++- javascript/jsapi/bindings/dom.bnd | 22 +++++++++++-- javascript/jsapi/bindings/htmlelement.bnd | 51 +++++++++++++++++++++++++++++++ javascript/jsapi/bindings/navigator.bnd | 12 ++++---- javascript/jsapi/bindings/window.bnd | 9 ++++-- 5 files changed, 89 insertions(+), 12 deletions(-) create mode 100644 javascript/jsapi/bindings/htmlelement.bnd (limited to 'javascript') diff --git a/javascript/jsapi/binding.h b/javascript/jsapi/binding.h index 80340a57a..8b32e91db 100644 --- a/javascript/jsapi/binding.h +++ b/javascript/jsapi/binding.h @@ -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 diff --git a/javascript/jsapi/bindings/dom.bnd b/javascript/jsapi/bindings/dom.bnd index f323ff2ad..0f54571b9 100644 --- a/javascript/jsapi/bindings/dom.bnd +++ b/javascript/jsapi/bindings/dom.bnd @@ -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)); + + + 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); + } +%} - dom_document_get_element_by_id(private->node, elementId_dom, &element); +getter textContent %{ + dom_exception exc; + dom_string *content; - jsretval = OBJECT_TO_JSVAL(jsapi_new_element(cx, JS_GetGlobalObject(cx), private->htmlc, element)); + 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)); + } %} diff --git a/javascript/jsapi/bindings/htmlelement.bnd b/javascript/jsapi/bindings/htmlelement.bnd new file mode 100644 index 000000000..596bb7de0 --- /dev/null +++ b/javascript/jsapi/bindings/htmlelement.bnd @@ -0,0 +1,51 @@ +/* Binding to generate HTMLElement interface + * + * Copyright 2012 Vincent Sanders + * + * 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 "; +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 + +#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; +} + diff --git a/javascript/jsapi/bindings/navigator.bnd b/javascript/jsapi/bindings/navigator.bnd index 596f1ac63..e63e9a9fd 100644 --- a/javascript/jsapi/bindings/navigator.bnd +++ b/javascript/jsapi/bindings/navigator.bnd @@ -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()); %} diff --git a/javascript/jsapi/bindings/window.bnd b/javascript/jsapi/bindings/window.bnd index ba2db52b2..beb2b128e 100644 --- a/javascript/jsapi/bindings/window.bnd +++ b/javascript/jsapi/bindings/window.bnd @@ -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 %{ -- cgit v1.2.3