Skip to content

Fix error with nested component #817

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
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
29 changes: 28 additions & 1 deletion src/TwigComponent/src/Twig/TwigPreLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ private function consumeBlock(string $componentName): string
if (!$this->doesStringEventuallyExist($closingTag)) {
throw new SyntaxError("Expected closing tag '{$closingTag}' for block '{$blockName}'.", $this->line);
}
$blockContents = $this->consumeUntil($closingTag);
$blockContents = $this->consumeUntilEndBlock();

$subLexer = new self($this->line);
$output .= $subLexer->preLexComponents($blockContents);
Expand All @@ -325,6 +325,33 @@ private function consumeBlock(string $componentName): string
return $output;
}

private function consumeUntilEndBlock(): string
{
$start = $this->position;

$depth = 1;
while ($this->position < $this->length) {
if ('</twig:block' === substr($this->input, $this->position, 12)) {
if (1 === $depth) {
break;
} else {
--$depth;
}
}

if ('<twig:block' === substr($this->input, $this->position, 11)) {
++$depth;
}

if ("\n" === $this->input[$this->position]) {
++$this->line;
}
++$this->position;
}

return substr($this->input, $start, $this->position - $start);
}

private function doesStringEventuallyExist(string $needle): bool
{
$remainingString = substr($this->input, $this->position);
Expand Down
4 changes: 4 additions & 0 deletions src/TwigComponent/tests/Unit/TwigPreLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,9 @@ public function getLexTests(): iterable
'<twig:foo.bar></twig:foo.bar>',
'{% component \'foo.bar\' %}{% endcomponent %}',
];
yield 'nested_component_2_levels' => [
'<twig:foo><twig:block name="child"><twig:bar><twig:block name="message">Hello World!</twig:block></twig:bar></twig:block></twig:foo>',
'{% component \'foo\' %}{% block child %}{% component \'bar\' %}{% block message %}Hello World!{% endblock %}{% endcomponent %}{% endblock %}{% endcomponent %}',
];
}
}