Skip to content

Commit 4ac1f61

Browse files
smnandrejaviereguiluz
authored andcommitted
[Twig] Handle aria-* attribute boolean values
1 parent c71337a commit 4ac1f61

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/TwigComponent/src/ComponentAttributes.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ function (string $carry, string $key) {
5151
$value = (string) $value;
5252
}
5353

54+
if (\is_bool($value) && str_starts_with($key, 'aria-')) {
55+
$value = $value ? 'true' : 'false';
56+
}
57+
5458
if (!\is_scalar($value) && null !== $value) {
5559
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)));
5660
}
@@ -85,6 +89,10 @@ public function render(string $attribute): ?string
8589
$value = (string) $value;
8690
}
8791

92+
if (\is_bool($value) && str_starts_with($attribute, 'aria-')) {
93+
$value = $value ? 'true' : 'false';
94+
}
95+
8896
if (!\is_string($value)) {
8997
throw new \LogicException(sprintf('Can only get string attributes (%s is a %s).', $attribute, get_debug_type($value)));
9098
}

src/TwigComponent/tests/Unit/ComponentAttributesTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,30 @@ public function testNestedAttributes(): void
258258
$this->assertSame(' class="baz"', (string) $attributes->nested('title')->nested('span'));
259259
$this->assertSame('', (string) $attributes->nested('invalid'));
260260
}
261+
262+
public function testConvertBooleanAriaAttributeValues(): void
263+
{
264+
$attributes = new ComponentAttributes([
265+
'aria-foo' => true,
266+
'aria-bar' => false,
267+
'aria-true' => 'true',
268+
'aria-false' => 'false',
269+
'aria-foobar' => 'foobar',
270+
'aria-number' => '1',
271+
]);
272+
273+
$this->assertStringContainsString('aria-foo="true"', (string) $attributes);
274+
$this->assertStringContainsString('aria-bar="false"', (string) $attributes);
275+
$this->assertStringContainsString('aria-true="true"', (string) $attributes);
276+
$this->assertStringContainsString('aria-false="false"', (string) $attributes);
277+
$this->assertStringContainsString('aria-foobar="foobar"', (string) $attributes);
278+
$this->assertStringContainsString('aria-number="1"', (string) $attributes);
279+
280+
$this->assertSame('true', $attributes->render('aria-foo'));
281+
$this->assertSame('false', $attributes->render('aria-bar'));
282+
$this->assertSame('true', $attributes->render('aria-true'));
283+
$this->assertSame('false', $attributes->render('aria-false'));
284+
$this->assertSame('foobar', $attributes->render('aria-foobar'));
285+
$this->assertSame('1', $attributes->render('aria-number'));
286+
}
261287
}

0 commit comments

Comments
 (0)