summaryrefslogtreecommitdiff
path: root/src/html/html_radio_nodelist.c
blob: 52e25f14b05d7d577410eae49208f02ceeb1ea98 (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 * This file is part of libdom.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2018 Daniel Silverstone <dsilvers@netsurf-browser.org>
 */

#include <assert.h>
#include <stdlib.h>

#include <libwapcaplet/libwapcaplet.h>

#include <dom/html/html_radio_nodelist.h>
#include <dom/html/html_input_element.h>

#include "html/html_radio_nodelist.h"
#include "html/html_form_controls_collection.h"
#include "html/html_document.h"
#include "html/html_form_element.h"

#include "core/node.h"
#include "core/element.h"
#include "core/string.h"

/*-----------------------------------------------------------------------*/
/* Internal functions */

/* Callback function to test whether certain node is a form control, see
 * src/html/html_collection.h for detail. */
static bool _dom_is_specific_form_control(struct dom_node_internal *node, void *ctx)
{
	struct dom_html_document *doc =
		(struct dom_html_document *)(node->owner);
	struct dom_html_radio_nodelist *nodelist = ctx;
	dom_string *str;
	dom_exception exc;

	if (_dom_is_form_control(node, nodelist->form)) {
		/* It's the control we care about if id or name are equal
		 * to nodelist->name.  We wouldn't be here if node wasn't
		 * an element node.
		 */
		exc = _dom_element_get_id((struct dom_element *)node, &str);

		if (exc != DOM_NO_ERR) {
			return false;
		}

		if (str != NULL && dom_string_isequal(str, nodelist->name)) {
			dom_string_unref(str);
			return true;
		}

		if (str != NULL)
			dom_string_unref(str);

		exc = _dom_element_get_attribute((struct dom_element *)node,
				doc->memoised[hds_name], &str);

		if (str != NULL && dom_string_isequal(str, nodelist->name)) {
			dom_string_unref(str);
			return true;
		}

		if (str != NULL)
			dom_string_unref(str);
	}

	return false;
}

/**
 * Create a dom_html_radio_nodelist
 *
 * \param form  The HTML form element for the radio nodelist
 * \param name  The element id/name for the radio nodelist
 * \param nodelist   The result radio nodelist object
 * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
 */
dom_exception _dom_html_radio_nodelist_create(struct dom_html_form_element *form,
		dom_string *name,
		struct dom_html_radio_nodelist **nodelist)
{
	dom_exception exc;
	*nodelist = malloc(sizeof(struct dom_html_radio_nodelist));

	if (*nodelist == NULL)
		return DOM_NO_MEM_ERR;

	exc = _dom_html_radio_nodelist_initialise(form, *nodelist, name);

	if (exc != DOM_NO_ERR) {
		/* This is safe because we only ever get an error before we
		 * ref anything which would need finalising
		 */
		free(*nodelist);
		*nodelist = NULL;
	}

	return exc;
}

/**
 * Initialise a dom_html_radio_nodelist
 *
 * \param form  The HTML form element for the radio nodelist
 * \param nodelist The radio nodelist object to initialise
 * \param name  The element id/name for the radio nodelist
 * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
 */
dom_exception _dom_html_radio_nodelist_initialise(struct dom_html_form_element *form,
		struct dom_html_radio_nodelist *nodelist,
		dom_string *name)
{
	dom_exception exc;
	dom_html_document *doc = (dom_html_document *) dom_node_get_owner(form);

	assert(form != NULL);
	assert(name != NULL);

	assert(doc != NULL);

	exc =  _dom_html_collection_create(doc,
			(dom_node_internal *) doc,
			_dom_is_specific_form_control,
			nodelist, &nodelist->col);

	if (exc != DOM_NO_ERR)
		return exc;

	nodelist->form = form;
	dom_node_ref(form);

	nodelist->name = name;
	dom_string_ref(name);

	nodelist->refcnt = 1;

	return DOM_NO_ERR;
}

/**
 * Finalise a dom_html_radio_nodelist object
 *
 * \param nodelist The dom_html_radio_nodelist object
 */
void _dom_html_radio_nodelist_finalise(struct dom_html_radio_nodelist *nodelist)
{
	assert(nodelist->refcnt == 0);
	dom_node_unref(nodelist->form);
	nodelist->form = NULL;
	dom_string_unref(nodelist->name);
	nodelist->name = NULL;
	dom_html_collection_unref(nodelist->col);
	nodelist->col = NULL;
}

/**
 * Destroy a dom_html_radio_nodelist object
 *
 * \param nodelist The dom_html_radio_nodelist object
 */
void _dom_html_radio_nodelist_destroy(struct dom_html_radio_nodelist *nodelist)
{
	_dom_html_radio_nodelist_finalise(nodelist);
	free(nodelist);
}

/*-----------------------------------------------------------------------*/
/* Public API */

/**
 * Claim a reference on this radio nodelist
 *
 * \param nodelist The radio nodelist object
 */
void dom_html_radio_nodelist_ref(dom_html_radio_nodelist *nodelist)
{
	if (nodelist == NULL)
		return;

	nodelist->refcnt ++;
}

/**
 * Relese a reference on this radio nodelist
 *
 * \param nodelist The radio nodelist object
 */
void dom_html_radio_nodelist_unref(dom_html_radio_nodelist *nodelist)
{
	if (nodelist == NULL)
		return;

	if (nodelist->refcnt > 0)
		nodelist->refcnt --;

	if (nodelist->refcnt == 0)
		_dom_html_radio_nodelist_destroy(nodelist);
}


/**
 * Retrieve the length of a node list
 *
 * \param nodelist List to retrieve length of
 * \param len  Pointer to location to receive length
 * \return DOM_NO_ERR.
 */
dom_exception dom_html_radio_nodelist_get_length(dom_html_radio_nodelist *nodelist,
		uint32_t *len)
{
	return dom_html_collection_get_length(nodelist->col, len);
}

/**
 * Retrieve an item from a node list
 *
 * \param nodelist The list to retrieve the item from
 * \param index  The list index to retrieve
 * \param node   Pointer to location to receive item
 * \return DOM_NO_ERR.
 *
 * ::index is a zero-based index into ::list.
 * ::index lies in the range [0, length-1]
 *
 * The returned node will have had its reference count increased. The client
 * should unref the node once it has finished with it.
 */
dom_exception dom_html_radio_nodelist_item(dom_html_radio_nodelist *nodelist,
		uint32_t index, struct dom_node **node)
{
	return dom_html_collection_item(nodelist->col, index, node);
}

/**
 * Retrieve the value of the radio nodelist per 2.7.2.2.
 *
 * \param nodelist The list to retrieve the value from
 * \param value The value string to fill out
 * \return DOM_NO_ERR
 */
dom_exception dom_html_radio_nodelist_get_value(dom_html_radio_nodelist *nodelist,
					       dom_string **value)
{
	dom_exception exc;
	dom_node *node = NULL;
	uint32_t count;
	struct dom_html_document *doc =
		(struct dom_html_document *)(((struct dom_node_internal *)nodelist->form)->owner);

	assert(doc != NULL);

	exc = dom_html_collection_get_length(nodelist->col, &count);

	if (exc != DOM_NO_ERR)
		return exc;

	/* 1. Let element be the first element in tree order represented by the
	 * RadioNodeList object that is an input element whose type attribute
	 * is in the Radio Button state and whose checkedness is
	 * true. Otherwise, let it be NULL.
	 */

	for (uint32_t idx = 0; idx < count; idx++) {
		exc = dom_html_collection_item(nodelist->col, idx, &node);
		if (exc != DOM_NO_ERR)
			return exc;

		assert(node->type == DOM_ELEMENT_NODE);

		if (dom_string_caseless_isequal(((struct dom_node_internal *)node)->name,
				doc->elements[DOM_HTML_ELEMENT_TYPE_INPUT])) {
			bool checked = false;
			dom_string *type;

			exc = dom_element_get_attribute(node, doc->memoised[hds_type], &type);
			if (exc != DOM_NO_ERR) {
				dom_node_unref(node);
				return exc;
			}

			if (dom_string_caseless_isequal(type, doc->memoised[hds_radio]) == false) {
				dom_node_unref(node);
				dom_string_unref(type);
				continue;
			}

			dom_string_unref(type);

			exc = dom_html_input_element_get_checked((dom_html_input_element *)node, &checked);

			if (exc != DOM_NO_ERR) {
				dom_node_unref(node);
				return exc;
			}
			if (checked == true)
				break;
		}

		dom_node_unref(node);
		node = NULL;
	}

	/* 2. If element is null, return the empty string. */
	if (node == NULL) {
		*value = doc->base._memo_empty;
		dom_string_ref(*value);
		return DOM_NO_ERR;
	}

	exc = dom_html_input_element_get_value((dom_html_input_element *)node, value);

	/* 3. If element is an element with no value attribute, return the
	 * string "on".
	 */
	if (*value == NULL) {
		*value = doc->memoised[hds_on];
		dom_string_ref(*value);
		return DOM_NO_ERR;
	}

	/* 4. Otherwise, return the value of element’s value attribute. */

	dom_node_unref(node);

	return DOM_NO_ERR;
}

dom_exception dom_html_radio_nodelist_set_value(dom_html_radio_nodelist *nodelist,
					       dom_string *value)
{
	dom_exception exc;
	dom_node *node = NULL;
	uint32_t count;
	bool goal_is_on = false;
	struct dom_html_document *doc =
		(struct dom_html_document *)(((struct dom_node_internal *)nodelist->form)->owner);

	assert(doc != NULL);

	exc = dom_html_collection_get_length(nodelist->col, &count);

	if (exc != DOM_NO_ERR)
		return exc;

	goal_is_on = dom_string_caseless_isequal(value, doc->memoised[hds_on]);

	/* 1. If the new value is the string "on": let element be the first
	 * element in tree order represented by the RadioNodeList object that
	 * is an input element whose type attribute is in the Radio Button
	 * state and whose value content attribute is either absent, or present
	 * and equal to the new value, if any. If no such element exists, then
	 * instead let element be null.
	 *
	 * Otherwise: let element be the first element in tree order
	 * represented by the RadioNodeList object that is an input element
	 * whose type attribute is in the Radio Button state and whose value
	 * content attribute is present and equal to the new value, if any. If
	 * no such element exists, then instead let element be null.
	 */

	for (uint32_t idx = 0; idx < count; idx++) {
		exc = dom_html_collection_item(nodelist->col, idx, &node);
		if (exc != DOM_NO_ERR)
			return exc;

		assert(node->type == DOM_ELEMENT_NODE);
		if (dom_string_caseless_isequal(((struct dom_node_internal *)node)->name,
				doc->elements[DOM_HTML_ELEMENT_TYPE_INPUT])) {
			dom_string *type;
			dom_string *test_value;

			exc = dom_element_get_attribute(node, doc->memoised[hds_type], &type);
			if (exc != DOM_NO_ERR) {
				dom_node_unref(node);
				return exc;
			}

			if (dom_string_caseless_isequal(type, doc->memoised[hds_radio]) == false) {
				dom_node_unref(node);
				dom_string_unref(type);
				continue;
			}

			dom_string_unref(type);

			exc = dom_html_input_element_get_value((dom_html_input_element *)node, &test_value);

			if (test_value == NULL) {
				if (goal_is_on) {
					dom_string_unref(test_value);
					break;
				}
			} else {
				if (dom_string_caseless_isequal(test_value, value)) {
					dom_string_unref(test_value);
					break;
				}
			}

			dom_string_unref(test_value);
		}

		dom_node_unref(node);
		node = NULL;
	}

	exc = DOM_NO_ERR;

	/* 2. If element is not null, then set its checkedness to true. */
	if (node != NULL) {
		exc = dom_html_input_element_set_checked((dom_html_input_element *)node, true);
	}

	dom_node_unref(node);

	return exc;
}