Skip to content

[TwigComponent] Fix opening of default block inside an open twig block #892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2023
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
12 changes: 4 additions & 8 deletions src/TwigComponent/src/Twig/TwigPreLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ public function __construct(int $startingLine = 1)
$this->line = $startingLine;
}

/**
* @param bool $insideOfBlock Are we sub-parsing the content inside a block?
*/
public function preLexComponents(string $input, bool $insideOfBlock = false): string
public function preLexComponents(string $input): string
{
$this->input = $input;
$this->length = \strlen($input);
Expand Down Expand Up @@ -79,11 +76,10 @@ public function preLexComponents(string $input, bool $insideOfBlock = false): st
continue;
}

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

$subLexer = new self($this->line);
$output .= $subLexer->preLexComponents($blockContents, true);
$output .= $subLexer->preLexComponents($blockContents);

$this->consume($closingTag);
$output .= '{% endblock %}';
Expand Down
12 changes: 11 additions & 1 deletion src/TwigComponent/tests/Unit/TwigPreLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,21 @@ public function getLexTests(): iterable
'Hello {% block foo_block %}Foo{% endblock %}{{ component(\'foo\') }}{% block bar_block %}Bar{% endblock %}',
];

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

yield 'component_with_embedded_component_inside_block' => [
'<twig:foo><twig:block name="foo_block"><twig:bar><twig:baz /></twig:bar></twig:block></twig:foo>',
'{% component \'foo\' %}{% block foo_block %}{% component \'bar\' %}{% block content %}{{ component(\'baz\') }}{% endblock %}{% endcomponent %}{% endblock %}{% endcomponent %}',
];

yield 'component_with_embedded_component' => [
'<twig:foo>foo_content<twig:bar><twig:baz /></twig:bar></twig:foo>',
'{% component \'foo\' %}{% block content %}foo_content{% component \'bar\' %}{% block content %}{{ component(\'baz\') }}{% endblock %}{% endcomponent %}{% endblock %}{% endcomponent %}',
];

yield 'attribute_with_no_value' => [
'<twig:foo bar />',
'{{ component(\'foo\', { bar: true }) }}',
Expand Down