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(reusePaths): pass test-regression task #1791

Merged
merged 1 commit into from
Sep 25, 2023
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
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
Loading
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
Loading
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
Loading
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading