1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

Factor out common "first container's child" code

This commit is contained in:
Niels Dossche
2024-04-02 23:29:52 +02:00
parent b7bc41341c
commit 15259a0a6c
3 changed files with 14 additions and 21 deletions

View File

@@ -180,11 +180,7 @@ static void php_dom_iterator_move_forward(zend_object_iterator *iter) /* {{{ */
if (php_dom_is_cache_tag_stale_from_node(&iterator->cache_tag, basenode)) {
php_dom_mark_cache_tag_up_to_date_from_node(&iterator->cache_tag, basenode);
previndex = 0;
if (basenode->type == XML_DOCUMENT_NODE || basenode->type == XML_HTML_DOCUMENT_NODE) {
curnode = xmlDocGetRootElement((xmlDoc *) basenode);
} else {
curnode = basenode->children;
}
curnode = php_dom_first_child_of_container_node(basenode);
} else {
previndex = iter->index - 1;
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
curnode = (xmlNodePtr) basep->children;
}
} else {
xmlNodePtr nodep = basep;
if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
nodep = xmlDocGetRootElement((xmlDoc *) nodep);
} else {
nodep = nodep->children;
}
xmlNodePtr nodep = php_dom_first_child_of_container_node(basep);
curnode = dom_get_elements_by_tag_name_ns_raw(
basep, nodep, objmap->ns, objmap->local, objmap->local_lower, &curindex, 0);
}

View File

@@ -93,11 +93,7 @@ int php_dom_get_nodelist_length(dom_object *obj)
}
} else {
xmlNodePtr basep = nodep;
if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
nodep = xmlDocGetRootElement((xmlDoc *) nodep);
} else {
nodep = nodep->children;
}
nodep = php_dom_first_child_of_container_node(basep);
dom_get_elements_by_tag_name_ns_raw(
basep, nodep, objmap->ns, objmap->local, objmap->local_lower, &count, INT_MAX - 1 /* because of <= */);
}
@@ -189,11 +185,7 @@ void php_dom_nodelist_get_item_into_zval(dom_nnodemap_object *objmap, zend_long
itemnode = nodep;
} else {
if (restart) {
if (basep->type == XML_DOCUMENT_NODE || basep->type == XML_HTML_DOCUMENT_NODE) {
nodep = xmlDocGetRootElement((xmlDoc*) basep);
} else {
nodep = basep->children;
}
nodep = php_dom_first_child_of_container_node(basep);
}
itemnode = dom_get_elements_by_tag_name_ns_raw(basep, nodep, objmap->ns, objmap->local, objmap->local_lower, &count, relative_index);
}

View File

@@ -275,6 +275,16 @@ static zend_always_inline bool php_dom_follow_spec_node(const xmlNode *node)
return false;
}
/* Returns the first child of a container node (e.g. elements, fragments, documents, ...). */
static zend_always_inline xmlNodePtr php_dom_first_child_of_container_node(xmlNodePtr parent)
{
if (parent->type == XML_DOCUMENT_NODE || parent->type == XML_HTML_DOCUMENT_NODE) {
return xmlDocGetRootElement((xmlDoc *) parent);
} else {
return parent->children;
}
}
PHP_MINIT_FUNCTION(dom);
PHP_MSHUTDOWN_FUNCTION(dom);
PHP_MINFO_FUNCTION(dom);