Add colour classes

This commit is contained in:
Brabli
2022-08-14 22:09:12 +01:00
parent 10c252ea6d
commit 562f841fd3
2 changed files with 20 additions and 4 deletions

View File

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

View File

@@ -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('/<svg.*?class=".*?fill-white.*?>/', $contents);
$this->assertMatchesRegularExpression('/<svg.*?class=".*?stroke-white?.*>/', $contents);
}
}