Skip to content

Commit

Permalink
Merge 70c8a33 into 07ca976
Browse files Browse the repository at this point in the history
  • Loading branch information
strarsis committed Jan 4, 2020
2 parents 07ca976 + 70c8a33 commit 384ca23
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 13 deletions.
5 changes: 3 additions & 2 deletions lib/svgo.js 100755 → 100644
Expand Up @@ -23,6 +23,8 @@ var SVGO = function(config) {

SVGO.prototype.optimize = function(svgstr, info) {
info = info || {};
info.multipassCount = 0;

return new Promise((resolve, reject) => {
if (this.config.error) {
reject(this.config.error);
Expand All @@ -39,8 +41,7 @@ SVGO.prototype.optimize = function(svgstr, info) {
return;
}

info.multipassCount = counter;
if (++counter < maxPassCount && svgjs.data.length < prevResultSize) {
if ((info.multipassCount = ++counter) < maxPassCount && svgjs.data.length < prevResultSize) {
prevResultSize = svgjs.data.length;
this._optimizeOnce(svgjs.data, info, optimizeOnceCallback);
} else {
Expand Down
96 changes: 85 additions & 11 deletions test/coa/_index.js
@@ -1,12 +1,23 @@
'use strict';

const fs = require('fs'),
yaml = require('js-yaml'),
svgo = require(process.env.COVERAGE ?
'../../lib-cov/svgo/coa.js' :
'../../lib/svgo/coa.js').api,
defaults = Object.assign({}, yaml.safeLoad(fs.readFileSync(__dirname + '/../../.svgo.yml', 'utf8'))),
path = require('path'),
svgPath = path.resolve(__dirname, 'test.svg'),
svgFolderPath = path.resolve(__dirname, 'testSvg'),

prefixIdsFolderPath = path.resolve(__dirname, 'testPrefixIds'),
prefixIdsSvgInPath = path.resolve(prefixIdsFolderPath, 'in.svg'),

mpDirPath = path.resolve(__dirname, 'testMultipass'),
mpSvgInPath = path.resolve(mpDirPath, 'in.svg'),
mpSvgExpPath = path.resolve(mpDirPath, 'out.svg'),
mpSvgExp = fs.readFileSync(mpSvgExpPath, 'utf8'),

svgFolderPathRecursively = path.resolve(__dirname, 'testSvgRecursively'),
svgFiles = [path.resolve(__dirname, 'testSvg/test.svg'), path.resolve(__dirname, 'testSvg/test.1.svg')],
tempFolder = 'temp',
Expand Down Expand Up @@ -157,6 +168,80 @@ describe('coa', function() {
}
});

it('should pass the filename to the prefixIds plugin', function(done) {
svgo({
input: prefixIdsSvgInPath,
output: 'temp.svg',
quiet: true,
multipass: false,
disable: defaults.plugins, // disable all plugins except ...
enable: [ 'prefixIds' ] // ... prefixIds
}).then(function() {
const svgOut = fs.readFileSync('temp.svg', 'utf8');

done(/in_svg__/.test(svgOut) ? null : 'filename isn\'t passed to prefixIds plugin.');
fse.removeSync('temp.svg');
}, error => done(error));
});

describe('multipass', function() {
it('should optimize using multiple passes with multipass enabled', function(done) {

svgo({
input: mpSvgInPath,
output: 'temp.svg',
quiet: true,
multipass: true
}).then(function() {
const mpSvgOut = fs.readFileSync('temp.svg', 'utf8');

done(mpSvgOut === mpSvgExp ? null : 'Multipass wasn\'t properly used.');
fse.removeSync('temp.svg');
}, error => done(error));
});

it('should allow prefixId plugin to detect subsequent passes with multipass enabled', function(done) {
svgo({
input: mpSvgInPath,
output: 'temp.svg',
quiet: true,
multipass: true,
disable: defaults.plugins, // disable all plugins except ...
enable: [ 'prefixIds' ] // ... prefixIds
}).then(function() {
const mpSvgOut = fs.readFileSync('temp.svg', 'utf8');

done(!/in_svg__in_svg__/.test(mpSvgOut) ? null : 'prefixIds plugin doesn\'t detect subsequent passes with multipass enabled.');

// https://github.com/svg/svgo/issues/659
// https://github.com/svg/svgo/issues/1133
fse.removeSync('temp.svg');
}, error => done(error));
});

it('should allow addAttributesToSVGElement plugin to correctly handle subsequent passes with multipass enabled', function(done) {
svgo({
input: mpSvgInPath,
output: 'temp.svg',
quiet: true,
multipass: true,
config: `{
"plugins": [{ "addAttributesToSVGElement": {
"attribute": "aria-hidden=\\"true\\""
} }]
}`
}).then(function() {
const mpSvgOut = fs.readFileSync('temp.svg', 'utf8');

done(!/aria-hidden="true" aria-hidden='true'/.test(mpSvgOut) ? null : 'addAttributesToSVGElement plugin doesn\'t correctly handle subsequent passes with multipass enabled.');

// https://github.com/svg/svgo/issues/659
// https://github.com/svg/svgo/issues/1133
fse.removeSync('temp.svg');
}, error => done(error));
});
});

describe('stdout', function() {
it('should show file content when no output set', function(done) {
replaceConsoleLog();
Expand Down Expand Up @@ -197,16 +282,5 @@ describe('coa', function() {
'Error "No SVG files have been found" was not shown');
}
});

it('should show plugins', function(done) {
replaceConsoleLog();

svgo({ 'show-plugins': true }).then(onComplete, onComplete);

function onComplete() {
restoreConsoleLog();
done(/Currently available plugins:/.test(output) ? null : 'List of plugins was not shown');
}
});
});
});
14 changes: 14 additions & 0 deletions test/coa/testMultipass/in.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/coa/testMultipass/out.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/coa/testPrefixIds/in.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions test/svgo/_index.js
Expand Up @@ -41,6 +41,39 @@ describe('indentation', function() {

});

describe('invocation', function() {

it('should optimize without an info object', function(done) {

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

FS.readFile(filepath, 'utf8', function(err, data) {
if (err) {
throw err;
}

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

svgo = new SVGO({
full : true,
plugins : [],
js2svg : { pretty: true, indent: 2 }
});

svgo.optimize(orig, undefined).then(function(result) {
normalize(result.data).should.be.equal(should);
done();
});

});

});

});

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

0 comments on commit 384ca23

Please sign in to comment.