implement dom-getElementsByTagName and nodelist and htmlcollection

This commit is contained in:
Vincent Sanders 2012-11-03 21:37:06 +00:00
parent 6648ba39ad
commit 3f1b683845
7 changed files with 263 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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));
}
%}

View File

@ -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 <vince@netsurf-browser.org>
*
* 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 <vince@netsurf-browser.org>";
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 <dom/dom.h>
#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);
}
%}

View File

@ -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 <vince@netsurf-browser.org>
*
* 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 <vince@netsurf-browser.org>";
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 <dom/dom.h>
#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);
}
%}

View File

@ -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 %{

View File

@ -0,0 +1,21 @@
<html>
<head>
<title>Call getElementsByTagName</title>
<link rel="stylesheet" type="text/css" href="tst.css">
</head>
<body>
<h1>Call getElementsByTagName</h1>
<p>p one</p>
<p>p two</p>
<p>p three</p>
<script>var pcol = document.getElementsByTagName("p");</script>
<p>length: <script>document.write(pcol.length);</script>
<p>
<script>
for (var i=0;i<pcol.length;i++)
{
document.write(pcol.item(i).textContent);
}
</script>
</body>
</html>