Skip to content

Commit

Permalink
bug #1709 [Twig] Handle aria-* attribute boolean values (smnandre)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 2.x branch.

Discussion
----------

[Twig] Handle aria-* attribute boolean values

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Issues        | Fix #1701
| License       | MIT

Convert aria-* boolean attributes to their expected string values.

Commits
-------

4ac1f61 [Twig] Handle aria-* attribute boolean values
  • Loading branch information
javiereguiluz committed Apr 10, 2024
2 parents 722ac82 + 4ac1f61 commit 5039e6f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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'));
}
}

0 comments on commit 5039e6f

Please sign in to comment.