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(cleanupIds): handle no-uri-encoded references #1982

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions plugins/cleanupIds.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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>