Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multipass #1332

Merged
merged 1 commit into from Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.