From 135aae3921322afc7241185080e0dfc8a2de4fd4 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sun, 5 Aug 2012 23:42:30 +0100 Subject: [PATCH] add element object --- Makefile.sources | 2 +- javascript/jsapi.h | 12 ++++ javascript/jsapi/document.c | 6 +- javascript/jsapi/element.c | 106 ++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 6 deletions(-) create mode 100644 javascript/jsapi/element.c diff --git a/Makefile.sources b/Makefile.sources index 95695f786..f7f05ca3a 100644 --- a/Makefile.sources +++ b/Makefile.sources @@ -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 diff --git a/javascript/jsapi.h b/javascript/jsapi.h index a44323f77..b23b4d30a 100644 --- a/javascript/jsapi.h +++ b/javascript/jsapi.h @@ -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 diff --git a/javascript/jsapi/document.c b/javascript/jsapi/document.c index 49144c4a9..41d889873 100644 --- a/javascript/jsapi/document.c +++ b/javascript/jsapi/document.c @@ -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; } diff --git a/javascript/jsapi/element.c b/javascript/jsapi/element.c new file mode 100644 index 000000000..11d8b1a14 --- /dev/null +++ b/javascript/jsapi/element.c @@ -0,0 +1,106 @@ +/* + * Copyright 2012 Vincent Sanders + * + * 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 . + */ + +#include + + +#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; +}