Skip to content
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

[Twig] Handle aria-* attribute boolean values #1709

Merged
merged 1 commit into from
Apr 10, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/TwigComponent/src/ComponentAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ 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)));
}
Expand Down Expand Up @@ -85,6 +89,10 @@ public function render(string $attribute): ?string
$value = (string) $value;
}

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

if (!\is_string($value)) {
throw new \LogicException(sprintf('Can only get string attributes (%s is a %s).', $attribute, get_debug_type($value)));
}
Expand Down
26 changes: 26 additions & 0 deletions src/TwigComponent/tests/Unit/ComponentAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,30 @@ public function testNestedAttributes(): void
$this->assertSame(' class="baz"', (string) $attributes->nested('title')->nested('span'));
$this->assertSame('', (string) $attributes->nested('invalid'));
}

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

$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'));
}
}