Skip to content

Commit 5b9bfed

Browse files
committed
Add attributes with if condition
1 parent a6b90c8 commit 5b9bfed

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/Compiler.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ class Compiler
105105
*/
106106
protected $includeAttributes = ['class', 'style'];
107107

108+
/**
109+
* @var string[]
110+
*/
111+
protected $ifAttributes = ['checked', 'selected'];
112+
113+
114+
108115
/**
109116
* Compiler constructor.
110117
*/
@@ -189,6 +196,7 @@ public function convert(): string
189196
throw new Exception('Generating html during conversion process failed.');
190197
}
191198

199+
$html = $this->replaceAttributeWithIfConditionPlaceholders($html);
192200
$html = $this->addVariableBlocks($html);
193201
$html = $this->replacePlaceholders($html);
194202
$html = $this->replaceScopedPlaceholders($html);
@@ -513,8 +521,15 @@ private function handleAttributeBinding(DOMElement $node): void
513521
continue;
514522
}
515523

524+
// makes no sense to use this in code, but it must handled.
525+
if ($value === 'false') {
526+
continue;
527+
}
528+
516529
$dynamicValues = $this->handleBinding($value, $name, $node);
517530

531+
$addIfAroundAttribute = in_array($name, $this->ifAttributes);
532+
518533
/* @see https://gitlab.gnome.org/GNOME/libxml2/-/blob/LIBXML2.6.32/HTMLtree.c#L657 */
519534
switch ($name) {
520535
case 'href':
@@ -535,7 +550,14 @@ private function handleAttributeBinding(DOMElement $node): void
535550
break;
536551
}
537552

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 = '__SHOW_ATTRIBUTE_IF_CONDITION_IS_TRUE__';
558+
}
559+
560+
$node->setAttribute($name, $value);
539561
}
540562
}
541563

@@ -1263,4 +1285,26 @@ private function replaceScopedPlaceholders(string $html): string
12631285

12641286
return $html;
12651287
}
1288+
1289+
private function replaceAttributeWithIfConditionPlaceholders(string $html): string
1290+
{
1291+
$pattern = '/__SHOW_ATTRIBUTE_IF_CONDITION_IS_TRUE__="([-a-zA-Z0-9]+)\|([a-zA-Z0-9+=]+)"/';
1292+
if (preg_match_all($pattern, $html, $matches, PREG_SET_ORDER)) {
1293+
foreach ($matches as $match) {
1294+
$name = $match[1];
1295+
$value = base64_decode($match[2]);
1296+
$condition = trim(str_replace(['__DOUBLE_CURLY_OPEN__', '__DOUBLE_CURLY_CLOSE__'], '', $value));
1297+
if (in_array($name, ['checked', 'selected'])) {
1298+
$value = $name;
1299+
}
1300+
$html = str_replace(
1301+
$match[0],
1302+
'{% if ' . $condition . ' %}' . $name . '="' . $value . '"{% endif %}',
1303+
$html
1304+
);
1305+
}
1306+
}
1307+
1308+
return $html;
1309+
}
12661310
}

tests/AttributeIfTrueTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public function dataProvider()
2828
return [
2929
[
3030
'<template><form><input type="checkbox" :checked="false"></form></template>',
31-
'<form class="{{ class|default(\'\') }}" style="{{ style|default(\'\') }}"><input type="checkbox" {% if false %}checked{% endif %}></form>',
31+
'<form class="{{ class|default(\'\') }}" style="{{ style|default(\'\') }}"><input type="checkbox"></form>',
32+
],
33+
[
34+
'<template><form><input type="checkbox" :checked="foo"></form></template>',
35+
'<form class="{{ class|default(\'\') }}" style="{{ style|default(\'\') }}"><input type="checkbox" {% if foo %}checked="checked"{% endif %}></form>',
3236
],
3337
];
3438
}

0 commit comments

Comments
 (0)