1
0
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:
Bogdan Chadkin
2021-09-13 16:16:38 +03:00
committed by GitHub
parent 23c7f48130
commit 3d22a5b23d
9 changed files with 286 additions and 318 deletions

View File

@ -30,14 +30,21 @@ describe('plugins tests', function () {
name,
params: params ? JSON.parse(params) : {},
};
const result = optimize(original, {
path: file,
plugins: [plugin],
js2svg: { pretty: true },
});
expect(result.error).not.toEqual(expect.anything());
//FIXME: results.data has a '\n' at the end while it should not
expect(normalize(result.data)).toEqual(should);
let lastResultData = original;
// test plugins idempotence
const exclude = ['addAttributesToSVGElement', 'convertTransform'];
const multipass = exclude.includes(name) ? 1 : 2;
for (let i = 0; i < multipass; i += 1) {
const result = optimize(lastResultData, {
path: file,
plugins: [plugin],
js2svg: { pretty: true },
});
lastResultData = result.data;
expect(result.error).not.toEqual(expect.anything());
//FIXME: results.data has a '\n' at the end while it should not
expect(normalize(result.data)).toEqual(should);
}
});
});
}

View File

@ -0,0 +1,24 @@
'use strict';
const { optimize } = require('../../lib/svgo.js');
test('should extract prefix from path basename', () => {
const svg = `<svg id="my-id"></svg>`;
expect(
optimize(svg, {
plugins: ['prefixIds'],
}).data
).toEqual(`<svg id="prefix__my-id"/>`);
expect(
optimize(svg, {
plugins: ['prefixIds'],
path: 'input.svg',
}).data
).toEqual(`<svg id="input_svg__my-id"/>`);
expect(
optimize(svg, {
plugins: ['prefixIds'],
path: 'path/to/input.svg',
}).data
).toEqual(`<svg id="input_svg__my-id"/>`);
});