Skip to content

Commit 491fff8

Browse files
authored
Merge pull request #45 from Paneon/master
Update main
2 parents 4fa0c2e + 36f1477 commit 491fff8

File tree

8 files changed

+153
-75
lines changed

8 files changed

+153
-75
lines changed

src/Compiler.php

Lines changed: 92 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -315,81 +315,8 @@ public function convertNode(DOMNode $node, int $level = 0): DOMNode
315315
$this->addScopedAttribute($node, $level);
316316
}
317317

318-
// Registered Component
319318
if (in_array($node->nodeName, array_keys($this->components))) {
320-
$matchedComponent = $this->components[$node->nodeName];
321-
$usedComponent = new Component($matchedComponent->getName(), $matchedComponent->getPath());
322-
323-
if ($node->hasAttributes()) {
324-
/** @var DOMAttr $attribute */
325-
foreach ($node->attributes as $attribute) {
326-
if (strpos($attribute->name, 'v-bind:') === 0 || strpos($attribute->name, ':') === 0) {
327-
$name = substr($attribute->name, strpos($attribute->name, ':') + 1);
328-
$value = $attribute->value;
329-
330-
if (substr_count($value, '`')) {
331-
$value = $this->refactorTemplateString($attribute->value);
332-
} else {
333-
$value = $this->builder->refactorCondition($value);
334-
}
335-
336-
$usedComponent->addProperty($name, $value, true);
337-
} else {
338-
$usedComponent->addProperty($attribute->name, '"' . $attribute->value . '"', false);
339-
}
340-
}
341-
}
342-
343-
foreach (iterator_to_array($node->childNodes) as $childNode) {
344-
$this->convertNode($childNode, $level + 1);
345-
}
346-
347-
// Slots
348-
if ($node->hasChildNodes()) {
349-
$this->handleNamedSlotsInclude($node, $usedComponent);
350-
// Slots (Default)
351-
if ($node->hasChildNodes() && !$usedComponent->hasSlot(Slot::SLOT_DEFAULT_NAME)) {
352-
$this->addSlot(Slot::SLOT_DEFAULT_NAME, $node, $usedComponent);
353-
}
354-
} else {
355-
$usedComponent->addEmptyDefaultSlot();
356-
}
357-
358-
// Include Partial
359-
$include = $this->document->createTextNode(
360-
$this->builder->createIncludePartial(
361-
$usedComponent->getPath(),
362-
$this->preparePropertiesForInclude($usedComponent->getProperties(), $level === 1),
363-
$this->vBind
364-
)
365-
);
366-
367-
$node->parentNode->insertBefore($include, $node);
368-
369-
if ($usedComponent->hasSlots()) {
370-
foreach ($usedComponent->getSlots() as $slotName => $slot) {
371-
// Add variable which contains the content (set)
372-
$openSet = $this->document->createTextNode(
373-
$this->builder->createSet($slot->getSlotValueName())
374-
);
375-
$node->parentNode->insertBefore($openSet, $include);
376-
377-
$setContent = $this->document->createTextNode($slot->getSlotContentVariableString());
378-
379-
$node->parentNode->insertBefore($setContent, $include);
380-
381-
// Close variable (endset)
382-
$closeSet = $this->document->createTextNode(
383-
$this->builder->closeSet()
384-
);
385-
$node->parentNode->insertBefore($closeSet, $include);
386-
}
387-
}
388-
389-
// Remove original node
390-
$this->nodeHelper->removeNode($node);
391-
392-
return $node;
319+
return $this->handleComponent($node, $level);
393320
}
394321

395322
if ($node instanceof DOMElement) {
@@ -416,6 +343,83 @@ public function convertNode(DOMNode $node, int $level = 0): DOMNode
416343
return $node;
417344
}
418345

346+
private function handleComponent(DOMNode $node, int $level): DOMNode
347+
{
348+
$matchedComponent = $this->components[$node->nodeName];
349+
$usedComponent = new Component($matchedComponent->getName(), $matchedComponent->getPath());
350+
351+
if ($node->hasAttributes()) {
352+
/** @var DOMAttr $attribute */
353+
foreach ($node->attributes as $attribute) {
354+
if (strpos($attribute->name, 'v-bind:') === 0 || strpos($attribute->name, ':') === 0) {
355+
$name = substr($attribute->name, strpos($attribute->name, ':') + 1);
356+
$value = $attribute->value;
357+
358+
if (substr_count($value, '`')) {
359+
$value = $this->refactorTemplateString($attribute->value);
360+
} else {
361+
$value = $this->builder->refactorCondition($value);
362+
}
363+
364+
$usedComponent->addProperty($name, $value, true);
365+
} else {
366+
$usedComponent->addProperty($attribute->name, '"' . $attribute->value . '"', false);
367+
}
368+
}
369+
}
370+
371+
foreach (iterator_to_array($node->childNodes) as $childNode) {
372+
$this->convertNode($childNode, $level + 1);
373+
}
374+
375+
// Slots
376+
if ($node->hasChildNodes()) {
377+
$this->handleNamedSlotsInclude($node, $usedComponent);
378+
// Slots (Default)
379+
if ($node->hasChildNodes() && !$usedComponent->hasSlot(Slot::SLOT_DEFAULT_NAME)) {
380+
$this->addSlot(Slot::SLOT_DEFAULT_NAME, $node, $usedComponent);
381+
}
382+
} else {
383+
$usedComponent->addEmptyDefaultSlot();
384+
}
385+
386+
// Include Partial
387+
$include = $this->document->createTextNode(
388+
$this->builder->createIncludePartial(
389+
$usedComponent->getPath(),
390+
$this->preparePropertiesForInclude($usedComponent->getProperties(), $level === 1),
391+
$this->vBind
392+
)
393+
);
394+
395+
$node->parentNode->insertBefore($include, $node);
396+
397+
if ($usedComponent->hasSlots()) {
398+
foreach ($usedComponent->getSlots() as $slotName => $slot) {
399+
// Add variable which contains the content (set)
400+
$openSet = $this->document->createTextNode(
401+
$this->builder->createSet($slot->getSlotValueName())
402+
);
403+
$node->parentNode->insertBefore($openSet, $include);
404+
405+
$setContent = $this->document->createTextNode($slot->getSlotContentVariableString());
406+
407+
$node->parentNode->insertBefore($setContent, $include);
408+
409+
// Close variable (endset)
410+
$closeSet = $this->document->createTextNode(
411+
$this->builder->closeSet()
412+
);
413+
$node->parentNode->insertBefore($closeSet, $include);
414+
}
415+
}
416+
417+
// Remove original node
418+
$this->nodeHelper->removeNode($node);
419+
420+
return $node;
421+
}
422+
419423
/**
420424
* @param Property[] $variables
421425
*
@@ -646,7 +650,14 @@ protected function replacePre(string $html): string
646650
public function replaceShowWithIf(DOMElement $node): void
647651
{
648652
if ($node->hasAttribute('v-show')) {
649-
$node->setAttribute('v-if', $node->getAttribute('v-show'));
653+
if ($node->hasAttribute('v-if')) {
654+
$node->setAttribute(
655+
'v-if',
656+
'(' . $node->getAttribute('v-if') . ') && (' . $node->getAttribute('v-show') . ')'
657+
);
658+
} else {
659+
$node->setAttribute('v-if', $node->getAttribute('v-show'));
660+
}
650661
$node->removeAttribute('v-show');
651662
}
652663
}
@@ -1340,6 +1351,12 @@ protected function handleSlots(DOMElement $node): void
13401351
return;
13411352
}
13421353

1354+
if ($node->hasChildNodes()) {
1355+
foreach ($node->childNodes as $childNode) {
1356+
$this->convertNode($childNode);
1357+
}
1358+
}
1359+
13431360
$slotFallback = $node->hasChildNodes() ? $this->innerHtmlOfNode($node) : null;
13441361

13451362
$slotName = Slot::SLOT_PREFIX;

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>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{% set slot_default_fallback %}{% include "/templates/ChildComponent.twig" with { 'slot_default': "", 'class': "", 'style': "" } %}{% endset %}
2+
<div class="{{ class|default('') }}" style="{{ style|default('') }}">
3+
{{ slot_default|default(slot_default_fallback) }}
4+
</div>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<template>
2+
<div>
3+
<slot>
4+
<ChildComponent/>
5+
</slot>
6+
</div>
7+
</template>
8+
<script>
9+
export default {
10+
name: 'component-with-child.vue',
11+
component: {
12+
ChildComponent,
13+
}
14+
};
15+
</script>

0 commit comments

Comments
 (0)