Skip to content

Commit

Permalink
fix(reusePaths): dont reuse id if referenced in href
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco committed Sep 23, 2023
1 parent 5f40d8b commit 251c05b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
23 changes: 19 additions & 4 deletions plugins/reusePaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ exports.fn = () => {
*/
let svgDefs;

/**
* Set of hrefs that reference the id of another node.
*
* @type {Set<string>}
*/
const hrefs = new Set();

return {
element: {
enter: (node, parentNode) => {
Expand All @@ -61,13 +68,20 @@ exports.fn = () => {
) {
svgDefs = node;
}

if (node.name === 'use') {
for (const name of ['href', 'xlink:href']) {
const href = node.attributes[name];

if (href != null && href.startsWith('#') && href.length > 1) {
hrefs.add(href.slice(1));
}
}
}
},

exit: (node, parentNode) => {
if (node.name === 'svg' && parentNode.type === 'root') {
/**
* @type {XastElement}
*/
let defsTag = svgDefs;

if (defsTag == null) {
Expand Down Expand Up @@ -99,7 +113,8 @@ exports.fn = () => {
};
delete reusablePath.attributes.transform;
let id;
if (reusablePath.attributes.id == null) {
const reusablePathId = reusablePath.attributes.id;
if (reusablePathId == null || hrefs.has(reusablePathId)) {
id = 'reuse-' + index;
index += 1;
reusablePath.attributes.id = id;
Expand Down
40 changes: 40 additions & 0 deletions test/plugins/reusePaths.06.svg
Loading
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 251c05b

Please sign in to comment.