Skip to content

Commit

Permalink
bug #1805 [TwigComponent] Fix aria attribute cannot be removed (smnan…
Browse files Browse the repository at this point in the history
…dre)

This PR was squashed before being merged into the 2.x branch.

Discussion
----------

[TwigComponent] Fix aria attribute cannot be removed

Handle only "true" (the original need) and restore using false to remove an attribute

cf #1709 and #1797

Commits
-------

e3e9825 [TwigComponent] Fix aria attribute cannot be removed
  • Loading branch information
kbond committed Apr 30, 2024
2 parents f8b2344 + e3e9825 commit 955764b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
16 changes: 8 additions & 8 deletions src/TwigComponent/src/ComponentAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ function (string $carry, string $key) {
$value = (string) $value;
}

if (\is_bool($value) && str_starts_with($key, 'aria-')) {
$value = $value ? 'true' : 'false';
}

if (!\is_scalar($value) && null !== $value) {
throw new \LogicException(sprintf('A "%s" prop was passed when creating the component. No matching "%s" property or mount() argument was found, so we attempted to use this as an HTML attribute. But, the value is not a scalar (it\'s a %s). Did you mean to pass this to your component or is there a typo on its name?', $key, $key, get_debug_type($value)));
throw new \LogicException(sprintf('A "%s" prop was passed when creating the component. No matching "%s" property or mount() argument was found, so we attempted to use this as an HTML attribute. But, the value is not a scalar (it\'s a "%s"). Did you mean to pass this to your component or is there a typo on its name?', $key, $key, get_debug_type($value)));
}

if (null === $value) {
trigger_deprecation('symfony/ux-twig-component', '2.8.0', 'Passing "null" as an attribute value is deprecated and will throw an exception in 3.0.');
$value = true;
}

if (true === $value && str_starts_with($key, 'aria-')) {
$value = 'true';
}

return match ($value) {
true => "{$carry} {$key}",
false => $carry,
Expand All @@ -89,12 +89,12 @@ public function render(string $attribute): ?string
$value = (string) $value;
}

if (\is_bool($value) && str_starts_with($attribute, 'aria-')) {
$value = $value ? 'true' : 'false';
if (true === $value && str_starts_with($attribute, 'aria-')) {
$value = 'true';
}

if (!\is_string($value)) {
throw new \LogicException(sprintf('Can only get string attributes (%s is a %s).', $attribute, get_debug_type($value)));
throw new \LogicException(sprintf('Can only get string attributes (%s is a "%s").', $attribute, get_debug_type($value)));
}

$this->rendered[$attribute] = true;
Expand Down
10 changes: 6 additions & 4 deletions src/TwigComponent/tests/Unit/ComponentAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,29 +259,31 @@ public function testNestedAttributes(): void
$this->assertSame('', (string) $attributes->nested('invalid'));
}

public function testConvertBooleanAriaAttributeValues(): void
public function testConvertTrueAriaAttributeValue(): void
{
$attributes = new ComponentAttributes([
'aria-foo' => true,
'aria-bar' => false,
'aria-foo' => true,
'aria-true' => 'true',
'aria-false' => 'false',
'aria-foobar' => 'foobar',
'aria-number' => '1',
]);

$this->assertStringNotContainsString('aria-bar', (string) $attributes);
$this->assertStringContainsString('aria-foo="true"', (string) $attributes);
$this->assertStringContainsString('aria-bar="false"', (string) $attributes);
$this->assertStringContainsString('aria-true="true"', (string) $attributes);
$this->assertStringContainsString('aria-false="false"', (string) $attributes);
$this->assertStringContainsString('aria-foobar="foobar"', (string) $attributes);
$this->assertStringContainsString('aria-number="1"', (string) $attributes);

$this->assertSame('true', $attributes->render('aria-foo'));
$this->assertSame('false', $attributes->render('aria-bar'));
$this->assertSame('true', $attributes->render('aria-true'));
$this->assertSame('false', $attributes->render('aria-false'));
$this->assertSame('foobar', $attributes->render('aria-foobar'));
$this->assertSame('1', $attributes->render('aria-number'));

$this->expectException(\LogicException::class);
$attributes->render('aria-bar');
}
}

0 comments on commit 955764b

Please sign in to comment.