Skip to content

Commit 15259a0

Browse files
committed
Factor out common "first container's child" code
1 parent b7bc413 commit 15259a0

File tree

3 files changed

+14
-21
lines changed

3 files changed

+14
-21
lines changed

ext/dom/dom_iterators.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,7 @@ static void php_dom_iterator_move_forward(zend_object_iterator *iter) /* {{{ */
180180
if (php_dom_is_cache_tag_stale_from_node(&iterator->cache_tag, basenode)) {
181181
php_dom_mark_cache_tag_up_to_date_from_node(&iterator->cache_tag, basenode);
182182
previndex = 0;
183-
if (basenode->type == XML_DOCUMENT_NODE || basenode->type == XML_HTML_DOCUMENT_NODE) {
184-
curnode = xmlDocGetRootElement((xmlDoc *) basenode);
185-
} else {
186-
curnode = basenode->children;
187-
}
183+
curnode = php_dom_first_child_of_container_node(basenode);
188184
} else {
189185
previndex = iter->index - 1;
190186
curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->ptr)->node;
@@ -264,12 +260,7 @@ zend_object_iterator *php_dom_get_iterator(zend_class_entry *ce, zval *object, i
264260
curnode = (xmlNodePtr) basep->children;
265261
}
266262
} else {
267-
xmlNodePtr nodep = basep;
268-
if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
269-
nodep = xmlDocGetRootElement((xmlDoc *) nodep);
270-
} else {
271-
nodep = nodep->children;
272-
}
263+
xmlNodePtr nodep = php_dom_first_child_of_container_node(basep);
273264
curnode = dom_get_elements_by_tag_name_ns_raw(
274265
basep, nodep, objmap->ns, objmap->local, objmap->local_lower, &curindex, 0);
275266
}

ext/dom/nodelist.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,7 @@ int php_dom_get_nodelist_length(dom_object *obj)
9393
}
9494
} else {
9595
xmlNodePtr basep = nodep;
96-
if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
97-
nodep = xmlDocGetRootElement((xmlDoc *) nodep);
98-
} else {
99-
nodep = nodep->children;
100-
}
96+
nodep = php_dom_first_child_of_container_node(basep);
10197
dom_get_elements_by_tag_name_ns_raw(
10298
basep, nodep, objmap->ns, objmap->local, objmap->local_lower, &count, INT_MAX - 1 /* because of <= */);
10399
}
@@ -189,11 +185,7 @@ void php_dom_nodelist_get_item_into_zval(dom_nnodemap_object *objmap, zend_long
189185
itemnode = nodep;
190186
} else {
191187
if (restart) {
192-
if (basep->type == XML_DOCUMENT_NODE || basep->type == XML_HTML_DOCUMENT_NODE) {
193-
nodep = xmlDocGetRootElement((xmlDoc*) basep);
194-
} else {
195-
nodep = basep->children;
196-
}
188+
nodep = php_dom_first_child_of_container_node(basep);
197189
}
198190
itemnode = dom_get_elements_by_tag_name_ns_raw(basep, nodep, objmap->ns, objmap->local, objmap->local_lower, &count, relative_index);
199191
}

ext/dom/php_dom.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,16 @@ static zend_always_inline bool php_dom_follow_spec_node(const xmlNode *node)
275275
return false;
276276
}
277277

278+
/* Returns the first child of a container node (e.g. elements, fragments, documents, ...). */
279+
static zend_always_inline xmlNodePtr php_dom_first_child_of_container_node(xmlNodePtr parent)
280+
{
281+
if (parent->type == XML_DOCUMENT_NODE || parent->type == XML_HTML_DOCUMENT_NODE) {
282+
return xmlDocGetRootElement((xmlDoc *) parent);
283+
} else {
284+
return parent->children;
285+
}
286+
}
287+
278288
PHP_MINIT_FUNCTION(dom);
279289
PHP_MSHUTDOWN_FUNCTION(dom);
280290
PHP_MINFO_FUNCTION(dom);

0 commit comments

Comments
 (0)