1
0
mirror of https://github.com/svg/svgo.git synced 2026-01-27 07:02:06 +03:00

Fix plugins order (#1334)

Ref https://github.com/svg/svgo/issues/1333

While refactoring broke plugin order.
This commit is contained in:
Bogdan Chadkin
2021-02-18 17:46:28 +03:00
committed by GitHub
parent efa62c8e30
commit 082aee027d
5 changed files with 38 additions and 23 deletions

View File

@@ -11,31 +11,30 @@
* @return {Object} output data
*/
module.exports = function(data, info, plugins) {
const perItemPlugins = [];
const perItemReversePlugins = [];
const fullPlugins = [];
// Try to group sequential elements of plugins array
// to optimize ast traversing
const groups = [];
let prev;
for (const plugin of plugins) {
switch(plugin.type) {
case 'perItem':
perItemPlugins.push(plugin);
break;
case 'perItemReverse':
perItemReversePlugins.push(plugin);
break;
case 'full':
fullPlugins.push(plugin);
break;
if (prev && plugin.type == prev[0].type) {
prev.push(plugin);
} else {
prev = [plugin];
groups.push(prev);
}
}
if (perItemPlugins.length !== 0) {
data = perItem(data, info, perItemPlugins);
}
if (perItemReversePlugins.length !== 0) {
data = perItem(data, info, perItemReversePlugins, true);
}
if (fullPlugins.length !== 0) {
data = full(data, info, fullPlugins);
for (const group of groups) {
switch(group[0].type) {
case 'perItem':
data = perItem(data, info, group);
break;
case 'perItemReverse':
data = perItem(data, info, group, true);
break;
case 'full':
data = full(data, info, group);
break;
}
}
return data;
};

2
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{
"name": "svgo",
"version": "1.3.2",
"version": "2.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@@ -46,4 +46,9 @@ describe('svgo', () => {
});
expect(normalize(result.data)).to.equal(expected);
});
it('should handle plugins order properly', async () => {
const [original, expected] = await parseFixture('plugins-order.svg');
const result = optimize(original, { input: 'file', path: 'input.svg' });
expect(normalize(result.data)).to.equal(expected);
});
});

View File

@@ -1,7 +1,7 @@
<!-- https://github.com/svg/svgo/pull/258#issue-23706813 -->
<svg xmlns="http://www.w3.org/2000/svg">
<g transform="translate(-39, -239)">
<g id="test" class="test" transform="translate(39, 37)">
<g id="test" transform="translate(39, 37)">
<g>
<rect id="Rectangle-1183" fill="#D4D4D4" x="8" y="223" width="7" height="19"></rect>
<rect id="Rectangle-1183" fill="#D4D4D4" x="4" y="227" width="42" height="3"></rect>

Before

Width:  |  Height:  |  Size: 912 B

After

Width:  |  Height:  |  Size: 899 B

View File

@@ -0,0 +1,11 @@
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" viewBox="0 0 733 801">
<path d="M375.6,443.1"/>
<path d="M289,397.4c7.7,24.8,7.8,47.3,7.8,47.3l-2.2,1.6l-6.7,8.1l-2,7.7l1.1,5.4l4.7,4.7l6.4,4.3l9.3,3.5l12.1,1.2
l23.8-2.4l15.3-6.4l7.4-5.5h0.2l7.9-7.3l3.4-8.3l-2.1-8.3l-4.6-5.2l-1.9-0.5v-0.1l9.3-44.4c77.3-50.5,85.1-214.5,85.1-214.5
s2.1-40.8-0.7-44c-2.8-3.2-28.1-38.7-28.1-38.7l-67.7-18l-82,6L233.7,97l-15.2,59.8l-2.2,15.5C216.3,172.3,208.4,352.8,289,397.4z"
/>
</svg>
@@@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 733 801"><path d="M289 397.4c7.7 24.8 7.8 47.3 7.8 47.3l-2.2 1.6-6.7 8.1-2 7.7 1.1 5.4 4.7 4.7 6.4 4.3 9.3 3.5 12.1 1.2 23.8-2.4 15.3-6.4 7.4-5.5h.2l7.9-7.3 3.4-8.3-2.1-8.3-4.6-5.2-1.9-.5v-.1l9.3-44.4c77.3-50.5 85.1-214.5 85.1-214.5s2.1-40.8-.7-44c-2.8-3.2-28.1-38.7-28.1-38.7l-67.7-18-82 6L233.7 97l-15.2 59.8-2.2 15.5s-7.9 180.5 72.7 225.1z"/></svg>

After

Width:  |  Height:  |  Size: 909 B