summaryrefslogtreecommitdiff
path: root/src/core/tokenlist.c
blob: cc93a8c639823e4caa3529c305c249aafca267c8 (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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
 * This file is part of libdom.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2022 Daniel Silverstone <dsilvers@digital-scurf.org>
 */

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

#include <dom/core/element.h>
#include <dom/core/nodelist.h>
#include <dom/core/tokenlist.h>
#include <dom/core/string.h>
#include <dom/events/event.h>
#include <dom/events/event_target.h>
#include <dom/events/event_listener.h>
#include <dom/events/mutation_event.h>

#include "core/element.h"
#include "core/document.h"

#include "utils/utils.h"

#define DOM_TOKENLIST_GROW_INCREMENT 4

struct dom_tokenlist {
	uint32_t refcnt;
	dom_element *ele;
	dom_string *attr;
	dom_event_listener *listener;
	dom_string *last_set;
	bool needs_parse;
	/* Parsed content, for optimal access */
	dom_string **entries;
	uint32_t len;
	uint32_t alloc;
};

/* Handle a DOMAttrModified event which might be to do with our attribute */

static void _dom_tokenlist_handle_attrmodified(dom_event *evt, void *pw)
{
	dom_mutation_event *mutevt = (dom_mutation_event *)evt;
	dom_tokenlist *list = (dom_tokenlist *)pw;
	dom_exception exc;
	dom_string *value;

	{
		dom_event_target *target;
		exc = dom_event_get_target(evt, &target);
		if (exc != DOM_NO_ERR)
			return;
		dom_node_unref(target);
		if (target != (dom_event_target *)list->ele)
			return;
	}

	{
		dom_string *attr;
		exc = dom_mutation_event_get_attr_name(mutevt, &attr);
		if (exc != DOM_NO_ERR)
			return;
		if (!dom_string_isequal(attr, list->attr)) {
			dom_string_unref(attr);
			return;
		}
		dom_string_unref(attr);
	}

	/* At this point we know that this is a mutation of our attribute on our
	 * node */

	exc = dom_mutation_event_get_new_value(mutevt, &value);
	if (exc != DOM_NO_ERR)
		return;

	if (list->last_set != NULL &&
	    dom_string_isequal(list->last_set, value)) {
		/* We've just seen the mutation event for one of our own set
		 * operations */
		dom_string_unref(value);
		return;
	}

	/* Mark that we need to re-parse the tokenlist on the next request */
	list->needs_parse = true;

	dom_string_unref(value);
}

static dom_exception _dom_tokenlist_make_room(dom_tokenlist *list)
{
	if (list->len == list->alloc) {
		uint32_t new_alloc = list->alloc + DOM_TOKENLIST_GROW_INCREMENT;
		dom_string **new_entries = realloc(
			list->entries, new_alloc * sizeof(dom_string *));
		if (new_entries == NULL)
			return DOM_NO_MEM_ERR;
		list->alloc = new_alloc;
		list->entries = new_entries;
	}

	return DOM_NO_ERR;
}

static dom_exception _dom_tokenlist_reparse(dom_tokenlist *list)
{
	dom_exception exc;
	dom_string *value;
	const char *pos;
	uint32_t remaining, check;
	uint32_t n_entries = 0;
	dom_string *temp;
	bool found;

	if (!list->needs_parse)
		return DOM_NO_ERR;

	/* Clean down the current entries */
	while (list->len-- > 0)
		dom_string_unref(list->entries[list->len]);
	list->len = 0;

	/* Get the "new" attribute value */
	exc = dom_element_get_attribute(list->ele, list->attr, &value);
	if (exc != DOM_NO_ERR)
		return exc;

	/* If there is no value, we're an empty list and we're done */
	if (value == NULL) {
		list->needs_parse = false;
		return DOM_NO_ERR;
	}

	/* OK, there's something here to do, so let's do it... */

	/* Count number of entries */
	for (pos = dom_string_data(value), remaining = dom_string_length(value);
	     remaining > 0;) {
		if (*pos != ' ') {
			while (*pos != ' ' && remaining > 0) {
				remaining--;
				pos++;
			}
			n_entries++;
		} else {
			while (*pos == ' ' && remaining > 0) {
				remaining--;
				pos++;
			}
		}
	}

	/* If there are no entries (all whitespace) just bail here */
	if (n_entries == 0) {
		list->needs_parse = false;
		dom_string_unref(value);
		return DOM_NO_ERR;
	}

	/* If we need more room, reallocate the buffer */
	if (list->alloc < n_entries) {
		dom_string **new_alloc = realloc(
			list->entries, n_entries * sizeof(dom_string *));
		if (new_alloc == NULL) {
			dom_string_unref(value);
			return DOM_NO_MEM_ERR;
		}
		list->entries = new_alloc;
		list->alloc = n_entries;
	}

	/* And now parse those entries into the buffer */
	for (pos = dom_string_data(value),
	    remaining = dom_string_length(value),
	    n_entries = 0;
	     remaining > 0;) {
		if (*pos != ' ') {
			const char *s = pos;
			while (*pos != ' ' && remaining > 0) {
				pos++;
				remaining--;
			}
			exc = dom_string_create_interned((const uint8_t *)s,
							 pos - s,
							 &temp);
			if (exc != DOM_NO_ERR) {
				dom_string_unref(value);
				return exc;
			}
			found = false;
			for (check = 0; check < list->len; check++) {
				if (dom_string_isequal(temp,
						       list->entries[check])) {
					found = true;
					break;
				}
			}
			if (found == true) {
				dom_string_unref(temp);
			} else {
				list->entries[list->len] = temp;
				list->len++;
			}
		} else {
			while (*pos == ' ' && remaining > 0) {
				pos++;
				remaining--;
			}
		}
	}

	dom_string_unref(value);
	list->needs_parse = false;

	return DOM_NO_ERR;
}

static dom_exception _dom_tokenlist_reify(dom_tokenlist *list)
{
	dom_exception exc;
	uint32_t nchars = 0, n;
	char *buffer, *next;
	dom_string *output;

	if (list->len == 0) {
		if (list->last_set != NULL) {
			dom_string_unref(list->last_set);
		}
		list->last_set = dom_string_ref(
			list->ele->base.owner->_memo_empty);
		return dom_element_set_attribute(list->ele,
						 list->attr,
						 list->last_set);
	}

	for (n = 0; n < list->len; ++n)
		nchars += dom_string_length(list->entries[n]);

	buffer = calloc(1, nchars + list->len);
	if (buffer == NULL)
		return DOM_NO_MEM_ERR;

	for (next = buffer, n = 0; n < list->len; ++n) {
		uint32_t slen = dom_string_length(list->entries[n]);
		memcpy(next, dom_string_data(list->entries[n]), slen);
		next[slen] = ' ';
		next += slen + 1;
	}

	exc = dom_string_create_interned((const uint8_t *)buffer,
					 nchars + list->len - 1,
					 &output);
	free(buffer);
	if (exc != DOM_NO_ERR)
		return exc;

	if (list->last_set != NULL) {
		dom_string_unref(list->last_set);
	}
	list->last_set = output;

	return dom_element_set_attribute(list->ele, list->attr, list->last_set);
}

/**********************************************************************************/

/**
 * Create a tokenlist
 *
 * \param ele  The element which owns the tokenlist attribute
 * \param attr The name of the attribute we are treating as a tokenlist
 * \param list The tokenlist output which is set on success
 * \return DOM_NO_ERR on success, DOM_NO_MEM_ERR on memory exhaustion
 *
 * The returned list will already be referenced, so the client need not
 * do so explicitly. The client must unref the list once finished with it.
 *
 * This list will take its own references to ::ele and ::attr
 */
dom_exception
dom_tokenlist_create(dom_element *ele, dom_string *attr, dom_tokenlist **list)
{
	dom_tokenlist *l;
	dom_exception exc;

	l = calloc(1, sizeof(dom_tokenlist));
	if (l == NULL)
		return DOM_NO_MEM_ERR;

	l->refcnt = 1;
	l->ele = (dom_element *)dom_node_ref(ele);
	l->attr = dom_string_ref(attr);
	l->needs_parse = true;

	exc = dom_event_listener_create(_dom_tokenlist_handle_attrmodified,
					l,
					&l->listener);
	if (exc != DOM_NO_ERR)
		goto fail;

	exc = dom_event_target_add_event_listener(
		ele,
		ele->base.owner->_memo_domattrmodified,
		l->listener,
		false);

	if (exc != DOM_NO_ERR)
		goto fail;

	*list = l;

	return DOM_NO_ERR;

fail:
	if (l->listener != NULL)
		dom_event_listener_unref(l->listener);
	dom_node_unref(l->ele);
	dom_string_unref(l->attr);
	free(l);
	return exc;
}

/**
 * Claim a ref on a tokenlist
 *
 * \param list The tokenlist to claim a ref on
 */
void dom_tokenlist_ref(dom_tokenlist *list)
{
	assert(list != NULL);
	list->refcnt++;
}

/**
 * Release a ref on a tokenlist
 *
 * \param list The list to release the reference of
 *
 * If you release the last ref, this cleans up the tokenlist
 */
void dom_tokenlist_unref(dom_tokenlist *list)
{
	assert(list != NULL);

	if (--list->refcnt > 0)
		return;

	if (list->alloc > 0) {
		while (list->len-- > 0)
			dom_string_unref(list->entries[list->len]);
		free(list->entries);
	}

	dom_event_target_remove_event_listener(
		list->ele,
		list->ele->base.owner->_memo_domattrmodified,
		list->listener,
		false);

	dom_event_listener_unref(list->listener);

	if (list->last_set != NULL)
		dom_string_unref(list->last_set);

	dom_string_unref(list->attr);
	dom_node_unref(list->ele);

	free(list);
}

/**
 * Get the length of the tokenlist
 *
 * \param list The list to get the length of
 * \param length Length of the list outputs here
 * \return DOM_NO_ERR on success, otherwise the failure code
 */
dom_exception dom_tokenlist_get_length(dom_tokenlist *list, uint32_t *length)
{
	dom_exception exc;
	assert(list != NULL);

	exc = _dom_tokenlist_reparse(list);
	if (exc != DOM_NO_ERR)
		return exc;

	*length = list->len;

	return DOM_NO_ERR;
}

/**
 * Get a particular item from the tokenlist
 *
 * \param list The list to retrieve the item from
 * \param index The index of the item to retrieve
 * \param value The value of the item returns here
 * \return DOM_NO_ERR on success, otherwise the failure code
 */
dom_exception
_dom_tokenlist_item(dom_tokenlist *list, uint32_t index, dom_string **value)
{
	dom_exception exc;
	assert(list != NULL);

	exc = _dom_tokenlist_reparse(list);
	if (exc != DOM_NO_ERR)
		return exc;

	if (index >= list->len) {
		*value = NULL;
		return DOM_NO_ERR;
	}

	*value = dom_string_ref(list->entries[index]);
	return DOM_NO_ERR;
}

/**
 * Retrieve the value of the tokenlist as a string
 *
 * \param list The list to retrieve the value of
 * \param value The value of the list returns here
 * \return DOM_NO_ERR on success, otherwise the failure code
 */
dom_exception dom_tokenlist_get_value(dom_tokenlist *list, dom_string **value)
{
	assert(list != NULL);

	return dom_element_get_attribute(list->ele, list->attr, value);
}

/**
 * Set the value of the tokenlist as a string
 *
 * \param list The list to set the value of
 * \param value The value to set
 * \return DOM_NO_ERR on success, otherwise the failure code
 *
 */
dom_exception dom_tokenlist_set_value(dom_tokenlist *list, dom_string *value)
{
	assert(list != NULL);

	return dom_element_set_attribute(list->ele, list->attr, value);
}

/**
 * Check if the given value is in the tokenlist
 *
 * \param list The list to scan for the given value
 * \param value The value to look for in the token list
 * \param contains This will be set based on whether or not the value is present
 * \return DOM_NO_ERR on success, otherwise the failure code
 */
dom_exception
dom_tokenlist_contains(dom_tokenlist *list, dom_string *value, bool *contains)
{
	dom_exception exc;
	uint32_t n;

	assert(list != NULL);

	exc = _dom_tokenlist_reparse(list);
	if (exc != DOM_NO_ERR)
		return exc;

	*contains = false;

	for (n = 0; n < list->len; n++) {
		if (dom_string_isequal(value, list->entries[n])) {
			*contains = true;
			break;
		}
	}

	return DOM_NO_ERR;
}

/**
 * Add the given value to the tokenlist
 *
 * \param list The list to add to
 * \param value The value to add
 * \return DOM_NO_ERR on success, otherwise the failure code
 */
dom_exception dom_tokenlist_add(dom_tokenlist *list, dom_string *value)
{
	dom_exception exc;
	bool present = false;

	assert(list != NULL);

	exc = dom_tokenlist_contains(list, value, &present);
	if (exc != DOM_NO_ERR)
		return exc;

	if (present == true)
		return DOM_NO_ERR;

	exc = _dom_tokenlist_make_room(list);
	if (exc != DOM_NO_ERR)
		return exc;

	list->entries[list->len++] = dom_string_ref(value);

	exc = _dom_tokenlist_reify(list);

	return exc;
}

/**
 * Remove the given value from the tokenlist
 *
 * \param list The list to remove from
 * \param value The value to remove
 * \return DOM_NO_ERR on success, otherwise the failure code
 */
dom_exception dom_tokenlist_remove(dom_tokenlist *list, dom_string *value)
{
	dom_exception exc;
	uint32_t n, m;

	assert(list != NULL);

	exc = _dom_tokenlist_reparse(list);
	if (exc != DOM_NO_ERR)
		return false;

	for (n = 0; n < list->len; ++n) {
		if (dom_string_isequal(value, list->entries[n])) {
			dom_string_unref(list->entries[n]);
			for (m = n + 1; m < list->len; ++m) {
				list->entries[m - 1] = list->entries[m];
			}
			list->len--;
			break;
		}
	}

	exc = _dom_tokenlist_reify(list);

	return exc;
}