Skip to content

Commit a57825e

Browse files
committed
Allow standard mixins to works without slot
1 parent edb5684 commit a57825e

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

src/Phug/Component/ComponentExtension.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Phug\CompilerEvent;
1010
use Phug\Formatter\Element\KeywordElement;
1111
use Phug\Formatter\Element\MixinElement;
12+
use Phug\Parser\NodeInterface as ParserNodeInterface;
1213
use Phug\Parser\Node\CodeNode;
1314
use Phug\Parser\Node\KeywordNode;
1415
use Phug\Parser\Node\MixinCallNode;
@@ -84,20 +85,32 @@ public function getKeywords(): array
8485
];
8586
}
8687

88+
protected function getCodeNode(NodeInterface $linkedNode, ParserNodeInterface $parentNode = null, $value = null, array $children = null)
89+
{
90+
$code = new CodeNode($linkedNode->getToken(), null, $linkedNode->getLevel(), $parentNode, $children);
91+
92+
if ($value !== null) {
93+
$code->setValue($value);
94+
}
95+
96+
return $code;
97+
}
98+
8799
public function handleNodeEvent(NodeEvent $event): void
88100
{
89101
$call = $event->getNode();
90102

91103
if ($call instanceof MixinCallNode) {
92104
$call->setChildren(array_merge(
93-
[(new CodeNode($call->getToken(), null, $call->getLevel(), $call))->setValue('$'.static::PUG_SLOT_NAME_VARIABLE.' = null')],
94-
array_map(static function (NodeInterface $node) use ($call) {
105+
[$this->getCodeNode($call, $call, '$'.static::PUG_SLOT_NAME_VARIABLE.' = null')],
106+
array_map(function (NodeInterface $node) use ($call) {
95107
if ($node instanceof KeywordNode && $node->getName() === 'slot') {
96108
return $node;
97109
}
98110

99-
return new CodeNode($node->getToken(), null, $node->getLevel(), $call, [
100-
(new TextNode($node->getToken(), null, $node->getLevel()))->setValue('if ($'.static::PUG_SLOT_NAME_VARIABLE.' === "__main__")'),
111+
return $this->getCodeNode($node, $call, null, [
112+
(new TextNode($node->getToken(), null, $node->getLevel()))->setValue('if (!isset($'.static::PUG_SLOT_NAME_VARIABLE.') || $'.static::PUG_SLOT_NAME_VARIABLE.' === "__main__")'),
113+
$this->getCodeNode($node, null, '// main slot'),
101114
$node,
102115
]);
103116
}, $call->getChildren())

tests/Phug/Component/ComponentExtensionTest.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ protected function getValue($object)
233233

234234
/**
235235
* @covers ::handleNodeEvent
236+
* @covers ::getCodeNode
236237
*/
237238
public function testHandleNodeEvent()
238239
{
@@ -252,10 +253,20 @@ public function testHandleNodeEvent()
252253

253254
$this->assertCount(5, $call->getChildren());
254255
$this->assertSame('$pug_component_slot = null', $this->getValue($call->getChildAt(0)));
255-
$this->assertSame('if ($pug_component_slot === "__main__")', $this->getValue($call->getChildAt(1)->getChildAt(0)));
256-
$this->assertSame($children[0], $call->getChildAt(1)->getChildAt(1));
256+
$this->assertSame('if (!isset($pug_component_slot) || $pug_component_slot === "__main__")', $this->getValue($call->getChildAt(1)->getChildAt(0)));
257+
$this->assertSame($children[0], $call->getChildAt(1)->getChildAt(2));
257258
$this->assertSame($children[1], $call->getChildAt(2));
258-
$this->assertSame('if ($pug_component_slot === "__main__")', $this->getValue($call->getChildAt(3)->getChildAt(0)));
259-
$this->assertSame($children[2], $call->getChildAt(3)->getChildAt(1));
259+
$this->assertSame('if (!isset($pug_component_slot) || $pug_component_slot === "__main__")', $this->getValue($call->getChildAt(3)->getChildAt(0)));
260+
$this->assertSame($children[2], $call->getChildAt(3)->getChildAt(2));
261+
}
262+
263+
public function testBasicMixinAreStillFine()
264+
{
265+
$this->assertSame('Hello', $this->renderAndFormat(implode("\n", [
266+
'mixin foo',
267+
' block',
268+
'+foo',
269+
' | Hello',
270+
])));
260271
}
261272
}

0 commit comments

Comments
 (0)