@@ -94,7 +94,7 @@ public function convertNode(DOMNode $node): DOMNode
94
94
switch ($ node ->nodeType ) {
95
95
case XML_TEXT_NODE :
96
96
$ this ->logger ->debug ('Text node found ' , ['name ' => $ node ->nodeName ]);
97
- // fall through to next case, because we don't need to handle either of these node-types
97
+ // fall through to next case, because we don't need to handle either of these node-types
98
98
case XML_COMMENT_NODE :
99
99
$ this ->logger ->debug ('Comment node found ' , ['name ' => $ node ->nodeName ]);
100
100
return $ node ;
@@ -125,7 +125,7 @@ public function convertNode(DOMNode $node): DOMNode
125
125
126
126
$ usedComponent ->addProperty ($ name , $ value , true );
127
127
} else {
128
- $ usedComponent ->addProperty ($ attribute ->name , '" ' . $ attribute ->value . '" ' , false );
128
+ $ usedComponent ->addProperty ($ attribute ->name , '" ' . $ attribute ->value . '" ' , false );
129
129
}
130
130
}
131
131
}
@@ -166,61 +166,63 @@ private function handleAttributeBinding(DOMElement $node)
166
166
foreach (iterator_to_array ($ node ->attributes ) as $ attribute ) {
167
167
168
168
if (strpos ($ attribute ->name , 'v-bind: ' ) !== 0 && strpos ($ attribute ->name , ': ' ) !== 0 ) {
169
- $ this ->logger ->debug ("- skip: " . $ attribute ->name );
169
+ $ this ->logger ->debug ("- skip: " . $ attribute ->name );
170
170
continue ;
171
171
}
172
172
173
173
$ name = substr ($ attribute ->name , strpos ($ attribute ->name , ': ' ) + 1 );
174
174
$ value = $ attribute ->value ;
175
- $ this ->logger ->debug ('- handle: ' . $ name. ' = ' . $ value );
175
+ $ this ->logger ->debug ('- handle: ' . $ name . ' = ' . $ value );
176
176
177
+ $ staticValues = $ node ->hasAttribute ($ name ) ? $ node ->getAttribute ($ name ) : '' ;
178
+ $ dynamicValues = [];
179
+
180
+ // Remove originally bound attribute
181
+ $ this ->logger ->debug ('- remove original ' . $ attribute ->name );
182
+ $ node ->removeAttribute ($ attribute ->name );
177
183
178
184
switch ($ name ) {
179
185
case 'key ' :
180
186
// Not necessary in twig
181
- break ;
187
+ $ this ->logger ->debug ('- skip: key ' );
188
+ return ;
182
189
case 'style ' :
183
190
break ;
184
191
case 'class ' :
185
192
break ;
186
- default :
187
- if ($ value === 'true ' ) {
188
- $ this ->logger ->debug ('- setAttribute ' .$ name );
189
- $ node ->setAttribute ($ name , $ name );
190
- } else {
191
- $ this ->logger ->debug ('- setAttribute " ' .$ name .'" with value ' );
192
- $ node ->setAttribute (
193
- $ name ,
194
- Replacements::getSanitizedConstant ('DOUBLE_CURLY_OPEN ' ).
195
- $ value .
196
- Replacements::getSanitizedConstant ('DOUBLE_CURLY_CLOSE ' )
197
- );
198
- }
199
193
}
200
194
201
- if (is_array ($ value )) {
195
+ if ($ value === 'true ' ) {
196
+ $ this ->logger ->debug ('- setAttribute ' . $ name );
197
+ $ node ->setAttribute ($ name , $ name );
198
+ } elseif (preg_match ('/^\[([^\]]+)\]$/ ' , $ value , $ matches )) {
199
+ $ this ->logger ->debug ('- is array ' .$ value );
200
+
201
+ if (preg_match_all ('/((?:[ \'"])(?<elements>[^ \'"])[ \'"])/ ' , $ value , $ arrayMatch )){
202
+ $ value = $ arrayMatch ['elements ' ];
203
+ $ this ->logger ->debug ('- ' , ['match ' => $ arrayMatch ]);
204
+ }
205
+ else {
206
+ $ value = [];
207
+ }
208
+
202
209
if ($ name === 'style ' ) {
203
- $ styles = [];
204
210
foreach ($ value as $ prop => $ setting ) {
205
211
if ($ setting ) {
206
212
$ prop = strtolower (preg_replace ('/([A-Z])/ ' , '-$1 ' , $ prop ));
207
- $ styles [] = sprintf ('%s:%s ' , $ prop , $ setting );
213
+ $ dynamicValues [] = sprintf ('%s:%s ' , $ prop , $ setting );
208
214
}
209
215
}
210
- $ node ->setAttribute ($ name , implode ('; ' , $ styles ));
211
216
} elseif ($ name === 'class ' ) {
212
- $ classes = [];
213
- foreach ($ value as $ className => $ setting ) {
214
- if ($ setting ) {
215
- $ classes [] = $ className ;
216
- }
217
+ foreach ($ value as $ className ) {
218
+ $ dynamicValues [] = $ className ;
217
219
}
218
- $ node ->setAttribute ($ name , implode (' ' , $ classes ));
219
220
}
220
- } /*
221
- * <div :class="`abc ${someDynamicClass}`">
222
- */
221
+ }
223
222
elseif (preg_match ('/^`(?P<content>.+)`$/ ' , $ value , $ matches )) {
223
+ /*
224
+ * <div :class="`abc ${someDynamicClass}`">
225
+ */
224
226
$ templateStringContent = $ matches ['content ' ];
225
227
226
228
$ templateStringContent = preg_replace (
@@ -229,13 +231,19 @@ private function handleAttributeBinding(DOMElement $node)
229
231
$ templateStringContent
230
232
);
231
233
232
- $ node -> setAttribute ( $ name , $ templateStringContent) ;
234
+ $ dynamicValues [] = $ templateStringContent ;
233
235
} else {
234
- $ this ->logger ->debug ('- No Handling for: ' .$ value );
236
+ $ this ->logger ->debug ('- setAttribute " ' . $ name . '" with value ' );
237
+ $ dynamicValues [] =
238
+ Replacements::getSanitizedConstant ('DOUBLE_CURLY_OPEN ' ) .
239
+ $ value .
240
+ Replacements::getSanitizedConstant ('DOUBLE_CURLY_CLOSE ' );
235
241
}
236
242
237
- $ this ->logger ->debug ('=> remove original ' .$ attribute ->name );
238
- $ node ->removeAttribute ($ attribute ->name );
243
+ $ node ->setAttribute (
244
+ $ name ,
245
+ $ this ->implodeAttributeValue ($ name , $ dynamicValues , $ staticValues )
246
+ );
239
247
}
240
248
}
241
249
@@ -319,7 +327,7 @@ private function handleFor(DOMElement $node)
319
327
320
328
// (2)
321
329
if (preg_match ('/(\d+)/ ' , $ listName )) {
322
- $ listName = '1.. ' . $ listName ;
330
+ $ listName = '1.. ' . $ listName ;
323
331
}
324
332
325
333
// (1)
@@ -408,6 +416,21 @@ protected function sanitizeCondition(string $condition)
408
416
return $ condition ;
409
417
}
410
418
419
+ protected function implodeAttributeValue (string $ attribute , array $ values , string $ oldValue ): string
420
+ {
421
+ $ glue = ' ' ;
422
+
423
+ if ($ attribute === 'style ' ) {
424
+ $ glue = '; ' ;
425
+ }
426
+
427
+ if (!empty ($ oldValue )) {
428
+ $ values = array_merge ([$ oldValue ], $ values );
429
+ }
430
+
431
+ return implode ($ glue , $ values );
432
+ }
433
+
411
434
protected function replacePlaceholders (string $ string )
412
435
{
413
436
foreach (Replacements::getConstants () as $ constant => $ value ) {
@@ -424,7 +447,7 @@ public function registerComponent(string $componentName, string $componentPath)
424
447
425
448
protected function addSingleLineBanner (string $ html )
426
449
{
427
- return $ this ->builder ->createComment (implode ('' , $ this ->banner )). "\n" . $ html ;
450
+ return $ this ->builder ->createComment (implode ('' , $ this ->banner )) . "\n" . $ html ;
428
451
}
429
452
430
453
protected function addBanner (string $ html )
@@ -436,20 +459,20 @@ protected function addBanner(string $html)
436
459
$ bannerLines = ['{# ' ];
437
460
438
461
foreach ($ this ->banner as $ line ) {
439
- $ bannerLines [] = ' # ' . $ line ;
462
+ $ bannerLines [] = ' # ' . $ line ;
440
463
}
441
464
442
465
$ bannerLines [] = ' #} ' ;
443
466
444
- $ html = implode (PHP_EOL , $ bannerLines ). PHP_EOL . $ html ;
467
+ $ html = implode (PHP_EOL , $ bannerLines ) . PHP_EOL . $ html ;
445
468
446
469
return $ html ;
447
470
}
448
471
449
472
public function refactorTemplateString ($ value )
450
473
{
451
474
if (preg_match ('/^`(?P<content>.+)`$/ ' , $ value , $ matches )) {
452
- $ templateStringContent = '" ' . $ matches ['content ' ]. '" ' ;
475
+ $ templateStringContent = '" ' . $ matches ['content ' ] . '" ' ;
453
476
$ value = preg_replace (
454
477
'/\$\{(.+)\}/ ' ,
455
478
'{{ $1 }} ' ,
0 commit comments