From c75369e0fa03c3f7cdd3f26ed5a7603c7591a24b Mon Sep 17 00:00:00 2001 From: Brabli <67018167+Brabli@users.noreply.github.com> Date: Sun, 14 Aug 2022 19:41:31 +0100 Subject: [PATCH] Remove fill and stroke attributes where their value was black --- src/Twig/Functions/IconExtension.php | 12 ++++++++++++ tests/Twig/Functions/IconExtensionTest.php | 18 ++++++++++++++++++ tests/icons/test.svg | 4 ++++ 3 files changed, 34 insertions(+) diff --git a/src/Twig/Functions/IconExtension.php b/src/Twig/Functions/IconExtension.php index c1e0c76..0a09e09 100644 --- a/src/Twig/Functions/IconExtension.php +++ b/src/Twig/Functions/IconExtension.php @@ -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 {}; diff --git a/tests/Twig/Functions/IconExtensionTest.php b/tests/Twig/Functions/IconExtensionTest.php index dbb1d8b..2791ca6 100644 --- a/tests/Twig/Functions/IconExtensionTest.php +++ b/tests/Twig/Functions/IconExtensionTest.php @@ -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); + } } diff --git a/tests/icons/test.svg b/tests/icons/test.svg index 0296bda..9a2211c 100644 --- a/tests/icons/test.svg +++ b/tests/icons/test.svg @@ -1,5 +1,9 @@ Some cross lol + + + +