Skip to content

Cleanup iterator instantiation code #17358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ext/dom/documenttype.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include "php_dom.h"
#include "dom_properties.h"
#include "internal_helpers.h"

/* {{{ name string
readonly=yes
Expand All @@ -47,7 +48,7 @@ zend_result dom_documenttype_entities_read(dom_object *obj, zval *retval)
{
DOM_PROP_NODE(xmlDtdPtr, dtdptr, obj);

php_dom_create_iterator(retval, DOM_DTD_NAMEDNODEMAP, php_dom_follow_spec_intern(obj));
object_init_ex(retval, dom_get_dtd_namednodemap_ce(php_dom_follow_spec_intern(obj)));

xmlHashTable *entityht = (xmlHashTable *) dtdptr->entities;

Expand All @@ -68,7 +69,7 @@ zend_result dom_documenttype_notations_read(dom_object *obj, zval *retval)
{
DOM_PROP_NODE(xmlDtdPtr, dtdptr, obj);

php_dom_create_iterator(retval, DOM_DTD_NAMEDNODEMAP, php_dom_follow_spec_intern(obj));
object_init_ex(retval, dom_get_dtd_namednodemap_ce(php_dom_follow_spec_intern(obj)));

xmlHashTable *notationht = (xmlHashTable *) dtdptr->notations;

Expand Down
24 changes: 8 additions & 16 deletions ext/dom/element.c
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ PHP_METHOD(Dom_Element, removeAttributeNode)
Modern spec URL: https://dom.spec.whatwg.org/#concept-getelementsbytagname
Since:
*/
static void dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAMETERS, bool modern)
static void dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *iter_ce)
{
dom_object *intern, *namednode;
zend_string *name;
Expand All @@ -825,23 +825,19 @@ static void dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAMETERS, b

DOM_GET_THIS_INTERN(intern);

if (modern) {
php_dom_create_iterator(return_value, DOM_HTMLCOLLECTION, true);
} else {
php_dom_create_iterator(return_value, DOM_NODELIST, false);
}
object_init_ex(return_value, iter_ce);
namednode = Z_DOMOBJ_P(return_value);
dom_namednode_iter(intern, 0, namednode, NULL, name, NULL);
}

PHP_METHOD(DOMElement, getElementsByTagName)
{
dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_nodelist_class_entry);
}

PHP_METHOD(Dom_Element, getElementsByTagName)
{
dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_html_collection_class_entry);
}
/* }}} end dom_element_get_elements_by_tag_name */

Expand Down Expand Up @@ -1236,7 +1232,7 @@ PHP_METHOD(Dom_Element, setAttributeNodeNS)
Modern spec URL: https://dom.spec.whatwg.org/#concept-getelementsbytagnamens
Since: DOM Level 2
*/
static void dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAMETERS, bool modern)
static void dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *iter_ce)
{
dom_object *intern, *namednode;
zend_string *uri, *name;
Expand All @@ -1261,23 +1257,19 @@ static void dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAMETERS

DOM_GET_THIS_INTERN(intern);

if (modern) {
php_dom_create_iterator(return_value, DOM_HTMLCOLLECTION, true);
} else {
php_dom_create_iterator(return_value, DOM_NODELIST, false);
}
object_init_ex(return_value, iter_ce);
namednode = Z_DOMOBJ_P(return_value);
dom_namednode_iter(intern, 0, namednode, NULL, name, uri);
}

PHP_METHOD(DOMElement, getElementsByTagNameNS)
{
dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_nodelist_class_entry);
}

PHP_METHOD(Dom_Element, getElementsByTagNameNS)
{
dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_html_collection_class_entry);
}
/* }}} end dom_element_get_elements_by_tag_name_ns */

Expand Down
4 changes: 2 additions & 2 deletions ext/dom/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ zend_result dom_node_child_nodes_read(dom_object *obj, zval *retval)
{
DOM_PROP_NODE(xmlNodePtr, nodep, obj);

php_dom_create_iterator(retval, DOM_NODELIST, php_dom_follow_spec_intern(obj));
object_init_ex(retval, dom_get_nodelist_ce(php_dom_follow_spec_intern(obj)));
dom_object *intern = Z_DOMOBJ_P(retval);
dom_namednode_iter(obj, XML_ELEMENT_NODE, intern, NULL, NULL, NULL);

Expand Down Expand Up @@ -415,7 +415,7 @@ zend_result dom_node_attributes_read(dom_object *obj, zval *retval)
DOM_PROP_NODE(xmlNodePtr, nodep, obj);

if (nodep->type == XML_ELEMENT_NODE) {
php_dom_create_iterator(retval, DOM_NAMEDNODEMAP, php_dom_follow_spec_intern(obj));
object_init_ex(retval, dom_get_namednodemap_ce(php_dom_follow_spec_intern(obj)));
dom_object *intern = Z_DOMOBJ_P(retval);
dom_namednode_iter(obj, XML_ATTRIBUTE_NODE, intern, NULL, NULL, NULL);
} else {
Expand Down
2 changes: 1 addition & 1 deletion ext/dom/parentnode/css_selectors.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ void dom_parent_node_query_selector_all(xmlNodePtr thisp, dom_object *intern, zv
zend_array_destroy(list);
RETURN_THROWS();
} else {
php_dom_create_iterator(return_value, DOM_NODELIST, true);
object_init_ex(return_value, dom_modern_nodelist_class_entry);
dom_object *ret_obj = Z_DOMOBJ_P(return_value);
dom_nnodemap_object *mapptr = (dom_nnodemap_object *) ret_obj->ptr;
ZVAL_ARR(&mapptr->baseobj_zv, list);
Expand Down
21 changes: 0 additions & 21 deletions ext/dom/php_dom.c
Original file line number Diff line number Diff line change
Expand Up @@ -1623,27 +1623,6 @@ zend_object *dom_nnodemap_objects_new(zend_class_entry *class_type)
return &intern->std;
}

void php_dom_create_iterator(zval *return_value, dom_iterator_type iterator_type, bool modern) /* {{{ */
{
zend_class_entry *ce;

if (iterator_type == DOM_NAMEDNODEMAP) {
ce = dom_get_namednodemap_ce(modern);
} else if (iterator_type == DOM_HTMLCOLLECTION) {
/* This only exists in modern DOM. */
ZEND_ASSERT(modern);
ce = dom_html_collection_class_entry;
} else if (iterator_type == DOM_DTD_NAMEDNODEMAP) {
ce = dom_get_dtd_namednodemap_ce(modern);
} else {
ZEND_ASSERT(iterator_type == DOM_NODELIST);
ce = dom_get_nodelist_ce(modern);
}

object_init_ex(return_value, ce);
}
/* }}} */

static zend_always_inline zend_class_entry *dom_get_element_ce(const xmlNode *node, bool modern)
{
if (modern) {
Expand Down
8 changes: 0 additions & 8 deletions ext/dom/php_dom.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,6 @@ typedef struct {
dom_object dom;
} dom_object_namespace_node;

typedef enum dom_iterator_type {
DOM_NODELIST,
DOM_NAMEDNODEMAP,
DOM_DTD_NAMEDNODEMAP,
DOM_HTMLCOLLECTION,
} dom_iterator_type;

struct php_dom_libxml_ns_mapper;
typedef struct php_dom_libxml_ns_mapper php_dom_libxml_ns_mapper;

Expand Down Expand Up @@ -151,7 +144,6 @@ int dom_hierarchy(xmlNodePtr parent, xmlNodePtr child);
bool dom_has_feature(zend_string *feature, zend_string *version);
bool dom_node_is_read_only(const xmlNode *node);
bool dom_node_children_valid(const xmlNode *node);
void php_dom_create_iterator(zval *return_value, dom_iterator_type iterator_type, bool modern);
void dom_namednode_iter(dom_object *basenode, int ntype, dom_object *intern, xmlHashTablePtr ht, zend_string *local, zend_string *ns);
xmlNodePtr create_notation(const xmlChar *name, const xmlChar *ExternalID, const xmlChar *SystemID);
xmlNode *php_dom_libxml_hash_iter(dom_nnodemap_object *objmap, int index);
Expand Down
3 changes: 2 additions & 1 deletion ext/dom/xpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "php_dom.h"
#include "namespace_compat.h"
#include "private_data.h"
#include "internal_helpers.h"

#define PHP_DOM_XPATH_QUERY 0
#define PHP_DOM_XPATH_EVALUATE 1
Expand Down Expand Up @@ -371,7 +372,7 @@ static void php_xpath_eval(INTERNAL_FUNCTION_PARAMETERS, int type, bool modern)
} else {
ZVAL_EMPTY_ARRAY(&retval);
}
php_dom_create_iterator(return_value, DOM_NODELIST, modern);
object_init_ex(return_value, dom_get_nodelist_ce(modern));
nodeobj = Z_DOMOBJ_P(return_value);
dom_xpath_iter(&retval, nodeobj);
break;
Expand Down
Loading