Skip to content

Cleanup and optimize attribute value reading #13897

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
Apr 7, 2024
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
14 changes: 2 additions & 12 deletions ext/dom/attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,9 @@ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-22
*/
zend_result dom_attr_value_read(dom_object *obj, zval *retval)
{
DOM_PROP_NODE(xmlAttrPtr, attrp, obj);
xmlChar *content;

/* Can't avoid a content copy because it's an attribute node */
if ((content = xmlNodeGetContent((xmlNodePtr) attrp)) != NULL) {
ZVAL_STRING(retval, (char *) content);
xmlFree(content);
} else {
ZVAL_EMPTY_STRING(retval);
}

DOM_PROP_NODE(xmlNodePtr, attrp, obj);
php_dom_get_content_into_zval(attrp, retval, false);
return SUCCESS;

}

zend_result dom_attr_value_write(dom_object *obj, zval *newval)
Expand Down
66 changes: 45 additions & 21 deletions ext/dom/php_dom.c
Original file line number Diff line number Diff line change
Expand Up @@ -2255,36 +2255,60 @@ void dom_remove_all_children(xmlNodePtr nodep)
}
}

void php_dom_get_content_into_zval(const xmlNode *nodep, zval *retval, bool default_is_null)
void php_dom_get_content_into_zval(const xmlNode *nodep, zval *return_value, bool null_on_failure)
{
ZEND_ASSERT(nodep != NULL);

if (nodep->type == XML_TEXT_NODE
|| nodep->type == XML_CDATA_SECTION_NODE
|| nodep->type == XML_PI_NODE
|| nodep->type == XML_COMMENT_NODE) {
char *str = (char * ) nodep->content;
if (str != NULL) {
ZVAL_STRING(retval, str);
} else {
goto failure;
switch (nodep->type) {
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
case XML_PI_NODE:
case XML_COMMENT_NODE: {
char *str = (char * ) nodep->content;
if (str != NULL) {
RETURN_STRING(str);
}

break;
}
return;
}

char *str = (char *) xmlNodeGetContent(nodep);
case XML_ATTRIBUTE_NODE: {
/* For attributes we can also 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). */

if (str != NULL) {
ZVAL_STRING(retval, str);
xmlFree(str);
return;
if (nodep->children == NULL) {
RETURN_EMPTY_STRING();
}

if (nodep->children->type == XML_TEXT_NODE && nodep->children->next == NULL) {
if (nodep->children->content == NULL) {
RETURN_EMPTY_STRING();
} else {
RETURN_STRING((const char *) nodep->children->content);
}
}

ZEND_FALLTHROUGH;
}

default: {
char *str = (char *) xmlNodeGetContent(nodep);
if (str != NULL) {
RETVAL_STRING(str);
xmlFree(str);
return;
}

break;
}
}

failure:
if (default_is_null) {
ZVAL_NULL(retval);
if (null_on_failure) {
RETURN_NULL();
} else {
ZVAL_EMPTY_STRING(retval);
RETURN_EMPTY_STRING();
}
}

Expand Down