-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathtoken_list.c
733 lines (629 loc) · 23.7 KB
/
token_list.c
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Niels Dossche <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "php.h"
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include "php_dom.h"
#include "token_list.h"
#include "infra.h"
#include "zend_interfaces.h"
#define TOKEN_LIST_GET_INTERNAL() php_dom_token_list_from_obj(Z_OBJ_P(ZEND_THIS))
#define TOKEN_LIST_GET_SET(intern) (&(intern)->token_set)
#define Z_TOKEN_LIST_P(zv) php_dom_token_list_from_obj(Z_OBJ_P(zv))
typedef struct dom_token_list_it {
zend_object_iterator it;
/* Store the hash position here to allow multiple (e.g. nested) iterations of the same token list. */
HashPosition pos;
php_libxml_cache_tag cache_tag;
} dom_token_list_it;
static zend_always_inline bool dom_contains_ascii_whitespace(const char *data)
{
return strpbrk(data, ascii_whitespace) != NULL;
}
static zend_always_inline void dom_add_token(HashTable *ht, zend_string *token)
{
/* Key outlives the value's lifetime because as long as the entry is in the table it is kept alive. */
zval zv;
ZVAL_STR(&zv, token);
zend_hash_add(ht, token, &zv);
}
/* https://dom.spec.whatwg.org/#concept-ordered-set-parser
* and https://infra.spec.whatwg.org/#split-on-ascii-whitespace */
static void dom_ordered_set_parser(HashTable *token_set, const char *position)
{
/* Adapted steps from "split on ASCII whitespace" such that that loop directly appends to the token set. */
/* 1. Let position be a position variable for input, initially pointing at the start of input.
* => That's the position pointer. */
/* 2. Let tokens be a list of strings, initially empty.
* => That's the token set. */
/* 3. Skip ASCII whitespace within input given position. */
position += strspn(position, ascii_whitespace);
/* 4. While position is not past the end of input: */
while (*position != '\0') {
/* 4.1. Let token be the result of collecting a sequence of code points that are not ASCII whitespace from input */
const char *start = position;
position += strcspn(position, ascii_whitespace);
size_t length = position - start;
/* 4.2. Append token to tokens. */
zend_string *token = zend_string_init(start, length, false);
dom_add_token(token_set, token);
zend_string_release_ex(token, false);
/* 4.3. Skip ASCII whitespace within input given position. */
position += strspn(position, ascii_whitespace);
}
/* 5. Return tokens.
* => That's the token set. */
}
/* https://dom.spec.whatwg.org/#concept-ordered-set-serializer */
static char *dom_ordered_set_serializer(HashTable *token_set)
{
size_t length = 0;
zend_string *token;
ZEND_HASH_MAP_FOREACH_STR_KEY(token_set, token) {
size_t needed_size = ZSTR_LEN(token) + 1; /* +1 for the space (or \0 at the end) */
if (UNEXPECTED(ZSTR_MAX_LEN - length < needed_size)) {
/* Shouldn't really be able to happen in practice. */
zend_throw_error(NULL, "Token set too large");
return NULL;
}
length += needed_size;
} ZEND_HASH_FOREACH_END();
if (length == 0) {
char *ret = emalloc(1);
*ret = '\0';
return ret;
}
char *ret = emalloc(length);
char *ptr = ret;
ZEND_HASH_MAP_FOREACH_STR_KEY(token_set, token) {
memcpy(ptr, ZSTR_VAL(token), ZSTR_LEN(token));
ptr += ZSTR_LEN(token);
*ptr++ = ' ';
} ZEND_HASH_FOREACH_END();
ptr[-1] = '\0'; /* replace last space with \0 */
return ret;
}
static zend_always_inline xmlNode *dom_token_list_get_element(dom_token_list_object *intern)
{
php_libxml_node_ptr *element_ptr = intern->dom.ptr;
return element_ptr->node;
}
static zend_always_inline const xmlAttr *dom_token_list_get_attr(dom_token_list_object *intern)
{
const xmlNode *element_node = dom_token_list_get_element(intern);
return xmlHasNsProp(element_node, BAD_CAST "class", NULL);
}
/* https://dom.spec.whatwg.org/#concept-dtl-update */
static void dom_token_list_update(dom_token_list_object *intern)
{
const xmlAttr *attr = dom_token_list_get_attr(intern);
HashTable *token_set = TOKEN_LIST_GET_SET(intern);
php_libxml_invalidate_cache_tag(&intern->cache_tag);
/* 1. If the associated element does not have an associated attribute and token set is empty, then return. */
if (attr == NULL && zend_hash_num_elements(token_set) == 0) {
return;
}
/* 2. Set an attribute value for the associated element using associated attribute’s local name and the result of
* running the ordered set serializer for token set. */
char *value = dom_ordered_set_serializer(token_set);
xmlSetNsProp(dom_token_list_get_element(intern), NULL, BAD_CAST "class", BAD_CAST value);
efree(intern->cached_string);
intern->cached_string = value;
}
static xmlChar *dom_token_list_get_class_value(const xmlAttr *attr, bool *should_free)
{
if (attr != NULL && attr->children != NULL) {
return php_libxml_attr_value(attr, should_free);
}
*should_free = false;
return NULL;
}
static void dom_token_list_update_set(dom_token_list_object *intern, HashTable *token_set)
{
/* https://dom.spec.whatwg.org/#ref-for-domtokenlist%E2%91%A0%E2%91%A1 */
bool should_free;
const xmlAttr *attr = dom_token_list_get_attr(intern);
/* 1. If the data is null, the token set remains empty. */
xmlChar *value = dom_token_list_get_class_value(attr, &should_free);
if (value != NULL) {
/* 2. Otherwise, parse the token set. */
dom_ordered_set_parser(token_set, (const char *) value);
intern->cached_string = estrdup((const char *) value);
} else {
intern->cached_string = NULL;
}
if (should_free) {
xmlFree(value);
}
}
static void dom_token_list_ensure_set_up_to_date(dom_token_list_object *intern)
{
bool should_free;
const xmlAttr *attr = dom_token_list_get_attr(intern);
xmlChar *value = dom_token_list_get_class_value(attr, &should_free);
/* xmlStrEqual will automatically handle equality rules of NULL vs "" (etc.) correctly. */
if (!xmlStrEqual(value, (const xmlChar *) intern->cached_string)) {
php_libxml_invalidate_cache_tag(&intern->cache_tag);
efree(intern->cached_string);
HashTable *token_set = TOKEN_LIST_GET_SET(intern);
zend_hash_destroy(token_set);
zend_hash_init(token_set, 0, NULL, NULL, false);
dom_token_list_update_set(intern, token_set);
}
if (should_free) {
xmlFree(value);
}
}
void dom_token_list_ctor(dom_token_list_object *intern, dom_object *element_obj)
{
php_libxml_node_ptr *ptr = element_obj->ptr;
ptr->refcount++;
intern->dom.ptr = ptr;
element_obj->document->refcount++;
intern->dom.document = element_obj->document;
intern->cache_tag.modification_nr = 0;
HashTable *token_set = TOKEN_LIST_GET_SET(intern);
zend_hash_init(token_set, 0, NULL, NULL, false);
dom_token_list_update_set(intern, token_set);
}
void dom_token_list_free_obj(zend_object *object)
{
dom_token_list_object *intern = php_dom_token_list_from_obj(object);
zend_object_std_dtor(object);
if (EXPECTED(intern->dom.ptr != NULL)) { /* Object initialized? */
xmlNodePtr node = dom_token_list_get_element(intern);
if (php_libxml_decrement_node_ptr_ref(intern->dom.ptr) == 0) {
php_libxml_node_free_resource(node);
}
php_libxml_decrement_doc_ref((php_libxml_node_object *) &intern->dom);
HashTable *token_set = TOKEN_LIST_GET_SET(intern);
zend_hash_destroy(token_set);
efree(intern->cached_string);
}
}
static bool dom_token_list_item_exists(dom_token_list_object *token_list, zend_long index)
{
dom_token_list_ensure_set_up_to_date(token_list);
HashTable *token_set = TOKEN_LIST_GET_SET(token_list);
return index >= 0 && index < zend_hash_num_elements(token_set);
}
static void dom_token_list_item_read(dom_token_list_object *token_list, zval *retval, zend_long index)
{
dom_token_list_ensure_set_up_to_date(token_list);
HashTable *token_set = TOKEN_LIST_GET_SET(token_list);
if (index >= 0 && index < zend_hash_num_elements(token_set)) {
HashPosition position;
zend_hash_internal_pointer_reset_ex(token_set, &position);
while (index > 0) {
zend_hash_move_forward_ex(token_set, &position);
index--;
}
zend_string *str_index;
zend_hash_get_current_key_ex(token_set, &str_index, NULL, &position);
ZVAL_STR_COPY(retval, str_index);
} else {
/* Not an out of bounds ValueError, but NULL, as according to spec.
* This design choice allows for constructs like `item(x) ?? ...`
*
* In particular:
* https://dom.spec.whatwg.org/#interface-domtokenlist states DOMTokenList implements iterable<DOMString>.
* From https://webidl.spec.whatwg.org/#idl-iterable:
* If a single type parameter is given,
* then the interface has a value iterator and provides values of the specified type.
* This applies, and reading the definition of value iterator means we should support indexed properties.
* From https://webidl.spec.whatwg.org/#dfn-support-indexed-properties:
* An interface that defines an indexed property getter is said to support indexed properties.
* And indexed property getter is defined here: https://webidl.spec.whatwg.org/#dfn-indexed-property-getter
* Down below in their note they give an example of how an out-of-bounds access evaluates to undefined,
* which would map to NULL for us.
* This would also be consistent with how out-of-bounds array accesses in PHP result in NULL. */
ZVAL_NULL(retval);
}
}
/* Adapted from spl_offset_convert_to_long */
static zend_long dom_token_list_offset_convert_to_long(zval *offset, bool *failed)
{
*failed = false;
while (true) {
switch (Z_TYPE_P(offset)) {
case IS_STRING: {
zend_ulong index;
if (ZEND_HANDLE_NUMERIC(Z_STR_P(offset), index)) {
return (zend_long) index;
}
ZEND_FALLTHROUGH;
}
default:
*failed = true;
return 0;
case IS_DOUBLE:
return zend_dval_to_lval_safe(Z_DVAL_P(offset));
case IS_LONG:
return Z_LVAL_P(offset);
case IS_FALSE:
return 0;
case IS_TRUE:
return 1;
case IS_REFERENCE:
offset = Z_REFVAL_P(offset);
break;
case IS_RESOURCE:
zend_use_resource_as_offset(offset);
return Z_RES_HANDLE_P(offset);
}
}
}
zval *dom_token_list_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
{
if (!offset) {
zend_throw_error(NULL, "Cannot append to Dom\\TokenList");
return NULL;
}
bool failed;
zend_long index = dom_token_list_offset_convert_to_long(offset, &failed);
if (UNEXPECTED(failed)) {
zend_illegal_container_offset(object->ce->name, offset, type);
return NULL;
} else {
dom_token_list_item_read(php_dom_token_list_from_obj(object), rv, index);
return rv;
}
}
int dom_token_list_has_dimension(zend_object *object, zval *offset, int check_empty)
{
bool failed;
zend_long index = dom_token_list_offset_convert_to_long(offset, &failed);
if (UNEXPECTED(failed)) {
zend_illegal_container_offset(object->ce->name, offset, BP_VAR_IS);
return 0;
} else {
dom_token_list_object *token_list = php_dom_token_list_from_obj(object);
if (check_empty) {
/* Need to perform an actual read to have the correct empty() semantics. */
zval rv;
dom_token_list_item_read(token_list, &rv, index);
int is_true = zend_is_true(&rv);
zval_ptr_dtor_nogc(&rv);
return is_true;
} else {
return dom_token_list_item_exists(token_list, index);
}
}
}
/* https://dom.spec.whatwg.org/#dom-domtokenlist-length */
zend_result dom_token_list_length_read(dom_object *obj, zval *retval)
{
dom_token_list_object *token_list = php_dom_token_list_from_dom_obj(obj);
dom_token_list_ensure_set_up_to_date(token_list);
ZVAL_LONG(retval, zend_hash_num_elements(TOKEN_LIST_GET_SET(token_list)));
return SUCCESS;
}
/* https://dom.spec.whatwg.org/#dom-domtokenlist-value
* and https://dom.spec.whatwg.org/#concept-dtl-serialize */
zend_result dom_token_list_value_read(dom_object *obj, zval *retval)
{
bool should_free;
dom_token_list_object *intern = php_dom_token_list_from_dom_obj(obj);
const xmlAttr *attr = dom_token_list_get_attr(intern);
xmlChar *value = dom_token_list_get_class_value(attr, &should_free);
ZVAL_STRING(retval, value ? (const char *) value : "");
if (should_free) {
xmlFree(value);
}
return SUCCESS;
}
/* https://dom.spec.whatwg.org/#dom-domtokenlist-value */
zend_result dom_token_list_value_write(dom_object *obj, zval *newval)
{
dom_token_list_object *intern = php_dom_token_list_from_dom_obj(obj);
if (UNEXPECTED(zend_str_has_nul_byte(Z_STR_P(newval)))) {
zend_value_error("Value must not contain any null bytes");
return FAILURE;
}
xmlSetNsProp(dom_token_list_get_element(intern), NULL, BAD_CAST "class", BAD_CAST Z_STRVAL_P(newval));
/* Note: we don't update the set here, the set is always lazily updated for performance reasons. */
return SUCCESS;
}
/* https://dom.spec.whatwg.org/#dom-domtokenlist-item */
PHP_METHOD(Dom_TokenList, item)
{
zend_long index;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(index)
ZEND_PARSE_PARAMETERS_END();
/* 1. If index is equal to or greater than this’s token set’s size, then return null. */
/* 2. Return this’s token set[index]. */
dom_token_list_item_read(TOKEN_LIST_GET_INTERNAL(), return_value, index);
}
/* https://dom.spec.whatwg.org/#dom-domtokenlist-contains */
PHP_METHOD(Dom_TokenList, contains)
{
zend_string *token;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_PATH_STR(token)
ZEND_PARSE_PARAMETERS_END();
dom_token_list_object *token_list = TOKEN_LIST_GET_INTERNAL();
dom_token_list_ensure_set_up_to_date(token_list);
HashTable *token_set = TOKEN_LIST_GET_SET(token_list);
RETURN_BOOL(zend_hash_exists(token_set, token));
}
/* Steps taken from the add, remove, toggle, replace methods. */
static bool dom_validate_token(const zend_string *str)
{
/* 1. If token is the empty string, then throw a "SyntaxError" DOMException. */
if (ZSTR_LEN(str) == 0) {
php_dom_throw_error_with_message(SYNTAX_ERR, "The empty string is not a valid token", true);
return false;
}
/* 2. If token contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException. */
if (dom_contains_ascii_whitespace(ZSTR_VAL(str))) {
php_dom_throw_error_with_message(INVALID_CHARACTER_ERR, "The token must not contain any ASCII whitespace", true);
return false;
}
return true;
}
static bool dom_validate_tokens_varargs(const zval *args, uint32_t argc)
{
for (uint32_t i = 0; i < argc; i++) {
if (Z_TYPE(args[i]) != IS_STRING) {
zend_argument_type_error(i + 1, "must be of type string, %s given", zend_zval_value_name(&args[i]));
return false;
}
if (zend_str_has_nul_byte(Z_STR(args[i]))) {
zend_argument_value_error(i + 1, "must not contain any null bytes");
return false;
}
if (!dom_validate_token(Z_STR(args[i]))) {
return false;
}
}
return true;
}
/* https://dom.spec.whatwg.org/#dom-domtokenlist-add */
PHP_METHOD(Dom_TokenList, add)
{
zval *args;
uint32_t argc;
ZEND_PARSE_PARAMETERS_START(0, -1)
Z_PARAM_VARIADIC('*', args, argc)
ZEND_PARSE_PARAMETERS_END();
/* 1. For each token in tokens (...) */
if (!dom_validate_tokens_varargs(args, argc)) {
RETURN_THROWS();
}
/* 2. For each token in tokens, append token to this’s token set. */
dom_token_list_object *intern = TOKEN_LIST_GET_INTERNAL();
dom_token_list_ensure_set_up_to_date(intern);
HashTable *token_set = TOKEN_LIST_GET_SET(intern);
for (uint32_t i = 0; i < argc; i++) {
dom_add_token(token_set, Z_STR(args[i]));
}
/* 3. Run the update steps. */
dom_token_list_update(intern);
}
/* https://dom.spec.whatwg.org/#dom-domtokenlist-remove */
PHP_METHOD(Dom_TokenList, remove)
{
zval *args;
uint32_t argc;
ZEND_PARSE_PARAMETERS_START(0, -1)
Z_PARAM_VARIADIC('*', args, argc)
ZEND_PARSE_PARAMETERS_END();
/* 1. For each token in tokens (...) */
if (!dom_validate_tokens_varargs(args, argc)) {
RETURN_THROWS();
}
/* 2. For each token in tokens, remove token from this’s token set. */
dom_token_list_object *intern = TOKEN_LIST_GET_INTERNAL();
dom_token_list_ensure_set_up_to_date(intern);
HashTable *token_set = TOKEN_LIST_GET_SET(intern);
for (uint32_t i = 0; i < argc; i++) {
zend_hash_del(token_set, Z_STR(args[i]));
}
/* 3. Run the update steps. */
dom_token_list_update(intern);
}
/* https://dom.spec.whatwg.org/#dom-domtokenlist-toggle */
PHP_METHOD(Dom_TokenList, toggle)
{
zend_string *token;
bool force, force_not_given = true;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_PATH_STR(token)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL_OR_NULL(force, force_not_given)
ZEND_PARSE_PARAMETERS_END();
/* Steps 1 - 2 */
if (!dom_validate_token(token)) {
RETURN_THROWS();
}
/* 3. If this’s token set[token] exists, then: */
dom_token_list_object *intern = TOKEN_LIST_GET_INTERNAL();
dom_token_list_ensure_set_up_to_date(intern);
HashTable *token_set = TOKEN_LIST_GET_SET(intern);
zval *found_token = zend_hash_find(token_set, token);
if (found_token != NULL) {
ZEND_ASSERT(XtOffsetOf(Bucket, val) == 0 && "the cast only works if this is true");
Bucket *bucket = (Bucket *) found_token;
/* 3.1. If force is either not given or is false, then remove token from this’s token set,
* run the update steps and return false. */
if (force_not_given || !force) {
zend_hash_del_bucket(token_set, bucket);
dom_token_list_update(intern);
RETURN_FALSE;
}
/* 3.2. Return true. */
RETURN_TRUE;
}
/* 4. Otherwise, if force not given or is true, append token to this’s token set,
* run the update steps, and return true. */
else if (force_not_given || force) {
dom_add_token(token_set, token);
dom_token_list_update(intern);
RETURN_TRUE;
}
/* 5. Return false. */
RETURN_FALSE;
}
/* https://dom.spec.whatwg.org/#dom-domtokenlist-replace */
PHP_METHOD(Dom_TokenList, replace)
{
zend_string *token, *new_token;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_PATH_STR(token)
Z_PARAM_PATH_STR(new_token)
ZEND_PARSE_PARAMETERS_END();
/* Steps 1 - 2 */
if (!dom_validate_token(token) || !dom_validate_token(new_token)) {
RETURN_THROWS();
}
/* 3. If this’s token set does not contain token, then return false. */
dom_token_list_object *intern = TOKEN_LIST_GET_INTERNAL();
dom_token_list_ensure_set_up_to_date(intern);
HashTable *token_set = TOKEN_LIST_GET_SET(intern);
zval *found_token = zend_hash_find(token_set, token);
if (found_token == NULL) {
RETURN_FALSE;
}
/* 4. Replace token in this’s token set with newToken. */
ZEND_ASSERT(XtOffsetOf(Bucket, val) == 0 && "the cast only works if this is true");
Bucket *bucket = (Bucket *) found_token;
if (zend_hash_set_bucket_key(token_set, bucket, new_token) == NULL) {
/* It already exists, remove token instead. */
zend_hash_del_bucket(token_set, bucket);
} else {
/* Need to use ZVAL_STR instead of Z_STR to reset the type flags. */
ZVAL_STR(&bucket->val, new_token);
}
/* 5. Run the update steps. */
dom_token_list_update(intern);
/* 6. Return true. */
RETURN_TRUE;
}
/* https://dom.spec.whatwg.org/#concept-domtokenlist-validation */
PHP_METHOD(Dom_TokenList, supports)
{
zend_string *token;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_PATH_STR(token)
ZEND_PARSE_PARAMETERS_END();
/* The spec designers have designed the TokenList API with future usages in mind.
* But right now, this should just always throw a TypeError because the only user is classList, which
* does not define a supported token set. */
zend_throw_error(zend_ce_type_error, "Attribute \"class\" does not define any supported tokens");
}
PHP_METHOD(Dom_TokenList, count)
{
ZEND_PARSE_PARAMETERS_NONE();
dom_token_list_object *intern = TOKEN_LIST_GET_INTERNAL();
dom_token_list_ensure_set_up_to_date(intern);
RETURN_LONG(zend_hash_num_elements(TOKEN_LIST_GET_SET(intern)));
}
PHP_METHOD(Dom_TokenList, getIterator)
{
ZEND_PARSE_PARAMETERS_NONE();
zend_create_internal_iterator_zval(return_value, ZEND_THIS);
}
static void dom_token_list_it_dtor(zend_object_iterator *iter)
{
zval_ptr_dtor(&iter->data);
}
static void dom_token_list_it_rewind(zend_object_iterator *iter)
{
dom_token_list_it *iterator = (dom_token_list_it *) iter;
dom_token_list_object *object = Z_TOKEN_LIST_P(&iter->data);
zend_hash_internal_pointer_reset_ex(TOKEN_LIST_GET_SET(object), &iterator->pos);
}
static zend_result dom_token_list_it_valid(zend_object_iterator *iter)
{
dom_token_list_it *iterator = (dom_token_list_it *) iter;
dom_token_list_object *object = Z_TOKEN_LIST_P(&iter->data);
HashTable *token_set = TOKEN_LIST_GET_SET(object);
dom_token_list_ensure_set_up_to_date(object);
iterator->pos = zend_hash_get_current_pos_ex(token_set, iterator->pos);
return iterator->pos >= token_set->nNumUsed ? FAILURE : SUCCESS;
}
static zval *dom_token_list_it_get_current_data(zend_object_iterator *iter)
{
dom_token_list_it *iterator = (dom_token_list_it *) iter;
dom_token_list_object *object = Z_TOKEN_LIST_P(&iter->data);
dom_token_list_ensure_set_up_to_date(object);
/* Caller manages the refcount of the data. */
return zend_hash_get_current_data_ex(TOKEN_LIST_GET_SET(object), &iterator->pos);
}
static void dom_token_list_it_get_current_key(zend_object_iterator *iter, zval *key)
{
dom_token_list_it *iterator = (dom_token_list_it *) iter;
dom_token_list_object *object = Z_TOKEN_LIST_P(&iter->data);
dom_token_list_ensure_set_up_to_date(object);
if (UNEXPECTED(php_libxml_is_cache_tag_stale(&object->cache_tag, &iterator->cache_tag))) {
iter->index = 0;
HashPosition pos;
HashTable *token_set = TOKEN_LIST_GET_SET(object);
zend_hash_internal_pointer_reset_ex(token_set, &pos);
while (pos != iterator->pos) {
iter->index++;
zend_hash_move_forward_ex(token_set, &pos);
}
}
ZVAL_LONG(key, iter->index);
}
static void dom_token_list_it_move_forward(zend_object_iterator *iter)
{
dom_token_list_it *iterator = (dom_token_list_it *) iter;
dom_token_list_object *object = Z_TOKEN_LIST_P(&iter->data);
HashTable *token_set = TOKEN_LIST_GET_SET(object);
dom_token_list_ensure_set_up_to_date(object);
HashPosition current = iterator->pos;
HashPosition validated = zend_hash_get_current_pos_ex(token_set, iterator->pos);
/* Check if already moved due to user operations, if so don't move again but reset to the first valid position,
* otherwise move one forward. */
if (validated != current) {
iterator->pos = validated;
} else {
zend_hash_move_forward_ex(token_set, &iterator->pos);
}
}
static const zend_object_iterator_funcs dom_token_list_it_funcs = {
dom_token_list_it_dtor,
dom_token_list_it_valid,
dom_token_list_it_get_current_data,
dom_token_list_it_get_current_key,
dom_token_list_it_move_forward,
dom_token_list_it_rewind,
NULL, /* invalidate_current */
NULL, /* get_gc */
};
zend_object_iterator *dom_token_list_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
{
if (by_ref) {
zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
return NULL;
}
dom_token_list_object *intern = Z_TOKEN_LIST_P(object);
dom_token_list_ensure_set_up_to_date(intern);
HashTable *token_set = TOKEN_LIST_GET_SET(intern);
dom_token_list_it *iterator = emalloc(sizeof(*iterator));
zend_iterator_init(&iterator->it);
zend_hash_internal_pointer_reset_ex(token_set, &iterator->pos);
ZVAL_OBJ_COPY(&iterator->it.data, Z_OBJ_P(object));
iterator->it.funcs = &dom_token_list_it_funcs;
iterator->cache_tag = intern->cache_tag;
return &iterator->it;
}
#endif