]> git.sesse.net Git - xml-template/blobdiff - php5/xml-template.php
More cloning fixes. clone.xml doesn't pass, but it passes morally.
[xml-template] / php5 / xml-template.php
index e6f92c21888135ef9038a9d79965a85f7d67a5aa..08c78ab57bcaf4998890628303b5790918c7eb99 100644 (file)
@@ -10,13 +10,13 @@ function XML_Template_process_file($filename, $obj, $clean = 1)
 
 function XML_Template_process($node, $obj, $clean = 1)
 {
-       if (is_a($obj, 'domnode')) {                          # overwrite
+       if (is_a($obj, 'DOMNode')) {                          # overwrite
                for ($i = 0; $i < $node->childNodes->length; ++$i) {
                        $child = $node->childNodes->item($i);
                        $node->removeChild($child);
                }
 
-               if (is_a($obj, 'domdocument')) {
+               if (is_a($obj, 'DOMDocument')) {
                        $obj = $obj->document_element();
                }
 
@@ -39,14 +39,15 @@ function XML_Template_process($node, $obj, $clean = 1)
                        if ($child->nodeType == XML_ELEMENT_NODE) {
                                unset($id);
 
-                               $tag = $child->nodeName;
+                               $tag = $child->localName;
                                $attrs = $child->attributes;
+
                                if (isset($attrs)) {
                                        foreach ($child->attributes as $attr) {
                                                if ($attr->namespaceURI == 'http://template.sesse.net/' && $attr->name == 'id') {
                                                        $id = $attr->value;
                                                        if ($clean) {
-                                                               # $attr->unlinkNode();
+                                                               $child->removeAttributeNode($attr);
                                                        }
                                                }
                                        }
@@ -58,7 +59,7 @@ function XML_Template_process($node, $obj, $clean = 1)
                                        # FIXME: we would want something like \Q and \E here...
                                        if (preg_match('/^' . $tag . '\/(.*)$/', $key, $matches) ||
                                            (isset($id) && preg_match('/^#' . $id . '\/(.*)$/', $key, $matches))) {
-                                               $child->set_attribute($matches[1], $obj[$key]);
+                                               $child->setAttribute($matches[1], $obj[$key]);
                                        }
 
                                        if ($processed) {
@@ -79,10 +80,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);
+               while ($node->childNodes->length > 0) {
+                       $child = $node->childNodes->item(0);
                        $frag->appendChild($child);
-                       $node->removeChild($child);
                }
 
                foreach ($obj as $instance) {
@@ -99,16 +99,16 @@ function XML_Template_process($node, $obj, $clean = 1)
                }
 
                # remove all the <fragment> tags
-
                for ($i = 0; $i < $node->childNodes->length; ++$i) {
                        $child = $node->childNodes->item($i);
-                       if ($child->name != 'temporary-fragment') {
+                       if ($child->localName != 'temporary-fragment') {
                                continue;
                        }
-                       for ($j = 0; $j < $child->childNodes->length; ++$j) {
-                               $child2 = $child->childNodes->item($j);
+                       while ($child->childNodes->length > 0) {
+                               $child2 = $child->childNodes->item(0);
                                $node->appendChild($child2);
                        }
+                       --$i;
                        $node->removeChild($child);
                }       
        }
@@ -129,10 +129,11 @@ function XML_Template_clean($node)
                # after we've done any required replacements
                $doc = $node->ownerDocument;
                $parent = $node->parentNode;
-               for ($i = 0; $i < $node->childNodes->length; ++$i) {
-                       $child = $node->childNodes->item($i);
+
+               while ($node->childNodes->length > 0) {
+                       $child = $node->childNodes->item(0);
                        $node->removeChild($child);
-                       $node->insert_before($child, $node);
+                       $parent->insertBefore($child, $node);
                }
                $parent->removeChild($node);
        }
@@ -153,15 +154,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,11 +171,13 @@ 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->prefix . ":" . $attr->name);
                                }
+                               $attr2->value = $attr->value;
                                $newnode->appendChild($attr2);
                        }
                }
@@ -183,7 +187,7 @@ function own_clone_node($node, $doc)
                }
                return $newnode;
        } else {
-               return $node->clone_node(true);
+               return $node->cloneNode(true);
        }
 }