summaryrefslogtreecommitdiff
path: root/javascript/duktape/document.c
blob: f09629ac6c37c197af4828bb6c373f71bdcef3f3 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* DO NOT USE, DODGY BIT FOR VINCE */

#include <dom/dom.h>

#include "utils/log.h"
#include "utils/corestrings.h"
#include "render/html_internal.h"
#include "utils/libdom.h"

#include "javascript/dukky.h"

DUKKY_FUNC_INIT(document, struct dom_document *document)
{
	DUKKY_FUNC_T(node, __init)(ctx, &priv->parent, (struct dom_node *)document);
	LOG("Initialise %p (priv=%p)", duk_get_heapptr(ctx, 0), priv);
}

DUKKY_FUNC_FINI(document)
{
	/* do any document finalisation here, priv ptr exists */
	LOG("Finalise %p", duk_get_heapptr(ctx, 0));
	DUKKY_FUNC_T(node, __fini)(ctx, &priv->parent);
}

static DUKKY_FUNC(document, __constructor)
{
	DUKKY_CREATE_PRIVATE(document);
	DUKKY_FUNC_T(document, __init)(ctx, priv,
				      duk_get_pointer(ctx, 1));
	duk_set_top(ctx, 1);
	return 1;
}

static DUKKY_FUNC(document, __destructor)
{
	DUKKY_SAFE_GET_PRIVATE(document, 0);
	DUKKY_FUNC_T(document, __fini)(ctx, priv);
	free(priv);
	return 0;
}

static DUKKY_FUNC(document, write)
{
	DUKKY_GET_METHOD_PRIVATE(document);
	struct html_content *htmlc;
	duk_size_t text_len;
	const char *text = duk_safe_to_lstring(ctx, 0, &text_len);
	LOG("Writing %*s", (int)text_len, text);
	dom_exception err;
	err = dom_node_get_user_data(priv->parent.node,
				     corestring_dom___ns_key_html_content_data,
				     &htmlc);
	if (err == DOM_NO_ERR && htmlc->parser != NULL) {
		dom_hubbub_parser_insert_chunk(htmlc->parser, (uint8_t *)text, text_len);
	}
	return 0;
}

static DUKKY_FUNC(document, createTextNode)
{
	DUKKY_GET_METHOD_PRIVATE(document);
	dom_node *newnode;
	dom_exception err;
	duk_size_t text_len;
	const char *text = duk_safe_to_lstring(ctx, 0, &text_len);
	dom_string *text_str;
	
	err = dom_string_create((const uint8_t*)text, text_len, &text_str);
	if (err != DOM_NO_ERR) return 0; /* coerced to undefined */
	
	err = dom_document_create_text_node(priv->parent.node,
					    text_str,
					    &newnode);
	if (err != DOM_NO_ERR) {
		dom_string_unref(text_str);
		return 0; /* coerced to undefined */
	}
	
	dom_string_unref(text_str);
	
	dukky_push_node(ctx, newnode);
	
	dom_node_unref(newnode);

	return 1;
}

static DUKKY_FUNC(document, createElement)
{
	DUKKY_GET_METHOD_PRIVATE(document);
	dom_node *newnode;
	dom_exception err;
	duk_size_t text_len;
	const char *text = duk_safe_to_lstring(ctx, 0, &text_len);
	dom_string *text_str;
	
	err = dom_string_create((const uint8_t*)text, text_len, &text_str);
	if (err != DOM_NO_ERR) return 0; /* coerced to undefined */
	
	err = dom_document_create_element_ns(priv->parent.node,
					     corestring_dom_html_namespace,
					     text_str,
					     &newnode);
	if (err != DOM_NO_ERR) {
		dom_string_unref(text_str);
		return 0; /* coerced to undefined */
	}
	
	dom_string_unref(text_str);
	
	dukky_push_node(ctx, newnode);
	
	dom_node_unref(newnode);

	return 1;
}

static DUKKY_GETTER(document, head)
{
	DUKKY_GET_METHOD_PRIVATE(document);
	struct dom_nodelist *nodes;
	struct dom_node *retnode;
	dom_exception err;
	err = dom_document_get_elements_by_tag_name(priv->parent.node,
						    corestring_dom_HEAD,
						    &nodes);
	if (err != DOM_NO_ERR) return 0; /* coerced to undefined */
	
	err = dom_nodelist_item(nodes, 0, &retnode);
	
	if (err != DOM_NO_ERR) {
		dom_nodelist_unref(nodes);
		return 0; /* coerced to undefined */
	}
	
	dom_nodelist_unref(nodes);
	
	if (retnode == NULL) return 0; /* coerced to undefined */
	
	dukky_push_node(ctx, retnode);
	
	dom_node_unref(retnode);
	
	return 1;
}

static DUKKY_GETTER(document, body)
{
	DUKKY_GET_METHOD_PRIVATE(document);
	struct dom_nodelist *nodes;
	struct dom_node *retnode;
	dom_exception err;
	err = dom_document_get_elements_by_tag_name(priv->parent.node,
						    corestring_dom_BODY,
						    &nodes);
	if (err != DOM_NO_ERR) return 0; /* coerced to undefined */
	
	err = dom_nodelist_item(nodes, 0, &retnode);
	
	if (err != DOM_NO_ERR) {
		dom_nodelist_unref(nodes);
		return 0; /* coerced to undefined */
	}
	
	dom_nodelist_unref(nodes);
	
	if (retnode == NULL) return 0; /* coerced to undefined */
	
	dukky_push_node(ctx, retnode);
	
	dom_node_unref(retnode);
	
	return 1;
}

static DUKKY_FUNC(document, getElementById)
{
	DUKKY_GET_METHOD_PRIVATE(document);
	dom_string *elementId_dom;
	dom_element *element;
	dom_exception exc;
	duk_size_t text_len;
	const char *text = duk_safe_to_lstring(ctx, 0, &text_len);

	exc = dom_string_create((uint8_t*)text, text_len, &elementId_dom);
	if (exc != DOM_NO_ERR) {
		return 0;
	}

	exc = dom_document_get_element_by_id(((node_private_t *)priv)->node,
					     elementId_dom, &element);
	dom_string_unref(elementId_dom);
	if (exc != DOM_NO_ERR) {
		return 0;
	}

	if (element != NULL) {
		dukky_push_node(ctx, (dom_node *)element);
		return 1;
	}
	
	return 0;
}

static DUKKY_FUNC(document, getElementsByTagName)
{
	DUKKY_GET_METHOD_PRIVATE(document);
	dom_nodelist *nodes;
	dom_exception err;
	duk_size_t text_len;
	const char *text = duk_safe_to_lstring(ctx, 0, &text_len);
	dom_string *tag;
	
	err = dom_string_create((uint8_t*)text, text_len, &tag);
	
	if (err != DOM_NO_ERR) return 0; /* coerced to undefined */
	
	err = dom_document_get_elements_by_tag_name(((node_private_t *)priv)->node,
						    tag, &nodes);
	dom_string_unref(tag);
	if (err != DOM_NO_ERR) return 0; /* coerced to undefined */
	
	if (nodes == NULL) return 0; /* coerced to undefined */
	
	duk_push_pointer(ctx, nodes);
	dukky_create_object(ctx, PROTO_NAME(NODELIST), 1);
	dom_nodelist_unref(nodes);
	return 1;
}

DUKKY_FUNC(document, __proto)
{
	/* Populate document's prototypical functionality */
	DUKKY_ADD_METHOD(document, write, 1);
	DUKKY_ADD_METHOD(document, createTextNode, 1);
	DUKKY_ADD_METHOD(document, createElement, 1);
	DUKKY_ADD_METHOD(document, getElementById, 1);
	DUKKY_ADD_METHOD(document, getElementsByTagName, 1);
	DUKKY_POPULATE_READONLY_PROPERTY(document, body);
	DUKKY_POPULATE_READONLY_PROPERTY(document, head);
	/* Set this prototype's prototype (left-parent)*/
	DUKKY_GET_PROTOTYPE(NODE);
	duk_set_prototype(ctx, 0);
	/* And the initialiser/finalizer */
	DUKKY_SET_DESTRUCTOR(0, document);
	DUKKY_SET_CONSTRUCTOR(0, document, 1);
	return 1; /* The proto object */
}