Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multipass, addClassesToSVGElement duplicating classes #659

Closed
Matt-Butler opened this issue Feb 10, 2017 · 13 comments
Closed

Multipass, addClassesToSVGElement duplicating classes #659

Matt-Butler opened this issue Feb 10, 2017 · 13 comments

Comments

@Matt-Butler
Copy link

const svgoConfig = (isClass1, isClass2, isClass3) => {
  const classes = classNames({ 'class1': isClass1 }, { 'class2': isClass2 }, { 'class3': isClass3 });

  return {
    multipass: true,
    floatPrecision: 2,
    plugins: [
      {
        mergePaths: false,
      },
      {
        removeTitle: true,
      },
      {
        addAttributesToSVGElement: {
          attribute: 'aria-hidden="true"',
        },
      },
      {
        addClassesToSVGElement: {
          className: classes,
        },
      },
    ],
  };
};

The class names are duplicated on the SVG.

@GreLI
Copy link
Member

GreLI commented Feb 11, 2017

Yep, multipass literally runs SVGO several times with all its plugins. Probably, it worth consider for class duplication checking before applying. /cc @AprilArcus.

@AprilArcus
Copy link
Contributor

Thanks, I'll take this issue.

@Matt-Butler
Copy link
Author

Thanks @AprilArcus @GreLI

@mdmoreau
Copy link

@AprilArcus @GreLI Has there been any progress on this issue? I'm running into the same sort of problem when using multipass with prefixIds or when using prefixIds on a file that has already had classes prefixed.

@strarsis
Copy link
Contributor

strarsis commented Oct 30, 2019

@mdmoreau: I am just writing some tests in svgo for multipass and I noticed that addClassesToSVGElement with its current code correctly adds classes only once.
addAttributesToSVGElement isn't idempotent though as I noticed during testing.

@strarsis
Copy link
Contributor

strarsis commented Oct 30, 2019

@Matt-Butler: So I wrote some tests for addAttributesToSVGElement and addClassesToSVGElement plugin and they both work with multipass...

@mdmoreau
Copy link

@strarsis I'm still seeing the same behavior on 1.3.2 unfortunately. Setup a demo at https://github.com/mdmoreau/svgo-demo where an additional class prefix gets added each time SVGO is run.

@strarsis
Copy link
Contributor

@mdmoreau: Yes, there is a bug that was uncovered with multipass tests and there is a yet unmerged PR to fix it, see #1177.

@mdmoreau
Copy link

mdmoreau commented Nov 1, 2019

@strarsis Tried that on my demo, and noticed a couple of things:

  1. IDs are no longer prefixed (worked correctly before)
  2. The class prefixes don't get duplicated by multipass, but they do get duplicated when re-running the same file through SVGO again

@strarsis
Copy link
Contributor

strarsis commented Nov 1, 2019

@mdmoreau: That they are still prefixed multiple times when they are passed multiple times to svgo is an intended behavior.

@mdmoreau
Copy link

mdmoreau commented Nov 1, 2019

@strarsis Oh okay - wasn't aware of that. Was hoping that there was some sort of check to see if the prefix existed before re-adding it. Is there a reason something like that couldn't be added?

@strarsis
Copy link
Contributor

strarsis commented Nov 1, 2019

@mdmoreau: The reason for all the issues is that the gulp plugin for svgo enables the multipass feature of svgo by default. The SVG file passed to that plugin, only once, is passed multiple times through all the enabled plugins, including the prefixIds plugin. So when you are using the gulp plugin for svgo, the SVG files you pass to it are passed multiple times through all the plugins, including prefixIds.
A fix has been added to svgo some days ago that sadly required another fix right afterwards because of the missing tests in svgo for multipass, it caused issues when no info object is passed to svgo. See this PR: #1177

@TrySound
Copy link
Member

TrySound commented Sep 1, 2021

addClassesToSVGElement does not add duplicated classes for some time. addAttributesToSVGElement produces duplicated attribute because the whole aria-hidden="true" string is treated as key.

{
    'aria-hidden': 'true',
    'aria-hidden="true"': undefined
}

Better use object { 'aria-hidden': 'true' '}

@TrySound TrySound closed this as completed Sep 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants