33namespace Paneon \VueToTwig ;
44
55use DOMAttr ;
6- use DOMCharacterData ;
76use DOMDocument ;
87use DOMElement ;
98use DOMNode ;
109use DOMText ;
1110use Exception ;
11+ use Paneon \VueToTwig \Models \Replacements ;
1212use Psr \Log \LoggerInterface ;
1313
1414class Compiler
1515{
16- protected const DOUBLE_CURLY_OPEN = '__DOUBLE_CURLY_OPEN__ ' ;
17- protected const DOUBLE_CURLY_CLOSE = '__DOUBLE_CURLY_CLOSE__ ' ;
1816
1917 /** @var String[] */
2018 protected $ components ;
@@ -50,8 +48,11 @@ public function convert(): string
5048
5149 $ rootNode = $ this ->getRootNode ($ templateElement );
5250 $ resultNode = $ this ->convertNode ($ rootNode );
51+ $ html = $ this ->document ->saveHTML ($ resultNode );
5352
54- return $ this ->replacePlaceholders ($ this ->document ->saveHTML ($ resultNode ));
53+ $ html = $ this ->replacePlaceholders ($ html );
54+
55+ return $ html ;
5556 }
5657
5758 public function convertNode (DOMNode $ node ): DOMNode
@@ -96,13 +97,13 @@ private function handleAttributeBinding(DOMElement $node)
9697 foreach (iterator_to_array ($ node ->attributes ) as $ attribute ) {
9798
9899 if (strpos ($ attribute ->name , 'v-bind: ' ) !== 0 && strpos ($ attribute ->name , ': ' ) !== 0 ) {
99- $ this ->logger ->debug ("- skip: " . $ attribute ->name );
100+ $ this ->logger ->debug ("- skip: " . $ attribute ->name );
100101 continue ;
101102 }
102103
103104 $ name = substr ($ attribute ->name , strpos ($ attribute ->name , ': ' ) + 1 );
104105 $ value = $ attribute ->value ;
105- $ this ->logger ->debug ('- handle: ' . $ name . ' = ' . $ value );
106+ $ this ->logger ->debug ('- handle: ' . $ name. ' = ' . $ value );
106107
107108
108109 switch ($ name ) {
@@ -121,7 +122,9 @@ private function handleAttributeBinding(DOMElement $node)
121122 $ this ->logger ->debug ('- setAttribute " ' .$ name .'" with value ' );
122123 $ node ->setAttribute (
123124 $ name ,
124- self ::DOUBLE_CURLY_OPEN .$ value .self ::DOUBLE_CURLY_CLOSE
125+ Replacements::getSanitizedConstant ('DOUBLE_CURLY_OPEN ' ) .
126+ $ value .
127+ Replacements::getSanitizedConstant ('DOUBLE_CURLY_CLOSE ' )
125128 );
126129 }
127130 }
@@ -147,7 +150,7 @@ private function handleAttributeBinding(DOMElement $node)
147150 }
148151 }
149152
150- $ this ->logger ->debug ('=> remove original ' . $ attribute ->name );
153+ $ this ->logger ->debug ('=> remove original ' . $ attribute ->name );
151154 $ node ->removeAttribute ($ attribute ->name );
152155 }
153156 }
@@ -162,9 +165,10 @@ private function handleIf(DOMElement $node): void
162165
163166 if ($ node ->hasAttribute ('v-if ' )) {
164167 $ condition = $ node ->getAttribute ('v-if ' );
168+ $ condition = $ this ->sanitizeCondition ($ condition );
165169
166170 // Open with if
167- $ openIf = $ this ->document ->createTextNode ('{% if ' . $ condition . ' %} ' );
171+ $ openIf = $ this ->document ->createTextNode ('{% if ' . $ condition. ' %} ' );
168172 $ node ->parentNode ->insertBefore ($ openIf , $ node );
169173
170174 // Close with endif
@@ -176,9 +180,10 @@ private function handleIf(DOMElement $node): void
176180 $ node ->removeAttribute ('v-if ' );
177181 } elseif ($ node ->hasAttribute ('v-else-if ' )) {
178182 $ condition = $ node ->getAttribute ('v-else-if ' );
183+ $ condition = $ this ->sanitizeCondition ($ condition );
179184
180185 // Replace old endif with else
181- $ this ->lastCloseIf ->textContent = '{% elseif ' . $ condition . ' %} ' ;
186+ $ this ->lastCloseIf ->textContent = '{% elseif ' . $ condition. ' %} ' ;
182187
183188 // Close with new endif
184189 $ closeIf = $ this ->document ->createTextNode ('{% endif %} ' );
@@ -211,12 +216,18 @@ private function handleFor(DOMElement $node)
211216 /*
212217 * Variations:
213218 * (1) item in array
214- * (2) key, item in array
215- * (3) key, item, index in object
219+ * (2)
220+ * (3) key, item in array
221+ * (4) key, item, index in object
216222 */
217223
224+ // (2)
225+ if (preg_match ('/(\d+)/ ' , $ listName )) {
226+ $ listName = '1.. ' .$ listName ;
227+ }
228+
218229 // (1)
219- $ forCommand = '{% for ' . $ forLeft . ' in ' . $ listName . ' %} ' ;
230+ $ forCommand = '{% for ' . $ forLeft. ' in ' . $ listName. ' %} ' ;
220231
221232 if (strpos ($ forLeft , ', ' )) {
222233 $ forLeft = str_replace ('( ' , '' , $ forLeft );
@@ -228,12 +239,12 @@ private function handleFor(DOMElement $node)
228239 $ forKey = $ forLeftArray [1 ];
229240 $ forIndex = $ forLeftArray [2 ] ?? null ;
230241
231- // (2 )
232- $ forCommand = '{% for ' . $ forKey . ', ' . $ forValue . ' in ' . $ listName . ' %} ' ;
242+ // (3 )
243+ $ forCommand = '{% for ' . $ forKey. ', ' . $ forValue. ' in ' . $ listName. ' %} ' ;
233244
234245 if ($ forIndex ) {
235- // (3 )
236- $ forCommand .= ' {% set ' . $ forIndex . ' = loop.index0 %} ' ;
246+ // (4 )
247+ $ forCommand .= ' {% set ' . $ forIndex. ' = loop.index0 %} ' ;
237248 }
238249 }
239250
@@ -284,10 +295,23 @@ private function getRootNode(DOMElement $element): \DOMNode
284295 return $ firstTagNode ;
285296 }
286297
298+ protected function sanitizeCondition (string $ condition )
299+ {
300+ $ condition = str_replace ('&& ' , 'and ' , $ condition );
301+ $ condition = str_replace ('|| ' , 'or ' , $ condition );
302+
303+ foreach (Replacements::getConstants () as $ constant => $ value ) {
304+ $ condition = str_replace ($ value , Replacements::getSanitizedConstant ($ constant ), $ condition );
305+ }
306+
307+ return $ condition ;
308+ }
309+
287310 protected function replacePlaceholders (string $ string )
288311 {
289- $ string = str_replace (self ::DOUBLE_CURLY_OPEN , '{{ ' , $ string );
290- $ string = str_replace (self ::DOUBLE_CURLY_CLOSE , '}} ' , $ string );
312+ foreach (Replacements::getConstants () as $ constant => $ value ) {
313+ $ string = str_replace (Replacements::getSanitizedConstant ($ constant ), $ value , $ string );
314+ }
291315
292316 return $ string ;
293317 }
0 commit comments