Skip to content

Commit 860fe93

Browse files
committed
Fix opening of default block inside an open block
1 parent e59691a commit 860fe93

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/TwigComponent/src/Twig/TwigPreLexer.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ public function __construct(int $startingLine = 1)
3030
$this->line = $startingLine;
3131
}
3232

33-
/**
34-
* @param bool $insideOfBlock Are we sub-parsing the content inside a block?
35-
*/
36-
public function preLexComponents(string $input, bool $insideOfBlock = false): string
33+
public function preLexComponents(string $input): string
3734
{
3835
$this->input = $input;
3936
$this->length = \strlen($input);
@@ -79,11 +76,10 @@ public function preLexComponents(string $input, bool $insideOfBlock = false): st
7976
continue;
8077
}
8178

82-
// if we're already inside a component, and we're not inside a block,
79+
// if we're already inside a component,
8380
// *and* we've just found a new component, then we should try to
8481
// open the default block
85-
if (!$insideOfBlock
86-
&& !empty($this->currentComponents)
82+
if (!empty($this->currentComponents)
8783
&& !$this->currentComponents[\count($this->currentComponents) - 1]['hasDefaultBlock']) {
8884
$output .= $this->addDefaultBlock();
8985
}
@@ -365,7 +361,7 @@ private function consumeBlock(string $componentName): string
365361
$blockContents = $this->consumeUntilEndBlock();
366362

367363
$subLexer = new self($this->line);
368-
$output .= $subLexer->preLexComponents($blockContents, true);
364+
$output .= $subLexer->preLexComponents($blockContents);
369365

370366
$this->consume($closingTag);
371367
$output .= '{% endblock %}';

src/TwigComponent/tests/Unit/TwigPreLexerTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,21 @@ public function getLexTests(): iterable
6262
'Hello {% block foo_block %}Foo{% endblock %}{{ component(\'foo\') }}{% block bar_block %}Bar{% endblock %}',
6363
];
6464

65-
yield 'component_with_embedded_component_inside_block' => [
65+
yield 'component_with_component_inside_block' => [
6666
'<twig:foo><twig:block name="foo_block"><twig:bar /></twig:block></twig:foo>',
6767
'{% component \'foo\' %}{% block foo_block %}{{ component(\'bar\') }}{% endblock %}{% endcomponent %}',
6868
];
6969

70+
yield 'component_with_embedded_component_inside_block' => [
71+
'<twig:foo><twig:block name="foo_block"><twig:bar><twig:baz /></twig:bar></twig:block></twig:foo>',
72+
'{% component \'foo\' %}{% block foo_block %}{% component \'bar\' %}{% block content %}{{ component(\'baz\') }}{% endblock %}{% endcomponent %}{% endblock %}{% endcomponent %}',
73+
];
74+
75+
yield 'component_with_embedded_component' => [
76+
'<twig:foo>foo_content<twig:bar><twig:baz /></twig:bar></twig:foo>',
77+
'{% component \'foo\' %}{% block content %}foo_content{% component \'bar\' %}{% block content %}{{ component(\'baz\') }}{% endblock %}{% endcomponent %}{% endblock %}{% endcomponent %}',
78+
];
79+
7080
yield 'attribute_with_no_value' => [
7181
'<twig:foo bar />',
7282
'{{ component(\'foo\', { bar: true }) }}',

0 commit comments

Comments
 (0)