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 : as value separator in {% props %} tag #2488

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 4 additions & 0 deletions src/TwigComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 2.23.0

- Allow to use `:` as value separator in `props` tag

## 2.20.0

- Add Anonymous Component support for 3rd-party bundles #2019
Expand Down
9 changes: 4 additions & 5 deletions src/TwigComponent/src/Twig/PropsTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,20 @@ class PropsTokenParser extends AbstractTokenParser
{
public function parse(Token $token): Node
{
$parser = $this->parser;
$stream = $parser->getStream();
$stream = $this->parser->getStream();

$names = [];
$values = [];
while (!$stream->nextIf(Token::BLOCK_END_TYPE)) {
$name = $stream->expect(Token::NAME_TYPE)->getValue();

if ($stream->nextIf(Token::OPERATOR_TYPE, '=')) {
$values[$name] = $parser->getExpressionParser()->parseExpression();
if ($stream->nextIf(Token::OPERATOR_TYPE, '=') || $stream->nextIf(Token::PUNCTUATION_TYPE, ':')) {
$values[$name] = $this->parser->getExpressionParser()->parseExpression();
}

$names[] = $name;

if (!$stream->nextIf(Token::PUNCTUATION_TYPE)) {
if (!$stream->nextIf(Token::PUNCTUATION_TYPE, ',')) {
$stream->expect(Token::BLOCK_END_TYPE);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ public static function providePropsData(): iterable
],
' foo ',
];
yield 'One Prop with value comma using : as value separator' => [
'{% props propA:123 %} foo ',
[
'propA' => 123,
],
' foo ',
];
yield 'One Prop without value' => [
'{% props propA %} foo ',
[
Expand All @@ -98,6 +105,14 @@ public static function providePropsData(): iterable
],
' foo ',
];
yield 'All Props with values comma using : as value separator' => [
'{% props propA:1, propB:2 %} foo ',
[
'propA' => 1,
'propB' => 2,
],
' foo ',
];
yield 'Some Props with values' => [
'{% props propA, propB=2 %} foo ',
[
Expand All @@ -106,13 +121,28 @@ public static function providePropsData(): iterable
],
' foo ',
];
yield 'Some Props with values comma using : as value separator' => [
'{% props propA, propB:2 %} foo ',
[
'propA' => null,
'propB' => 2,
],
' foo ',
];
yield 'One Prop with value and trailing comma' => [
'{% props propA=123, %} foo ',
[
'propA' => 123,
],
' foo ',
];
yield 'One Prop with value and trailing comma using : as value separator' => [
'{% props propA:123, %} foo ',
[
'propA' => 123,
],
' foo ',
];
yield 'One Prop without value and trailing comma' => [
'{% props propA, %} foo ',
[
Expand All @@ -136,6 +166,14 @@ public static function providePropsData(): iterable
],
' foo ',
];
yield 'All Props with values and trailing comma using : as value separator' => [
'{% props propA:1, propB:2, %} foo ',
[
'propA' => 1,
'propB' => 2,
],
' foo ',
];
yield 'Some Props with values and trailing comma' => [
'{% props propA, propB=2, %} foo ',
[
Expand All @@ -144,5 +182,13 @@ public static function providePropsData(): iterable
],
' foo ',
];
yield 'Some Props with values and trailing comma using : as value separator' => [
'{% props propA, propB:2, %} foo ',
[
'propA' => null,
'propB' => 2,
],
' foo ',
];
}
}
Loading