]> git.sesse.net Git - xml-template/blobdiff - php5/xml-template.php
Some fixes for php5 cloning. Still doesn't work properly.
[xml-template] / php5 / xml-template.php
index f98e67462357ddc30c07dd61471f2ca1d6a9e9fb..af055917757f63f7902294636b615c15231ab5fe 100644 (file)
@@ -21,7 +21,7 @@ function XML_Template_process($node, $obj, $clean = 1)
                }
 
                $newobj = own_clone_node($obj, $node->ownerDocument);
-               $node->append_child($newobj);
+               $node->appendChild($newobj);
 
                XML_Template_process($newobj, array(), $clean);
        } else if (!is_array($obj)) {                         # overwrite
@@ -46,7 +46,7 @@ function XML_Template_process($node, $obj, $clean = 1)
                                                if ($attr->namespaceURI == 'http://template.sesse.net/' && $attr->name == 'id') {
                                                        $id = $attr->value;
                                                        if ($clean) {
-                                                               # $attr->unlinkNode();
+                                                               $child->removeAttributeNode($attr);
                                                        }
                                                }
                                        }
@@ -79,10 +79,9 @@ function XML_Template_process($node, $obj, $clean = 1)
                $doc = $node->ownerDocument;
                $frag = $doc->createElement("temporary-fragment");    # ugh
 
-               for ($i = 0; $i < $node->childNodes->length; ++$i) {
-                       $child = $node->childNodes->item($i);
-                       $frag->append_child($child);
-                       $node->removeChild($child);
+               while ($node->childNodes->length > 0) {
+                       $child = $node->childNodes->item(0);
+                       $frag->appendChild($child);
                }
 
                foreach ($obj as $instance) {
@@ -91,7 +90,7 @@ function XML_Template_process($node, $obj, $clean = 1)
                        }
 
                        $newnode = own_clone_node($frag, $frag->ownerDocument);
-                       $node->append_child($newnode);
+                       $node->appendChild($newnode);
                        XML_Template_process($newnode, $instance, $clean);
                        if ($clean) {
                                XML_Template_clean($newnode);
@@ -107,7 +106,7 @@ function XML_Template_process($node, $obj, $clean = 1)
                        }
                        for ($j = 0; $j < $child->childNodes->length; ++$j) {
                                $child2 = $child->childNodes->item($j);
-                               $node->append_child($child2);
+                               $node->appendChild($child2);
                        }
                        $node->removeChild($child);
                }       
@@ -128,7 +127,7 @@ function XML_Template_clean($node)
                # as this is a dummy node, we want to remove it and move everything further up
                # after we've done any required replacements
                $doc = $node->ownerDocument;
-               $parent = $node->parent_node();
+               $parent = $node->parentNode;
                for ($i = 0; $i < $node->childNodes->length; ++$i) {
                        $child = $node->childNodes->item($i);
                        $node->removeChild($child);
@@ -153,15 +152,16 @@ function XML_Template_alternate($tag, $array, $elems)
 }
                
 # Ideally, this would be "return $obj->clone_node(true)". But surprise, surprise,
-# PHP is buggy (at least PHP4); it removes the prefix information from all attributes
-# during a clone. IOW, we'll have to clone evverything ourselves.
+# PHP is buggy (at least both PHP4 and PHP5); it removes the prefix information
+# from all attributes during a clone. IOW, we'll have to clone evverything
+# ourselves.
 function own_clone_node($node, $doc)
 {
        // we only need these two
        if ($node->nodeType == XML_ELEMENT_NODE) {
                $nsuri = $node->namespaceURI;
                if (isset($nsuri)) {
-                       $newnode = $doc->createElementNS($node->namespaceURI, $node->nodeName, $node->prefix());
+                       $newnode = $doc->createElementNS($node->namespaceURI, $node->nodeName, $node->prefix);
                } else {
                        $newnode = $doc->createElement($node->nodeName);
                }
@@ -169,21 +169,24 @@ function own_clone_node($node, $doc)
                $attrs = $node->attributes;
                if (isset($attrs)) {
                        foreach ($node->attributes as $attr) {
-                               $attr2 = $doc->createAttribute($attr->name, $attr->value);
                                $nsuri = $attr->namespaceURI;
                                if (isset($nsuri)) {
-                                       $attr2->set_namespace($nsuri, $attr->prefix);
+                                       $attr2 = $doc->createAttribute($attr->name);
+                               } else {
+                                       $attr2 = $doc->createAttributeNS($nsuri, $attr->name);
+                                       $attr2->prefix = $attr->prefix;
                                }
-                               $newnode->append_child($attr2);
+                               $attr2->value = $attr->value;
+                               $newnode->appendChild($attr2);
                        }
                }
                for ($i = 0; $i < $node->childNodes->length; ++$i) {
                        $child = $node->childNodes->item($i);
-                       $newnode->append_child(own_clone_node($child, $doc));
+                       $newnode->appendChild(own_clone_node($child, $doc));
                }
                return $newnode;
        } else {
-               return $node->clone_node(true);
+               return $node->cloneNode(true);
        }
 }