Skip to content

[Icons] Improve aria attributes rendering #1797

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
15 changes: 12 additions & 3 deletions src/Icons/src/Icon.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,20 @@ public function toHtml(): string
if (false === $value) {
continue;
}

// Special case for aria-* attributes
// https://www.w3.org/TR/wai-aria-1.1/#state_prop_def
if (true === $value && str_starts_with($name, 'aria-')) {
$value = 'true';
}

$htmlAttributes .= ' '.$name;
if (true !== $value) {
$value = htmlspecialchars($value, \ENT_QUOTES | \ENT_SUBSTITUTE, 'UTF-8');
$htmlAttributes .= '="'.$value.'"';
if (true === $value) {
continue;
}

$value = htmlspecialchars($value, \ENT_QUOTES | \ENT_SUBSTITUTE, 'UTF-8');
$htmlAttributes .= '="'.$value.'"';
}

return '<svg'.$htmlAttributes.'>'.$this->innerSvg.'</svg>';
Expand Down
33 changes: 23 additions & 10 deletions src/Icons/src/IconRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,34 @@ public function __construct(
*/
public function renderIcon(string $name, array $attributes = []): string
{
return $this->registry->get($name)
->withAttributes($this->getIconAttributes($name, $attributes))
->toHtml()
;
$icon = $this->registry->get($name)
->withAttributes($this->defaultIconAttributes)
->withAttributes($attributes);

foreach ($this->getPreRenderers() as $preRenderer) {
$icon = $preRenderer($icon);
}

return $icon->toHtml();
}

private function getIconAttributes(string $name, array $attributes): array
/**
* @return iterable<callable(Icon): Icon>
*/
private function getPreRenderers(): iterable
{
$iconAttributes = $this->defaultIconAttributes;
yield self::setAriaHidden(...);
}

// Add aria-hidden attribute
if ([] === array_intersect(['aria-hidden', 'aria-label', 'aria-labelledby', 'title'], array_keys($attributes))) {
$iconAttributes['aria-hidden'] = 'true';
/**
* Set `aria-hidden=true` if not defined & no textual alternative provided.
*/
private static function setAriaHidden(Icon $icon): Icon
{
if ([] === array_intersect(['aria-hidden', 'aria-label', 'aria-labelledby', 'title'], array_keys($icon->getAttributes()))) {
return $icon->withAttributes(['aria-hidden' => 'true']);
}

return [...$iconAttributes, ...$attributes];
return $icon;
}
}
2 changes: 1 addition & 1 deletion src/Icons/tests/Integration/RenderIconsInTwigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testRenderIcons(): void
<ul class="svg">
<li id="first"><svg viewBox="0 0 24 24" fill="currentColor" class="h-8 w-8" aria-hidden="true"><path fill-rule="evenodd" d="M7.5 6a4.5 4.5 0 1 1 9 0 4.5 4.5 0 0 1-9 0ZM3.751 20.105a8.25 8.25 0 0 1 16.498 0 .75.75 0 0 1-.437.695A18.683 18.683 0 0 1 12 22.5c-2.786 0-5.433-.608-7.812-1.7a.75.75 0 0 1-.437-.695Z" clip-rule="evenodd"></path></svg></li>
<li id="second"><svg viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6" aria-label="AriaLabel"><path fill-rule="evenodd" d="M7.5 6a4.5 4.5 0 1 1 9 0 4.5 4.5 0 0 1-9 0ZM3.751 20.105a8.25 8.25 0 0 1 16.498 0 .75.75 0 0 1-.437.695A18.683 18.683 0 0 1 12 22.5c-2.786 0-5.433-.608-7.812-1.7a.75.75 0 0 1-.437-.695Z" clip-rule="evenodd"></path></svg></li>
<li id="third"><svg viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6" aria-hidden="true" data-action="string &quot;with&quot; quotes"><path fill-rule="evenodd" d="M19.916 4.626a.75.75 0 0 1 .208 1.04l-9 13.5a.75.75 0 0 1-1.154.114l-6-6a.75.75 0 0 1 1.06-1.06l5.353 5.353 8.493-12.74a.75.75 0 0 1 1.04-.207Z" clip-rule="evenodd"></path></svg></li>
<li id="third"><svg viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6" data-action="string &quot;with&quot; quotes" aria-hidden="true"><path fill-rule="evenodd" d="M19.916 4.626a.75.75 0 0 1 .208 1.04l-9 13.5a.75.75 0 0 1-1.154.114l-6-6a.75.75 0 0 1 1.06-1.06l5.353 5.353 8.493-12.74a.75.75 0 0 1 1.04-.207Z" clip-rule="evenodd"></path></svg></li>
<li id="fifth"><svg viewBox="0 0 24 24" fill="currentColor" class="h-8 w-8" aria-hidden="true"><path fill-rule="evenodd" d="M7.5 6a4.5 4.5 0 1 1 9 0 4.5 4.5 0 0 1-9 0ZM3.751 20.105a8.25 8.25 0 0 1 16.498 0 .75.75 0 0 1-.437.695A18.683 18.683 0 0 1 12 22.5c-2.786 0-5.433-.608-7.812-1.7a.75.75 0 0 1-.437-.695Z" clip-rule="evenodd"></path></svg></li>
<li id="sixth"><svg viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6" aria-labelledby="foo"><path fill-rule="evenodd" d="M19.916 4.626a.75.75 0 0 1 .208 1.04l-9 13.5a.75.75 0 0 1-1.154.114l-6-6a.75.75 0 0 1 1.06-1.06l5.353 5.353 8.493-12.74a.75.75 0 0 1 1.04-.207Z" clip-rule="evenodd"></path></svg></li>
<li id="seventh"><svg viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6" aria-hidden="true"><path fill-rule="evenodd" d="M19.916 4.626a.75.75 0 0 1 .208 1.04l-9 13.5a.75.75 0 0 1-1.154.114l-6-6a.75.75 0 0 1 1.06-1.06l5.353 5.353 8.493-12.74a.75.75 0 0 1 1.04-.207Z" clip-rule="evenodd"></path></svg></li>
Expand Down
33 changes: 28 additions & 5 deletions src/Icons/tests/Unit/IconRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testRenderIconWithAttributes(): void

$svg = $iconRenderer->renderIcon('foo', $attributes);

$this->assertSame('<svg aria-hidden="true" viewBox="0 0 24 24" class="icon" id="FooBar"><path d="M0 0L12 12"/></svg>', $svg);
$this->assertSame('<svg viewBox="0 0 24 24" class="icon" id="FooBar" aria-hidden="true"><path d="M0 0L12 12"/></svg>', $svg);
}

public function testRenderIconWithDefaultAttributes(): void
Expand Down Expand Up @@ -163,12 +163,14 @@ public static function provideAttributesWithDefaultAttributesCases()
/**
* @dataProvider provideAriaHiddenCases
*
* @param array<string, string> $attributes
* @param string|array{string, array<string, string|bool>} $icon
* @param array<string, string|bool> $attributes
*/
public function testRenderIconWithAutoAriaHidden(array $attributes, string $expectedSvg): void
public function testRenderIconWithAutoAriaHidden(string|array $icon, array $attributes, string $expectedSvg): void
{
$icon = (array) $icon;
$registry = $this->createRegistry([
'foo' => '<path d="M0 0L12 12"/>',
'foo' => $icon,
]);
$iconRenderer = new IconRenderer($registry);

Expand All @@ -177,31 +179,52 @@ public function testRenderIconWithAutoAriaHidden(array $attributes, string $expe
}

/**
* @return iterable<array{array<string, string>, string}>
* @return iterable<array{string|array{string, array<string, string|bool>}, array<string, string|bool>, string}>
*/
public static function provideAriaHiddenCases(): iterable
{
yield 'no attributes' => [
'<path d="M0 0L12 12"/>',
[],
'<svg aria-hidden="true"><path d="M0 0L12 12"/></svg>',
];
yield 'no attributes and icon attribute' => [
['<path d="M0 0L12 12"/>', ['aria-hidden' => 'false']],
[],
'<svg aria-hidden="false"><path d="M0 0L12 12"/></svg>',
];
yield 'no attributes and icon label' => [
['<path d="M0 0L12 12"/>', ['aria-label' => 'foo']],
[],
'<svg aria-label="foo"><path d="M0 0L12 12"/></svg>',
];
yield 'aria-hidden attribute' => [
'<path d="M0 0L12 12"/>',
['aria-hidden' => 'true'],
'<svg aria-hidden="true"><path d="M0 0L12 12"/></svg>',
];
yield 'aria-hidden attribute and icon attribute' => [
['<path d="M0 0L12 12"/>', ['aria-hidden' => 'false']],
['aria-hidden' => 'true'],
'<svg aria-hidden="true"><path d="M0 0L12 12"/></svg>',
];
yield 'aria-hidden false + aria-label' => [
'<path d="M0 0L12 12"/>',
['aria-hidden' => 'false', 'aria-label' => 'foo'],
'<svg aria-hidden="false" aria-label="foo"><path d="M0 0L12 12"/></svg>',
];
yield 'title attribute' => [
'<path d="M0 0L12 12"/>',
['title' => 'foo'],
'<svg title="foo"><path d="M0 0L12 12"/></svg>',
];
yield 'aria-labelledby attribute' => [
'<path d="M0 0L12 12"/>',
['aria-labelledby' => 'foo'],
'<svg aria-labelledby="foo"><path d="M0 0L12 12"/></svg>',
];
yield 'aria-label attribute' => [
'<path d="M0 0L12 12"/>',
['aria-label' => 'foo'],
'<svg aria-label="foo"><path d="M0 0L12 12"/></svg>',
];
Expand Down
12 changes: 10 additions & 2 deletions src/Icons/tests/Unit/IconTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,18 @@ public static function provideRenderAttributesTestCases(): iterable
['foo' => true],
'<svg foo>',
];
yield 'it_does_not_render_attribute_with_false_value' => [
['foo' => false],
yield 'it_does_not_render_attributes_with_false_value' => [
['foo' => false, 'aria-foo' => false],
'<svg>',
];
yield 'it_converts_aria_attribute_with_true_value' => [
['aria-bar' => true],
'<svg aria-bar="true">',
];
yield 'it_does_not_convert_aria_attribute_with_string_value' => [
['aria-foo' => '0', 'aria-bar' => 'true', 'aria-baz' => 'false'],
'<svg aria-foo="0" aria-bar="true" aria-baz="false">',
];
}

public static function provideWithAttributesTestCases(): iterable
Expand Down