Skip to content

Commit c502c58

Browse files
committed
Split off some methods so they can be reused in different places
1 parent ea794e9 commit c502c58

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

ext/dom/element.c

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,15 @@ PHP_METHOD(DOMElement, getAttribute)
237237
}
238238
/* }}} end dom_element_get_attribute */
239239

240+
static xmlNodePtr dom_create_attribute(xmlNodePtr nodep, const char *name, const char* value)
241+
{
242+
if (xmlStrEqual((xmlChar *)name, (xmlChar *)"xmlns")) {
243+
return (xmlNodePtr) xmlNewNs(nodep, (xmlChar *)value, NULL);
244+
} else {
245+
return (xmlNodePtr) xmlSetProp(nodep, (xmlChar *) name, (xmlChar *)value);
246+
}
247+
}
248+
240249
/* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-F68F082
241250
Since:
242251
*/
@@ -287,23 +296,40 @@ PHP_METHOD(DOMElement, setAttribute)
287296

288297
}
289298

290-
if (xmlStrEqual((xmlChar *)name, (xmlChar *)"xmlns")) {
291-
if (xmlNewNs(nodep, (xmlChar *)value, NULL)) {
292-
RETURN_TRUE;
293-
}
294-
} else {
295-
attr = (xmlNodePtr)xmlSetProp(nodep, (xmlChar *) name, (xmlChar *)value);
296-
}
299+
attr = dom_create_attribute(nodep, name, value);
297300
if (!attr) {
298301
zend_argument_value_error(1, "must be a valid XML attribute");
299302
RETURN_THROWS();
300303
}
304+
if (attr->type == XML_NAMESPACE_DECL) {
305+
RETURN_TRUE;
306+
}
301307

302308
DOM_RET_OBJ(attr, &ret, intern);
303309

304310
}
305311
/* }}} end dom_element_set_attribute */
306312

313+
static bool dom_remove_attribute(xmlNodePtr attrp)
314+
{
315+
ZEND_ASSERT(attrp != NULL);
316+
switch (attrp->type) {
317+
case XML_ATTRIBUTE_NODE:
318+
if (php_dom_object_get_data(attrp) == NULL) {
319+
node_list_unlink(attrp->children);
320+
xmlUnlinkNode(attrp);
321+
xmlFreeProp((xmlAttrPtr)attrp);
322+
} else {
323+
xmlUnlinkNode(attrp);
324+
}
325+
break;
326+
case XML_NAMESPACE_DECL:
327+
return false;
328+
EMPTY_SWITCH_DEFAULT_CASE();
329+
}
330+
return true;
331+
}
332+
307333
/* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-6D6AC0F9
308334
Since:
309335
*/
@@ -332,23 +358,7 @@ PHP_METHOD(DOMElement, removeAttribute)
332358
RETURN_FALSE;
333359
}
334360

335-
switch (attrp->type) {
336-
case XML_ATTRIBUTE_NODE:
337-
if (php_dom_object_get_data(attrp) == NULL) {
338-
node_list_unlink(attrp->children);
339-
xmlUnlinkNode(attrp);
340-
xmlFreeProp((xmlAttrPtr)attrp);
341-
} else {
342-
xmlUnlinkNode(attrp);
343-
}
344-
break;
345-
case XML_NAMESPACE_DECL:
346-
RETURN_FALSE;
347-
default:
348-
break;
349-
}
350-
351-
RETURN_TRUE;
361+
RETURN_BOOL(dom_remove_attribute(attrp));
352362
}
353363
/* }}} end dom_element_remove_attribute */
354364

0 commit comments

Comments
 (0)