Skip to content

Commit

Permalink
fix(cleanupIds): handle no-uri-encoded references (#1982)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuweifeng committed Apr 25, 2024
1 parent 888d078 commit 89b0c6c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
7 changes: 3 additions & 4 deletions plugins/cleanupIds.js
Expand Up @@ -232,10 +232,9 @@ export const fn = (_root, params) => {
const value = element.attributes[name];
if (value.includes('#')) {
// replace id in href and url()
element.attributes[name] = value.replace(
`#${encodeURI(id)}`,
`#${currentIdString}`,
);
element.attributes[name] = value
.replace(`#${encodeURI(id)}`, `#${currentIdString}`)
.replace(`#${id}`, `#${currentIdString}`);
} else {
// replace id in begin attribute
element.attributes[name] = value.replace(
Expand Down
25 changes: 25 additions & 0 deletions test/plugins/cleanupIds.26.svg.txt
@@ -0,0 +1,25 @@
Should handle non-ASCII IDs and resolve not correctly URI encoded references.

See: https://github.com/svg/svgo/issues/1981

===

<svg xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="渐变_1" x1="0%" y1="0%" x2="100%" y2="0%">
<stop stop-color="#5a2100" />
</linearGradient>
</defs>
<rect x="30" y="30" height="150" width="370" fill="url(#渐变_1)" />
</svg>

@@@

<svg xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="a" x1="0%" y1="0%" x2="100%" y2="0%">
<stop stop-color="#5a2100"/>
</linearGradient>
</defs>
<rect x="30" y="30" height="150" width="370" fill="url(#a)"/>
</svg>

0 comments on commit 89b0c6c

Please sign in to comment.