netsurf/javascript/jsapi/dom.bnd

173 lines
3.6 KiB
Plaintext
Raw Normal View History

2012-12-17 19:15:11 +04:00
/* Binding to generate interfaces for the DOM IDL
*
* 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
*/
webidlfile "dom.idl";
preamble %{
#include "comment.h"
#include "text.h"
#include "htmlelement.h"
%}
prologue %{
/* CAUTION this expects all javascript Node objects private pointers
* to have private->node in the same place.
*/
static struct dom_node *jsnode_to_domnode(JSContext *cx, JSObject *jsnode)
{
struct jsclass_private *jsnode_private;
if (jsnode == NULL) {
return NULL;
}
/* element */
jsnode_private = JS_GetInstancePrivate(cx,
jsnode,
&JSClass_HTMLElement,
NULL);
if (jsnode_private != NULL) {
return (struct dom_node *)jsnode_private->node;
}
/* text */
jsnode_private = JS_GetInstancePrivate(cx,
jsnode,
&JSClass_Text,
NULL);
if (jsnode_private != NULL) {
return (struct dom_node *)jsnode_private->node;
}
/* comment */
jsnode_private = JS_GetInstancePrivate(cx,
jsnode,
&JSClass_Comment,
NULL);
if (jsnode_private != NULL) {
return (struct dom_node *)jsnode_private->node;
}
return NULL;
}
%}
2012-11-07 22:52:30 +04:00
/* interface Node members */
2012-11-19 01:12:49 +04:00
getter nodeType %{
dom_exception exc;
dom_node_type node_type;
exc = dom_node_get_node_type(private->node, &node_type);
if (exc != DOM_NO_ERR) {
return JS_FALSE;
}
jsret = node_type;
%}
2012-11-19 01:24:29 +04:00
getter nodeName %{
dom_exception exc;
dom_string *name;
exc = dom_node_get_node_name(private->node, &name);
if (exc != DOM_NO_ERR) {
return JS_FALSE;
}
if (name != NULL) {
2012-11-19 01:50:11 +04:00
jsret = JS_NewStringCopyN(cx,
dom_string_data(name),
dom_string_length(name));
2012-11-19 01:24:29 +04:00
dom_string_unref(name);
2012-11-19 01:50:11 +04:00
}
%}
getter nodeValue %{
dom_exception exc;
dom_string *value;
exc = dom_node_get_node_value(private->node, &value);
if (exc != DOM_NO_ERR) {
return JS_FALSE;
}
2012-11-19 01:24:29 +04:00
2012-11-19 01:50:11 +04:00
if (value != NULL) {
jsret = JS_NewStringCopyN(cx,
dom_string_data(value),
dom_string_length(value));
dom_string_unref(value);
2012-11-19 01:24:29 +04:00
}
%}
2012-11-02 01:13:14 +04:00
getter textContent %{
2012-11-09 14:52:32 +04:00
dom_exception exc;
dom_string *content;
2012-11-09 14:52:32 +04:00
exc = dom_node_get_text_content(private->node, &content);
if (exc != DOM_NO_ERR) {
2012-11-04 15:18:37 +04:00
return JS_FALSE;
}
if (content != NULL) {
2012-11-09 14:52:32 +04:00
jsret = JS_NewStringCopyN(cx, dom_string_data(content), dom_string_length(content));
2012-11-04 15:18:37 +04:00
dom_string_unref(content);
2012-11-09 14:52:32 +04:00
}
%}
/* interface Node { Node appendChild(Node node); } */
2012-11-07 22:52:30 +04:00
operation appendChild %{
struct dom_node *domnode; /* dom node from js input node */
struct dom_node *result = NULL;
2012-11-09 14:52:32 +04:00
dom_exception exc;
dom_node_type node_type;
domnode = jsnode_to_domnode(cx, node);
if (domnode == NULL) {
/* should cause Error: NOT_FOUND_ERR: DOM Exception 8 */
JSLOG("Error: NOT_FOUND_ERR: DOM Exception 8");
2012-11-09 14:52:32 +04:00
return JS_FALSE;
}
JSLOG("appending js node %p (dom %p)", node, domnode);
2012-11-09 14:52:32 +04:00
/* append the found element */
exc = dom_node_append_child(private->node, domnode, &result);
2012-11-09 14:52:32 +04:00
if (exc != DOM_NO_ERR) {
JSLOG("Error: DOM Exception (libdom append child)");
2012-11-09 14:52:32 +04:00
return JS_FALSE;
}
if (result != NULL) {
exc = dom_node_get_node_type(result, &node_type);
if (exc != DOM_NO_ERR) {
return JS_FALSE;
}
switch (node_type) {
case DOM_ELEMENT_NODE:
jsret = jsapi_new_HTMLElement(cx, NULL, NULL, (dom_element *)result, private->htmlc);
break;
case DOM_TEXT_NODE:
jsret = jsapi_new_Text(cx, NULL, NULL, (dom_text *)result, private->htmlc);
break;
default:
JSLOG("Unsupported result node type %d", node_type);
}
} else {
JSLOG("No result");
}
2012-11-07 22:52:30 +04:00
%}