@@ -66,10 +66,10 @@ public function convertNode(DOMNode $node): DOMNode
6666// }
6767
6868 $ this ->stripEventHandlers ($ node );
69- // $this->handleFor($node);
69+ $ this ->handleFor ($ node );
7070 //$this->handleRawHtml($node, $data);
7171
72- // $this->handleAttributeBinding($node);
72+ $ this ->handleAttributeBinding ($ node );
7373
7474 foreach (iterator_to_array ($ node ->childNodes ) as $ childNode ) {
7575 $ this ->convertNode ($ childNode );
@@ -92,13 +92,16 @@ private function handleAttributeBinding(DOMElement $node)
9292 {
9393 /** @var DOMAttr $attribute */
9494 foreach (iterator_to_array ($ node ->attributes ) as $ attribute ) {
95- dump ( $ attribute -> name );
95+
9696 if (strpos ($ attribute ->name , 'v-bind: ' ) !== 0 && strpos ($ attribute ->name , ': ' ) !== 0 ) {
97- dump ("- skip " );
97+ var_dump ("- skip: " . $ attribute -> name );
9898 continue ;
9999 }
100100
101101 $ name = substr ($ attribute ->name , 1 );
102+ $ value = $ attribute ->value ;
103+ var_dump ('- handle: ' .$ name .' = ' .$ value );
104+
102105 if (is_bool ($ value )) {
103106 if ($ value ) {
104107 $ node ->setAttribute ($ name , $ name );
@@ -180,29 +183,45 @@ private function handleIf(DOMElement $node): void
180183 }
181184 }
182185
183- private function handleFor (DOMNode $ node )
186+ private function handleFor (DOMElement $ node )
184187 {
185- if ($ this ->isTextNode ($ node )) {
186- return ;
187- }
188-
189188 /** @var DOMElement $node */
190189 if (!$ node ->hasAttribute ('v-for ' )) {
191190 return ;
192191 }
193192
194- [$ itemName , $ listName ] = explode (' in ' , $ node ->getAttribute ('v-for ' ));
195-
196- // Start For
197- if (strpos ($ itemName , ', ' ) !== false ) {
198- [$ keyName , $ itemName ] = explode (', ' , $ itemName );
199- $ keyName = trim ($ keyName , ' ( ' );
200- $ itemName = trim ($ itemName , ') ' );
201- $ startFor = $ this ->document ->createTextNode ('{% for ' . $ itemName . ' in ' . $ listName . ' %} ' .
202- '{% set ' . $ keyName . ' = index0 %} ' );
203- } else {
204- $ startFor = $ this ->document ->createTextNode ('{% for ' . $ itemName . ' in ' . $ listName . ' %} ' );
193+ [$ forLeft , $ listName ] = explode (' in ' , $ node ->getAttribute ('v-for ' ));
194+
195+ /*
196+ * Variations:
197+ * (1) item in array
198+ * (2) key, item in array
199+ * (3) key, item, index in object
200+ */
201+
202+ // (1)
203+ $ forCommand = '{% for ' . $ forLeft . ' in ' . $ listName . ' %} ' ;
204+
205+ if (strpos ($ forLeft , ', ' )) {
206+ $ forLeft = str_replace ('( ' , '' , $ forLeft );
207+ $ forLeft = str_replace (') ' , '' , $ forLeft );
208+
209+ $ forLeftArray = explode (', ' , $ forLeft );
210+
211+ $ forValue = $ forLeftArray [0 ];
212+ $ forKey = $ forLeftArray [1 ];
213+ $ forIndex = $ forLeftArray [2 ] ?? null ;
214+
215+ // (2)
216+ $ forCommand = '{% for ' . $ forKey . ', ' . $ forValue . ' in ' . $ listName . ' %} ' ;
217+
218+ if ($ forIndex ) {
219+ // (3)
220+ $ forCommand .= ' {% set ' . $ forIndex . ' = loop.index0 %} ' ;
221+ }
205222 }
223+
224+ $ startFor = $ this ->document ->createTextNode ($ forCommand );
206225 $ node ->parentNode ->insertBefore ($ startFor , $ node );
207226
208227 // End For
0 commit comments