Skip to content

Commit 01697e7

Browse files
authored
Merge pull request #69 from tronsha/feature/remove-attribute-if-condition-false
Add twig config block for attributes with if
2 parents 6a6977e + 3e5002c commit 01697e7

File tree

6 files changed

+121
-6
lines changed

6 files changed

+121
-6
lines changed

src/Compiler.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ class Compiler
108108
/**
109109
* @var string[]
110110
*/
111-
protected $ifAttributes = ['checked', 'selected', 'disabled'];
112-
113-
111+
protected $attributesWithIf = ['checked', 'selected', 'disabled'];
114112

115113
/**
116114
* Compiler constructor.
@@ -161,6 +159,15 @@ public function convert(): string
161159

162160
$twigBlocks = $this->document->getElementsByTagName('twig');
163161

162+
$twigConfigBlocks = $this->document->getElementsByTagName('twig-config');
163+
164+
if ($twigConfigBlocks->length) {
165+
foreach ($twigConfigBlocks as $twigConfigBlock) {
166+
/* @var DOMText $twigConfigBlock */
167+
$this->handleTwigConfig(trim($twigConfigBlock->textContent));
168+
}
169+
}
170+
164171
if ($scriptElement) {
165172
$this->registerProperties($scriptElement);
166173
$this->insertDefaultValues();
@@ -219,6 +226,16 @@ public function convert(): string
219226
return $html;
220227
}
221228

229+
private function handleTwigConfig(string $twigConfig): void
230+
{
231+
$config = parse_ini_string($twigConfig);
232+
if ($config['attributes-with-if'] ?? false) {
233+
$attributes = explode(',', $config['attributes-with-if']);
234+
$attributes = array_map(function ($item) { return trim($item); }, $attributes);
235+
$this->attributesWithIf = array_merge($this->attributesWithIf, $attributes);
236+
}
237+
}
238+
222239
/**
223240
* @throws Exception
224241
*/
@@ -528,7 +545,7 @@ private function handleAttributeBinding(DOMElement $node): void
528545

529546
$dynamicValues = $this->handleBinding($value, $name, $node);
530547

531-
$addIfAroundAttribute = in_array($name, $this->ifAttributes);
548+
$addIfAroundAttribute = in_array($name, $this->attributesWithIf);
532549

533550
/* @see https://gitlab.gnome.org/GNOME/libxml2/-/blob/LIBXML2.6.32/HTMLtree.c#L657 */
534551
switch ($name) {
@@ -1299,8 +1316,8 @@ private function replaceAttributeWithIfConditionPlaceholders(string $html): stri
12991316
if (preg_match_all($pattern, $html, $matches, PREG_SET_ORDER)) {
13001317
foreach ($matches as $match) {
13011318
$name = $match[1];
1302-
$value = base64_decode($match[2]);
1303-
$condition = trim(str_replace(['__DOUBLE_CURLY_OPEN__', '__DOUBLE_CURLY_CLOSE__'], '', $value));
1319+
$value = $this->replacePlaceholders(base64_decode($match[2]));
1320+
$condition = trim(str_replace(['{{', '}}'], '', $value));
13041321
if (in_array($name, ['checked', 'selected', 'disabled'])) {
13051322
$value = $name;
13061323
}

tests/TwigConfigTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Paneon\VueToTwig\Tests;
4+
5+
use Exception;
6+
7+
class TwigConfigTest extends AbstractTestCase
8+
{
9+
/**
10+
* @dataProvider ifProvider
11+
*
12+
* @param mixed $html
13+
* @param mixed $expected
14+
*
15+
* @throws Exception
16+
*/
17+
public function testTwigConfig($html, $expected)
18+
{
19+
$compiler = $this->createCompiler($html);
20+
21+
$actual = $compiler->convert();
22+
23+
$this->assertEqualHtml($expected, $actual);
24+
}
25+
26+
public function ifProvider()
27+
{
28+
return $this->loadFixturesFromDir('twig-config');
29+
}
30+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% set count = count|default(0) %}
2+
<div class="{{ class|default('') }}" style="{{ style|default('') }}">
3+
<div {% if count > 5 %}foo="{{ count > 5 }}"{% endif %}>
4+
Foo
5+
</div>
6+
<div {% if count > 5 %}bar="{{ count > 5 }}"{% endif %}>
7+
Bar
8+
</div>
9+
</div>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<template>
2+
<div>
3+
<div :foo="count > 5">
4+
Foo
5+
</div>
6+
<div :bar="count > 5">
7+
Bar
8+
</div>
9+
</div>
10+
</template>
11+
12+
<twig-config>
13+
attributes-with-if = foo,bar
14+
</twig-config>
15+
16+
<script>
17+
export default {
18+
props: {
19+
count: {
20+
type: Number,
21+
default: 0,
22+
},
23+
},
24+
};
25+
</script>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% set count = count|default(0) %}
2+
<div class="{{ class|default('') }}" style="{{ style|default('') }}">
3+
<div {% if count > 5 %}foo="{{ count > 5 }}"{% endif %}>
4+
Foo
5+
</div>
6+
<div bar="{{ count > 5 }}">
7+
Bar
8+
</div>
9+
</div>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<template>
2+
<div>
3+
<div :foo="count > 5">
4+
Foo
5+
</div>
6+
<div :bar="count > 5">
7+
Bar
8+
</div>
9+
</div>
10+
</template>
11+
12+
<twig-config>
13+
attributes-with-if = foo
14+
</twig-config>
15+
16+
<script>
17+
export default {
18+
props: {
19+
count: {
20+
type: Number,
21+
default: 0,
22+
},
23+
},
24+
};
25+
</script>

0 commit comments

Comments
 (0)