darylldoyle / svg-sanitizer

A PHP SVG/XML Sanitizer
GNU General Public License v2.0
454 stars 67 forks source link

Check/Remove unused id-attributes #67

Closed xerc closed 2 years ago

xerc commented 2 years ago

INPUT

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 145 45">
  <metadata id="metadata"></metadata>
  <g id="whatever">
    <path id="path123"/>
  </g>
</svg>

OUTPUT

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 145 45">
  <metadata></metadata>
  <g>
    <path/>
  </g>
</svg>
xerc commented 2 years ago

optional/ also remove empty TAGs; /<([a-z]+)\s*>\s*<\/\1>\s*/i

ohader commented 2 years ago

External consumers might reference to a specific section using the id attribute - finding "unused" ids seems to be impossible in this scenario, e.g.

<img src="https://example.com/sprites.svg#some-icon">
<svg><use href="https://example.com/sprites.svg#some-icon"></use></svg>

Cleaning out SVG elements that don't have any children might happen by introducing a new removeEmptyElements($flags), and flags e.g. being enum constants NO_ATTRS, NO_CHILDREN. In any way, I think consumers of this library should explicitly use this particular behavior, I would not make it part of the default processing.

xerc commented 2 years ago

definitely ; but this ID should be in the root line of or not within the content - or?

ohader commented 2 years ago

There are no restrictions to which nodes and how deep down in document hierarchy id attributes are assigned.

xerc commented 2 years ago

true