svg / svgo

⚙️ Node.js tool for optimizing SVG files
https://svgo.dev/
MIT License
21.03k stars 1.39k forks source link

fix: don't insert 0 at start or end of attribute if whitespace #2036

Closed SethFalco closed 5 months ago

SethFalco commented 5 months ago

Docs

Updates the documentation to clarify explicitly that the plugin impacts whitespace. Internally, it's because we parse all numbers in the attribute into an array, then rewrite it back.

Bug Fix

I also fix a bug that I discovered while testing the issue reported. When an attribute like " 0 0 150 100 " comes up, SVGO inserts a 0 to the start and end of the attribute, completely changing the meaning.

This was because when we do the split, the array we get back starts with an empty string and ends with an empty string, which were then converted to 0, resulting in "0 0 0 150 100 0" instead of "0 0 150 100".

seth@seth-pc-tux:~$ node
Welcome to Node.js v20.11.0.
Type ".help" for more information.
> Number("")
0

Now we filter them out before parsing.

Performance

Just applying a recommendation from SonarLint, use RegExp#exec instead of String#match.

Related