Skip to content

Commit 539d8d9

Browse files
committed
Use common helper macro for getting the node in property handlers
1 parent 649394d commit 539d8d9

13 files changed

+123
-458
lines changed

ext/dom/attr.c

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
2525

2626
#include "php_dom.h"
27+
#include "dom_properties.h"
2728

2829
/*
2930
* class DOMAttr extends DOMNode
@@ -77,12 +78,7 @@ Modern spec URL: https://dom.spec.whatwg.org/#dom-attr-name
7778
*/
7879
zend_result dom_attr_name_read(dom_object *obj, zval *retval)
7980
{
80-
xmlAttrPtr attrp = (xmlAttrPtr) dom_object_get_node(obj);
81-
82-
if (attrp == NULL) {
83-
php_dom_throw_error(INVALID_STATE_ERR, true);
84-
return FAILURE;
85-
}
81+
DOM_PROP_NODE(xmlAttrPtr, attrp, obj);
8682

8783
if (php_dom_follow_spec_intern(obj)) {
8884
zend_string *str = dom_node_get_node_name_attribute_or_element((xmlNodePtr) attrp, false);
@@ -117,14 +113,9 @@ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-22
117113
*/
118114
zend_result dom_attr_value_read(dom_object *obj, zval *retval)
119115
{
120-
xmlAttrPtr attrp = (xmlAttrPtr) dom_object_get_node(obj);
116+
DOM_PROP_NODE(xmlAttrPtr, attrp, obj);
121117
xmlChar *content;
122118

123-
if (attrp == NULL) {
124-
php_dom_throw_error(INVALID_STATE_ERR, true);
125-
return FAILURE;
126-
}
127-
128119
/* Can't avoid a content copy because it's an attribute node */
129120
if ((content = xmlNodeGetContent((xmlNodePtr) attrp)) != NULL) {
130121
ZVAL_STRING(retval, (char *) content);
@@ -139,12 +130,7 @@ zend_result dom_attr_value_read(dom_object *obj, zval *retval)
139130

140131
zend_result dom_attr_value_write(dom_object *obj, zval *newval)
141132
{
142-
xmlAttrPtr attrp = (xmlAttrPtr) dom_object_get_node(obj);
143-
144-
if (attrp == NULL) {
145-
php_dom_throw_error(INVALID_STATE_ERR, true);
146-
return FAILURE;
147-
}
133+
DOM_PROP_NODE(xmlAttrPtr, attrp, obj);
148134

149135
/* Typed property, this is already a string */
150136
ZEND_ASSERT(Z_TYPE_P(newval) == IS_STRING);
@@ -171,24 +157,16 @@ Since: DOM Level 2
171157
*/
172158
zend_result dom_attr_owner_element_read(dom_object *obj, zval *retval)
173159
{
174-
xmlNodePtr nodep, nodeparent;
160+
DOM_PROP_NODE(xmlNodePtr, nodep, obj);
175161

176-
nodep = dom_object_get_node(obj);
177-
178-
if (nodep == NULL) {
179-
php_dom_throw_error(INVALID_STATE_ERR, true);
180-
return FAILURE;
181-
}
182-
183-
nodeparent = nodep->parent;
162+
xmlNodePtr nodeparent = nodep->parent;
184163
if (!nodeparent) {
185164
ZVAL_NULL(retval);
186165
return SUCCESS;
187166
}
188167

189168
php_dom_create_object(nodeparent, retval, obj);
190169
return SUCCESS;
191-
192170
}
193171

194172
/* }}} */

ext/dom/characterdata.c

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "php.h"
2323
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
2424
#include "php_dom.h"
25+
#include "dom_properties.h"
2526

2627
/*
2728
* class DOMCharacterData extends DOMNode
@@ -55,26 +56,14 @@ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-
5556
*/
5657
zend_result dom_characterdata_data_read(dom_object *obj, zval *retval)
5758
{
58-
xmlNodePtr nodep = dom_object_get_node(obj);
59-
60-
if (nodep == NULL) {
61-
php_dom_throw_error(INVALID_STATE_ERR, true);
62-
return FAILURE;
63-
}
64-
59+
DOM_PROP_NODE(xmlNodePtr, nodep, obj);
6560
php_dom_get_content_into_zval(nodep, retval, false);
66-
6761
return SUCCESS;
6862
}
6963

7064
zend_result dom_characterdata_data_write(dom_object *obj, zval *newval)
7165
{
72-
xmlNode *nodep = dom_object_get_node(obj);
73-
74-
if (nodep == NULL) {
75-
php_dom_throw_error(INVALID_STATE_ERR, true);
76-
return FAILURE;
77-
}
66+
DOM_PROP_NODE(xmlNodePtr, nodep, obj);
7867

7968
/* Typed property, this is already a string */
8069
ZEND_ASSERT(Z_TYPE_P(newval) == IS_STRING);
@@ -94,14 +83,9 @@ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-
9483
*/
9584
zend_result dom_characterdata_length_read(dom_object *obj, zval *retval)
9685
{
97-
xmlNodePtr nodep = dom_object_get_node(obj);
98-
long length = 0;
99-
100-
if (nodep == NULL) {
101-
php_dom_throw_error(INVALID_STATE_ERR, true);
102-
return FAILURE;
103-
}
86+
DOM_PROP_NODE(xmlNodePtr, nodep, obj);
10487

88+
long length = 0;
10589
if (nodep->content) {
10690
length = xmlUTF8Strlen(nodep->content);
10791
}

ext/dom/document.c

Lines changed: 21 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "namespace_compat.h"
2626
#include "xml_serializer.h"
2727
#include "internal_helpers.h"
28+
#include "dom_properties.h"
2829
#include <libxml/SAX.h>
2930
#ifdef LIBXML_SCHEMAS_ENABLED
3031
#include <libxml/relaxng.h>
@@ -45,15 +46,9 @@ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-
4546
*/
4647
zend_result dom_document_doctype_read(dom_object *obj, zval *retval)
4748
{
48-
xmlDoc *docp = (xmlDocPtr) dom_object_get_node(obj);
49-
xmlDtdPtr dtdptr;
49+
DOM_PROP_NODE(xmlDocPtr, docp, obj);
5050

51-
if (docp == NULL) {
52-
php_dom_throw_error(INVALID_STATE_ERR, true);
53-
return FAILURE;
54-
}
55-
56-
dtdptr = xmlGetIntSubset(docp);
51+
xmlDtdPtr dtdptr = xmlGetIntSubset(docp);
5752
if (!dtdptr) {
5853
ZVAL_NULL(retval);
5954
return SUCCESS;
@@ -84,15 +79,9 @@ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-
8479
*/
8580
zend_result dom_document_document_element_read(dom_object *obj, zval *retval)
8681
{
87-
xmlDoc *docp = (xmlDocPtr) dom_object_get_node(obj);
88-
xmlNode *root;
82+
DOM_PROP_NODE(xmlDocPtr, docp, obj);
8983

90-
if (docp == NULL) {
91-
php_dom_throw_error(INVALID_STATE_ERR, true);
92-
return FAILURE;
93-
}
94-
95-
root = xmlDocGetRootElement(docp);
84+
xmlNodePtr root = xmlDocGetRootElement(docp);
9685
if (!root) {
9786
ZVAL_NULL(retval);
9887
return SUCCESS;
@@ -110,15 +99,9 @@ Since: DOM Level 3
11099
*/
111100
zend_result dom_document_encoding_read(dom_object *obj, zval *retval)
112101
{
113-
xmlDoc *docp = (xmlDocPtr) dom_object_get_node(obj);
114-
char *encoding;
115-
116-
if (docp == NULL) {
117-
php_dom_throw_error(INVALID_STATE_ERR, true);
118-
return FAILURE;
119-
}
102+
DOM_PROP_NODE(xmlDocPtr, docp, obj);
120103

121-
encoding = (char *) docp->encoding;
104+
const char *encoding = (const char *) docp->encoding;
122105

123106
if (encoding != NULL) {
124107
ZVAL_STRING(retval, encoding);
@@ -131,13 +114,7 @@ zend_result dom_document_encoding_read(dom_object *obj, zval *retval)
131114

132115
zend_result dom_document_encoding_write(dom_object *obj, zval *newval)
133116
{
134-
xmlDoc *docp = (xmlDocPtr) dom_object_get_node(obj);
135-
xmlCharEncodingHandlerPtr handler;
136-
137-
if (docp == NULL) {
138-
php_dom_throw_error(INVALID_STATE_ERR, true);
139-
return FAILURE;
140-
}
117+
DOM_PROP_NODE(xmlDocPtr, docp, obj);
141118

142119
/* Typed property, can only be IS_STRING or IS_NULL. */
143120
ZEND_ASSERT(Z_TYPE_P(newval) == IS_STRING || Z_TYPE_P(newval) == IS_NULL);
@@ -146,9 +123,9 @@ zend_result dom_document_encoding_write(dom_object *obj, zval *newval)
146123
goto invalid_encoding;
147124
}
148125

149-
zend_string *str = Z_STR_P(newval);
126+
const zend_string *str = Z_STR_P(newval);
150127

151-
handler = xmlFindCharEncodingHandler(ZSTR_VAL(str));
128+
xmlCharEncodingHandlerPtr handler = xmlFindCharEncodingHandler(ZSTR_VAL(str));
152129

153130
if (handler != NULL) {
154131
xmlCharEncCloseFunc(handler);
@@ -176,30 +153,16 @@ Since: DOM Level 3
176153
*/
177154
zend_result dom_document_standalone_read(dom_object *obj, zval *retval)
178155
{
179-
xmlDoc *docp;
180-
181-
docp = (xmlDocPtr) dom_object_get_node(obj);
182-
183-
if (docp == NULL) {
184-
php_dom_throw_error(INVALID_STATE_ERR, true);
185-
return FAILURE;
186-
}
187-
156+
DOM_PROP_NODE(xmlDocPtr, docp, obj);
188157
ZVAL_BOOL(retval, docp->standalone > 0);
189158
return SUCCESS;
190159
}
191160

192161
zend_result dom_document_standalone_write(dom_object *obj, zval *newval)
193162
{
194-
xmlDoc *docp = (xmlDocPtr) dom_object_get_node(obj);
195-
zend_long standalone;
196-
197-
if (docp == NULL) {
198-
php_dom_throw_error(INVALID_STATE_ERR, true);
199-
return FAILURE;
200-
}
163+
DOM_PROP_NODE(xmlDocPtr, docp, obj);
201164

202-
standalone = zval_get_long(newval);
165+
zend_long standalone = zval_get_long(newval);
203166
docp->standalone = ZEND_NORMALIZE_BOOL(standalone);
204167

205168
return SUCCESS;
@@ -214,15 +177,9 @@ Since: DOM Level 3
214177
*/
215178
zend_result dom_document_version_read(dom_object *obj, zval *retval)
216179
{
217-
xmlDoc *docp = (xmlDocPtr) dom_object_get_node(obj);
218-
char *version;
180+
DOM_PROP_NODE(xmlDocPtr, docp, obj);
219181

220-
if (docp == NULL) {
221-
php_dom_throw_error(INVALID_STATE_ERR, true);
222-
return FAILURE;
223-
}
224-
225-
version = (char *) docp->version;
182+
const char *version = (const char *) docp->version;
226183

227184
if (version != NULL) {
228185
ZVAL_STRING(retval, version);
@@ -235,15 +192,9 @@ zend_result dom_document_version_read(dom_object *obj, zval *retval)
235192

236193
zend_result dom_document_version_write(dom_object *obj, zval *newval)
237194
{
238-
xmlDoc *docp = (xmlDocPtr) dom_object_get_node(obj);
239-
zend_string *str;
240-
241-
if (docp == NULL) {
242-
php_dom_throw_error(INVALID_STATE_ERR, true);
243-
return FAILURE;
244-
}
195+
DOM_PROP_NODE(xmlDocPtr, docp, obj);
245196

246-
str = zval_try_get_string(newval);
197+
zend_string *str = zval_try_get_string(newval);
247198
if (UNEXPECTED(!str)) {
248199
return FAILURE;
249200
}
@@ -425,15 +376,9 @@ Since: DOM Level 3
425376
*/
426377
zend_result dom_document_document_uri_read(dom_object *obj, zval *retval)
427378
{
428-
xmlDoc *docp = (xmlDocPtr) dom_object_get_node(obj);
429-
char *url;
379+
DOM_PROP_NODE(xmlDocPtr, docp, obj);
430380

431-
if (docp == NULL) {
432-
php_dom_throw_error(INVALID_STATE_ERR, true);
433-
return FAILURE;
434-
}
435-
436-
url = (char *) docp->URL;
381+
const char *url = (const char *) docp->URL;
437382
if (url != NULL) {
438383
ZVAL_STRING(retval, url);
439384
} else {
@@ -449,15 +394,9 @@ zend_result dom_document_document_uri_read(dom_object *obj, zval *retval)
449394

450395
zend_result dom_document_document_uri_write(dom_object *obj, zval *newval)
451396
{
452-
xmlDoc *docp = (xmlDocPtr) dom_object_get_node(obj);
453-
zend_string *str;
454-
455-
if (docp == NULL) {
456-
php_dom_throw_error(INVALID_STATE_ERR, true);
457-
return FAILURE;
458-
}
397+
DOM_PROP_NODE(xmlDocPtr, docp, obj);
459398

460-
str = zval_try_get_string(newval);
399+
zend_string *str = zval_try_get_string(newval);
461400
if (UNEXPECTED(!str)) {
462401
return FAILURE;
463402
}

0 commit comments

Comments
 (0)