Skip to content

Allow to disable nested attributes with ! #2325

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

Closed
wants to merge 1 commit 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.21.0

- Allow to disable nested attributes with !

## 2.20.0

- Add Anonymous Component support for 3rd-party bundles #2019
Expand Down
6 changes: 5 additions & 1 deletion src/TwigComponent/src/ComponentAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
final class ComponentAttributes implements \Stringable, \IteratorAggregate, \Countable
{
private const NESTED_REGEX = '#^([\w-]+):(.+)$#';
private const NESTED_REGEX = '#^([\w-]+):(.+)(?<!!)$#';

/** @var array<string,true> */
private array $rendered = [];
Expand Down Expand Up @@ -64,6 +64,10 @@ function (string $carry, string $key) {
$value = 'true';
}

if (str_ends_with($key, '!')) {
$key = substr($key, 0, -1);
}

return match ($value) {
true => "{$carry} {$key}",
false => $carry,
Expand Down
6 changes: 5 additions & 1 deletion src/TwigComponent/src/Twig/TwigPreLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function preLexComponents(string $input): string
private function consumeComponentName(?string $customExceptionMessage = null): string
{
$start = $this->position;
while ($this->position < $this->length && preg_match('/[A-Za-z0-9_:@\-.]/', $this->input[$this->position])) {
while ($this->position < $this->length && preg_match('/[A-Za-z0-9_:@!\-.]/', $this->input[$this->position])) {
++$this->position;
}

Expand Down Expand Up @@ -253,6 +253,10 @@ private function consumeAttributes(string $componentName): string

$key = $this->consumeAttributeName($componentName);

if (str_ends_with($key, '!')) {
$isAttributeDynamic = false;
}

// <twig:component someProp> -> someProp: true
if (!$this->check('=')) {
// don't allow "<twig:component :someProp>"
Expand Down
38 changes: 38 additions & 0 deletions src/TwigComponent/tests/Integration/ComponentExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,44 @@ public function testRenderingComponentWithNestedAttributes(): void
);
}

public function testRenderingComponentWithNestedAndNotNestedAttributes(): void
{
$output = $this->renderComponent('NestedAttributes');

$this->assertSame(<<<HTML
<main>
<div>
<span>
<div/>

</span>
</div>
</main>
HTML,
trim($output)
);

$output = $this->renderComponent('NestedAttributes', [
'class' => 'foo',
'x-bind:class!' => 'alpine',
':class!' => 'alpine',
'title:span:class' => 'baz',
]);

$this->assertSame(<<<HTML
<main class="foo" x-bind:class="alpine" :class="alpine">
<div>
<span class="baz">
<div/>

</span>
</div>
</main>
HTML,
trim($output)
);
}

public function testRenderingHtmlSyntaxComponentWithNestedAttributes(): void
{
$output = self::getContainer()
Expand Down
14 changes: 14 additions & 0 deletions src/TwigComponent/tests/Unit/ComponentAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,20 @@ public function testNestedAttributes(): void
$this->assertSame('', (string) $attributes->nested('invalid'));
}

public function testNotNestedAttributes(): void
{
$attributes = new ComponentAttributes([
'class' => 'foo',
'x-bind:class!' => 'alpine',
':class!' => 'alpine',
'title:span:class' => 'baz',
]);

$this->assertSame(' class="foo" x-bind:class="alpine" :class="alpine"', (string) $attributes);
$this->assertSame(' class="baz"', (string) $attributes->nested('title')->nested('span'));
$this->assertSame('', (string) $attributes->nested('invalid'));
}

public function testConvertTrueAriaAttributeValue(): void
{
$attributes = new ComponentAttributes([
Expand Down