darylldoyle / svg-sanitizer

A PHP SVG/XML Sanitizer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Check/Remove unused id-attributes

xerc opened this issue · comments

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>

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

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.

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

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