Skip to content

Commit 2c0bd98

Browse files
committed
Refactor dom_html_collection_named_item()
This factors out the specific objmap handling to virtual functions. This is the last step in preparation for GH-18550.
1 parent 52280ef commit 2c0bd98

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

ext/dom/html_collection.c

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,47 +46,35 @@ static dom_named_item dom_html_collection_named_item(zend_string *key, zend_obje
4646

4747
/* 2. Return the first element in the collection for which at least one of the following is true: */
4848
xmlNodePtr basep = dom_object_get_node(objmap->baseobj);
49-
if (basep != NULL) {
50-
zend_long cur = 0;
51-
zend_long next = cur; /* not +1, otherwise we skip the first candidate */
52-
xmlNodePtr candidate = basep->children;
53-
bool iterate_tag_name = objmap->handler == &php_dom_obj_map_by_tag_name;
54-
while (candidate != NULL) {
55-
if (iterate_tag_name) {
56-
candidate = dom_get_elements_by_tag_name_ns_raw(basep, candidate, objmap->ns, objmap->local, objmap->local_lower, &cur, next);
57-
if (candidate == NULL) {
58-
break;
59-
}
60-
next = cur + 1;
61-
} else {
62-
if (candidate->type != XML_ELEMENT_NODE) {
63-
candidate = candidate->next;
64-
continue;
65-
}
49+
if (basep != NULL && basep->children != NULL) {
50+
php_dom_obj_map_collection_iter iter = {0};
51+
iter.candidate = basep->children;
52+
iter.basep = basep;
53+
54+
while (true) {
55+
objmap->handler->collection_named_item_iter(objmap, &iter);
56+
if (iter.candidate == NULL) {
57+
break;
6658
}
6759

68-
ZEND_ASSERT(candidate->type == XML_ELEMENT_NODE);
60+
ZEND_ASSERT(iter.candidate->type == XML_ELEMENT_NODE);
6961

7062
xmlAttrPtr attr;
7163

7264
/* it has an ID which is key; */
73-
if ((attr = xmlHasNsProp(candidate, BAD_CAST "id", NULL)) != NULL && dom_compare_value(attr, BAD_CAST ZSTR_VAL(key))) {
65+
if ((attr = xmlHasNsProp(iter.candidate, BAD_CAST "id", NULL)) != NULL && dom_compare_value(attr, BAD_CAST ZSTR_VAL(key))) {
7466
ret.context_intern = objmap->baseobj;
75-
ret.node = candidate;
67+
ret.node = iter.candidate;
7668
return ret;
7769
}
7870
/* it is in the HTML namespace and has a name attribute whose value is key; */
79-
else if (php_dom_ns_is_fast(candidate, php_dom_ns_is_html_magic_token)) {
80-
if ((attr = xmlHasNsProp(candidate, BAD_CAST "name", NULL)) != NULL && dom_compare_value(attr, BAD_CAST ZSTR_VAL(key))) {
71+
else if (php_dom_ns_is_fast(iter.candidate, php_dom_ns_is_html_magic_token)) {
72+
if ((attr = xmlHasNsProp(iter.candidate, BAD_CAST "name", NULL)) != NULL && dom_compare_value(attr, BAD_CAST ZSTR_VAL(key))) {
8173
ret.context_intern = objmap->baseobj;
82-
ret.node = candidate;
74+
ret.node = iter.candidate;
8375
return ret;
8476
}
8577
}
86-
87-
if (!iterate_tag_name) {
88-
candidate = candidate->next;
89-
}
9078
}
9179
}
9280

ext/dom/obj_map.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,16 @@ static void dom_map_get_elements_item(dom_nnodemap_object *map, zend_long index,
266266
}
267267
}
268268

269+
static void dom_map_collection_named_item_elements_iter(dom_nnodemap_object *map, php_dom_obj_map_collection_iter *iter)
270+
{
271+
if (iter->candidate != iter->basep->children) {
272+
iter->candidate = iter->candidate->next;
273+
}
274+
while (iter->candidate && iter->candidate->type != XML_ELEMENT_NODE) {
275+
iter->candidate = iter->candidate->next;
276+
}
277+
}
278+
269279
static void dom_map_get_by_tag_name_item(dom_nnodemap_object *map, zend_long index, zval *return_value)
270280
{
271281
xmlNodePtr nodep = dom_object_get_node(map->baseobj);
@@ -282,6 +292,12 @@ static void dom_map_get_by_tag_name_item(dom_nnodemap_object *map, zend_long ind
282292
}
283293
}
284294

295+
static void dom_map_collection_named_item_by_tag_name_iter(dom_nnodemap_object *map, php_dom_obj_map_collection_iter *iter)
296+
{
297+
iter->candidate = dom_get_elements_by_tag_name_ns_raw(iter->basep, iter->candidate, map->ns, map->local, map->local_lower, &iter->cur, iter->next);
298+
iter->next = iter->cur + 1;
299+
}
300+
285301
static void dom_map_get_null_item(dom_nnodemap_object *map, zend_long index, zval *return_value)
286302
{
287303
RETURN_NULL();
@@ -450,6 +466,7 @@ const php_dom_obj_map_handler php_dom_obj_map_attributes = {
450466
.get_item = dom_map_get_attributes_item,
451467
.get_ns_named_item = dom_map_get_ns_named_item_prop,
452468
.has_ns_named_item = dom_map_has_ns_named_item_prop,
469+
.collection_named_item_iter = NULL,
453470
.use_cache = false,
454471
.nameless = false,
455472
};
@@ -459,6 +476,7 @@ const php_dom_obj_map_handler php_dom_obj_map_by_tag_name = {
459476
.get_item = dom_map_get_by_tag_name_item,
460477
.get_ns_named_item = dom_map_get_ns_named_item_null,
461478
.has_ns_named_item = dom_map_has_ns_named_item_null,
479+
.collection_named_item_iter = dom_map_collection_named_item_by_tag_name_iter,
462480
.use_cache = true,
463481
.nameless = true,
464482
};
@@ -468,6 +486,7 @@ const php_dom_obj_map_handler php_dom_obj_map_child_nodes = {
468486
.get_item = dom_map_get_nodes_item,
469487
.get_ns_named_item = dom_map_get_ns_named_item_null,
470488
.has_ns_named_item = dom_map_has_ns_named_item_null,
489+
.collection_named_item_iter = NULL,
471490
.use_cache = true,
472491
.nameless = true,
473492
};
@@ -477,6 +496,7 @@ const php_dom_obj_map_handler php_dom_obj_map_nodeset = {
477496
.get_item = dom_map_get_nodeset_item,
478497
.get_ns_named_item = dom_map_get_ns_named_item_null,
479498
.has_ns_named_item = dom_map_has_ns_named_item_null,
499+
.collection_named_item_iter = NULL,
480500
.use_cache = false,
481501
.nameless = true,
482502
};
@@ -486,6 +506,7 @@ const php_dom_obj_map_handler php_dom_obj_map_entities = {
486506
.get_item = dom_map_get_entity_item,
487507
.get_ns_named_item = dom_map_get_ns_named_item_entity,
488508
.has_ns_named_item = dom_map_has_ns_named_item_xmlht,
509+
.collection_named_item_iter = NULL,
489510
.use_cache = false,
490511
.nameless = false,
491512
};
@@ -495,6 +516,7 @@ const php_dom_obj_map_handler php_dom_obj_map_notations = {
495516
.get_item = dom_map_get_notation_item,
496517
.get_ns_named_item = dom_map_get_ns_named_item_notation,
497518
.has_ns_named_item = dom_map_has_ns_named_item_xmlht,
519+
.collection_named_item_iter = NULL,
498520
.use_cache = false,
499521
.nameless = false,
500522
};
@@ -504,6 +526,7 @@ const php_dom_obj_map_handler php_dom_obj_map_child_elements = {
504526
.get_item = dom_map_get_elements_item,
505527
.get_ns_named_item = dom_map_get_ns_named_item_null,
506528
.has_ns_named_item = dom_map_has_ns_named_item_null,
529+
.collection_named_item_iter = dom_map_collection_named_item_elements_iter,
507530
.use_cache = true,
508531
.nameless = true,
509532
};
@@ -513,6 +536,7 @@ const php_dom_obj_map_handler php_dom_obj_map_noop = {
513536
.get_item = dom_map_get_null_item,
514537
.get_ns_named_item = dom_map_get_ns_named_item_null,
515538
.has_ns_named_item = dom_map_has_ns_named_item_null,
539+
.collection_named_item_iter = NULL,
516540
.use_cache = false,
517541
.nameless = true,
518542
};

ext/dom/obj_map.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@
1919

2020
typedef struct dom_nnodemap_object dom_nnodemap_object;
2121

22+
typedef struct php_dom_obj_map_collection_iter {
23+
zend_long cur, next;
24+
xmlNodePtr candidate, basep;
25+
} php_dom_obj_map_collection_iter;
26+
2227
typedef struct php_dom_obj_map_handler {
2328
zend_long (*length)(dom_nnodemap_object *);
2429
void (*get_item)(dom_nnodemap_object *, zend_long, zval *);
2530
xmlNodePtr (*get_ns_named_item)(dom_nnodemap_object *, const zend_string *, const char *);
2631
bool (*has_ns_named_item)(dom_nnodemap_object *, const zend_string *, const char *);
32+
void (*collection_named_item_iter)(dom_nnodemap_object *, php_dom_obj_map_collection_iter *);
2733
bool use_cache;
2834
bool nameless;
2935
} php_dom_obj_map_handler;

0 commit comments

Comments
 (0)