Compare commits

...

3 Commits

4 changed files with 42 additions and 0 deletions

View File

@@ -2,6 +2,8 @@
## [X.X.X] - XXXX-XX-XX ## [X.X.X] - XXXX-XX-XX
- Allow passing in a space-separated string to the `classes` option - Allow passing in a space-separated string to the `classes` option
- Add new exceptions and messages to make debugging a little bit faster
- Set `aria-hidden` attribute depending on whether a `title` has been set
## [1.0.0] - 2023-06-27 ## [1.0.0] - 2023-06-27
- First major version release - First major version release

View File

@@ -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 {

View File

@@ -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);
}
} }

10
tests/icons/attr.svg Normal file
View File

@@ -0,0 +1,10 @@
<svg aria-hidden="false" xmlns="http://www.w3.org/2000/svg" width="512" height="512" viewBox="96 96 320 320">
<title>Some cross lol</title>
<line fill="rgb(0, 0, 0)"></line>
<line fill="#000"></line>
<line fill="#000000"></line>
<line stroke="currentColor" fill="black"></line>
<line x1="256" y1="112" x2="256" y2="400" width="101" height="101" style="fill: none; stroke: rgb(0,0, 0); stroke-linecap: round; stroke-linejoin: round; stroke-width: 32px;"></line>
<line x1="400" y1="256" x2="112" y2="256" width="102" height="102" style="fill: none; stroke: #000; stroke-linecap: round; stroke-linejoin: round; stroke-width: 32px;"></line>
<polyline points="112 244 256 100 400 244" style="fill:#000;fill:#000000;fill: black;fill:rgb(0,0,0);stroke:#000;stroke:#000000;stroke:black;stroke:rgb(0, 0, 0);stroke-linecap:round;stroke-linejoin:round;stroke-width:48px" stroke='currentColor'/>
</svg>

After

Width:  |  Height:  |  Size: 924 B