]> git.sesse.net Git - xml-template/blobdiff - php5/xml-template.php
Fix the oddest PHP5 bug in the world...
[xml-template] / php5 / xml-template.php
index 43b8b577c264513db1b70f5825fdc3b0f5d90338..ed1e8f85529fc389120184c8023dee97a516518a 100644 (file)
@@ -11,7 +11,7 @@ function XML_Template_process_file($filename, $obj, $clean = 1)
 function XML_Template_process($node, $obj, $clean = 1)
 {
        if (is_a($obj, 'DOMNode')) {                          # overwrite
-               while ($node->childeNodes->length > 0) {
+               while ($node->childNodes->length > 0) {
                        $child = $node->childNodes->item(0);
                        $node->removeChild($child);
                }
@@ -25,14 +25,14 @@ function XML_Template_process($node, $obj, $clean = 1)
                XML_Template_process($frag, array(), $clean);
                $node->appendChild($frag);
        } else if (!is_array($obj)) {                         # overwrite
-               for ($i = 0; $i < $node->childNodes->length; ++$i) {
-                       $child = $node->childNodes->item($i);
-                       $node->removeChild($child);
+               while ($node->childNodes->length > 0) {
+                       $node->removeChild($node->firstChild);
                }
                $doc = $node->ownerDocument;
                $node->appendChild($doc->createTextNode($obj));
        } else if (is_associative_array($obj)) {              # substitute
-               for ($i = 0; $i < $node->childNodes->length; ++$i) {
+               $num_children = ($node->childNodes == null) ? 0 : $node->childNodes->length;
+               for ($i = 0; $i < $num_children; ++$i) {
                        $child = $node->childNodes->item($i);
                        $processed = false;
 
@@ -154,10 +154,10 @@ function XML_Template_alternate($tag, $array, $elems)
        return $array;
 }
                
-# Ideally, this would be "return $obj->clone_node(true)". But surprise, surprise,
-# 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.
+# Ideally, this would be "return $obj->clone_node(true)". But surprise,
+# surprise, PHP is buggy; it does not preserve the prefix information on
+# attributes properly during a clone. (PHP4 and PHP5 are broken in different
+# ways here...) IOW, we'll have to clone everything ourselves.
 function own_clone_node($node, $doc)
 {
        // we only need these two
@@ -202,7 +202,9 @@ function own_clone_element($node, $doc)
                        } else {
                                $attr2 = $doc->createAttribute($attr->localName);
                        }
-                       $attr2->value = $attr->value;
+
+                       # You've got to be kidding me...
+                       $attr2->value = htmlentities($attr->value);
                        $newnode->appendChild($attr2);
                }
        }