From 8b698bb3fd8193d63d330cf03ba974dc258cf678 Mon Sep 17 00:00:00 2001 From: Bradley Date: Tue, 27 Jun 2023 09:51:26 +0100 Subject: [PATCH] Use references to edit string in place --- src/Twig/Runtime/IconRuntime.php | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Twig/Runtime/IconRuntime.php b/src/Twig/Runtime/IconRuntime.php index 28430c5..47e6da6 100644 --- a/src/Twig/Runtime/IconRuntime.php +++ b/src/Twig/Runtime/IconRuntime.php @@ -84,12 +84,13 @@ final class IconRuntime implements RuntimeExtensionInterface private function getSanitisedIconSvg(string $iconName): string { $iconFilepath = $this->findSvgFilepath($iconName); - $rawSvgMarkup = $this->getSvgMarkup($iconFilepath); - $cleanSvgMarkup = $this->removeExistingTitleElement($rawSvgMarkup); - $cleanSvgMarkup = $this->removeBlackStrokeAttributes($cleanSvgMarkup); - $cleanSvgMarkup = $this->removeStrokeCurrentColor($cleanSvgMarkup); + $svg = $this->getSvgMarkup($iconFilepath); - return $this->removeBlackFillAttributes($cleanSvgMarkup); + $this->removeExistingTitleElement($svg); + $this->removeBlackStrokeAttributes($svg); + $this->removeStrokeCurrentColor($svg); + + return $this->removeBlackFillAttributes($svg); } private function findSvgFilepath(string $iconName): string @@ -110,19 +111,19 @@ final class IconRuntime implements RuntimeExtensionInterface return file_get_contents($filepath); } - private function removeExistingTitleElement(string $svg): string + private function removeExistingTitleElement(string &$svg): void { - return preg_replace('/.*<\/title>/', '', $svg); + $svg = preg_replace('/<title>.*<\/title>/', '', $svg); } - private function removeBlackStrokeAttributes(string $content): string + private function removeBlackStrokeAttributes(string &$svg): void { - return preg_replace('/stroke(=|:)"?\s*(#0{6}|#000|rgb\(\s*0,\s*0,\s*0\s*\)|black)\s*"?/', '', $content); + $svg = preg_replace('/stroke(=|:)"?\s*(#0{6}|#000|rgb\(\s*0,\s*0,\s*0\s*\)|black)\s*"?/', '', $svg); } - private function removeStrokeCurrentColor(string $content): string + private function removeStrokeCurrentColor(string &$svg): void { - return \preg_replace('/stroke=(\'|")currentColor(\'|")/', '', $content); + $svg = \preg_replace('/stroke=(\'|")currentColor(\'|")/', '', $svg); } private function removeBlackFillAttributes(string $content): string