Set aria-hidden attribute depending on whether a title has been passed in or not
This commit is contained in:
@@ -62,8 +62,11 @@ final class IconRuntime implements RuntimeExtensionInterface
|
|||||||
$classes = trim($colourClasses.' '.$extraClasses);
|
$classes = trim($colourClasses.' '.$extraClasses);
|
||||||
$this->addClassesToSvg($svg, $classes);
|
$this->addClassesToSvg($svg, $classes);
|
||||||
|
|
||||||
|
$svg = $this->addAttribute($svg, 'aria-hidden', 'true');
|
||||||
|
|
||||||
if (null !== $options['title']) {
|
if (null !== $options['title']) {
|
||||||
$this->addTitleToSvg($svg, $options['title']);
|
$this->addTitleToSvg($svg, $options['title']);
|
||||||
|
$svg = $this->addAttribute($svg, 'aria-hidden', 'false');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->setSvgHeightAndWidth($svg, $options['size']);
|
$this->setSvgHeightAndWidth($svg, $options['size']);
|
||||||
@@ -217,6 +220,14 @@ final class IconRuntime implements RuntimeExtensionInterface
|
|||||||
$svg = $this->removeXMLDeclaration($svgAsXml->saveXML());
|
$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
|
private function isValidXml(string $input): bool
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -280,4 +280,23 @@ class IconRuntimeTest extends TestCase
|
|||||||
$this->assertStringNotContainsString($needleA, $contents);
|
$this->assertStringNotContainsString($needleA, $contents);
|
||||||
$this->assertStringNotContainsString($needleB, $contents);
|
$this->assertStringNotContainsString($needleB, $contents);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAriaHiddenAttributeIsAdded(): void
|
||||||
|
{
|
||||||
|
$contents = $this->icon->renderIcon(['icon' => self::ICON]);
|
||||||
|
$this->assertMatchesRegularExpression('/<svg.+aria-hidden="true".*>/', $contents);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAriaHiddenAttributeIsSetToTrueIfAlreadyPresent(): void
|
||||||
|
{
|
||||||
|
$contents = $this->icon->renderIcon(['icon' => 'attr']);
|
||||||
|
$this->assertMatchesRegularExpression('/<svg.+aria-hidden="true".*>/', $contents);
|
||||||
|
$this->assertDoesNotMatchRegularExpression('/<svg.+aria-hidden="false".*>/', $contents);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAriaHiddenAttributeIsSetToFalseIfATitleIsPassedIn(): void
|
||||||
|
{
|
||||||
|
$contents = $this->icon->renderIcon(['icon' => self::ICON, 'title' => 'something']);
|
||||||
|
$this->assertMatchesRegularExpression('/<svg.+aria-hidden="false".*>/', $contents);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user