From b06bcaa3f2492343e1ade6cd1df766be27fbff66 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 4 Jul 2024 12:41:16 -0400 Subject: [PATCH] feat: escape nesting separator with `::` --- src/TwigComponent/src/ComponentAttributes.php | 4 +++- .../tests/Integration/ComponentExtensionTest.php | 4 ++-- src/TwigComponent/tests/Unit/ComponentAttributesTest.php | 7 +++++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/TwigComponent/src/ComponentAttributes.php b/src/TwigComponent/src/ComponentAttributes.php index 57c5e4676d6..e61e3eb064f 100644 --- a/src/TwigComponent/src/ComponentAttributes.php +++ b/src/TwigComponent/src/ComponentAttributes.php @@ -21,7 +21,7 @@ */ final class ComponentAttributes implements \Stringable, \IteratorAggregate, \Countable { - private const NESTED_REGEX = '#^([\w-]+):(.+)$#'; + private const NESTED_REGEX = '#^([\w-]+):(?!:)(.+)$#'; /** @var array */ private array $rendered = []; @@ -64,6 +64,8 @@ function (string $carry, string $key) { $value = 'true'; } + $key = str_replace('::', ':', $key); + return match ($value) { true => "{$carry} {$key}", false => $carry, diff --git a/src/TwigComponent/tests/Integration/ComponentExtensionTest.php b/src/TwigComponent/tests/Integration/ComponentExtensionTest.php index b6eaa07d651..15fa58f26c0 100644 --- a/src/TwigComponent/tests/Integration/ComponentExtensionTest.php +++ b/src/TwigComponent/tests/Integration/ComponentExtensionTest.php @@ -331,12 +331,12 @@ public function testRenderingHtmlSyntaxComponentWithNestedAttributes(): void $output = self::getContainer() ->get(Environment::class) - ->createTemplate('') + ->createTemplate('') ->render() ; $this->assertSame(<< +
diff --git a/src/TwigComponent/tests/Unit/ComponentAttributesTest.php b/src/TwigComponent/tests/Unit/ComponentAttributesTest.php index 8892d0068b2..95f2ae26cf6 100644 --- a/src/TwigComponent/tests/Unit/ComponentAttributesTest.php +++ b/src/TwigComponent/tests/Unit/ComponentAttributesTest.php @@ -251,15 +251,18 @@ public function testNestedAttributes(): void 'class' => 'foo', 'data-class' => 'qux', 'title:class' => 'bar', + 'title:x-on::click' => '!close', 'title:span:class' => 'baz', 'data-title:class' => 'flo', 'data-title:data-class' => 'shi', + 'x-on::click' => '!open', ]); - $this->assertSame(' class="foo" data-class="qux"', (string) $attributes); - $this->assertSame(' class="bar"', (string) $attributes->nested('title')); + $this->assertSame(' class="foo" data-class="qux" x-on:click="!open"', (string) $attributes); + $this->assertSame(' class="bar" x-on:click="!close"', (string) $attributes->nested('title')); $this->assertSame(' class="baz"', (string) $attributes->nested('title')->nested('span')); $this->assertSame('', (string) $attributes->nested('invalid')); + $this->assertSame('', (string) $attributes->nested('x-on')); $this->assertSame(' class="flo" data-class="shi"', (string) $attributes->nested('data-title')); }