# HG changeset patch # User Franck Deroche # Date 1192965717 -7200 # Node ID f80e9010e64d7da028b6ef1a68836ca72b687fda # Parent 888b9807b2b35d1a8e53dfd1504f5ab02c351dc4 Fix du clonage (removeAttributeNS et pas removeAttribute), refactoring diff -r 888b9807b2b3 -r f80e9010e64d class.template.php --- 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 +}