add element object

This commit is contained in:
Vincent Sanders 2012-08-05 23:42:30 +01:00
parent e1b37918e8
commit 135aae3921
4 changed files with 120 additions and 6 deletions

View File

@ -31,7 +31,7 @@ S_DESKTOP := cookies.c history_global_core.c hotlist.c knockout.c \
# Javascript sources
ifeq ($(NETSURF_USE_JS),YES)
S_JSAPI = window.c document.c navigator.c console.c
S_JSAPI = window.c document.c navigator.c console.c element.c
S_JAVASCRIPT += content.c jsapi.c $(addprefix jsapi/,$(S_JSAPI))
else
S_JAVASCRIPT += none.c

View File

@ -136,6 +136,9 @@ JS_NewCompartmentAndGlobalObject(JSContext *cx,
#endif
typedef struct dom_element dom_element;
typedef struct html_content html_content;
/** Create a new javascript window object
*
* @param cx The javascript context.
@ -170,4 +173,13 @@ JSObject *jsapi_new_console(JSContext *cx, JSObject *parent);
*/
JSObject *jsapi_new_navigator(JSContext *cx, JSObject *parent);
/** Create a new javascript element object
*
* @param cx The javascript context.
* @param parent The parent object, usually a global window object
* @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);
#endif

View File

@ -144,11 +144,7 @@ static JSBool JSAPI_NATIVE(getElementById, JSContext *cx, uintN argc, jsval *vp)
dom_document_get_element_by_id(htmlc->document, idstr, &idelement);
if (idelement==NULL) {
JSAPI_SET_RVAL(cx, vp, JSVAL_NULL);
} else {
/* create element object and return it*/
}
JSAPI_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(jsapi_new_element(cx, JS_GetGlobalObject(cx), htmlc, idelement)));
return JS_TRUE;
}

106
javascript/jsapi/element.c Normal file
View File

@ -0,0 +1,106 @@
/*
* Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
*
* This file is part of NetSurf, http://www.netsurf-browser.org/
*
* NetSurf is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* NetSurf is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <dom/dom.h>
#include "javascript/jsapi.h"
#include "utils/config.h"
#include "render/html_internal.h"
#include "utils/log.h"
static void jsfinalize_element(JSContext *cx, JSObject *obj);
typedef struct {
struct html_content *htmlc;
struct dom_element *dom_element;
} elementp;
static JSClass jsclass_element =
{
"element",
JSCLASS_HAS_PRIVATE,
JS_PropertyStub,
JS_PropertyStub,
JS_PropertyStub,
JS_StrictPropertyStub,
JS_EnumerateStub,
JS_ResolveStub,
JS_ConvertStub,
jsfinalize_element,
JSCLASS_NO_OPTIONAL_MEMBERS
};
static void jsfinalize_element(JSContext *cx, JSObject *obj)
{
elementp *element;
element = JS_GetInstancePrivate(cx, obj, &jsclass_element, NULL);
if (element != NULL) {
free(element);
}
}
static JSFunctionSpec jsfunctions_element[] = {
JSAPI_FS_END
};
JSObject *
jsapi_new_element(JSContext *cx,
JSObject *parent,
struct html_content *htmlc,
struct dom_element *domelement)
{
/* create element object and return it*/
JSObject *jselement;
elementp *element;
element = malloc(sizeof(element));
if (element == NULL) {
return NULL;
}
element->htmlc = htmlc;
element->dom_element = domelement;
jselement = JS_InitClass(cx,
parent,
NULL,
&jsclass_element,
NULL,
0,
NULL,
jsfunctions_element,
NULL,
NULL);
if (jselement == NULL) {
free(element);
return NULL;
}
LOG(("setting element private to %p", element));
/* private pointer to browsing context */
if (JS_SetPrivate(cx, jselement, element) != JS_TRUE) {
LOG(("failed to set content"));
free(element);
return NULL;
}
return jselement;
}