Skip to content

Commit 0e4931f

Browse files
authored
Merge pull request #67 from tronsha/feature/remove-attribute-if-condition-false
Add attribute with if condition
2 parents 8d94673 + be896fe commit 0e4931f

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

src/Compiler.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ class Compiler
105105
*/
106106
protected $includeAttributes = ['class', 'style'];
107107

108+
/**
109+
* @var string[]
110+
*/
111+
protected $ifAttributes = ['checked', 'selected', 'disabled'];
112+
113+
114+
108115
/**
109116
* Compiler constructor.
110117
*/
@@ -192,6 +199,7 @@ public function convert(): string
192199
$html = $this->addVariableBlocks($html);
193200
$html = $this->replacePlaceholders($html);
194201
$html = $this->replaceScopedPlaceholders($html);
202+
$html = $this->replaceAttributeWithIfConditionPlaceholders($html);
195203

196204
$html = preg_replace('/<template>\s*(.*)\s*<\/template>/ism', '$1', $html);
197205
$html = preg_replace('/<\/?template[^>]*?>/i', '', $html);
@@ -513,8 +521,15 @@ private function handleAttributeBinding(DOMElement $node): void
513521
continue;
514522
}
515523

524+
// makes no sense to use this in code, but it must handled.
525+
if ($value === 'false') {
526+
continue;
527+
}
528+
516529
$dynamicValues = $this->handleBinding($value, $name, $node);
517530

531+
$addIfAroundAttribute = in_array($name, $this->ifAttributes);
532+
518533
/* @see https://gitlab.gnome.org/GNOME/libxml2/-/blob/LIBXML2.6.32/HTMLtree.c#L657 */
519534
switch ($name) {
520535
case 'href':
@@ -535,7 +550,14 @@ private function handleAttributeBinding(DOMElement $node): void
535550
break;
536551
}
537552

538-
$node->setAttribute($name, $this->implodeAttributeValue($name, $dynamicValues, $staticValues));
553+
$value = $this->implodeAttributeValue($name, $dynamicValues, $staticValues);
554+
555+
if ($addIfAroundAttribute && $value) {
556+
$value = $name . '|' . base64_encode($value);
557+
$name = '__ATTRIBUTE_WITH_IF_CONDITION__';
558+
}
559+
560+
$node->setAttribute($name, $value);
539561
}
540562
}
541563

@@ -1263,4 +1285,31 @@ private function replaceScopedPlaceholders(string $html): string
12631285

12641286
return $html;
12651287
}
1288+
1289+
/**
1290+
* @throws ReflectionException
1291+
*/
1292+
private function replaceAttributeWithIfConditionPlaceholders(string $html): string
1293+
{
1294+
$pattern = '/__ATTRIBUTE_WITH_IF_CONDITION__="([-a-zA-Z0-9]+)\|([a-zA-Z0-9+=]+)"/';
1295+
if (preg_match_all($pattern, $html, $matches, PREG_SET_ORDER)) {
1296+
foreach ($matches as $match) {
1297+
$name = $match[1];
1298+
$value = base64_decode($match[2]);
1299+
$condition = trim(str_replace(['__DOUBLE_CURLY_OPEN__', '__DOUBLE_CURLY_CLOSE__'], '', $value));
1300+
$value = $this->replacePlaceholders($value);
1301+
$condition = $this->replacePlaceholders($condition);
1302+
if (in_array($name, ['checked', 'selected', 'disabled'])) {
1303+
$value = $name;
1304+
}
1305+
$html = str_replace(
1306+
$match[0],
1307+
'{% if ' . $condition . ' %}' . $name . '="' . $value . '"{% endif %}',
1308+
$html
1309+
);
1310+
}
1311+
}
1312+
1313+
return $html;
1314+
}
12661315
}

tests/AttributeIfTrueTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Paneon\VueToTwig\Tests;
4+
5+
use Exception;
6+
7+
class IfAttributesTest extends AbstractTestCase
8+
{
9+
/**
10+
* @dataProvider dataProvider
11+
*
12+
* @throws Exception
13+
*/
14+
public function testAttributeIfTrue($template, $expected)
15+
{
16+
$compiler = $this->createCompiler($template);
17+
18+
$actual = $compiler->convert();
19+
20+
$this->assertEqualHtml($expected, $actual);
21+
}
22+
23+
/**
24+
* @return array
25+
*/
26+
public function dataProvider()
27+
{
28+
return [
29+
[
30+
'<template><form><input type="checkbox" :checked="false"></form></template>',
31+
'<form class="{{ class|default(\'\') }}" style="{{ style|default(\'\') }}"><input type="checkbox"></form>',
32+
],
33+
[
34+
'<template><form><input type="checkbox" :checked="foo"></form></template>',
35+
'<form class="{{ class|default(\'\') }}" style="{{ style|default(\'\') }}"><input type="checkbox" {% if foo %}checked="checked"{% endif %}></form>',
36+
],
37+
];
38+
}
39+
}

tests/VuePreTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class VuePreTest extends AbstractTestCase
88
{
99
/**
1010
* @dataProvider dataProvider
11-
1211
*
1312
* @throws Exception
1413
*/

0 commit comments

Comments
 (0)