@@ -105,6 +105,13 @@ class Compiler
105
105
*/
106
106
protected $ includeAttributes = ['class ' , 'style ' ];
107
107
108
+ /**
109
+ * @var string[]
110
+ */
111
+ protected $ ifAttributes = ['checked ' , 'selected ' , 'disabled ' ];
112
+
113
+
114
+
108
115
/**
109
116
* Compiler constructor.
110
117
*/
@@ -192,6 +199,7 @@ public function convert(): string
192
199
$ html = $ this ->addVariableBlocks ($ html );
193
200
$ html = $ this ->replacePlaceholders ($ html );
194
201
$ html = $ this ->replaceScopedPlaceholders ($ html );
202
+ $ html = $ this ->replaceAttributeWithIfConditionPlaceholders ($ html );
195
203
196
204
$ html = preg_replace ('/<template>\s*(.*)\s*<\/template>/ism ' , '$1 ' , $ html );
197
205
$ html = preg_replace ('/<\/?template[^>]*?>/i ' , '' , $ html );
@@ -513,8 +521,15 @@ private function handleAttributeBinding(DOMElement $node): void
513
521
continue ;
514
522
}
515
523
524
+ // makes no sense to use this in code, but it must handled.
525
+ if ($ value === 'false ' ) {
526
+ continue ;
527
+ }
528
+
516
529
$ dynamicValues = $ this ->handleBinding ($ value , $ name , $ node );
517
530
531
+ $ addIfAroundAttribute = in_array ($ name , $ this ->ifAttributes );
532
+
518
533
/* @see https://gitlab.gnome.org/GNOME/libxml2/-/blob/LIBXML2.6.32/HTMLtree.c#L657 */
519
534
switch ($ name ) {
520
535
case 'href ' :
@@ -535,7 +550,14 @@ private function handleAttributeBinding(DOMElement $node): void
535
550
break ;
536
551
}
537
552
538
- $ node ->setAttribute ($ name , $ this ->implodeAttributeValue ($ name , $ dynamicValues , $ staticValues ));
553
+ $ value = $ this ->implodeAttributeValue ($ name , $ dynamicValues , $ staticValues );
554
+
555
+ if ($ addIfAroundAttribute && $ value ) {
556
+ $ value = $ name . '| ' . base64_encode ($ value );
557
+ $ name = '__ATTRIBUTE_WITH_IF_CONDITION__ ' ;
558
+ }
559
+
560
+ $ node ->setAttribute ($ name , $ value );
539
561
}
540
562
}
541
563
@@ -1263,4 +1285,31 @@ private function replaceScopedPlaceholders(string $html): string
1263
1285
1264
1286
return $ html ;
1265
1287
}
1288
+
1289
+ /**
1290
+ * @throws ReflectionException
1291
+ */
1292
+ private function replaceAttributeWithIfConditionPlaceholders (string $ html ): string
1293
+ {
1294
+ $ pattern = '/__ATTRIBUTE_WITH_IF_CONDITION__="([-a-zA-Z0-9]+)\|([a-zA-Z0-9+=]+)"/ ' ;
1295
+ if (preg_match_all ($ pattern , $ html , $ matches , PREG_SET_ORDER )) {
1296
+ foreach ($ matches as $ match ) {
1297
+ $ name = $ match [1 ];
1298
+ $ value = base64_decode ($ match [2 ]);
1299
+ $ condition = trim (str_replace (['__DOUBLE_CURLY_OPEN__ ' , '__DOUBLE_CURLY_CLOSE__ ' ], '' , $ value ));
1300
+ $ value = $ this ->replacePlaceholders ($ value );
1301
+ $ condition = $ this ->replacePlaceholders ($ condition );
1302
+ if (in_array ($ name , ['checked ' , 'selected ' , 'disabled ' ])) {
1303
+ $ value = $ name ;
1304
+ }
1305
+ $ html = str_replace (
1306
+ $ match [0 ],
1307
+ '{% if ' . $ condition . ' %} ' . $ name . '=" ' . $ value . '"{% endif %} ' ,
1308
+ $ html
1309
+ );
1310
+ }
1311
+ }
1312
+
1313
+ return $ html ;
1314
+ }
1266
1315
}
0 commit comments