Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ Compile vue files to twig templates with PHP
|Directive|Implemented|
|---------|:---------:|
|v-text||
|v-html||
|v-html|:white_check_mark:|
|v-show|:white_check_mark:|
|v-if|:white_check_mark:|
|v-else|:white_check_mark:|
|v-else-if|:white_check_mark:|
|v-for|:white_check_mark:|
|v-on|:white_check_mark:|
|v-bind|partially working|
|v-bind:style|partially working|
|v-bind:class|partially working|
|v-bind:style|:white_check_mark:|
|v-bind:class|:white_check_mark:|
|v-model||
|v-pre||
|v-cloak||
Expand Down
26 changes: 26 additions & 0 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ public function convertNode(DOMNode $node, int $level = 0): DOMNode
$this->replaceShowWithIf($node);
$this->handleIf($node);
$this->handleFor($node);
$this->handleHtml($node);
$this->stripEventHandlers($node);
//$this->handleRawHtml($node, $data);
$this->handleDefaultSlot($node);
$this->cleanupAttributes($node);
}

/*
Expand Down Expand Up @@ -408,6 +410,15 @@ protected function handleTextNode(DOMText $node)
return $node;
}

private function cleanupAttributes(DOMElement $node) {
if ($node->hasAttribute('ref')) {
$node->removeAttribute('ref');
}
if ($node->hasAttribute(':ref')) {
$node->removeAttribute(':ref');
}
}

private function handleIf(DOMElement $node): void
{
if (!$node->hasAttribute('v-if') &&
Expand Down Expand Up @@ -521,6 +532,21 @@ private function handleFor(DOMElement $node)
$node->removeAttribute('v-for');
}

private function handleHtml(DOMElement $node)
{
if (!$node->hasAttribute('v-html')) {
return;
}

$html = $node->getAttribute('v-html');
$node->removeAttribute('v-html');
while ($node->hasChildNodes()) {
$node->removeChild($node->firstChild);
}
$node->appendChild(new DOMText('{{ ' . $html . '|raw }}'));
}


protected function addDefaultsToVariable($varName, $string): string
{
if (!in_array($varName, array_keys($this->properties))) {
Expand Down
19 changes: 19 additions & 0 deletions tests/CleanupAttributesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Paneon\VueToTwig\Tests;

class CleanupAttributesTest extends AbstractTestCase
{
public function testCleanupAttributes()
{
$vueTemplate = '<template><div ref="reference">dummy</div></template>';

$expected = '<div class="{{class|default(\'\')}}">dummy</div>';

$compiler = $this->createCompiler($vueTemplate);

$actual = $compiler->convert();

$this->assertEqualHtml($expected, $actual);
}
}
23 changes: 23 additions & 0 deletions tests/VueHtmlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Paneon\VueToTwig\Tests;

class VueHtmlTest extends AbstractTestCase
{
public function testHtml()
{
$component = file_get_contents(__DIR__.'/fixtures/vue-html/html.vue');
$expected = file_get_contents(__DIR__.'/fixtures/vue-html/html.twig');

if(!$component){
self::fail('Component not found.');
return;
}

$compiler = $this->createCompiler($component);

$actual = $compiler->convert();

$this->assertEqualHtml($expected, $actual);
}
}
6 changes: 6 additions & 0 deletions tests/fixtures/vue-html/html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% set rawHtml = '<strong>text</strong>' %}
<div class="{{class|default('')}}">
<span>
{{ rawHtml|raw }}
</span>
</div>
19 changes: 19 additions & 0 deletions tests/fixtures/vue-html/html.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<template>
<div>
<span v-html="rawHtml" />
</div>
</template>

<twig>
{% set rawHtml = '&lt;strong&gt;text&lt;/strong&gt;' %}
</twig>

<script>
export default {
computed: {
rawHtml(){
return '<strong>text</strong>';
},
}
}
</script>