Skip to content

Implement namedItem() and equivalent dimension handlers for HTMLCollection #13937

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

Closed
wants to merge 6 commits into from
Closed
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
41 changes: 41 additions & 0 deletions ext/dom/attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,45 @@ PHP_METHOD(DOMAttr, isId)
}
/* }}} end dom_attr_is_id */

xmlChar *dom_attr_value(const xmlAttr *attr, bool *free)
{
/* For attributes we can have an optimized fast-path.
* This fast-path is only possible in the (common) case where the attribute
* has a single text child. Note that if the child or the content is NULL, this
* is equivalent to not having content (i.e. the attribute has the empty string as value). */

*free = false;

if (attr->children == NULL) {
return BAD_CAST "";
}

if (attr->children->type == XML_TEXT_NODE && attr->children->next == NULL) {
if (attr->children->content == NULL) {
return BAD_CAST "";
} else {
return attr->children->content;
}
}

xmlChar *value = xmlNodeGetContent((const xmlNode *) attr);
if (UNEXPECTED(value == NULL)) {
return BAD_CAST "";
}

*free = true;
return value;
}

bool dom_compare_value(const xmlAttr *attr, const xmlChar *value)
{
bool free;
xmlChar *attr_value = dom_attr_value(attr, &free);
bool result = xmlStrEqual(attr_value, value);
if (free) {
xmlFree(attr_value);
}
return result;
}

#endif
2 changes: 1 addition & 1 deletion ext/dom/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ if test "$PHP_DOM" != "no"; then
documentfragment.c domimplementation.c \
element.c node.c characterdata.c \
documenttype.c entity.c \
nodelist.c text.c comment.c \
nodelist.c html_collection.c text.c comment.c \
entityreference.c \
notation.c xpath.c dom_iterators.c \
namednodemap.c xpath_callbacks.c \
Expand Down
2 changes: 1 addition & 1 deletion ext/dom/config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if (PHP_DOM == "yes") {
domexception.c parentnode.c processinginstruction.c \
cdatasection.c documentfragment.c domimplementation.c element.c \
node.c characterdata.c documenttype.c \
entity.c nodelist.c text.c comment.c \
entity.c nodelist.c html_collection.c text.c comment.c \
entityreference.c \
notation.c xpath.c dom_iterators.c \
namednodemap.c xpath_callbacks.c", null, "-Iext/dom/lexbor");
Expand Down
143 changes: 143 additions & 0 deletions ext/dom/html_collection.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
+----------------------------------------------------------------------+
| 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 "nodelist.h"
#include "html_collection.h"
#include "namespace_compat.h"

typedef struct _dom_named_item {
dom_object *context_intern;
xmlNodePtr node;
} dom_named_item;

/* https://dom.spec.whatwg.org/#dom-htmlcollection-nameditem-key */
static dom_named_item dom_html_collection_named_item(zend_string *key, zend_object *zobj)
{
dom_named_item ret = {NULL, NULL};

/* 1. If key is the empty string, return null. */
if (ZSTR_LEN(key) == 0) {
return ret;
}

dom_object *intern = php_dom_obj_from_obj(zobj);
dom_nnodemap_object *objmap = intern->ptr;

/* 2. Return the first element in the collection for which at least one of the following is true: */
xmlNodePtr basep = dom_object_get_node(objmap->baseobj);
if (basep != NULL) {
int cur = 0;
int next = cur; /* not +1, otherwise we skip the first candidate */
xmlNodePtr candidate = basep->children;
while (candidate != NULL) {
candidate = dom_get_elements_by_tag_name_ns_raw(basep, candidate, objmap->ns, objmap->local, objmap->local_lower, &cur, next);
if (candidate == NULL) {
break;
}

xmlAttrPtr attr;

/* it has an ID which is key; */
if ((attr = xmlHasNsProp(candidate, BAD_CAST "id", NULL)) != NULL && dom_compare_value(attr, BAD_CAST ZSTR_VAL(key))) {
ret.context_intern = objmap->baseobj;
ret.node = candidate;
return ret;
}
/* it is in the HTML namespace and has a name attribute whose value is key; */
else if (php_dom_ns_is_fast(candidate, php_dom_ns_is_html_magic_token)) {
if ((attr = xmlHasNsProp(candidate, BAD_CAST "name", NULL)) != NULL && dom_compare_value(attr, BAD_CAST ZSTR_VAL(key))) {
ret.context_intern = objmap->baseobj;
ret.node = candidate;
return ret;
}
}

next = cur + 1;
}
}

return ret;
}

static void dom_html_collection_named_item_into_zval(zval *return_value, zend_string *key, zend_object *zobj)
{
dom_named_item named_item = dom_html_collection_named_item(key, zobj);
if (named_item.node != NULL) {
DOM_RET_OBJ(named_item.node, named_item.context_intern);
} else {
RETURN_NULL();
}
}

PHP_METHOD(DOM_HTMLCollection, namedItem)
{
zend_string *key;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(key)
ZEND_PARSE_PARAMETERS_END();
dom_html_collection_named_item_into_zval(return_value, key, Z_OBJ_P(ZEND_THIS));
}

zval *dom_html_collection_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
{
if (UNEXPECTED(!offset)) {
zend_throw_error(NULL, "Cannot append to %s", ZSTR_VAL(object->ce->name));
return NULL;
}

dom_nodelist_dimension_index index = dom_modern_nodelist_get_index(offset);
if (UNEXPECTED(index.type == DOM_NODELIST_DIM_ILLEGAL)) {
zend_illegal_container_offset(object->ce->name, offset, type);
return NULL;
}

if (index.type == DOM_NODELIST_DIM_STRING) {
dom_html_collection_named_item_into_zval(rv, index.str, object);
} else {
ZEND_ASSERT(index.type == DOM_NODELIST_DIM_LONG);
php_dom_nodelist_get_item_into_zval(php_dom_obj_from_obj(object)->ptr, index.lval, rv);
}

return rv;
}

int dom_html_collection_has_dimension(zend_object *object, zval *member, int check_empty)
{
/* If it exists, it cannot be empty because nodes aren't empty. */
ZEND_IGNORE_VALUE(check_empty);

dom_nodelist_dimension_index index = dom_modern_nodelist_get_index(member);
if (UNEXPECTED(index.type == DOM_NODELIST_DIM_ILLEGAL)) {
zend_illegal_container_offset(object->ce->name, member, BP_VAR_IS);
return 0;
}

if (index.type == DOM_NODELIST_DIM_STRING) {
return dom_html_collection_named_item(index.str, object).node != NULL;
} else {
ZEND_ASSERT(index.type == DOM_NODELIST_DIM_LONG);
return index.lval >= 0 && index.lval < php_dom_get_nodelist_length(php_dom_obj_from_obj(object));
}
}

#endif
23 changes: 23 additions & 0 deletions ext/dom/html_collection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
+----------------------------------------------------------------------+
| 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]> |
+----------------------------------------------------------------------+
*/

#ifndef PHP_HTML_COLLECTION_H
#define PHP_HTML_COLLECTION_H

zval *dom_html_collection_read_dimension(zend_object *object, zval *offset, int type, zval *rv);
int dom_html_collection_has_dimension(zend_object *object, zval *member, int check_empty);

#endif
60 changes: 60 additions & 0 deletions ext/dom/nodelist.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "php.h"
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include "php_dom.h"
#include "nodelist.h"
#include "zend_interfaces.h"

/*
Expand Down Expand Up @@ -249,4 +250,63 @@ ZEND_METHOD(DOMNodeList, getIterator)
zend_create_internal_iterator_zval(return_value, ZEND_THIS);
}

dom_nodelist_dimension_index dom_modern_nodelist_get_index(const zval *offset)
{
dom_nodelist_dimension_index ret;

ZVAL_DEREF(offset);

if (Z_TYPE_P(offset) == IS_LONG) {
ret.type = DOM_NODELIST_DIM_LONG;
ret.lval = Z_LVAL_P(offset);
} else if (Z_TYPE_P(offset) == IS_DOUBLE) {
ret.type = DOM_NODELIST_DIM_LONG;
ret.lval = zend_dval_to_lval_safe(Z_DVAL_P(offset));
} else if (Z_TYPE_P(offset) == IS_STRING) {
zend_ulong lval;
if (ZEND_HANDLE_NUMERIC(Z_STR_P(offset), lval)) {
ret.type = DOM_NODELIST_DIM_LONG;
ret.lval = (zend_long) lval;
} else {
ret.type = DOM_NODELIST_DIM_STRING;
ret.str = Z_STR_P(offset);
}
} else {
ret.type = DOM_NODELIST_DIM_ILLEGAL;
}

return ret;
}

zval *dom_modern_nodelist_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
{
if (UNEXPECTED(!offset)) {
zend_throw_error(NULL, "Cannot append to %s", ZSTR_VAL(object->ce->name));
return NULL;
}

dom_nodelist_dimension_index index = dom_modern_nodelist_get_index(offset);
if (UNEXPECTED(index.type == DOM_NODELIST_DIM_ILLEGAL || index.type == DOM_NODELIST_DIM_STRING)) {
zend_illegal_container_offset(object->ce->name, offset, type);
return NULL;
}

php_dom_nodelist_get_item_into_zval(php_dom_obj_from_obj(object)->ptr, index.lval, rv);
return rv;
}

int dom_modern_nodelist_has_dimension(zend_object *object, zval *member, int check_empty)
{
/* If it exists, it cannot be empty because nodes aren't empty. */
ZEND_IGNORE_VALUE(check_empty);

dom_nodelist_dimension_index index = dom_modern_nodelist_get_index(member);
if (UNEXPECTED(index.type == DOM_NODELIST_DIM_ILLEGAL || index.type == DOM_NODELIST_DIM_STRING)) {
zend_illegal_container_offset(object->ce->name, member, BP_VAR_IS);
return 0;
}

return index.lval >= 0 && index.lval < php_dom_get_nodelist_length(php_dom_obj_from_obj(object));
}

#endif
40 changes: 40 additions & 0 deletions ext/dom/nodelist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
+----------------------------------------------------------------------+
| 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]> |
+----------------------------------------------------------------------+
*/

#ifndef PHP_NODELIST_H
#define PHP_NODELIST_H

enum dom_nodelist_dimension_index_type {
DOM_NODELIST_DIM_ILLEGAL,
DOM_NODELIST_DIM_STRING,
DOM_NODELIST_DIM_LONG,
};

typedef struct _dom_nodelist_dimension_index {
union {
zend_long lval;
zend_string *str;
};
enum dom_nodelist_dimension_index_type type;
} dom_nodelist_dimension_index;

void php_dom_nodelist_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value);
int php_dom_get_nodelist_length(dom_object *obj);
dom_nodelist_dimension_index dom_modern_nodelist_get_index(const zval *offset);
zval *dom_modern_nodelist_read_dimension(zend_object *object, zval *offset, int type, zval *rv);
int dom_modern_nodelist_has_dimension(zend_object *object, zval *member, int check_empty);

#endif
Loading
Loading