summaryrefslogtreecommitdiff
path: root/javascript/jsapi/htmldocument.bnd
blob: 72bc814823f15b82dc8d96a0523f066fd9820d92 (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
249
250
251
252
253
254
255
256
/* Binding to generate HTMLdocument interface
 *
 * 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
 */

#include "dom.bnd"

webidlfile "html.idl";

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 "utils/corestrings.h"
#include "utils/libdom.h"

#include "content/urldb.h"

#include "javascript/jsapi.h"
#include "javascript/jsapi/binding.h"

%}

binding document {
	type js_libdom; /* the binding type */

	interface Document; /* Web IDL interface to generate */

	/* parameters to constructor value stored in private
	 * context structure.
	 */
	private "dom_document *" node;
	private "struct html_content *" htmlc; 

	/** location instantiated on first use */
	property unshared location; 
}

api finalise %{
	if (private != NULL) {
		JSLOG("dom_document %p in content %p",
		     private->node, private->htmlc);
		dom_node_unref(private->node);
	}
%}

getter location %{
	if (!JSVAL_IS_VOID(JSAPI_PROP_RVAL(cx,vp))) {
		/* already created - return it */
		return JS_TRUE;
	}
	jsret = jsapi_new_Location(cx, 
				   NULL, 
				   NULL, 
				   private->htmlc->bw, 
				   llcache_handle_get_url(private->htmlc->base.llcache));
%}


getter cookie %{
	char *cookie_str;
	cookie_str = urldb_get_cookie(llcache_handle_get_url(private->htmlc->base.llcache), false);
	if (cookie_str != NULL) {
		jsret = JS_NewStringCopyN(cx, cookie_str, strlen(cookie_str));
		free(cookie_str);
	}
%}

getter documentElement %{
	dom_exception exc;
	dom_element *element;

	/* document (html) element */
	exc = dom_document_get_document_element(private->node, (void *)&element);
        if (exc != DOM_NO_ERR) {
		return JS_FALSE;
	}

	if (element != NULL) {
		jsret = jsapi_new_HTMLElement(cx, NULL, NULL, element, private->htmlc);
	}
%}

getter head %{
	dom_node *element;
	dom_node *head;
        dom_exception exc;

	/* document (html) element */
	exc = dom_document_get_document_element(private->node, &element);
        if (exc != DOM_NO_ERR) {
		return JS_FALSE;
	}

	if (element != NULL) {
		head = libdom_find_first_element(element, corestring_lwc_head) ;
		if (head != NULL) {
			jsret = jsapi_new_HTMLElement(cx, NULL, NULL, (dom_element *)head, private->htmlc);
		}
		dom_node_unref(element);
	}
%}

getter body %{
	dom_node *element;
	dom_node *body;
        dom_exception exc;

	JSLOG("Getting your body");

	/* document (html) element */
	exc = dom_document_get_document_element(private->node, &element);
        if (exc != DOM_NO_ERR) {
		return JS_FALSE;
	}

	if (element != NULL) {
		body = libdom_find_first_element(element, corestring_lwc_body) ;
		if (body != NULL) {
			jsret = jsapi_new_HTMLElement(cx, NULL, NULL, (dom_element *)body, private->htmlc);
		}
		dom_node_unref(element);
	}

	JSLOG("returning jsobject %p",jsret);

%}

operation getElementById %{
	dom_string *elementId_dom;
	dom_element *element;
        dom_exception exc;

	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);
        dom_string_unref(elementId_dom);
        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;
        }

        exc = dom_document_get_elements_by_tag_name(private->node, localName_dom, /*&collection*/&nodelist);
        dom_string_unref(localName_dom);
        if (exc != DOM_NO_ERR) {
                return JS_FALSE;
        }

        if (/*collection*/nodelist != NULL) {
                /*jsret = jsapi_new_HTMLCollection(cx,
                                                 NULL,
                                                 NULL,
                                                 collection,
                                                 private->htmlc);*/
                jsret = jsapi_new_NodeList(cx,
                                                 NULL,
                                                 NULL,
                                                 nodelist,
                                                 private->htmlc);
        }

%}

operation write %{
	if (private->htmlc->parser != NULL) {
		dom_hubbub_parser_insert_chunk(private->htmlc->parser, (uint8_t *)text, text_len);
	}
%}

/* in dom Document */
operation createTextNode %{
	dom_string *data_dom;
        dom_exception exc;
	dom_text *text;

	if (data != NULL) {

		JSLOG("Creating text node for string \"%s\"", data);
		exc = dom_string_create((unsigned char*)data, data_len, &data_dom);
		if (exc != DOM_NO_ERR) {
			return JS_FALSE;
		}

		exc = dom_document_create_text_node(private->node, data_dom, &text);
		dom_string_unref(data_dom);
		if (exc != DOM_NO_ERR) {
			return JS_FALSE;
		}

		jsret = jsapi_new_Text(cx, NULL, NULL, text, private->htmlc);
	}

	JSLOG("returning jsobject %p",jsret);

%}

/* in dom Document */
operation createElement %{
	dom_string *localName_dom;
        dom_exception exc;
	dom_element *element;

	if (localName != NULL) {
		JSLOG("Creating text node for string \"%s\"", localName);
		exc = dom_string_create((unsigned char*)localName, localName_len, &localName_dom);
		if (exc != DOM_NO_ERR) {
			return JS_FALSE;
		}

		exc = dom_document_create_element(private->node, localName_dom, &element);
		dom_string_unref(localName_dom);
		if (exc != DOM_NO_ERR) {
			return JS_FALSE;
		}

		jsret = jsapi_new_HTMLElement(cx, NULL, NULL, element, private->htmlc);
	}

	JSLOG("returning jsobject %p",jsret);

%}