Skip to content

Commit

Permalink
[TwigComponent] Fix usage of {% embed %} with {% block %} in <twig:> …
Browse files Browse the repository at this point in the history
…components, close symfony#952
  • Loading branch information
Kocal committed Jan 16, 2024
1 parent 3c5d7a4 commit 06a5bf5
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/TwigComponent/src/Twig/TwigPreLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\UX\TwigComponent\Twig;

use Twig\Error\SyntaxError;
use Twig\Lexer;

/**
* Rewrites <twig:component> syntaxes to {% component %} syntaxes.
Expand Down Expand Up @@ -42,6 +43,8 @@ public function preLexComponents(string $input): string
$this->length = \strlen($input);
$output = '';

$inTwigEmbed = false;

while ($this->position < $this->length) {
// ignore content inside verbatim block #947
if ($this->consume('{% verbatim %}')) {
Expand All @@ -67,24 +70,54 @@ public function preLexComponents(string $input): string
}
}

if ($this->consume('{% embed')) {
$inTwigEmbed = true;
$output .= '{% embed';
$output .= $this->consumeUntil('%}');

continue;
}

if ($this->consume('{% endembed %}')) {
$inTwigEmbed = false;
$output .= '{% endembed %}';

continue;
}

$isTwigHtmlOpening = $this->consume('<twig:');
$isTraditionalBlockOpening = false;

if ($isTwigHtmlOpening || (0 !== \count($this->currentComponents) && $isTraditionalBlockOpening = $this->consume('{% block'))) {
$componentName = $isTraditionalBlockOpening ? 'block' : $this->consumeComponentName();

if ('block' === $componentName) {
// if we're already inside the "default" block, let's close it
if (!empty($this->currentComponents) && $this->currentComponents[\count($this->currentComponents) - 1]['hasDefaultBlock']) {
if (!empty($this->currentComponents) && $this->currentComponents[\count($this->currentComponents) - 1]['hasDefaultBlock'] && !$inTwigEmbed) {
$output .= '{% endblock %}';

$this->currentComponents[\count($this->currentComponents) - 1]['hasDefaultBlock'] = false;
}

if ($isTraditionalBlockOpening) {
$prevPosition = $this->position;

// add what we've consumed so far
$output .= '{% block';
$output .= $this->consumeUntil('%}');
$output .= $this->consumeUntilEndBlock();

// If the last consumed string does not match the Twig's block name regex, we assume the block is self-closing
$isBlockSelfClosing = '' !== preg_replace(
Lexer::REGEX_NAME,
'',
trim(mb_substr($this->input, $prevPosition, $this->position - $prevPosition)))
;

if ($isBlockSelfClosing && $this->consume('%}')) {
$output .= '%}';
} else {
$output .= $this->consumeUntilEndBlock();
}

continue;
}
Expand Down Expand Up @@ -420,8 +453,8 @@ private function consumeUntilEndBlock(): string

if (!$inComment && '{% endblock %}' === substr($this->input, $this->position, 14)) {
if (1 === $depth) {
// in this case, we want to advanced ALL the way beyond the endblock
$this->position += 14;
// in this case, we want to advance ALL the way beyond the endblock
$this->position += 14 /* strlen('{% endblock %}') */;
break;
} else {
--$depth;
Expand Down
39 changes: 39 additions & 0 deletions src/TwigComponent/tests/Unit/TwigPreLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,45 @@ public static function getLexTests(): iterable
EOF
];

yield 'component_where_entire_default_block_is_twig_embed' => [
<<<EOF
<twig:Alert>
<p>
{% embed "my_embed.html.twig" with { foo: 'bar' } %}{% endembed %}
</p>
</twig:Alert>
EOF,
<<<EOF
{% component 'Alert' %}
{% block content %}<p>
{% embed "my_embed.html.twig" with { foo: 'bar' } %}{% endembed %}
</p>
{% endblock %}{% endcomponent %}
EOF,
];
yield 'component_where_entire_default_block_is_twig_embed_with_blocks' => [
<<<EOF
<twig:Alert>
<p>
{% embed "my_embed.html.twig" %}
{% block my_embed_block_1 "foo" %}
{% block my_embed_block_2 %}bar{% endblock %}
{% endembed %}
</p>
</twig:Alert>
EOF,
<<<EOF
{% component 'Alert' %}
{% block content %}<p>
{% embed "my_embed.html.twig" %}
{% block my_embed_block_1 "foo" %}
{% block my_embed_block_2 %}bar{% endblock %}
{% endembed %}
</p>
{% endblock %}{% endcomponent %}
EOF,
];

yield 'string_inside_of_twig_code_not_escaped' => [
<<<EOF
<twig:TabbedCodeBlocks :files="[
Expand Down

0 comments on commit 06a5bf5

Please sign in to comment.