Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6a6977e

Browse files
authoredSep 7, 2020
Merge pull request #68 from tronsha/feature/remove-attribute-if-condition-false
Fix attributes with if condition
2 parents 0e4931f + ea5d04f commit 6a6977e

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed
 

‎src/Compiler.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,11 @@ private function handleAttributeBinding(DOMElement $node): void
553553
$value = $this->implodeAttributeValue($name, $dynamicValues, $staticValues);
554554

555555
if ($addIfAroundAttribute && $value) {
556-
$value = $name . '|' . base64_encode($value);
556+
$value = $name . '|' . base64_encode(
557+
$this->builder->refactorCondition(
558+
$this->replacePlaceholders($value)
559+
)
560+
);
557561
$name = '__ATTRIBUTE_WITH_IF_CONDITION__';
558562
}
559563

@@ -1297,8 +1301,6 @@ private function replaceAttributeWithIfConditionPlaceholders(string $html): stri
12971301
$name = $match[1];
12981302
$value = base64_decode($match[2]);
12991303
$condition = trim(str_replace(['__DOUBLE_CURLY_OPEN__', '__DOUBLE_CURLY_CLOSE__'], '', $value));
1300-
$value = $this->replacePlaceholders($value);
1301-
$condition = $this->replacePlaceholders($condition);
13021304
if (in_array($name, ['checked', 'selected', 'disabled'])) {
13031305
$value = $name;
13041306
}

‎tests/AttributeIfTrueTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ public function dataProvider()
3434
'<template><form><input type="checkbox" :checked="foo"></form></template>',
3535
'<form class="{{ class|default(\'\') }}" style="{{ style|default(\'\') }}"><input type="checkbox" {% if foo %}checked="checked"{% endif %}></form>',
3636
],
37+
[
38+
'<template><form><input type="checkbox" :checked="foo || bar"></form></template>',
39+
'<form class="{{ class|default(\'\') }}" style="{{ style|default(\'\') }}"><input type="checkbox" {% if foo or bar %}checked="checked"{% endif %}></form>',
40+
],
41+
[
42+
'<template><form><input type="checkbox" :checked="foo === 1"></form></template>',
43+
'<form class="{{ class|default(\'\') }}" style="{{ style|default(\'\') }}"><input type="checkbox" {% if foo == 1 %}checked="checked"{% endif %}></form>',
44+
],
3745
];
3846
}
3947
}

0 commit comments

Comments
 (0)