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

fix(plugin): removeAttrs: warn without attrs #1582

Merged
merged 1 commit into from Sep 23, 2021

Conversation

samouss
Copy link
Contributor

@samouss samouss commented Sep 20, 2021

This is a follow-up of #1560 and should fix #1581. The PR attempt to fix an issue when the plugin is called without the attrs option. The code expects that the attrs option is present and throw an error when it's not because we end up having attrs = [undefined] (see below).

Technically this should be an error but it would be a breaking change. We can start by introducing a warning (what this PR does) and in a future major version throw an error. I added a test where input = output when attrs is not provided i.e. noop.

TypeError: Cannot read properties of undefined (reading 'includes')

      94 |         for (let pattern of attrs) {
      95 |           // if no element separators (:), assume it's attribute name, and apply to all elements *regardless of value*
    > 96 |           if (pattern.includes(elemSeparator) === false) {
         |                       ^
      97 |             pattern = ['.*', elemSeparator, pattern, elemSeparator, '.*'].join(
      98 |               ''
      99 |             );

      at Object.enter (plugins/removeAttrs.js:96:23)
      at visit (lib/xast.js:68:30)
      at visit (lib/xast.js:77:7)
      at invokePlugins (lib/svgo/plugins.js:38:11)
      at optimize (lib/svgo.js:55:13)
      at test/plugins/_index.test.js:38:28

Comment on lines +1 to +17
<svg xmlns="http://www.w3.org/2000/svg">
<circle fill="red" stroke-width="6" stroke-dashoffset="5" cx="60" cy="60" r="50"/>
<circle fill="red" stroke="#000" stroke-width="6" stroke-dashoffset="5" stroke-opacity="0" cx="60" cy="60" r="50"/>
<circle stroke="#000" stroke-width="6" stroke-dashoffset="5" stroke-opacity="0" cx="60" cy="60" r="50"/>
<circle stroke="#FFF" stroke-width="6" stroke-dashoffset="5" stroke-opacity="0" cx="60" cy="60" r="25"/>
<path fill="red" stroke="red" d="M100,200 300,400 H100 V300 C100,100 250,100 250,200 S400,300 400,200 Q400,50 600,300 T1000,300 z"/>
</svg>

@@@

<svg xmlns="http://www.w3.org/2000/svg">
<circle fill="red" stroke-width="6" stroke-dashoffset="5" cx="60" cy="60" r="50"/>
<circle fill="red" stroke="#000" stroke-width="6" stroke-dashoffset="5" stroke-opacity="0" cx="60" cy="60" r="50"/>
<circle stroke="#000" stroke-width="6" stroke-dashoffset="5" stroke-opacity="0" cx="60" cy="60" r="50"/>
<circle stroke="#FFF" stroke-width="6" stroke-dashoffset="5" stroke-opacity="0" cx="60" cy="60" r="25"/>
<path fill="red" stroke="red" d="M100,200 300,400 H100 V300 C100,100 250,100 250,200 S400,300 400,200 Q400,50 600,300 T1000,300 z"/>
</svg>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The input must be equal to the output because without attrs the plugin is a noop.

@samouss samouss marked this pull request as ready for review September 20, 2021 18:38
@TrySound TrySound merged commit 8af10de into svg:master Sep 23, 2021
@samouss samouss deleted the fix/remove-attrs-no-attrs branch September 23, 2021 20:51
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

Successfully merging this pull request may close these issues.

TypeError: Cannot read property 'includes' of undefined
2 participants