Skip to content

Commit

Permalink
Fix prefixIds plugin to properly handle url()s in style="..." (#1592)
Browse files Browse the repository at this point in the history
`prefixIds` plugin currently breaks url()-links inside `style` attributes:

```javascript
optimize(
  `<g style="fill:url(#brush-id);stroke:url(#pen-id)"/>`,
  { plugins: ['prefixIds'] }
).data
```
will generate `<g style=""/>`. Seems like `prefixIds` assumes that attribute's whole value might be `url()`, but this is not the case for the `style` attribute.

This fix solves the issue by preserving all attribute's content other than #id inside url(). It also adds some more tests for the `prefixIds` plugin.
  • Loading branch information
IlyaSkriblovsky committed Sep 30, 2021
1 parent 203db9a commit 6235264
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
17 changes: 9 additions & 8 deletions plugins/prefixIds.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,16 @@ exports.fn = (_root, params, info) => {
node.attributes[name] != null &&
node.attributes[name].length !== 0
) {
// extract id reference from url(...) value
const matches = /url\((.*?)\)/gi.exec(node.attributes[name]);
if (matches != null) {
const value = matches[1];
const prefixed = prefixReference(prefix, value);
if (prefixed != null) {
node.attributes[name] = `url(${prefixed})`;
node.attributes[name] = node.attributes[name].replace(
/url\((.*?)\)/gi,
(match, url) => {
const prefixed = prefixReference(prefix, url);
if (prefixed == null) {
return match;
}
return `url(${prefixed})`;
}
}
);
}
}

Expand Down
21 changes: 21 additions & 0 deletions test/plugins/prefixIds.11.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6235264

Please sign in to comment.