mirror of
https://github.com/svg/svgo.git
synced 2025-07-31 07:44:22 +03:00
Refactor prefixIds (#1561)
Ref https://github.com/svg/svgo/issues/1499 - migrated to visitor plugin api - covered with tsdoc - made the plugin idempotent as requested a few times Now even manually running svgo a few times will not duplicate prefix in ids and classes - run each plugin test twice to see which plugin need to run many times ideally idempotent plugins will allow to get rid of multipass option in v3
This commit is contained in:
@ -1,5 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* @typedef {import('../lib/types').Plugin} Plugin
|
||||
*/
|
||||
|
||||
const { optimize } = require('./svgo.js');
|
||||
|
||||
test('allow to setup default preset', () => {
|
||||
@ -324,3 +328,28 @@ test('provides legacy error message', () => {
|
||||
4 |
|
||||
`);
|
||||
});
|
||||
|
||||
test('multipass option should trigger plugins multiple times', () => {
|
||||
const svg = `<svg id="abcdefghijklmnopqrstuvwxyz"></svg>`;
|
||||
const list = [];
|
||||
/**
|
||||
* @type {Plugin<void>}
|
||||
*/
|
||||
const testPlugin = {
|
||||
type: 'visitor',
|
||||
name: 'testPlugin',
|
||||
fn: (_root, _params, info) => {
|
||||
list.push(info.multipassCount);
|
||||
return {
|
||||
element: {
|
||||
enter: (node) => {
|
||||
node.attributes.id = node.attributes.id.slice(1);
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
const { data } = optimize(svg, { multipass: true, plugins: [testPlugin] });
|
||||
expect(list).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
expect(data).toEqual(`<svg id="klmnopqrstuvwxyz"/>`);
|
||||
});
|
||||
|
Reference in New Issue
Block a user