[php-src] master: Factor out reading an attribute value
| From: | Niels Dossche | Date: | Sun, 14 Apr 2024 12:48:36 +0000 |
| Subject: | [php-src] master: Factor out reading an attribute value | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-128092@lists.php.net to get a copy of this message | ||
Author: Niels Dossche (nielsdos)
Date: 2024-04-14T14:45:45+02:00
Commit: https://github.com/php/php-src/commit/5c69b2e86ffab85a09db475a1587c57d3579a3b6
Raw diff: https://github.com/php/php-src/commit/5c69b2e86ffab85a09db475a1587c57d3579a3b6.diff
Factor out reading an attribute value
Changed paths:
M ext/dom/attr.c
M ext/dom/php_dom.c
M ext/dom/php_dom.h
Diff:
diff --git a/ext/dom/attr.c b/ext/dom/attr.c
index e48d124976c2..746ab4efa360 100644
--- a/ext/dom/attr.c
+++ b/ext/dom/attr.c
@@ -199,4 +199,34 @@ 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;
+}
+
#endif
diff --git a/ext/dom/php_dom.c b/ext/dom/php_dom.c
index dce9a0fb490c..d7cb423cd78c 100644
--- a/ext/dom/php_dom.c
+++ b/ext/dom/php_dom.c
@@ -2273,24 +2273,13 @@ void php_dom_get_content_into_zval(const xmlNode *nodep, zval *return_value,
boo
}
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 (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);
- }
+ bool free;
+ xmlChar *value = dom_attr_value((const xmlAttr *) nodep, &free);
+ RETURN_STRING_FAST((const char *) value);
+ if (free) {
+ xmlFree(value);
}
-
- ZEND_FALLTHROUGH;
+ return;
}
default: {
diff --git a/ext/dom/php_dom.h b/ext/dom/php_dom.h
index 26035832d957..0cd62a1afced 100644
--- a/ext/dom/php_dom.h
+++ b/ext/dom/php_dom.h
@@ -174,6 +174,8 @@ void dom_document_convert_to_modern(php_libxml_ref_obj *document, xmlDocPtr lxml
dom_object *php_dom_instantiate_object_helper(zval *return_value, zend_class_entry *ce, xmlNodePtr
obj, dom_object *parent);
xmlDocPtr php_dom_create_html_doc(void);
+xmlChar *dom_attr_value(const xmlAttr *attr, bool *free);
+
typedef enum {
DOM_LOAD_STRING = 0,
DOM_LOAD_FILE = 1,