changeset 6:f80e9010e64d

Fix du clonage (removeAttributeNS et pas removeAttribute), refactoring
author Franck Deroche <webmaster@defr.org>
date Sun, 21 Oct 2007 13:21:57 +0200
parents 888b9807b2b3
children 61dee7e34a9d
files class.template.php
diffstat 1 files changed, 31 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/class.template.php	Sun Oct 21 13:20:25 2007 +0200
+++ b/class.template.php	Sun Oct 21 13:21:57 2007 +0200
@@ -5,41 +5,19 @@
     
     const NS = 'http://defr.net/2007/template';
     
-    function __construct($fileName) {
+    function __construct($fileName, $shouldValidate = false) {
         $this->xmlDocument = new DOMDocument();
-        $this->xmlDocument->validateOnParse = true;
+        $this->xmlDocument->validateOnParse = $shouldValidate;
         $this->xmlDocument->loadXML(file_get_contents($fileName));
         $this->xmlXPath = new DOMXPath($this->xmlDocument);
     }
     
     function apply($selector, $obj, DOMElement $root = null) {
-        if(!($root instanceof DOMElement)) {
+        if(!($root instanceof DOMElement))
             $root = $this->xmlDocument->documentElement;
-        }
         $rootSelector = $this->parseSelector($selector, $root);
-        // Cloning test
-        if(is_array($obj)) {
-            foreach($obj as $array) {
-                foreach($rootSelector->nodes as $node) {
-                    $nodeName = key($array);
-                    $tmp = $this->getClonedNode($node, $nodeName);
-                    $futureNode = $tmp->clone;
-                    $node->insertBefore($futureNode, $tmp->orig);
-                    foreach($array as $sel => $test) {
-                        $content = $this->parseReplacement($test);
-                        $localSelector = $this->parseSelector($sel, $node);
-                        if(isset($localSelector->attribute))
-                            $futureNode->setAttribute($localSelector->attribute, $test);
-                        else {
-                            if($futureNode->namespaceURI == Template::NS)
-                                $this->replaceNode($futureNode, $content);
-                            else
-                                $this->setNodeContent($futureNode, $content);
-                        }
-                    }
-                }
-            }
-        } 
+        if(is_array($obj))
+            $this->applyClone($rootSelector, $obj, $root);
         else {
             foreach($rootSelector->nodes as $node) {
                 $content = $this->parseReplacement($obj); 
@@ -54,6 +32,30 @@
             }
         }
     }
+
+    function applyClone($rootSelector, $obj, $root) {
+        foreach($obj as $array) {
+            $nodeName = key($array);
+	        foreach($rootSelector->nodes as $node) {
+                $tmp = $this->getClonedNode($node, $nodeName);
+                $futureNode = $tmp->clone;
+                $futureNode = $node->insertBefore($futureNode, $tmp->orig);
+                foreach($array as $sel => $test) {
+                    $locSelector = $this->parseSelector($sel, $node);
+                    if(isset($localSelector->attribute))
+                        $futureNode->setAttribute($locSelector->attribute, $test);
+                    else {
+                        $content = $this->parseReplacement($test);
+                        $node->insertBefore($content, $tmp->orig);
+                        if($futureNode->namespaceURI == Template::NS)
+                            $this->replaceNode($futureNode, $content);
+                        else
+                            $this->setNodeContent($futureNode, $content);
+                    }
+                }
+	        }
+        }
+    }
     
     function getClonedNode($node, $childNodeName) {
         $candidates = $this->parseSelector($childNodeName, $node)->nodes;
@@ -62,7 +64,7 @@
                 $tmp = array();
                 $tmp['orig'] = $candidate;
                 $cnode = $candidate->cloneNode(true);
-                $cnode->removeAttribute('toClone');
+                $cnode->removeAttributeNS(Template::NS, 'toClone');
                 $tmp['clone'] = $cnode;
                 return (object)$tmp;
             }
@@ -151,4 +153,4 @@
         $this->xmlDocument->saveXML();
         return $this->xmlDocument->saveXML();
     }
-}
\ No newline at end of file
+}