Skip to content

Commit dbba164

Browse files
committed
Fix binding array
1 parent d82d706 commit dbba164

File tree

4 files changed

+43
-32
lines changed

4 files changed

+43
-32
lines changed

src/Compiler.php

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,6 @@ public function handleBinding(string $value, string $name, ?DOMElement $node = n
728728
$regexArrayBinding = '/^\[([^\]]+)\]$/';
729729
$regexTemplateString = '/^`(?P<content>.+)`$/';
730730
$regexObjectBinding = '/^\{(?<elements>[^\}]+)\}$/';
731-
$regexObjectElements = '/["\']?(?<class>[^"\']+)["\']?\s*:\s*(?<condition>[^,]+)/x';
732731

733732
if ($value === 'true') {
734733
$this->logger->debug('- setAttribute ' . $name);
@@ -739,43 +738,22 @@ public function handleBinding(string $value, string $name, ?DOMElement $node = n
739738
$elements = explode(',', $match[1]);
740739
foreach ($elements as $element) {
741740
$element = trim($element);
742-
if (preg_match('/^`.*`$/', $element)) {
743-
$element = ' {{ ' . str_replace('"', '\'', $this->refactorTemplateString($element)) . ' }} ';
741+
if (preg_match('/^`(.*)`$/', $element, $match)) {
742+
$dynamicValues[] = $this->handleTemplateStringBinding($match[1], $twigOutput);
743+
// $dynamicValues[] = '{{ ' . str_replace('"', '\'', $this->refactorTemplateString($element)) . ' }}';
744744
} elseif (preg_match('/^\{(.*)\}$/', $element, $match)) {
745-
[$value, $condition] = explode(':', $match[1]);
746-
$element = ' {% if ' . trim($condition) . ' %}{{' .
747-
trim(str_replace('"', '\'', $value)) .
748-
'}}{% endif %} ';
745+
$this->handleObjectBinding([$match[1]], $dynamicValues, $twigOutput);
746+
} else {
747+
$dynamicValues[] = trim($element, '"\'');
749748
}
750-
$dynamicValues[] = trim($element, '"\'');
751749
}
752750
} elseif (preg_match($regexObjectBinding, $value, $matches)) {
753751
$this->logger->debug('- object binding ', ['value' => $value]);
754-
755752
$items = explode(',', $matches['elements']);
756-
757-
foreach ($items as $item) {
758-
if (preg_match($regexObjectElements, $item, $matchElement)) {
759-
$dynamicValues[] = $this->builder->prepareBindingOutput(
760-
$this->builder->refactorCondition($matchElement['condition']) . ' ? \'' . $matchElement['class'] . ' \'',
761-
$twigOutput
762-
);
763-
}
764-
}
753+
$this->handleObjectBinding($items, $dynamicValues, $twigOutput);
765754
} elseif (preg_match($regexTemplateString, $value, $matches)) {
766755
// <div :class="`abc ${someDynamicClass}`">
767-
$templateStringContent = $matches['content'];
768-
769-
preg_match_all('/\${([^}]+)}/', $templateStringContent, $matches, PREG_SET_ORDER);
770-
foreach ($matches as $match) {
771-
$templateStringContent = str_replace(
772-
$match[0],
773-
$this->builder->prepareBindingOutput($this->builder->refactorCondition($match[1]), $twigOutput),
774-
$templateStringContent
775-
);
776-
}
777-
778-
$dynamicValues[] = $templateStringContent;
756+
$dynamicValues[] = $this->handleTemplateStringBinding($matches['content'], $twigOutput);
779757
} else {
780758
$value = $this->builder->refactorCondition($value);
781759
$this->logger->debug(sprintf('- setAttribute "%s" with value "%s"', $name, $value));
@@ -791,6 +769,39 @@ public function handleBinding(string $value, string $name, ?DOMElement $node = n
791769
return $dynamicValues;
792770
}
793771

772+
/**
773+
* @param string[] $items
774+
* @param string[] $dynamicValues
775+
776+
* @throws ReflectionException
777+
*/
778+
protected function handleObjectBinding(array $items, array &$dynamicValues, bool $twigOutput): void
779+
{
780+
$regexObjectElements = '/["\']?(?<class>[^"\']+)["\']?\s*:\s*(?<condition>[^,]+)/x';
781+
782+
foreach ($items as $item) {
783+
if (preg_match($regexObjectElements, $item, $matchElement)) {
784+
$dynamicValues[] = $this->builder->prepareBindingOutput(
785+
$this->builder->refactorCondition($matchElement['condition']) . ' ? \'' . $matchElement['class'] . ' \'',
786+
$twigOutput
787+
);
788+
}
789+
}
790+
}
791+
792+
protected function handleTemplateStringBinding(string $templateStringContent, bool $twigOutput): string
793+
{
794+
preg_match_all('/\${([^}]+)}/', $templateStringContent, $matches, PREG_SET_ORDER);
795+
foreach ($matches as $match) {
796+
$templateStringContent = str_replace(
797+
$match[0],
798+
$this->builder->prepareBindingOutput($this->builder->refactorCondition($match[1]), $twigOutput),
799+
$templateStringContent
800+
);
801+
}
802+
return $templateStringContent;
803+
}
804+
794805
/**
795806
* @throws ReflectionException
796807
*/
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{% set isDisabled = isDisabled|default(false) %}
22
<div class="{{ class|default('') }}" style="{{ style|default('') }}">
3-
<div class="a {% if isDisabled %}{{'a--disabled'}}{% endif %} b">
3+
<div class="a {{ isDisabled ? 'a--disabled ' }} b">
44
Foo
55
</div>
66
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{% set isDisabled = isDisabled|default(false) %}
22
<div class="{{ class|default('') }}" style="{{ style|default('') }}">
3-
<div class="a {{ 'b ' ~ ( isDisabled ? 'b--disabled' : '' ) ~ '' }}">
3+
<div class="a b {{ isDisabled ? 'b--disabled' : '' }}">
44
Foo
55
</div>
66
</div>

0 commit comments

Comments
 (0)