Remove fill and stroke attributes where their value was black

This commit is contained in:
Brabli
2022-08-14 19:41:31 +01:00
parent 9a95804f3c
commit c75369e0fa
3 changed files with 34 additions and 0 deletions

View File

@@ -80,6 +80,8 @@ final class IconExtension extends AbstractExtension
throw new \TypeError('Size value must be an integer');
$markup = $this->setSize($markup, $options['size']);
$markup = $this->removeBlackStrokeAttributes($markup);
$markup = $this->removeBlackFillAttributes($markup);
return $markup;
}
@@ -153,6 +155,16 @@ final class IconExtension extends AbstractExtension
return $xml;
}
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);
}
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);
}
}
class IconNotFound extends \Exception {};

View File

@@ -194,4 +194,22 @@ class IconExtensionTest extends TestCase
$this->expectException(\Exception::class);
new IconExtension(['/'], [['stroke' => '']]);
}
public function testBlackStrokeAttributeValuesAreRemoved(): void
{
$content = $this->icon->renderIcon(['icon' => self::ICON]);
$this->assertDoesNotMatchRegularExpression('/stroke="\s*#000\s*"/', $content);
$this->assertDoesNotMatchRegularExpression('/stroke="\s*#000000\s*"/', $content);
$this->assertDoesNotMatchRegularExpression('/stroke="\s*#black\s*"/', $content);
$this->assertDoesNotMatchRegularExpression('/stroke="\s*rgb\(0,\s*0,\s*0\)\s*"/', $content);
}
public function testBlackFillAttributeValuesAreRemoved(): void
{
$content = $this->icon->renderIcon(['icon' => self::ICON]);
$this->assertDoesNotMatchRegularExpression('/fill="\s*#000\s*"/', $content);
$this->assertDoesNotMatchRegularExpression('/fill="\s*#000000\s*"/', $content);
$this->assertDoesNotMatchRegularExpression('/fill="\s*#black\s*"/', $content);
$this->assertDoesNotMatchRegularExpression('/fill="\s*rgb\(0,\s*0,\s*0\)\s*"/', $content);
}
}

View File

@@ -1,5 +1,9 @@
<svg 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 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>
</svg>

Before

Width:  |  Height:  |  Size: 498 B

After

Width:  |  Height:  |  Size: 631 B