Skip to content

Commit

Permalink
fix(reusePaths): pass test-regression task (#1791)
Browse files Browse the repository at this point in the history
Signed-off-by: Seth Falco <seth@falco.fun>
  • Loading branch information
SethFalco committed Sep 25, 2023
1 parent 82593c6 commit 078a09c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
43 changes: 27 additions & 16 deletions plugins/reusePaths.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const { collectStylesheet } = require('../lib/style');
const { detachNodeFromParent, querySelectorAll } = require('../lib/xast');

/**
Expand All @@ -22,7 +23,9 @@ exports.description =
*
* @type {import('./plugins-types').Plugin<'reusePaths'>}
*/
exports.fn = () => {
exports.fn = (root) => {
const stylesheet = collectStylesheet(root);

/**
* @type {Map<string, Array<XastElement>>}
*/
Expand Down Expand Up @@ -101,25 +104,31 @@ exports.fn = () => {
let index = 0;
for (const list of paths.values()) {
if (list.length > 1) {
// add reusable path to defs
/**
* @type {XastElement}
*/
/** @type {XastElement} */
const reusablePath = {
type: 'element',
name: 'path',
attributes: { ...list[0].attributes },
attributes: {},
children: [],
};
delete reusablePath.attributes.transform;
let id;
const reusablePathId = reusablePath.attributes.id;
if (reusablePathId == null || hrefs.has(reusablePathId)) {
id = 'reuse-' + index;
index += 1;
reusablePath.attributes.id = id;

for (const attr of ['fill', 'stroke', 'd']) {
if (list[0].attributes[attr] != null) {
reusablePath.attributes[attr] = list[0].attributes[attr];
}
}

const originalId = list[0].attributes.id;
if (
originalId == null ||
hrefs.has(originalId) ||
stylesheet.rules.some(
(rule) => rule.selector === `#${originalId}`
)
) {
reusablePath.attributes.id = 'reuse-' + index++;
} else {
id = reusablePath.attributes.id;
reusablePath.attributes.id = originalId;
delete list[0].attributes.id;
}
// TODO remove legacy parentNode in v4
Expand Down Expand Up @@ -155,7 +164,8 @@ exports.fn = () => {
}
for (const name of ['href', 'xlink:href']) {
if (child.attributes[name] != null) {
child.attributes[name] = '#' + id;
child.attributes[name] =
'#' + reusablePath.attributes.id;
}
}
}
Expand All @@ -164,7 +174,8 @@ exports.fn = () => {
}

pathNode.name = 'use';
pathNode.attributes['xlink:href'] = '#' + id;
pathNode.attributes['xlink:href'] =
'#' + reusablePath.attributes.id;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/plugins/reusePaths.01.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions test/plugins/reusePaths.02.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/plugins/reusePaths.05.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/plugins/reusePaths.06.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 078a09c

Please sign in to comment.