diff --git a/src/Twig/Functions/IconExtension.php b/src/Twig/Functions/IconExtension.php index 4fc6ee4..a7ca8a5 100644 --- a/src/Twig/Functions/IconExtension.php +++ b/src/Twig/Functions/IconExtension.php @@ -77,10 +77,14 @@ final class IconExtension extends AbstractExtension $rawSvgMarkup = $this->getSvgMarkup($iconFilepath); $cleanSvgMarkup = $this->cleanSvgMarkup($rawSvgMarkup); - $mainColour = $this->getMainPallete($options['colour']); + $mainPallete = $this->getMainPallete($options['colour']); + + $xml = new \SimpleXMLElement($cleanSvgMarkup); + $xml = $this->addAttributeToXmlElement($xml, 'class', $mainPallete['stroke'] . ' ' . $mainPallete['fill']); + $markup = $this->removeXMLDeclaration($xml->saveXML()); if ($this->isNonEmptyString($options['title'])) - $markup = $this->addTitleToMarkup($cleanSvgMarkup, $options['title']); + $markup = $this->addTitleToMarkup($markup, $options['title']); if ($options['size'] < 0) throw new \InvalidArgumentException('Size must not be negative'); @@ -175,12 +179,12 @@ final class IconExtension extends AbstractExtension private function removeBlackStrokeAttributes(string $content): string { - return preg_replace('/stroke="\s*(#0{6}|#000)|(rgb\(\s*0,\s*0,\s*0\s*\))|(black)\s*"/', '', $content); + return preg_replace('/stroke="\s*(#0{6}|#000|rgb\(\s*0,\s*0,\s*0\s*\)|black)\s*"/', '', $content); } private function removeBlackFillAttributes(string $content): string { - return preg_replace('/fill="\s*(#0{6}|#000)|(rgb\(\s*0,\s*0,\s*0\s*\))|(black)\s*"/', '', $content); + return preg_replace('/fill="\s*(#0{6}|#000|rgb\(\s*0,\s*0,\s*0\s*\)|black)\s*"/', '', $content); } } diff --git a/tests/Twig/Functions/IconExtensionTest.php b/tests/Twig/Functions/IconExtensionTest.php index 78716af..7dcf618 100644 --- a/tests/Twig/Functions/IconExtensionTest.php +++ b/tests/Twig/Functions/IconExtensionTest.php @@ -17,6 +17,10 @@ class IconExtensionTest extends TestCase 'primary' => [ 'stroke' => 'stroke-primary', 'fill' => 'fill-primary' + ], + 'white' => [ + 'stroke' => 'stroke-white', + 'fill' => 'fill-white' ] ]; @@ -211,4 +215,12 @@ class IconExtensionTest extends TestCase $this->icon->renderIcon(['icon' => self::ICON, 'colour' => 'red']); } + public function testSvgClassContainsPalleteClasses(): void + { + $contents = $this->icon->renderIcon(['icon' => self::ICON, 'colour' => 'white']); + $this->assertMatchesRegularExpression('//', $contents); + $this->assertMatchesRegularExpression('//', $contents); + } + + }