changeset 3:a76a46cbb401

Fix lie a quelques problemes de selections. + Utilisation de parseSelector dans le mode de clonage. + parseReplacement clone maintenant ce qu'on lui passe pour pouvoir l'utiliser plusieurs fois + parseSelector n'affecte plus deux fois $obj['nodes']
author Franck Deroche <webmaster@defr.net>
date Fri, 16 Mar 2007 16:12:46 +0100
parents ad12469f4db4
children 3903da0c2bac
files class.template.php
diffstat 1 files changed, 14 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/class.template.php	Fri Mar 16 16:01:10 2007 +0100
+++ b/class.template.php	Fri Mar 16 16:12:46 2007 +0100
@@ -12,8 +12,11 @@
         $this->xmlXPath = new DOMXPath($this->xmlDocument);
     }
     
-    function apply($selector, $obj) {
-        $rootSelector = $this->parseSelector($selector);
+    function apply($selector, $obj, DOMElement $root = null) {
+        if(!($root instanceof DOMElement)) {
+            $root = $this->xmlDocument->documentElement;
+        }
+        $rootSelector = $this->parseSelector($selector, $root);
         // Cloning test
         if(is_array($obj)) {
             foreach($obj as $array) {
@@ -24,7 +27,7 @@
                     $node->insertBefore($futureNode, $tmp->orig);
                     foreach($array as $sel => $test) {
                         $content = $this->parseReplacement($test);
-                        $localSelector = $this->parseSelector($sel);
+                        $localSelector = $this->parseSelector($sel, $node);
                         if(isset($localSelector->attribute))
                             $futureNode->setAttribute($localSelector->attribute, $test);
                         else {
@@ -36,10 +39,10 @@
                     }
                 }
             }
-        } else
-        {
-            $content = $this->parseReplacement($obj);
+        } 
+        else {
             foreach($rootSelector->nodes as $node) {
+                $content = $this->parseReplacement($obj); 
                 if(isset($rootSelector->attribute)) {
                     $node->setAttribute($rootSelector->attribute, $obj);
                 } else {
@@ -53,9 +56,9 @@
     }
     
     function getClonedNode($node, $childNodeName) {
-        $candidates = $node->getElementsByTagName($childNodeName);
+        $candidates = $this->parseSelector($childNodeName, $node)->nodes;
         foreach($candidates as $candidate) {
-            if($candidate->nodeName == $childNodeName && $candidate->getAttributeNS(Template::NS, 'toClone') == 'true') {
+            if($candidate->getAttributeNS(Template::NS, 'toClone') == 'true') {
                 $tmp = array();
                 $tmp['orig'] = $candidate;
                 $cnode = $candidate->cloneNode(true);
@@ -66,10 +69,7 @@
         }
     }
     
-    function parseSelector($selector, DOMElement $root=NULL) {
-        if(!($root instanceof DOMElement)) {
-            $root = $this->xmlDocument->documentElement;
-        }
+    function parseSelector($selector, DOMElement $root) {
         $obj = array();
         $pos = strpos($selector, '@');
         if($pos !== false) {
@@ -83,7 +83,6 @@
         else {
             $obj['nodeName'] = $selector;
             $obj['nodes'] = $root->getElementsByTagName($selector);
-            $obj['nodes'] = $this->xmlDocument->getElementsByTagName($selector);
         }
         return (object)$obj;
     }
@@ -111,9 +110,9 @@
         if(is_string($obj))
             $retVal = $this->xmlDocument->createTextNode($obj);
         else if($obj instanceof DOMDocument)
-            $retVal = $obj->documentElement;
+            $retVal = $obj->documentElement->clone(true);
         else if($obj instanceof DOMNode)
-            $retVal = $obj;
+            $retVal = $obj->clone(true);
         return $retVal;
     }