]> granicus.if.org Git - php/commitdiff
Fix #23326: Attributes via append_child not supported
authorMelvyn Sopacua <msopacua@php.net>
Tue, 9 Sep 2003 19:43:45 +0000 (19:43 +0000)
committerMelvyn Sopacua <msopacua@php.net>
Tue, 9 Sep 2003 19:43:45 +0000 (19:43 +0000)
Add testcase

ext/domxml/php_domxml.c
ext/domxml/tests/bug23326.phpt [new file with mode: 0644]

index f6666c0e57523b367b0af47eff08e719a8af88db..e222d462e54a05abe64760cd5475d87cfbc8d43b 100644 (file)
@@ -2425,11 +2425,6 @@ PHP_FUNCTION(domxml_node_append_child)
        
        DOMXML_GET_OBJ(child, node, le_domxmlnodep);
 
-       if (child->type == XML_ATTRIBUTE_NODE) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't append attribute node");
-               RETURN_FALSE;
-       }
-
        /* XXX:ls */
        if (child == parent) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't append node to itself");
@@ -2469,6 +2464,28 @@ PHP_FUNCTION(domxml_node_append_child)
                }
        }
        /* end libxml2 code */
+       else if (child->type == XML_ATTRIBUTE_NODE) {
+               if (parent->properties != NULL) {
+                       /* Check if an attribute with the same name exists */
+                       xmlAttrPtr foundattrp;
+                       if (child->ns == NULL)
+                               foundattrp = xmlHasProp(parent, child->name);
+                       else
+                               foundattrp = xmlHasNsProp(parent, child->name, child->ns->href);
+                       if ((foundattrp != NULL) && (foundattrp != (xmlAttrPtr) child)) {
+                               xmlUnlinkNode((xmlNodePtr) foundattrp);
+                               (void)xmlCopyProp(parent, (xmlAttrPtr) child);
+                               /* We're in the dark here, what happened to the parent, let's
+                                * assume it's handled properly and return the new(?) parent
+                                */
+                               new_child = parent;
+                       }
+               }
+               /* For all other intents and purposes fall through to the xmlAddChild
+                * call
+                */
+       }
+
        
        if (NULL == new_child) {
                new_child = xmlAddChild(parent, child);
diff --git a/ext/domxml/tests/bug23326.phpt b/ext/domxml/tests/bug23326.phpt
new file mode 100644 (file)
index 0000000..03abe3a
--- /dev/null
@@ -0,0 +1,21 @@
+--TEST--
+Attributes via append_child not supported (bug #23326)
+--SKIPIF--
+<?php require_once('skipif.inc'); ?>
+--FILE--
+<?php
+$document = domxml_new_doc('1.0');
+$test = $document->create_element('test');
+$j = 0;
+for($i=0;$i<10;$i++)
+{
+       if( $j < 5 )
+               $j = $i;
+       $test->append_child($document->create_attribute("key$j", $i));
+}
+$document->append_child($test);
+echo $document->dump_mem(1);
+?>
+--EXPECT--
+<?xml version="1.0"?>
+<test key0="0" key1="1" key2="2" key3="3" key4="4" key5="9"/>