summaryrefslogtreecommitdiff
path: root/javascript/jsapi/dom.bnd
blob: 3fc7f9ed1e461a13bf9367ecf7d7f15239ae38b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* 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;
}

%}

/* interface Node members */

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


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) {
		jsret = JS_NewStringCopyN(cx,
					  dom_string_data(name),
					  dom_string_length(name));
		dom_string_unref(name);
	}
%}

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;
	}

	if (value != NULL) {
		jsret = JS_NewStringCopyN(cx,
					  dom_string_data(value),
					  dom_string_length(value));
		dom_string_unref(value);
	}
%}

getter textContent %{
	dom_exception exc;
	dom_string *content;

	exc = dom_node_get_text_content(private->node, &content);
	if (exc != DOM_NO_ERR) {
		return JS_FALSE;
	}

	if (content != NULL) {
		jsret = JS_NewStringCopyN(cx, dom_string_data(content), dom_string_length(content));
		dom_string_unref(content);

	}
%}

/* interface Node { Node appendChild(Node node); } */
operation appendChild %{
	struct dom_node *domnode; /* dom node from js input node */
	struct dom_node *result = NULL;
	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");
		return JS_FALSE;
	}

	JSLOG("appending js node %p (dom %p)", node, domnode);

	/* append the found element */
	exc = dom_node_append_child(private->node, domnode, &result);
	if (exc != DOM_NO_ERR) {
		JSLOG("Error: DOM Exception (libdom append child)");
		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");
	}
%}