diff --git a/src/Twig/Runtime/IconRuntime.php b/src/Twig/Runtime/IconRuntime.php index a4e906b..d51031d 100644 --- a/src/Twig/Runtime/IconRuntime.php +++ b/src/Twig/Runtime/IconRuntime.php @@ -62,8 +62,11 @@ final class IconRuntime implements RuntimeExtensionInterface $classes = trim($colourClasses.' '.$extraClasses); $this->addClassesToSvg($svg, $classes); + $svg = $this->addAttribute($svg, 'aria-hidden', 'true'); + if (null !== $options['title']) { $this->addTitleToSvg($svg, $options['title']); + $svg = $this->addAttribute($svg, 'aria-hidden', 'false'); } $this->setSvgHeightAndWidth($svg, $options['size']); @@ -217,6 +220,14 @@ final class IconRuntime implements RuntimeExtensionInterface $svg = $this->removeXMLDeclaration($svgAsXml->saveXML()); } + private function addAttribute(string $svg, string $attribute, string $value): string + { + $xml = new \SimpleXMLElement($svg); + $xml = $this->addAttributeToXmlElement($xml, $attribute, $value); + + return $this->removeXMLDeclaration($xml->saveXML()); + } + private function isValidXml(string $input): bool { try { diff --git a/tests/Twig/Runtime/IconRuntimeTest.php b/tests/Twig/Runtime/IconRuntimeTest.php index 165a845..a090e6d 100644 --- a/tests/Twig/Runtime/IconRuntimeTest.php +++ b/tests/Twig/Runtime/IconRuntimeTest.php @@ -280,4 +280,23 @@ class IconRuntimeTest extends TestCase $this->assertStringNotContainsString($needleA, $contents); $this->assertStringNotContainsString($needleB, $contents); } + + public function testAriaHiddenAttributeIsAdded(): void + { + $contents = $this->icon->renderIcon(['icon' => self::ICON]); + $this->assertMatchesRegularExpression('//', $contents); + } + + public function testAriaHiddenAttributeIsSetToTrueIfAlreadyPresent(): void + { + $contents = $this->icon->renderIcon(['icon' => 'attr']); + $this->assertMatchesRegularExpression('//', $contents); + $this->assertDoesNotMatchRegularExpression('//', $contents); + } + + public function testAriaHiddenAttributeIsSetToFalseIfATitleIsPassedIn(): void + { + $contents = $this->icon->renderIcon(['icon' => self::ICON, 'title' => 'something']); + $this->assertMatchesRegularExpression('//', $contents); + } }