Skip to content

Commit fa3f0c6

Browse files
committed
bug #1966 [TwigComponent] Allow @ prefix in nested attributes (smnandre)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [TwigComponent] Allow @ prefix in nested attributes This was not compiled ```twig <twig foo:`@bar`="baz" /> ``` Should fix #1960 Commits ------- 829c84f [TwigComponent] Allow @ prefix in nested attributes
2 parents d0bb978 + 829c84f commit fa3f0c6

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/TwigComponent/src/Twig/TwigPreLexer.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function preLexComponents(string $input): string
157157
$lastComponentName = $lastComponent['name'];
158158

159159
if ($closingComponentName !== $lastComponentName) {
160-
throw new SyntaxError("Expected closing tag '</twig:{$lastComponentName}>' but found '</twig:{$closingComponentName}>'", $this->line);
160+
throw new SyntaxError("Expected closing tag '</twig:{$lastComponentName}>' but found '</twig:{$closingComponentName}>'.", $this->line);
161161
}
162162

163163
// we've reached the end of this component. If we're inside the
@@ -260,7 +260,7 @@ private function consumeAttributes(string $componentName): string
260260
throw new SyntaxError(\sprintf('Expected "=" after ":%s" when parsing the "<twig:%s" syntax.', $key, $componentName), $this->line);
261261
}
262262

263-
$attributes[] = \sprintf('%s: true', preg_match('/[-:]/', $key) ? "'$key'" : $key);
263+
$attributes[] = \sprintf('%s: true', preg_match('/[-:@]/', $key) ? "'$key'" : $key);
264264
$this->consumeWhitespace();
265265
continue;
266266
}
@@ -275,7 +275,7 @@ private function consumeAttributes(string $componentName): string
275275
$attributeValue = $this->consumeAttributeValue($quote);
276276
}
277277

278-
$attributes[] = \sprintf('%s: %s', preg_match('/[-:]/', $key) ? "'$key'" : $key, '' === $attributeValue ? "''" : $attributeValue);
278+
$attributes[] = \sprintf('%s: %s', preg_match('/[-:@]/', $key) ? "'$key'" : $key, '' === $attributeValue ? "''" : $attributeValue);
279279

280280
$this->expectAndConsumeChar($quote);
281281
$this->consumeWhitespace();
@@ -303,13 +303,13 @@ private function consume(string $string): bool
303303
private function consumeChar($validChars = null): string
304304
{
305305
if ($this->position >= $this->length) {
306-
throw new SyntaxError('Unexpected end of input', $this->line);
306+
throw new SyntaxError('Unexpected end of input.', $this->line);
307307
}
308308

309309
$char = $this->input[$this->position];
310310

311311
if (null !== $validChars && !\in_array($char, (array) $validChars, true)) {
312-
throw new SyntaxError('Expected one of ['.implode('', (array) $validChars)."] but found '{$char}'.", $this->line);
312+
throw new SyntaxError('Expected one of [.'.implode('', (array) $validChars)."] but found '{$char}'.", $this->line);
313313
}
314314

315315
++$this->position;
@@ -358,7 +358,7 @@ private function consumeWhitespace(): void
358358
private function expectAndConsumeChar(string $char): void
359359
{
360360
if (1 !== \strlen($char)) {
361-
throw new \InvalidArgumentException('Expected a single character');
361+
throw new \InvalidArgumentException('Expected a single character.');
362362
}
363363

364364
if ($this->position >= $this->length) {

src/TwigComponent/tests/Unit/TwigPreLexerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,16 @@ public static function getLexTests(): iterable
311311
'{% component \'foobar\' with { \'my:attribute\': \'yo\' } %}{% endcomponent %}',
312312
];
313313

314+
yield 'component_with_@_attribute' => [
315+
'<twig:foobar @attribute="yo"></twig:foobar>',
316+
'{% component \'foobar\' with { \'@attribute\': \'yo\' } %}{% endcomponent %}',
317+
];
318+
319+
yield 'component_with_nested_@_attribute' => [
320+
'<twig:foobar foo:@attribute="yo"></twig:foobar>',
321+
'{% component \'foobar\' with { \'foo:@attribute\': \'yo\' } %}{% endcomponent %}',
322+
];
323+
314324
yield 'component_with_truthy_attribute' => [
315325
'<twig:foobar data-turbo-stream></twig:foobar>',
316326
'{% component \'foobar\' with { \'data-turbo-stream\': true } %}{% endcomponent %}',

0 commit comments

Comments
 (0)