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
71 changes: 69 additions & 2 deletions src/Utils/TwigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ private function refactorConditionPart($condition) {
$condition = str_replace('.length', '|length', $condition);
$condition = str_replace('.trim', '|trim', $condition);

// $condition = $this->convertConcat($condition);

foreach (Replacements::getConstants() as $constant => $value) {
$condition = str_replace($value, Replacements::getSanitizedConstant($constant), $condition);
}
Expand All @@ -223,9 +225,74 @@ private function refactorConditionPart($condition) {

public function refactorTextNode(string $content): string
{
$content = str_replace('.length', '|length', $content);
$content = str_replace('.trim', '|trim', $content);
$refactoredContent = '';
$charsCount = mb_strlen($content, 'UTF-8');
$open = false;
$lastChar = null;
$quoteChar = null;
$buffer = '';

for ($i = 0; $i < $charsCount; $i++) {
$char = mb_substr($content, $i, 1, 'UTF-8');
if ($open === false) {
$refactoredContent .= $char;
if ($char === '{' && $lastChar === '{') {
$open = true;
}
} else {
$buffer .= $char;
if ($quoteChar === null && ($char === '"' || $char === '\'')) {
$quoteChar = $char;
} elseif ($quoteChar === $char && $lastChar !== '\\') {
$quoteChar = null;
}
if ($quoteChar === null && $char === '}' && $lastChar === '}') {
$open = false;
$buffer = $this->convertTemplateString(trim($buffer, '}'));
$refactoredContent .= $this->refactorCondition($buffer) . '}}';
$buffer = '';
}
}
$lastChar = $char;
}

return $refactoredContent;
}

private function convertConcat($content) {
if (preg_match_all('/(\S*)(\s*\+\s*(\S+))+/', $content, $matches, PREG_SET_ORDER )) {
foreach ($matches as $match) {
$parts = explode('+', $match[0]);
$lastPart = null;
$convertedContent = '';
foreach ($parts as $part) {
$part = trim($part);
if ($lastPart !== null) {
if (is_numeric($lastPart) && is_numeric($part)) {
$convertedContent .= ' + ' . $part;
} else {
$convertedContent .= ' ~ ' . $part;
}
} else {
$convertedContent = $part;
}
$lastPart = $part;
}
$content = str_replace($match[0], $convertedContent, $content);
}
}

return $content;
}

private function convertTemplateString($content) {
if (preg_match_all('/\`([^\`]+)\`/', $content, $matches, PREG_SET_ORDER )) {
foreach ($matches as $match) {
$match[1] = str_replace('${', '\' ~ ', $match[1]);
$match[1] = str_replace('}', ' ~ \'', $match[1]);
$content = str_replace($match[0], '\'' . $match[1] . '\'', $content);
}
}
return $content;
}

Expand Down
67 changes: 67 additions & 0 deletions tests/TextNodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Paneon\VueToTwig\Tests;

class TextNodeTest extends AbstractTestCase
{
public function testTextNode()
{
$html = '<template><div>foo {{ bar.trim }}</div></template>';
$expected = '<div class="{{class|default(\'\')}}">foo {{ bar|trim }}</div>';

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

$actual = $compiler->convert();

$this->assertEqualHtml($expected, $actual);
}

public function testTextNodeNoReplace()
{
$html = '<template><div>foo.trim {{ \'foo === bar\' }}</div></template>';
$expected = '<div class="{{class|default(\'\')}}">foo.trim {{ \'foo === bar\' }}</div>';

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

$actual = $compiler->convert();

$this->assertEqualHtml($expected, $actual);
}

public function testTextNodeDontCloseInQuote()
{
$html = '<template><div>{{ \'}}\' || foo.length }}</div></template>';
$expected = '<div class="{{class|default(\'\')}}">{{ \'}}\' or foo|length }}</div>';

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

$actual = $compiler->convert();

$this->assertEqualHtml($expected, $actual);
}

public function testTextNodeWithTemplateString()
{
$html = '<template><div>{{ `Var = ${var}` }}</div></template>';
$expected = '<div class="{{class|default(\'\')}}">{{ \'Var = \' ~ var ~ \'\' }}</div>';

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

$actual = $compiler->convert();

$this->assertEqualHtml($expected, $actual);
}


public function testTextNodeNumbers()
{
$html = '<template><div>{{ 1 + 1 }}</div></template>';
$expected = '<div class="{{class|default(\'\')}}">{{ 1 + 1 }}</div>';

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

$actual = $compiler->convert();

$this->assertEqualHtml($expected, $actual);
}
}