Skip to content

Commit 188859e

Browse files
authored
Merge pull request #97 from tronsha/fix/v-show
Fix v-show with v-if
2 parents dc1af82 + b8632ce commit 188859e

File tree

6 files changed

+50
-1
lines changed

6 files changed

+50
-1
lines changed

src/Compiler.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,14 @@ protected function replacePre(string $html): string
646646
public function replaceShowWithIf(DOMElement $node): void
647647
{
648648
if ($node->hasAttribute('v-show')) {
649-
$node->setAttribute('v-if', $node->getAttribute('v-show'));
649+
if ($node->hasAttribute('v-if')) {
650+
$node->setAttribute(
651+
'v-if',
652+
'(' . $node->getAttribute('v-if') . ') && (' . $node->getAttribute('v-show') . ')'
653+
);
654+
} else {
655+
$node->setAttribute('v-if', $node->getAttribute('v-show'));
656+
}
650657
$node->removeAttribute('v-show');
651658
}
652659
}

tests/VueShowTest.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 VueShowTest extends AbstractTestCase
8+
{
9+
/**
10+
* @dataProvider showProvider
11+
*
12+
* @param mixed $html
13+
* @param mixed $expected
14+
*
15+
* @throws Exception
16+
*/
17+
public function testIf($html, $expected)
18+
{
19+
$compiler = $this->createCompiler($html);
20+
21+
$actual = $compiler->convert();
22+
23+
$this->assertEqualHtml($expected, $actual);
24+
}
25+
26+
public function showProvider()
27+
{
28+
return $this->loadFixturesFromDir('vue-show');
29+
}
30+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% if foo %}
2+
<div class="{{ class|default('') }}" style="{{ style|default('') }}">Text</div>
3+
{% endif %}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div v-show="foo">Text</div>
3+
</template>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% if (foo) and (bar) %}
2+
<div class="{{ class|default('') }}" style="{{ style|default('') }}">Text</div>
3+
{% endif %}

tests/fixtures/vue-show/with-v-if.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div v-if="foo" v-show="bar">Text</div>
3+
</template>

0 commit comments

Comments
 (0)