svgo icon indicating copy to clipboard operation
svgo copied to clipboard

Removal of prefix option from cleanupIds plugin is a major feature loss

Open jacott opened this issue 2 years ago • 4 comments

In commit bc07c483c3b986c66834b5fe019bb75597dd45ab the prefix option in cleanupIds was removed. In the v2 version this option used to only prefix non preserved ids.

Now the prefixIds plugin conflicts with cleanupIds in a broken manner and causes the removal of preserved ids.

Example

const {optimize} = require('svgo');

const result = optimize(`
<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10">
  <g id="preserveMe">
    <rect id="other" x="0" y="0" width="1" height="1" />
  </g>
</svg>
`, {
  path: 'path-to.svg',
  multipass: true,
  js2svg: {
    indent: 2, // string with spaces or number of spaces. 4 by default
    pretty: true // boolean, false by default
  },
  plugins: [{
    name: 'preset-default',
    params: {
      overrides: {
        cleanupIds: {
          remove: true,
          minify: true,
          preserve: ['preserveMe'],
          force: true
        },
        convertShapeToPath: false,
        collapseGroups: false,
      }
    }
  }, {
    name: 'prefixIds',
    params: {
      prefix: 'myPrefix',
      prefixClassNames: false,
    }
  }],
});
console.log(result.data);

// output  is missing id:
// svg xmlns="http://www.w3.org/2000/svg" width="10" height="10">
//   <g>
//     <rect width="1" height="1"/>
//   </g>
// </svg>

Please restore the prefix option.

jacott avatar Feb 16 '23 03:02 jacott

Putting prefix into cleanupIds is semantically wrong. So let's fix prefixIds instead. What would you like prefix function to return nullable value?

{
  name: 'prefixIds',
  params: {
    prefix: (node) => {
      if (node.id === 'preserveMe') {
        return null
      }
      return 'myPrefix'
    },
    prefixClassNames: false,
  }
}

Or the same preserve option?

{
  name: 'prefixIds',
  params: {
    prefix: 'myPrefix',
    preserve: ['preserveMe'],
    prefixClassNames: false,
  }
}

TrySound avatar Feb 16 '23 07:02 TrySound

I prefer the same preserve option plus the preservePrefixes option. That way same value can be used in cleanupIds and preserveIds

Thanks

jacott avatar Feb 16 '23 07:02 jacott

Could you clarify what preservePrefixes should do?

TrySound avatar Feb 16 '23 07:02 TrySound

The same as in cleanupIds; any id starting with a string listed in preservePrefixes should not have the additional prefix added to it. so if an id is say "foo-123" and preservePrefixes is ["foo-", ...] then "foo-123" should not change to "myPrefix__foo-123" but stay "foo-123"

jacott avatar Feb 16 '23 07:02 jacott