Skip to content

Commit 955764b

Browse files
committed
bug #1805 [TwigComponent] Fix aria attribute cannot be removed (smnandre)
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
2 parents f8b2344 + e3e9825 commit 955764b

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/TwigComponent/src/ComponentAttributes.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,19 @@ 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-
5854
if (!\is_scalar($value) && null !== $value) {
59-
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)));
55+
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)));
6056
}
6157

6258
if (null === $value) {
6359
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.');
6460
$value = true;
6561
}
6662

63+
if (true === $value && str_starts_with($key, 'aria-')) {
64+
$value = 'true';
65+
}
66+
6767
return match ($value) {
6868
true => "{$carry} {$key}",
6969
false => $carry,
@@ -89,12 +89,12 @@ public function render(string $attribute): ?string
8989
$value = (string) $value;
9090
}
9191

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

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

100100
$this->rendered[$attribute] = true;

src/TwigComponent/tests/Unit/ComponentAttributesTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,29 +259,31 @@ public function testNestedAttributes(): void
259259
$this->assertSame('', (string) $attributes->nested('invalid'));
260260
}
261261

262-
public function testConvertBooleanAriaAttributeValues(): void
262+
public function testConvertTrueAriaAttributeValue(): void
263263
{
264264
$attributes = new ComponentAttributes([
265-
'aria-foo' => true,
266265
'aria-bar' => false,
266+
'aria-foo' => true,
267267
'aria-true' => 'true',
268268
'aria-false' => 'false',
269269
'aria-foobar' => 'foobar',
270270
'aria-number' => '1',
271271
]);
272272

273+
$this->assertStringNotContainsString('aria-bar', (string) $attributes);
273274
$this->assertStringContainsString('aria-foo="true"', (string) $attributes);
274-
$this->assertStringContainsString('aria-bar="false"', (string) $attributes);
275275
$this->assertStringContainsString('aria-true="true"', (string) $attributes);
276276
$this->assertStringContainsString('aria-false="false"', (string) $attributes);
277277
$this->assertStringContainsString('aria-foobar="foobar"', (string) $attributes);
278278
$this->assertStringContainsString('aria-number="1"', (string) $attributes);
279279

280280
$this->assertSame('true', $attributes->render('aria-foo'));
281-
$this->assertSame('false', $attributes->render('aria-bar'));
282281
$this->assertSame('true', $attributes->render('aria-true'));
283282
$this->assertSame('false', $attributes->render('aria-false'));
284283
$this->assertSame('foobar', $attributes->render('aria-foobar'));
285284
$this->assertSame('1', $attributes->render('aria-number'));
285+
286+
$this->expectException(\LogicException::class);
287+
$attributes->render('aria-bar');
286288
}
287289
}

0 commit comments

Comments
 (0)