Skip to content
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

[TwigComponent] Allow @ prefix in nested attributes #1966

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
12 changes: 6 additions & 6 deletions src/TwigComponent/src/Twig/TwigPreLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function preLexComponents(string $input): string
$lastComponentName = $lastComponent['name'];

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

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

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

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

$this->expectAndConsumeChar($quote);
$this->consumeWhitespace();
Expand Down Expand Up @@ -303,13 +303,13 @@ private function consume(string $string): bool
private function consumeChar($validChars = null): string
{
if ($this->position >= $this->length) {
throw new SyntaxError('Unexpected end of input', $this->line);
throw new SyntaxError('Unexpected end of input.', $this->line);
}

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

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

++$this->position;
Expand Down Expand Up @@ -358,7 +358,7 @@ private function consumeWhitespace(): void
private function expectAndConsumeChar(string $char): void
{
if (1 !== \strlen($char)) {
throw new \InvalidArgumentException('Expected a single character');
throw new \InvalidArgumentException('Expected a single character.');
}

if ($this->position >= $this->length) {
Expand Down
10 changes: 10 additions & 0 deletions src/TwigComponent/tests/Unit/TwigPreLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,16 @@ public static function getLexTests(): iterable
'{% component \'foobar\' with { \'my:attribute\': \'yo\' } %}{% endcomponent %}',
];

yield 'component_with_@_attribute' => [
'<twig:foobar @attribute="yo"></twig:foobar>',
'{% component \'foobar\' with { \'@attribute\': \'yo\' } %}{% endcomponent %}',
];

yield 'component_with_nested_@_attribute' => [
'<twig:foobar foo:@attribute="yo"></twig:foobar>',
'{% component \'foobar\' with { \'foo:@attribute\': \'yo\' } %}{% endcomponent %}',
];

yield 'component_with_truthy_attribute' => [
'<twig:foobar data-turbo-stream></twig:foobar>',
'{% component \'foobar\' with { \'data-turbo-stream\': true } %}{% endcomponent %}',
Expand Down