Skip to content

Commit

Permalink
Refactor prefixIds (#1561)
Browse files Browse the repository at this point in the history
Ref #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
  • Loading branch information
TrySound authored Sep 13, 2021
1 parent 23c7f48 commit 3d22a5b
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 312 deletions.
29 changes: 29 additions & 0 deletions lib/svgo.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'use strict';

/**
* @typedef {import('../lib/types').Plugin} Plugin
*/

const { optimize } = require('./svgo.js');

test('allow to setup default preset', () => {
Expand Down Expand Up @@ -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"/>`);
});
11 changes: 10 additions & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,16 @@ export type Visitor = {
root?: VisitorRoot;
};

export type Plugin<Params> = (root: XastRoot, params: Params) => null | Visitor;
export type PluginInfo = {
path?: string;
multipassCount: number;
};

export type Plugin<Params> = (
root: XastRoot,
params: Params,
info: PluginInfo
) => null | Visitor;

export type Specificity = [number, number, number, number];

Expand Down
Loading

0 comments on commit 3d22a5b

Please sign in to comment.