Skip to content

Commit f5ace45

Browse files
authored
Merge pull request #99 from tronsha/fix/style-attribute
Fix for style binding
2 parents 36f1477 + 2c47465 commit f5ace45

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

src/Compiler.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public function convertNode(DOMNode $node, int $level = 0): DOMNode
310310
$this->handleHtml($node);
311311
$this->handleText($node);
312312
$this->stripEventHandlers($node);
313-
$this->handleSlots($node);
313+
$this->handleSlots($node, $level);
314314
$this->cleanupAttributes($node);
315315
$this->addScopedAttribute($node, $level);
316316
}
@@ -762,15 +762,15 @@ public function handleBinding(string $value, string $name, ?DOMElement $node = n
762762
if (preg_match('/^`(.*)`$/', $element, $match)) {
763763
$dynamicValues[] = $this->handleTemplateStringBinding($match[1], $twigOutput);
764764
} elseif (preg_match('/^\{(.*)\}$/', $element, $match)) {
765-
$this->handleObjectBinding([$match[1]], $dynamicValues, $twigOutput);
765+
$this->handleObjectBinding([$match[1]], $dynamicValues, $twigOutput, $name === 'style');
766766
} else {
767767
$dynamicValues[] = $element;
768768
}
769769
}
770770
} elseif (preg_match($regexObjectBinding, $value, $matches)) {
771771
$this->logger->debug('- object binding ', ['value' => $value]);
772772
$items = explode(',', $matches['elements']);
773-
$this->handleObjectBinding($items, $dynamicValues, $twigOutput);
773+
$this->handleObjectBinding($items, $dynamicValues, $twigOutput, $name === 'style');
774774
} elseif (preg_match($regexTemplateString, $value, $matches)) {
775775
// <div :class="`abc ${someDynamicClass}`">
776776
$this->logger->debug('- template string binding ', ['value' => $value]);
@@ -795,13 +795,15 @@ public function handleBinding(string $value, string $name, ?DOMElement $node = n
795795
* @param string[] $dynamicValues
796796
* @throws ReflectionException
797797
*/
798-
protected function handleObjectBinding(array $items, array &$dynamicValues, bool $twigOutput): void
798+
protected function handleObjectBinding(array $items, array &$dynamicValues, bool $twigOutput, bool $isStyle): void
799799
{
800-
$regexObjectElements = '/["\']?(?<class>[^"\']+)["\']?\s*:\s*(?<condition>[^,]+)/x';
800+
$regexObjectElements = '/(?<isString>["\']?)(?<class>[^"\']+)["\']?\s*:\s*(?<condition>[^,]+)/x';
801801
foreach ($items as $item) {
802802
if (preg_match($regexObjectElements, $item, $matchElement)) {
803803
$dynamicValues[] = $this->builder->prepareBindingOutput(
804-
$this->builder->refactorCondition($matchElement['condition']) . ' ? \'' . $matchElement['class'] . '\'',
804+
$isStyle && !$matchElement['isString']
805+
? '\'' . $this->camelCaseToKebabCase($matchElement['class']) . ':\' + ' . $matchElement['condition']
806+
: $this->builder->refactorCondition($matchElement['condition']) . ' ? \'' . $matchElement['class'] . '\'',
805807
$twigOutput
806808
);
807809
}
@@ -1345,15 +1347,15 @@ protected function createVariableBlock(): string
13451347
/**
13461348
* @throws Exception
13471349
*/
1348-
protected function handleSlots(DOMElement $node): void
1350+
protected function handleSlots(DOMElement $node, int $level): void
13491351
{
13501352
if ($node->nodeName !== 'slot') {
13511353
return;
13521354
}
13531355

13541356
if ($node->hasChildNodes()) {
13551357
foreach ($node->childNodes as $childNode) {
1356-
$this->convertNode($childNode);
1358+
$this->convertNode($childNode, $level + 1);
13571359
}
13581360
}
13591361

@@ -1528,4 +1530,9 @@ private function replaceAttributeWithIfConditionPlaceholders(string $html): stri
15281530

15291531
return $html;
15301532
}
1533+
1534+
private function camelCaseToKebabCase(string $text): string
1535+
{
1536+
return strtolower(preg_replace('/([a-z])([A-Z])/', '\1-\2', $text));
1537+
}
15311538
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<div class="{{ class|default('') }}" style="{{ style|default('') }}">
2+
<span style="color: #ff0000">
3+
test
4+
</span>
5+
<span style="{{ 'color: #ff0000' }};">
6+
test
7+
</span>
8+
<span style="{{ 'color:' ~ '#ff0000' }};">
9+
test
10+
</span>
11+
<span style="{{ 'width:' ~ w ~ 'px' }}; {{ ' height:' ~ h ~ 'px' }};">
12+
test
13+
</span>
14+
<span style="{{ isSomething ? 'color: red' }}; {{ not isSomething ? 'color: blue' }};">
15+
test
16+
</span>
17+
<span style="{{ isSomething ? 'display: block;' : 'display: inline;' }};">
18+
test
19+
</span>
20+
<span style="{{ 'background-color:' ~ color }};">
21+
test
22+
</span>
23+
<span style="{{ 'background-color:' ~ color }};">
24+
test
25+
</span>
26+
</div>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<template>
2+
<div>
3+
<span style="color: #ff0000">
4+
test
5+
</span>
6+
<span :style="'color: #ff0000'">
7+
test
8+
</span>
9+
<span :style="{color: '#ff0000'}">
10+
test
11+
</span>
12+
<span :style="{width: w + 'px', height: h + 'px'}">
13+
test
14+
</span>
15+
<span :style="{'color: red': isSomething, 'color: blue': !isSomething}">
16+
test
17+
</span>
18+
<span :style="isSomething ? 'display: block;' : 'display: inline;'">
19+
test
20+
</span>
21+
<span :style="'background-color:' + color">
22+
test
23+
</span>
24+
<span :style="{backgroundColor: color}">
25+
test
26+
</span>
27+
</div>
28+
</template>

0 commit comments

Comments
 (0)