diff --git a/lib/svgo.js b/lib/svgo.js index 1eacb4cf4..d93652fc3 100755 --- a/lib/svgo.js +++ b/lib/svgo.js @@ -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 = {}; } @@ -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) { @@ -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) { diff --git a/test/svgo/_index.js b/test/svgo/_index.js index fa3ab9f0e..b2fd2169f 100644 --- a/test/svgo/_index.js +++ b/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'); -} diff --git a/test/svgo/multipass-prefix-ids.svg b/test/svgo/multipass-prefix-ids.svg new file mode 100644 index 000000000..d61bcd8d9 --- /dev/null +++ b/test/svgo/multipass-prefix-ids.svg @@ -0,0 +1,7 @@ + + + + +@@@ + + diff --git a/test/svgo/multipass.svg b/test/svgo/multipass.svg new file mode 100644 index 000000000..424324b0b --- /dev/null +++ b/test/svgo/multipass.svg @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + +@@@ + +