Skip to content

Commit

Permalink
Fix multipass (#1332)
Browse files Browse the repository at this point in the history
Ref #1330 #1148 #1133 #1227 #985 #943
Took tests from #1177
  • Loading branch information
TrySound committed Feb 18, 2021
1 parent 0c85caa commit efa62c8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 39 deletions.
7 changes: 4 additions & 3 deletions lib/svgo.js
Expand Up @@ -23,7 +23,7 @@ const { encodeSVGDatauri } = require('./svgo/tools.js');

exports.extendDefaultPlugins = extendDefaultPlugins;

const optimize = (svgstr, config) => {
const optimize = (input, config) => {
if (config == null) {
config = {};
}
Expand All @@ -38,7 +38,8 @@ const optimize = (svgstr, config) => {
info.path = config.path;
}
for (let i = 0; i < maxPassCount; i += 1) {
svgjs = svg2js(svgstr);
info.multipassCount = i;
svgjs = svg2js(input);
if (svgjs.error == null) {
const plugins = config.plugins || defaultPlugins;
if (Array.isArray(plugins) === false) {
Expand All @@ -51,8 +52,8 @@ const optimize = (svgstr, config) => {
if (svgjs.error) {
throw Error(svgjs.error);
}
info.multipassCount = i;
if (svgjs.data.length < prevResultSize) {
input = svgjs.data;
prevResultSize = svgjs.data.length
} else {
if (config.datauri) {
Expand Down
79 changes: 43 additions & 36 deletions test/svgo/_index.js
@@ -1,42 +1,49 @@
'use strict';

const { expect } = require('chai');
const FS = require('fs');
const PATH = require('path');
const EOL = require('os').EOL;
const regEOL = new RegExp(EOL, 'g');
const { optimize } = require('../../lib/svgo.js');

describe('indentation', function() {

it('should create indent with 2 spaces', function(done) {

var filepath = PATH.resolve(__dirname, './test.svg'),
svgo;

FS.readFile(filepath, 'utf8', function(err, data) {
if (err) {
throw err;
}
const fs = require('fs');
const path = require('path');
const { EOL } = require('os');
const { optimize, extendDefaultPlugins } = require('../../lib/svgo.js');

var splitted = normalize(data).split(/\s*@@@\s*/),
orig = splitted[0],
should = splitted[1];

const result = optimize(orig, {
path: filepath,
plugins : [],
js2svg : { pretty: true, indent: 2 }
});
expect(normalize(result.data)).to.equal(should);
done();

});

});
const regEOL = new RegExp(EOL, 'g');

const normalize = (file) => {
return file.trim().replace(regEOL, '\n');
};

const parseFixture = async file => {
const filepath = path.resolve(__dirname, file);
const content = await fs.promises.readFile(filepath, 'utf-8');
return normalize(content).split(/\s*@@@\s*/);
};

describe('svgo', () => {
it('should create indent with 2 spaces', async () => {
const [original, expected] = await parseFixture('test.svg');
const result = optimize(original, {
plugins: [],
js2svg: { pretty: true, indent: 2 },
});
expect(normalize(result.data)).to.equal(expected);
});
it('should run multiple times', async () => {
const [original, expected] = await parseFixture('multipass.svg');
const result = optimize(original, {
multipass: true,
});
expect(normalize(result.data)).to.equal(expected);
});
it('should pass multipass count to plugins', async () => {
const [original, expected] = await parseFixture('multipass-prefix-ids.svg');
const result = optimize(original, {
multipass: true,
plugins: extendDefaultPlugins([
{
name: 'prefixIds',
},
]),
});
expect(normalize(result.data)).to.equal(expected);
});
});

function normalize(file) {
return file.trim().replace(regEOL, '\n');
}
7 changes: 7 additions & 0 deletions test/svgo/multipass-prefix-ids.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions test/svgo/multipass.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit efa62c8

Please sign in to comment.