scour-project / scour

Scour - An SVG Optimizer / Cleaner
Apache License 2.0
770 stars 60 forks source link

Referenced element deleted #303

Open anvish2 opened 1 year ago

anvish2 commented 1 year ago

scour breaks SVG by deleting referenced element.

Original file:

<?xml version="1.0" encoding="UTF-8"?>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
 <defs id="defs7285">
  <defs id="defs241">
   <rect id="SVGID_9_" x="423.69" y="131.81" width="350.88" height="413.67"/>
  </defs>
  <clipPath id="SVGID_10_">
   <use id="use245" width="841.89001" height="595.276" xlink:href="#SVGID_9_"/>
  </clipPath>
 </defs>
 <path id="path4615" transform="matrix(1.1464 0 0 1.1712 -486.82 -157.86)" d="m700.52 304.98c13.331-13.353 28.933-28.998 36.853-37.016 2.537-2.568 0.719 99.61 0.719 144.98 0 21.273-13.272 96.323-91.104 58.556 0 0-38.89-13.962-50.247-30.79 2.646 0 5.267-0.169 7.851-0.495 1.183 0.634 2.446 1.226 3.775 1.775 44.896 30.248 71.666 7.998 71.666 7.998 24.757-15.589 28.807-63.192 28.425-69.688" clip-path="url(#SVGID_10_)" fill="#fd0" stroke="#000" stroke-width=".458"/>
</svg>

Compressed file:

<?xml version="1.0" encoding="UTF-8"?>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
 <defs id="defs7285">
  <clipPath id="SVGID_10_">
   <use id="use245" width="841.89001" height="595.276" xlink:href="#SVGID_9_"/>
  </clipPath>
 </defs>
 <path id="path4615" transform="matrix(1.1464 0 0 1.1712 -486.82 -157.86)" d="m700.52 304.98c13.331-13.353 28.933-28.998 36.853-37.016 2.537-2.568 0.719 99.61 0.719 144.98 0 21.273-13.272 96.323-91.104 58.556 0 0-38.89-13.962-50.247-30.79 2.646 0 5.267-0.169 7.851-0.495 1.183 0.634 2.446 1.226 3.775 1.775 44.896 30.248 71.666 7.998 71.666 7.998 24.757-15.589 28.807-63.192 28.425-69.688" clip-path="url(#SVGID_10_)" fill="#fd0" stroke="#000" stroke-width=".458"/>
</svg>

Note that "defs241" is removed despite "SVGID9" being referenced. This leads to broken image.

Workaround is to set "--keep-unreferenced-defs".

JoKalliauer commented 1 year ago

I think you might have a malformed file, since you have a defs in defs, don't know if thats allowed.

defs in defs

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
 <defs>
  <defs>
   <path id="a" d="M423.69 131.81h350.88v413.67H423.69z"/>
  </defs>
  <clipPath id="b">
   <use width="841.89" height="595.28" xlink:href="#a"/>
  </clipPath>                                                                
 </defs>
 <path fill="#fd0" stroke="#000" stroke-width=".458" d="M700.52 304.98c13.331-13.353 28.933-28.998 36.853-37.016 2.537-2.568.719 99.61.719 144.98 0 21.273-13.272 96.323-91.104 58.556 0 0-38.89-13.962-50.247-30.79 2.646 0 5.267-.169 7.851-.495 1.183.634 2.446 1.226 3.775 1.775 44.896 30.248 71.666 7.998 71.666 7.998 24.757-15.589 28.807-63.192 28.425-69.688" clip-path="url(#b)" transform="matrix(1.1464 0 0 1.1712 -486.82 -157.86)"/>
</svg>

good file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
 <defs>
   <path id="a" d="M423.69 131.81h350.88v413.67H423.69z"/>
  <clipPath id="b">
   <use width="841.89" height="595.28" xlink:href="#a"/>
  </clipPath>                                                                
 </defs>
 <path fill="#fd0" stroke="#000" stroke-width=".458" d="M700.52 304.98c13.331-13.353 28.933-28.998 36.853-37.016 2.537-2.568.719 99.61.719 144.98 0 21.273-13.272 96.323-91.104 58.556 0 0-38.89-13.962-50.247-30.79 2.646 0 5.267-.169 7.851-.495 1.183.634 2.446 1.226 3.775 1.775 44.896 30.248 71.666 7.998 71.666 7.998 24.757-15.589 28.807-63.192 28.425-69.688" clip-path="url(#b)" transform="matrix(1.1464 0 0 1.1712 -486.82 -157.86)"/>
</svg>
anvish2 commented 1 year ago

This was excerpt from a real-world example: https://en.wikipedia.org/wiki/Coat_of_arms_of_Ecuador

JoKalliauer commented 1 year ago

This was excerpt from a real-world example: https://en.wikipedia.org/wiki/Coat_of_arms_of_Ecuador

FYI: I repaired https://commons.wikimedia.org/wiki/File:Coat_of_arms_of_Ecuador.svg

https://upload.wikimedia.org/wikipedia/commons/e/e7/Coat_of_arms_of_Ecuador.svg

MJDSys commented 1 year ago

I ran into this issue as well. According to the SVG standard, <defs> can contain structural elements, which includes <defs> (source).

Removing the hierarchy as a workaround does work.