Skip to content

[TwigComponent] Fix aria attribute cannot be removed #1805

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

Merged
merged 1 commit into from
Apr 30, 2024
Merged
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
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');
}
}