Skip to content

Commit

Permalink
Remove comments from svg
Browse files Browse the repository at this point in the history
  • Loading branch information
Gappa committed Nov 28, 2023
1 parent 6d3a21c commit 055380f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
26 changes: 23 additions & 3 deletions src/SvgFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}


Expand Down
8 changes: 5 additions & 3 deletions tests/fixtures/test.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 055380f

Please sign in to comment.