diff --git a/src/SvgFilter.php b/src/SvgFilter.php index 066b080..57a5e10 100644 --- a/src/SvgFilter.php +++ b/src/SvgFilter.php @@ -5,6 +5,9 @@ use DOMDocument; use DOMElement; +use DOMNode; +use DOMNodeList; +use DOMXPath; use Nelson\SvgFilter\DI\SvgFilterConfig; use Nette\Caching\Cache; use Nette\Caching\Storage; @@ -61,10 +64,27 @@ public function getSvg(string $filepath): ?string $document = $this->getDOMDocument($content); $element = $this->getSVGElement($document); - $result = new DOMDocument; - $result->appendChild($result->importNode($element, true)); + $dom = new DOMDocument; + $dom->appendChild($dom->importNode($element, true)); - return $this->saveHtml($result); + $this->removeComments($dom); + return $this->saveHtml($dom); + } + + + private function removeComments(DOMDocument $document): void + { + $xpath = new DOMXPath($document); + $comments = $xpath->query('//comment()'); + + if (!$comments instanceof DOMNodeList) { + return; + } + + /** @var DOMNode $comment */ + foreach ($comments as $comment) { + $comment->parentNode?->removeChild($comment); + } } diff --git a/tests/fixtures/test.svg b/tests/fixtures/test.svg index 3171c49..27254c7 100644 --- a/tests/fixtures/test.svg +++ b/tests/fixtures/test.svg @@ -1,3 +1,5 @@ - - - \ No newline at end of file + + + + + \ No newline at end of file