From 3f1b68384562fe294a1a263214a3fd26ea869bc9 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sat, 3 Nov 2012 21:37:06 +0000 Subject: implement dom-getElementsByTagName and nodelist and htmlcollection --- Makefile.sources.javascript | 2 + javascript/jsapi/binding.h | 28 ++++++++++++ javascript/jsapi/dom.bnd | 52 ++++++++++++++++++++- javascript/jsapi/htmlcollection.bnd | 85 +++++++++++++++++++++++++++++++++++ javascript/jsapi/nodelist.bnd | 67 +++++++++++++++++++++++++++ javascript/jsapi/window.bnd | 10 +++++ test/js/dom-getElementsByTagName.html | 21 +++++++++ 7 files changed, 263 insertions(+), 2 deletions(-) create mode 100644 javascript/jsapi/htmlcollection.bnd create mode 100644 javascript/jsapi/nodelist.bnd create mode 100644 test/js/dom-getElementsByTagName.html diff --git a/Makefile.sources.javascript b/Makefile.sources.javascript index 9803f7a83..ca0c146ee 100644 --- a/Makefile.sources.javascript +++ b/Makefile.sources.javascript @@ -16,6 +16,8 @@ JSAPI_BINDING_window := javascript/jsapi/window.bnd JSAPI_BINDING_navigator := javascript/jsapi/navigator.bnd 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 # 1: input file # 2: output file diff --git a/javascript/jsapi/binding.h b/javascript/jsapi/binding.h index 68ab6d1ed..c1006589e 100644 --- a/javascript/jsapi/binding.h +++ b/javascript/jsapi/binding.h @@ -96,4 +96,32 @@ JSObject *jsapi_new_HTMLElement(JSContext *cx, dom_element *node, struct html_content *htmlc); +JSObject *jsapi_InitClass_HTMLCollection(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_HTMLCollection(JSContext *cx, + JSObject *prototype, + JSObject *parent, + dom_html_collection *collection, + struct html_content *htmlc); + +JSObject *jsapi_InitClass_NodeList(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_NodeList(JSContext *cx, + JSObject *prototype, + JSObject *parent, + dom_nodelist *nodelist, + struct html_content *htmlc); + #endif diff --git a/javascript/jsapi/dom.bnd b/javascript/jsapi/dom.bnd index 362c828f6..14068ba2e 100644 --- a/javascript/jsapi/dom.bnd +++ b/javascript/jsapi/dom.bnd @@ -7,14 +7,59 @@ operation getElementById %{ dom_element *element; dom_exception exc; - dom_string_create((unsigned char*)elementId, elementId_len, &elementId_dom); + exc = dom_string_create((unsigned char*)elementId, elementId_len, &elementId_dom); + if (exc != DOM_NO_ERR) { + return JS_FALSE; + } exc = dom_document_get_element_by_id(private->node, elementId_dom, &element); - if ((exc == DOM_NO_ERR) && (element != NULL)) { + if (exc != DOM_NO_ERR) { + return JS_FALSE; + } + + if (element != NULL) { jsret = jsapi_new_HTMLElement(cx, NULL, NULL, element, private->htmlc); } %} +/* Dom 4 says this should return a htmlcollection, libdom currently + * returns DOM 3 spec of a nodelist + */ + +operation getElementsByTagName %{ + dom_string *localName_dom; + /* dom_html_collection *collection;*/ + dom_nodelist *nodelist; + dom_exception exc; + + exc = dom_string_create((uint8_t *)localName, localName_len, &localName_dom); + if (exc != DOM_NO_ERR) { + return JS_FALSE; + } + + LOG(("here")); + + exc = dom_document_get_elements_by_tag_name(private->node, localName_dom, /*&collection*/&nodelist); + if (exc != DOM_NO_ERR) { + return JS_FALSE; + } + LOG(("nodelist %p", nodelist)); + + if (/*collection*/nodelist != NULL) { + /* jsret = jsapi_new_HTMLCollection(cx, + NULL, + NULL, + collection, + private->htmlc);*/ + jsret = jsapi_new_NodeList(cx, + NULL, + NULL, + nodelist, + private->htmlc); + } + +%} + getter textContent %{ dom_exception exc; dom_string *content; @@ -24,3 +69,6 @@ getter textContent %{ jsret = JS_NewStringCopyN(cx, dom_string_data(content), dom_string_length(content)); } %} + + + diff --git a/javascript/jsapi/htmlcollection.bnd b/javascript/jsapi/htmlcollection.bnd new file mode 100644 index 000000000..a7947cd29 --- /dev/null +++ b/javascript/jsapi/htmlcollection.bnd @@ -0,0 +1,85 @@ +/* Binding to generate HTMLcolelction interface + * + * The js_libdom (javascript to libdom) binding type is currently the + * only one implemented and this principly describes that binding. + * + * 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 + */ + +/* The hdrcomment are added into the geenrated output comment header */ +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" + +%} + +webidlfile "dom.idl"; + +binding htmlcollection { + type js_libdom; /* the binding type */ + + interface HTMLCollection; /* The WebIDL interface to generate a binding for */ + + private "dom_html_collection *" collection; + private "struct html_content *" htmlc; +} + +getter length %{ + dom_exception err; + + err = dom_html_collection_get_length(private->collection, &jsret); + if (err != DOM_NO_ERR) { + return JS_FALSE; + } + %} + +operation item %{ + dom_exception err; + dom_node *domnode; + + err = dom_html_collection_item(private->collection, index, &domnode); + if (err != DOM_NO_ERR) { + return JS_FALSE; + } + + if (domnode != NULL) { + jsret = jsapi_new_HTMLElement(cx, NULL, NULL, (dom_element *)domnode, private->htmlc); + } + %} + +operation namedItem %{ + dom_exception err; + dom_node *domnode; + dom_string *name_dom; + + err = dom_string_create((uint8_t *)name, name_len, &name_dom); + if (err != DOM_NO_ERR) { + return JS_FALSE; + } + + err = dom_html_collection_named_item(private->collection, name_dom, &domnode); + if (err != DOM_NO_ERR) { + return JS_FALSE; + } + + if (domnode != NULL) { + jsret = jsapi_new_HTMLElement(cx, NULL, NULL, (dom_element *)domnode, private->htmlc); + } + + %} diff --git a/javascript/jsapi/nodelist.bnd b/javascript/jsapi/nodelist.bnd new file mode 100644 index 000000000..710536dcf --- /dev/null +++ b/javascript/jsapi/nodelist.bnd @@ -0,0 +1,67 @@ +/* Binding to generate NodeList interface + * + * The js_libdom (javascript to libdom) binding type is currently the + * only one implemented and this principly describes that binding. + * + * 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 + */ + +/* The hdrcomment are added into the geenrated output comment header */ +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" + +%} + +webidlfile "dom.idl"; + +binding nodelist { + type js_libdom; /* the binding type */ + + interface NodeList; /* The WebIDL interface to generate a binding for */ + + private "dom_nodelist *" nodelist; + private "struct html_content *" htmlc; +} + +getter length %{ + dom_exception err; + + err = dom_nodelist_get_length(private->nodelist, &jsret); + if (err != DOM_NO_ERR) { + return JS_FALSE; + } +%} + +operation item %{ + dom_exception err; + dom_node *domnode; + + err = dom_nodelist_item(private->nodelist, index, &domnode); + if (err != DOM_NO_ERR) { + return JS_FALSE; + } + + if (domnode != NULL) { + jsret = jsapi_new_HTMLElement(cx, NULL, NULL, (dom_element *)domnode, private->htmlc); + } +%} + + + diff --git a/javascript/jsapi/window.bnd b/javascript/jsapi/window.bnd index da6400332..6f5e1af47 100644 --- a/javascript/jsapi/window.bnd +++ b/javascript/jsapi/window.bnd @@ -103,6 +103,16 @@ api init %{ return NULL; } + user_proto = jsapi_InitClass_HTMLCollection(cx, prototype); + if (user_proto == NULL) { + return NULL; + } + + user_proto = jsapi_InitClass_NodeList(cx, prototype); + if (user_proto == NULL) { + return NULL; + } + %} api new %{ diff --git a/test/js/dom-getElementsByTagName.html b/test/js/dom-getElementsByTagName.html new file mode 100644 index 000000000..02cdffc4d --- /dev/null +++ b/test/js/dom-getElementsByTagName.html @@ -0,0 +1,21 @@ + + +Call getElementsByTagName + + + +

Call getElementsByTagName

+

p one

+

p two

+

p three

+ +

length: +

+ + + -- cgit v1.2.3