Skip to content

Commit bfe1487

Browse files
committed
[TwigComponent] Allow @ prefix in nested attributes
Fix this was not compiled ```twig <twig foo:@bar="baz" /> ```
1 parent f9bbaf5 commit bfe1487

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/TwigComponent/src/Twig/TwigPreLexer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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();

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)