Skip to content

Commit a7ce18f

Browse files
authored
Merge pull request #39 from tronsha/feature/v-html
Add v-html support
2 parents ce8247d + 1693a4a commit a7ce18f

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

src/Compiler.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public function convertNode(DOMNode $node, int $level = 0): DOMNode
149149
$this->replaceShowWithIf($node);
150150
$this->handleIf($node);
151151
$this->handleFor($node);
152+
$this->handleHtml($node);
152153
$this->stripEventHandlers($node);
153154
//$this->handleRawHtml($node, $data);
154155
$this->handleDefaultSlot($node);
@@ -521,6 +522,21 @@ private function handleFor(DOMElement $node)
521522
$node->removeAttribute('v-for');
522523
}
523524

525+
private function handleHtml(DOMElement $node)
526+
{
527+
if (!$node->hasAttribute('v-html')) {
528+
return;
529+
}
530+
531+
$html = $node->getAttribute('v-html');
532+
$node->removeAttribute('v-html');
533+
while ($node->hasChildNodes()) {
534+
$node->removeChild($node->firstChild);
535+
}
536+
$node->appendChild(new DOMText('{{ ' . $html . '|raw }}'));
537+
}
538+
539+
524540
protected function addDefaultsToVariable($varName, $string): string
525541
{
526542
if (!in_array($varName, array_keys($this->properties))) {

tests/VueHtmlTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Paneon\VueToTwig\Tests;
4+
5+
class VueHtmlTest extends AbstractTestCase
6+
{
7+
public function testHtml()
8+
{
9+
$component = file_get_contents(__DIR__.'/fixtures/vue-html/html.vue');
10+
$expected = file_get_contents(__DIR__.'/fixtures/vue-html/html.twig');
11+
12+
if(!$component){
13+
self::fail('Component not found.');
14+
return;
15+
}
16+
17+
$compiler = $this->createCompiler($component);
18+
19+
$actual = $compiler->convert();
20+
21+
$this->assertEqualHtml($expected, $actual);
22+
}
23+
}

tests/fixtures/vue-html/html.twig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{% set rawHtml = '<strong>text</strong>' %}
2+
<div class="{{class|default('')}}">
3+
<span>
4+
{{ rawHtml|raw }}
5+
</span>
6+
</div>

tests/fixtures/vue-html/html.vue

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<template>
2+
<div>
3+
<span v-html="rawHtml" />
4+
</div>
5+
</template>
6+
7+
<twig>
8+
{% set rawHtml = '&lt;strong&gt;text&lt;/strong&gt;' %}
9+
</twig>
10+
11+
<script>
12+
export default {
13+
computed: {
14+
rawHtml(){
15+
return '<strong>text</strong>';
16+
},
17+
}
18+
}
19+
</script>

0 commit comments

Comments
 (0)