Mercurial > defr > templates
comparison class.template.php @ 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 | f80e9010e64d |
comparison
equal
deleted
inserted
replaced
2:ad12469f4db4 | 3:a76a46cbb401 |
---|---|
10 $this->xmlDocument->validateOnParse = true; | 10 $this->xmlDocument->validateOnParse = true; |
11 $this->xmlDocument->loadXML(file_get_contents($fileName)); | 11 $this->xmlDocument->loadXML(file_get_contents($fileName)); |
12 $this->xmlXPath = new DOMXPath($this->xmlDocument); | 12 $this->xmlXPath = new DOMXPath($this->xmlDocument); |
13 } | 13 } |
14 | 14 |
15 function apply($selector, $obj) { | 15 function apply($selector, $obj, DOMElement $root = null) { |
16 $rootSelector = $this->parseSelector($selector); | 16 if(!($root instanceof DOMElement)) { |
17 $root = $this->xmlDocument->documentElement; | |
18 } | |
19 $rootSelector = $this->parseSelector($selector, $root); | |
17 // Cloning test | 20 // Cloning test |
18 if(is_array($obj)) { | 21 if(is_array($obj)) { |
19 foreach($obj as $array) { | 22 foreach($obj as $array) { |
20 foreach($rootSelector->nodes as $node) { | 23 foreach($rootSelector->nodes as $node) { |
21 $nodeName = key($array); | 24 $nodeName = key($array); |
22 $tmp = $this->getClonedNode($node, $nodeName); | 25 $tmp = $this->getClonedNode($node, $nodeName); |
23 $futureNode = $tmp->clone; | 26 $futureNode = $tmp->clone; |
24 $node->insertBefore($futureNode, $tmp->orig); | 27 $node->insertBefore($futureNode, $tmp->orig); |
25 foreach($array as $sel => $test) { | 28 foreach($array as $sel => $test) { |
26 $content = $this->parseReplacement($test); | 29 $content = $this->parseReplacement($test); |
27 $localSelector = $this->parseSelector($sel); | 30 $localSelector = $this->parseSelector($sel, $node); |
28 if(isset($localSelector->attribute)) | 31 if(isset($localSelector->attribute)) |
29 $futureNode->setAttribute($localSelector->attribute, $test); | 32 $futureNode->setAttribute($localSelector->attribute, $test); |
30 else { | 33 else { |
31 if($futureNode->namespaceURI == Template::NS) | 34 if($futureNode->namespaceURI == Template::NS) |
32 $this->replaceNode($futureNode, $content); | 35 $this->replaceNode($futureNode, $content); |
34 $this->setNodeContent($futureNode, $content); | 37 $this->setNodeContent($futureNode, $content); |
35 } | 38 } |
36 } | 39 } |
37 } | 40 } |
38 } | 41 } |
39 } else | 42 } |
40 { | 43 else { |
41 $content = $this->parseReplacement($obj); | |
42 foreach($rootSelector->nodes as $node) { | 44 foreach($rootSelector->nodes as $node) { |
45 $content = $this->parseReplacement($obj); | |
43 if(isset($rootSelector->attribute)) { | 46 if(isset($rootSelector->attribute)) { |
44 $node->setAttribute($rootSelector->attribute, $obj); | 47 $node->setAttribute($rootSelector->attribute, $obj); |
45 } else { | 48 } else { |
46 if($node->namespaceURI == Template::NS) | 49 if($node->namespaceURI == Template::NS) |
47 $this->replaceNode($node, $content); | 50 $this->replaceNode($node, $content); |
51 } | 54 } |
52 } | 55 } |
53 } | 56 } |
54 | 57 |
55 function getClonedNode($node, $childNodeName) { | 58 function getClonedNode($node, $childNodeName) { |
56 $candidates = $node->getElementsByTagName($childNodeName); | 59 $candidates = $this->parseSelector($childNodeName, $node)->nodes; |
57 foreach($candidates as $candidate) { | 60 foreach($candidates as $candidate) { |
58 if($candidate->nodeName == $childNodeName && $candidate->getAttributeNS(Template::NS, 'toClone') == 'true') { | 61 if($candidate->getAttributeNS(Template::NS, 'toClone') == 'true') { |
59 $tmp = array(); | 62 $tmp = array(); |
60 $tmp['orig'] = $candidate; | 63 $tmp['orig'] = $candidate; |
61 $cnode = $candidate->cloneNode(true); | 64 $cnode = $candidate->cloneNode(true); |
62 $cnode->removeAttribute('toClone'); | 65 $cnode->removeAttribute('toClone'); |
63 $tmp['clone'] = $cnode; | 66 $tmp['clone'] = $cnode; |
64 return (object)$tmp; | 67 return (object)$tmp; |
65 } | 68 } |
66 } | 69 } |
67 } | 70 } |
68 | 71 |
69 function parseSelector($selector, DOMElement $root=NULL) { | 72 function parseSelector($selector, DOMElement $root) { |
70 if(!($root instanceof DOMElement)) { | |
71 $root = $this->xmlDocument->documentElement; | |
72 } | |
73 $obj = array(); | 73 $obj = array(); |
74 $pos = strpos($selector, '@'); | 74 $pos = strpos($selector, '@'); |
75 if($pos !== false) { | 75 if($pos !== false) { |
76 $obj['attribute'] = substr($selector,$pos +1); | 76 $obj['attribute'] = substr($selector,$pos +1); |
77 $selector = substr($selector, 0, $pos); | 77 $selector = substr($selector, 0, $pos); |
81 $obj['nodes'] = $this->xmlXPath->query($obj['xpath'], $root); | 81 $obj['nodes'] = $this->xmlXPath->query($obj['xpath'], $root); |
82 } | 82 } |
83 else { | 83 else { |
84 $obj['nodeName'] = $selector; | 84 $obj['nodeName'] = $selector; |
85 $obj['nodes'] = $root->getElementsByTagName($selector); | 85 $obj['nodes'] = $root->getElementsByTagName($selector); |
86 $obj['nodes'] = $this->xmlDocument->getElementsByTagName($selector); | |
87 } | 86 } |
88 return (object)$obj; | 87 return (object)$obj; |
89 } | 88 } |
90 | 89 |
91 function getNodesMatching($selector, DOMElement $root) { | 90 function getNodesMatching($selector, DOMElement $root) { |
109 function parseReplacement($obj) { | 108 function parseReplacement($obj) { |
110 $retVal = NULL; | 109 $retVal = NULL; |
111 if(is_string($obj)) | 110 if(is_string($obj)) |
112 $retVal = $this->xmlDocument->createTextNode($obj); | 111 $retVal = $this->xmlDocument->createTextNode($obj); |
113 else if($obj instanceof DOMDocument) | 112 else if($obj instanceof DOMDocument) |
114 $retVal = $obj->documentElement; | 113 $retVal = $obj->documentElement->clone(true); |
115 else if($obj instanceof DOMNode) | 114 else if($obj instanceof DOMNode) |
116 $retVal = $obj; | 115 $retVal = $obj->clone(true); |
117 return $retVal; | 116 return $retVal; |
118 } | 117 } |
119 | 118 |
120 function setParams($array) { | 119 function setParams($array) { |
121 foreach($array as $selector => $obj) { | 120 foreach($array as $selector => $obj) { |