Skip to content

Commit 79a404d

Browse files
committed
bug #817 Fix error with nested component (matheo, WebMamba)
This PR was merged into the 2.x branch. Discussion ---------- Fix error with nested component | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | Fix #805 | License | MIT Commits ------- c340d29 fix error for nested component
2 parents 298f75b + c340d29 commit 79a404d

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/TwigComponent/src/Twig/TwigPreLexer.php

+28-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ private function consumeBlock(string $componentName): string
314314
if (!$this->doesStringEventuallyExist($closingTag)) {
315315
throw new SyntaxError("Expected closing tag '{$closingTag}' for block '{$blockName}'.", $this->line);
316316
}
317-
$blockContents = $this->consumeUntil($closingTag);
317+
$blockContents = $this->consumeUntilEndBlock();
318318

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

328+
private function consumeUntilEndBlock(): string
329+
{
330+
$start = $this->position;
331+
332+
$depth = 1;
333+
while ($this->position < $this->length) {
334+
if ('</twig:block' === substr($this->input, $this->position, 12)) {
335+
if (1 === $depth) {
336+
break;
337+
} else {
338+
--$depth;
339+
}
340+
}
341+
342+
if ('<twig:block' === substr($this->input, $this->position, 11)) {
343+
++$depth;
344+
}
345+
346+
if ("\n" === $this->input[$this->position]) {
347+
++$this->line;
348+
}
349+
++$this->position;
350+
}
351+
352+
return substr($this->input, $start, $this->position - $start);
353+
}
354+
328355
private function doesStringEventuallyExist(string $needle): bool
329356
{
330357
$remainingString = substr($this->input, $this->position);

src/TwigComponent/tests/Unit/TwigPreLexerTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,9 @@ public function getLexTests(): iterable
9292
'<twig:foo.bar></twig:foo.bar>',
9393
'{% component \'foo.bar\' %}{% endcomponent %}',
9494
];
95+
yield 'nested_component_2_levels' => [
96+
'<twig:foo><twig:block name="child"><twig:bar><twig:block name="message">Hello World!</twig:block></twig:bar></twig:block></twig:foo>',
97+
'{% component \'foo\' %}{% block child %}{% component \'bar\' %}{% block message %}Hello World!{% endblock %}{% endcomponent %}{% endblock %}{% endcomponent %}',
98+
];
9599
}
96100
}

0 commit comments

Comments
 (0)