Skip to content

addClassesToSVGElement doesn't clear out existing classes #2209

@anselmbradford

Description

@anselmbradford

Describe the bug

The docs say:

If there is no class attribute to begin with, it will be added. However, if the[sic] there were already classes assigned, these are removed and replaced with the classes configured in this plugin.

However, this does not appear to be the case because the logic inside the addClassesToSVGElement builds its classList from the existing classes that may be present here.

To Reproduce
Steps to reproduce the behavior:

  1. Take an existing .svg file and ensure it has a class field on the svg element and has an existing classname, such as class="first".
  2. Add config for the addClassesToSVGElement, such as:
   {
      name: 'addClassesToSVGElement',
      params: {
        className: 'second',
      },
    }
  1. Run svgo on the .svg file.
  2. See that the class field now says class="first second", instead of class="second".

Expected behavior
The plugin should clear out existing classes and/or not say in the docs it should clear out the existing classes and add the option to clear them out.

  • SVGO Version 4.0.1
  • Node.js Version v25.6.1
  • OS: macOS 15.7.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions