From 43fb761f450e463dbce7431d20606cf795daf04c Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Wed, 7 Nov 2012 18:47:35 +0000 Subject: add document.body, head and documentElement getters add a dom utility file and use it --- javascript/jsapi/htmldocument.bnd | 61 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) (limited to 'javascript/jsapi') diff --git a/javascript/jsapi/htmldocument.bnd b/javascript/jsapi/htmldocument.bnd index 7574512dd..1432ce5da 100644 --- a/javascript/jsapi/htmldocument.bnd +++ b/javascript/jsapi/htmldocument.bnd @@ -12,6 +12,8 @@ preamble %{ #include "utils/config.h" #include "utils/log.h" +#include "utils/corestrings.h" +#include "utils/domutils.h" #include "content/urldb.h" @@ -23,13 +25,13 @@ preamble %{ binding document { type js_libdom; /* the binding type */ + interface Document; /* Web IDL interface to generate */ + /* parameters to constructor value stored in private * context structure. */ private "dom_document *" node; private "struct html_content *" htmlc; - - interface Document; /* Web IDL interface to generate */ } api finalise %{ @@ -49,6 +51,61 @@ getter cookie %{ } %} +getter documentElement %{ + dom_exception exc; + dom_element *element; + + /* document (html) element */ + exc = dom_document_get_document_element(private->node, (void *)&element); + if (exc != DOM_NO_ERR) { + return JS_FALSE; + } + + if (element != NULL) { + jsret = jsapi_new_HTMLElement(cx, NULL, NULL, element, private->htmlc); + } +%} + +getter head %{ + dom_node *element; + dom_node *head; + dom_exception exc; + + /* document (html) element */ + exc = dom_document_get_document_element(private->node, &element); + if (exc != DOM_NO_ERR) { + return JS_FALSE; + } + + if (element != NULL) { + head = find_first_named_dom_element(element, corestring_lwc_head) ; + if (head != NULL) { + jsret = jsapi_new_HTMLElement(cx, NULL, NULL, (dom_element *)head, private->htmlc); + } + dom_node_unref(element); + } +%} + +getter body %{ + dom_node *element; + dom_node *body; + dom_exception exc; + + /* document (html) element */ + exc = dom_document_get_document_element(private->node, &element); + if (exc != DOM_NO_ERR) { + return JS_FALSE; + } + + if (element != NULL) { + body = find_first_named_dom_element(element, corestring_lwc_body) ; + if (body != NULL) { + jsret = jsapi_new_HTMLElement(cx, NULL, NULL, (dom_element *)body, private->htmlc); + } + dom_node_unref(element); + } +%} + operation getElementById %{ dom_string *elementId_dom; dom_element *element; -- cgit v1.2.3 From ca24b238ed2c12f39e699ae16d15f00b0b064795 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Wed, 7 Nov 2012 18:52:30 +0000 Subject: add Text interface --- Makefile.sources.javascript | 1 + javascript/jsapi/dom.bnd | 7 +++++++ javascript/jsapi/text.bnd | 44 ++++++++++++++++++++++++++++++++++++++++++++ javascript/jsapi/window.bnd | 5 +++++ 4 files changed, 57 insertions(+) create mode 100644 javascript/jsapi/text.bnd (limited to 'javascript/jsapi') diff --git a/Makefile.sources.javascript b/Makefile.sources.javascript index da7c2101e..88412ec57 100644 --- a/Makefile.sources.javascript +++ b/Makefile.sources.javascript @@ -19,6 +19,7 @@ JSAPI_BINDING_console := javascript/jsapi/console.bnd JSAPI_BINDING_location := javascript/jsapi/location.bnd JSAPI_BINDING_htmlcollection := javascript/jsapi/htmlcollection.bnd JSAPI_BINDING_nodelist := javascript/jsapi/nodelist.bnd +JSAPI_BINDING_text := javascript/jsapi/text.bnd # 1: input file # 2: output file diff --git a/javascript/jsapi/dom.bnd b/javascript/jsapi/dom.bnd index cd252fc27..c2f9e787e 100644 --- a/javascript/jsapi/dom.bnd +++ b/javascript/jsapi/dom.bnd @@ -2,6 +2,8 @@ webidlfile "dom.idl"; +/* interface Node members */ + getter textContent %{ dom_exception exc; @@ -21,3 +23,8 @@ getter textContent %{ +operation appendChild %{ +%} + +operation createTextNode %{ +%} diff --git a/javascript/jsapi/text.bnd b/javascript/jsapi/text.bnd new file mode 100644 index 000000000..d440e4a9e --- /dev/null +++ b/javascript/jsapi/text.bnd @@ -0,0 +1,44 @@ +/* Binding to generate Text 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 text { + type js_libdom; /* the binding type */ + + interface Text; /* Web IDL interface to generate */ + + private "dom_node *" node; +} + +api finalise %{ + if (private != NULL) { + dom_node_unref(private->node); + } +%} diff --git a/javascript/jsapi/window.bnd b/javascript/jsapi/window.bnd index 865cbf3d4..5a7de530f 100644 --- a/javascript/jsapi/window.bnd +++ b/javascript/jsapi/window.bnd @@ -130,6 +130,11 @@ api init %{ return NULL; } + user_proto = jsapi_InitClass_Text(cx, prototype); + if (user_proto == NULL) { + return NULL; + } + %} api new %{ -- cgit v1.2.3 From 882fd4f5c5eead48d1f056596a6e66329bd9c144 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Wed, 7 Nov 2012 19:55:42 +0000 Subject: implement document.createTextNode operation --- javascript/jsapi/binding.h | 14 ++++++++++++++ javascript/jsapi/dom.bnd | 4 +--- javascript/jsapi/htmldocument.bnd | 22 ++++++++++++++++++++++ javascript/jsapi/text.bnd | 2 +- 4 files changed, 38 insertions(+), 4 deletions(-) (limited to 'javascript/jsapi') diff --git a/javascript/jsapi/binding.h b/javascript/jsapi/binding.h index c1006589e..446fb0c51 100644 --- a/javascript/jsapi/binding.h +++ b/javascript/jsapi/binding.h @@ -124,4 +124,18 @@ JSObject *jsapi_new_NodeList(JSContext *cx, dom_nodelist *nodelist, struct html_content *htmlc); + +JSObject *jsapi_InitClass_Text(JSContext *cx, JSObject *parent); +/** Create a new javascript text object + * + * @param cx The javascript context. + * @param parent The parent object, usually a global window object + * @param node The dom node to use in the object + * @return new javascript object or NULL on error + */ +JSObject *jsapi_new_Text(JSContext *cx, + JSObject *prototype, + JSObject *parent, + dom_text *node); + #endif diff --git a/javascript/jsapi/dom.bnd b/javascript/jsapi/dom.bnd index c2f9e787e..bf3b44ea3 100644 --- a/javascript/jsapi/dom.bnd +++ b/javascript/jsapi/dom.bnd @@ -24,7 +24,5 @@ getter textContent %{ operation appendChild %{ -%} - -operation createTextNode %{ +/* void * JS_GetInstancePrivate(JSContext *cx, JSObject *obj, JSClass *clasp, jsval *argv); */ %} diff --git a/javascript/jsapi/htmldocument.bnd b/javascript/jsapi/htmldocument.bnd index 1432ce5da..b642cabe6 100644 --- a/javascript/jsapi/htmldocument.bnd +++ b/javascript/jsapi/htmldocument.bnd @@ -169,3 +169,25 @@ operation write %{ dom_hubbub_parser_insert_chunk(private->htmlc->parser, (uint8_t *)text, text_len); } %} + +/* in dom Document */ +operation createTextNode %{ + dom_string *data_dom; + dom_element *element; + dom_exception exc; + dom_text *text; + + exc = dom_string_create((unsigned char*)data, data_len, &data_dom); + if (exc != DOM_NO_ERR) { + return JS_FALSE; + } + + exc = dom_document_create_text_node(private->node, data_dom, &text); + dom_string_unref(data_dom); + if (exc != DOM_NO_ERR) { + return JS_FALSE; + } + + jsret = jsapi_new_Text(cx, NULL, NULL, text); + +%} diff --git a/javascript/jsapi/text.bnd b/javascript/jsapi/text.bnd index d440e4a9e..53e93803d 100644 --- a/javascript/jsapi/text.bnd +++ b/javascript/jsapi/text.bnd @@ -34,7 +34,7 @@ binding text { interface Text; /* Web IDL interface to generate */ - private "dom_node *" node; + private "dom_text *" node; } api finalise %{ -- cgit v1.2.3