Skip to content

Commit f489443

Browse files
authored
Merge pull request #70 from tronsha/feature/remove-attribute-if-condition-false
Fix for more than one attribute with if in one tag
2 parents 01697e7 + 7442006 commit f489443

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/Compiler.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,10 @@ private function handleAttributeBinding(DOMElement $node): void
576576
)
577577
);
578578
$name = '__ATTRIBUTE_WITH_IF_CONDITION__';
579+
$oldValue = $node->getAttribute($name);
580+
if ($oldValue) {
581+
$value = $oldValue . ',' . $value;
582+
}
579583
}
580584

581585
$node->setAttribute($name, $value);
@@ -1312,20 +1316,21 @@ private function replaceScopedPlaceholders(string $html): string
13121316
*/
13131317
private function replaceAttributeWithIfConditionPlaceholders(string $html): string
13141318
{
1315-
$pattern = '/__ATTRIBUTE_WITH_IF_CONDITION__="([-a-zA-Z0-9]+)\|([a-zA-Z0-9+=]+)"/';
1319+
$pattern = '/__ATTRIBUTE_WITH_IF_CONDITION__="([^"]+)"/';
13161320
if (preg_match_all($pattern, $html, $matches, PREG_SET_ORDER)) {
13171321
foreach ($matches as $match) {
1318-
$name = $match[1];
1319-
$value = $this->replacePlaceholders(base64_decode($match[2]));
1320-
$condition = trim(str_replace(['{{', '}}'], '', $value));
1321-
if (in_array($name, ['checked', 'selected', 'disabled'])) {
1322-
$value = $name;
1322+
$attributes = explode(',', $match[1]);
1323+
$replaceHtml = '';
1324+
foreach ($attributes as $attribute) {
1325+
[$name, $encodedValue] = explode('|', $attribute);
1326+
$value = $this->replacePlaceholders(base64_decode($encodedValue));
1327+
$condition = trim(str_replace(['{{', '}}'], '', $value));
1328+
if (in_array($name, ['checked', 'selected', 'disabled'])) {
1329+
$value = $name;
1330+
}
1331+
$replaceHtml .= ' {% if ' . $condition . ' %}' . $name . '="' . $value . '"{% endif %}';
13231332
}
1324-
$html = str_replace(
1325-
$match[0],
1326-
'{% if ' . $condition . ' %}' . $name . '="' . $value . '"{% endif %}',
1327-
$html
1328-
);
1333+
$html = str_replace($match[0], trim($replaceHtml), $html);
13291334
}
13301335
}
13311336

tests/AttributeIfTrueTest.php renamed to tests/AttributeWithIfTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Exception;
66

7-
class IfAttributesTest extends AbstractTestCase
7+
class AttributeWithIfTest extends AbstractTestCase
88
{
99
/**
1010
* @dataProvider dataProvider
@@ -42,6 +42,10 @@ public function dataProvider()
4242
'<template><form><input type="checkbox" :checked="foo === 1"></form></template>',
4343
'<form class="{{ class|default(\'\') }}" style="{{ style|default(\'\') }}"><input type="checkbox" {% if foo == 1 %}checked="checked"{% endif %}></form>',
4444
],
45+
[
46+
'<template><form><input type="checkbox" :disabled="foo === 0" :checked="foo === 1"></form></template>',
47+
'<form class="{{ class|default(\'\') }}" style="{{ style|default(\'\') }}"><input type="checkbox" {% if foo == 0 %}disabled="disabled"{% endif %} {% if foo == 1 %}checked="checked"{% endif %}></form>',
48+
],
4549
];
4650
}
4751
}

0 commit comments

Comments
 (0)