summaryrefslogtreecommitdiff
path: root/render/libdom_binding.c
blob: 57f97cf24708d9a71046c7b2f352f2f11db3e539 (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright 2011 Vincent Sanders <vince@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <dom/dom.h>
#include <dom/bindings/hubbub/parser.h>

#include "render/form.h"
#include "render/parser_binding.h"

#include "utils/log.h"

typedef struct binding_ctx {
	dom_hubbub_parser *parser;
	dom_document *extracted;
} binding_ctx;

binding_error binding_create_tree(void **ctx, const char *charset, bool enable_script, dom_script script, void *context)
{
	dom_hubbub_parser *parser = NULL;
	binding_ctx *bctx = NULL;

	bctx = calloc(sizeof(*bctx), 1);
	if (bctx == NULL) {
		LOG(("Can't allocate memory for binding context"));
		return BINDING_NOMEM;
	}

	parser = dom_hubbub_parser_create(charset, true, enable_script, NULL, script, context);
        if (parser == NULL) {
                LOG(("Can't create Hubbub Parser\n"));
                return BINDING_NOMEM;
        }
	bctx->parser = parser;
	*ctx = bctx;
	return BINDING_OK;
}

binding_error binding_destroy_tree(void *ctx)
{
	struct binding_ctx *bctx = ctx;
	dom_hubbub_parser_destroy(bctx->parser);
	free(bctx);
	return BINDING_OK;
}

binding_error binding_parse_chunk(void *ctx, const uint8_t *data, size_t len)
{
	struct binding_ctx *bctx = ctx;
	dom_hubbub_error error;
	error = dom_hubbub_parser_parse_chunk(bctx->parser, data, len);
	if (error == (DOM_HUBBUB_HUBBUB_ERR | HUBBUB_ENCODINGCHANGE)) {
		return BINDING_ENCODINGCHANGE;
	} else if (error != DOM_HUBBUB_OK) {
		return BINDING_NOMEM;
	}
	return BINDING_OK;
}

binding_error binding_parse_completed(void *ctx)
{
	struct binding_ctx *bctx = ctx;
	dom_hubbub_error error;
	error = dom_hubbub_parser_completed(bctx->parser);
        if (error != DOM_HUBBUB_OK) {
		return BINDING_NOMEM;
        }
	return BINDING_OK;
}

const char *binding_get_encoding(void *ctx, binding_encoding_source *source)
{
	struct binding_ctx *bctx = ctx;
	dom_hubbub_encoding_source hubbub_src;
	const char *encoding;

	encoding = dom_hubbub_parser_get_encoding(bctx->parser, &hubbub_src);

	switch (hubbub_src) {
	case DOM_HUBBUB_ENCODING_SOURCE_HEADER:
		*source = ENCODING_SOURCE_HEADER;
		break;

	case DOM_HUBBUB_ENCODING_SOURCE_DETECTED:
		*source = ENCODING_SOURCE_DETECTED;
		break;

	case DOM_HUBBUB_ENCODING_SOURCE_META:
		*source = ENCODING_SOURCE_META;
		break;
	}

	return encoding;
}

dom_document *binding_get_document(void *ctx, binding_quirks_mode *quirks)
{
	struct binding_ctx *bctx = ctx;
	if (bctx->extracted == NULL)
		bctx->extracted = dom_hubbub_parser_get_document(bctx->parser);
	return bctx->extracted;
}

static struct form *
parse_form_element(dom_hubbub_parser *parser, dom_node *node)
{
	dom_string *ds_action = NULL;
	dom_string *ds_charset = NULL;
	dom_string *ds_target = NULL;
	dom_string *ds_method = NULL;
	dom_string *ds_enctype = NULL;
	char *action = NULL, *charset = NULL, *target = NULL;
	form_method method;
	dom_html_form_element *formele = (dom_html_form_element *)(node);
	struct form * ret = NULL;
	const char *docenc;

	/* Retrieve the attributes from the node */
	if (dom_html_form_element_get_action(formele, &ds_action) != DOM_NO_ERR)
		goto out;

	if (dom_html_form_element_get_accept_charset(formele, &ds_charset) != DOM_NO_ERR)
		goto out;

	if (dom_html_form_element_get_target(formele, &ds_target) != DOM_NO_ERR)
		goto out;

	if (dom_html_form_element_get_method(formele, &ds_method) != DOM_NO_ERR)
		goto out;

	if (dom_html_form_element_get_enctype(formele, &ds_enctype) != DOM_NO_ERR)
		goto out;

	/* Extract the plain attributes ready for use.  We have to do this
	 * because we cannot guarantee that the dom_strings are NULL terminated
	 * and thus we copy them.
	 */
	if (ds_action != NULL)
		action = strndup(dom_string_data(ds_action),
				 dom_string_byte_length(ds_action));

	if (ds_charset != NULL)
		charset = strndup(dom_string_data(ds_charset),
				  dom_string_byte_length(ds_charset));

	if (ds_target != NULL)
		target = strndup(dom_string_data(ds_target),
				 dom_string_byte_length(ds_target));

	/* Determine the method */
	method = method_GET;
	if (ds_method != NULL) {
		if (strncasecmp("post", dom_string_data(ds_method),
				dom_string_byte_length(ds_method)) == 0) {
			method = method_POST_URLENC;
			if (ds_enctype != NULL) {
				if (strncasecmp("multipart/form-data",
						dom_string_data(ds_enctype),
						dom_string_byte_length(ds_enctype)) == 0) {
					method = method_POST_MULTIPART;
				}
			}
		}
	}

	/* Retrieve the document encoding */
	{
		dom_hubbub_encoding_source hubbub_src;
		docenc = dom_hubbub_parser_get_encoding(parser, &hubbub_src);
	}

	/* Construct the form object */
	ret = form_new(node, action, target, method, charset, docenc);

out:
	if (ds_action != NULL)
		dom_string_unref(ds_action);
	if (ds_charset != NULL)
		dom_string_unref(ds_charset);
	if (ds_target != NULL)
		dom_string_unref(ds_target);
	if (ds_method != NULL)
		dom_string_unref(ds_method);
	if (ds_enctype != NULL)
		dom_string_unref(ds_enctype);
	if (action != NULL)
		free(action);
	if (charset != NULL)
		free(charset);
	if (target != NULL)
		free(charset);
	return ret;
}

struct form *binding_get_forms(void *ctx)
{
	binding_ctx *bctx = ctx;
	binding_quirks_mode ignored;
	dom_html_document *doc =
		(dom_html_document *)binding_get_document(ctx, &ignored);
	dom_html_collection *forms;
	struct form *ret = NULL, *newf;
	dom_node *node;
	unsigned long nforms, n;

	if (doc == NULL)
		return NULL;

	/* Attempt to build a set of all the forms */
	if (dom_html_document_get_forms(doc, &forms) != DOM_NO_ERR)
		return NULL;

	/* Count the number of forms so we can iterate */
	if (dom_html_collection_get_length(forms, &nforms) != DOM_NO_ERR)
		goto out;

	/* Iterate the forms collection, making form structs for returning */
	for (n = 0; n < nforms; ++n) {
		if (dom_html_collection_item(forms, n, &node) != DOM_NO_ERR) {
			goto out;
		}
		newf = parse_form_element(bctx->parser, node);
		dom_node_unref(node);
		if (newf == NULL) {
			goto err;
		}
		newf->prev = ret;
		ret = newf;
	}

	/* All went well */
	goto out;
err:
	while (ret != NULL) {
		struct form *prev = ret->prev;
		/* Destroy ret */
		free(ret);
		ret = prev;
	}
out:
	/* Finished with the collection, return it */
	dom_html_collection_unref(forms);

	return ret;
}

struct form_control *binding_get_control_for_node(void *ctx, dom_node *node)
{
	/** \todo implement properly */
	struct form_control *ctl = form_new_control(node, GADGET_HIDDEN);
	if (ctl != NULL) {
		ctl->value = strdup("");
		ctl->initial_value = strdup("");
		ctl->name = strdup("foo");

		if (ctl->value == NULL || ctl->initial_value == NULL ||
				ctl->name == NULL) {
			form_free_control(ctl);
			ctl = NULL;
		}
	}

	return ctl;
}

void binding_destroy_document(dom_document *doc)
{
	dom_node_unref(doc);
}