-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #1298 [TwigComponent] Allow trailing coma in "props" tags (smnandre)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [TwigComponent] Allow trailing coma in "props" tags | Q | A | ------------- | --- | Bug fix? | no | New feature? | not really | Issues | Fix #1286 | License | MIT Allow trailing comas in props tag ```twig {% props foo = 'foo', bar = 'bar', %} ``` Commits ------- 799df54 [TwigComponent] Allow trailing coma in "props" tags
- Loading branch information
Showing
6 changed files
with
167 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...TwigComponent/tests/Fixtures/templates/anonymous_component_props_trailing_comma.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<twig:CommaProps bar="bar" /> | ||
<twig:CommaProps foo="FOO" bar="123" foobar="456" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
<button {{ attributes.defaults({class: primary ? 'primary' : 'secondary'}) }}> | ||
{{ label }} | ||
</button> | ||
</button> |
7 changes: 7 additions & 0 deletions
7
src/TwigComponent/tests/Fixtures/templates/components/CommaProps.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{% props | ||
foo = 'foo', | ||
bar, | ||
foobar = 'foobar', | ||
%} | ||
|
||
<p>Hello {{ foo }}, {{ bar }}, and {{ foobar }}</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
src/TwigComponent/tests/Integration/Twig/ComponentPropsParserTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\TwigComponent\Tests\Integration\Twig; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\UX\TwigComponent\Twig\PropsNode; | ||
use Twig\Environment; | ||
use Twig\Loader\ArrayLoader; | ||
use Twig\Node\TextNode; | ||
use Twig\Source; | ||
|
||
/** | ||
* @author Simon André <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
class ComponentPropsParserTest extends KernelTestCase | ||
{ | ||
/** | ||
* @dataProvider providePropsData | ||
*/ | ||
public function testPropsData(string $template, array $props, string $text): void | ||
{ | ||
$loader = new ArrayLoader(['template' => $template]); | ||
|
||
/** @var Environment $twig */ | ||
$twig = self::getContainer()->get(Environment::class); | ||
$twig->setLoader($loader); | ||
|
||
$tokenStream = $twig->tokenize(new Source($template, 'template')); | ||
$foo = $twig->parse($tokenStream); | ||
|
||
$body = $foo->getNode('body')->getNode('0'); | ||
$this->assertTrue($body->hasNode(0)); | ||
|
||
$propsNode = $body->getNode(0); | ||
$this->assertInstanceOf(PropsNode::class, $propsNode); | ||
$this->assertTrue($propsNode->hasAttribute('names')); | ||
|
||
foreach ($props as $name => $value) { | ||
$this->assertContains($name, $propsNode->getAttribute('names')); | ||
if (null === $value) { | ||
$this->assertFalse($propsNode->hasNode($name)); | ||
continue; | ||
} | ||
$this->assertTrue($propsNode->hasNode($name)); | ||
$this->assertTrue($propsNode->getNode($name)->hasAttribute('value')); | ||
$this->assertSame($value, $propsNode->getNode($name)->getAttribute('value')); | ||
} | ||
|
||
$this->assertTrue($body->hasNode(1)); | ||
$this->assertInstanceOf(TextNode::class, $body->getNode(1)); | ||
$this->assertTrue($body->getNode(1)->hasAttribute('data')); | ||
$this->assertSame($text, $body->getNode(1)->getAttribute('data')); | ||
} | ||
|
||
/** | ||
* @return iterable<string, array{0: string, 1: array<string, string|int|null>, 2: string}> | ||
*/ | ||
public static function providePropsData(): iterable | ||
{ | ||
yield 'One Prop with value' => [ | ||
'{% props propA=123 %} foo ', | ||
[ | ||
'propA' => 123, | ||
], | ||
' foo ', | ||
]; | ||
yield 'One Prop without value' => [ | ||
'{% props propA %} foo ', | ||
[ | ||
'propA' => null, | ||
], | ||
' foo ', | ||
]; | ||
yield 'No Props with values' => [ | ||
'{% props propA, propB %} foo ', | ||
[ | ||
'propA' => null, | ||
'propB' => null, | ||
], | ||
' foo ', | ||
]; | ||
yield 'All Props with values' => [ | ||
'{% props propA=1, propB=2 %} foo ', | ||
[ | ||
'propA' => 1, | ||
'propB' => 2, | ||
], | ||
' foo ', | ||
]; | ||
yield 'Some Props with values' => [ | ||
'{% 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 without value and trailing comma' => [ | ||
'{% props propA, %} foo ', | ||
[ | ||
'propA' => null, | ||
], | ||
' foo ', | ||
]; | ||
yield 'No Props with values and trailing comma' => [ | ||
'{% props propA, propB, %} foo ', | ||
[ | ||
'propA' => null, | ||
'propB' => null, | ||
], | ||
' foo ', | ||
]; | ||
yield 'All Props with values and trailing comma' => [ | ||
'{% props propA=1, propB=2, %} foo ', | ||
[ | ||
'propA' => 1, | ||
'propB' => 2, | ||
], | ||
' foo ', | ||
]; | ||
yield 'Some Props with values and trailing comma' => [ | ||
'{% props propA, propB=2, %} foo ', | ||
[ | ||
'propA' => null, | ||
'propB' => 2, | ||
], | ||
' foo ', | ||
]; | ||
} | ||
} |