Skip to content

Commit

Permalink
Initialize multipassCounter with 0 at start.
Browse files Browse the repository at this point in the history
Add test from PR #1182.
  • Loading branch information
strarsis committed Jan 4, 2020
2 parents b5357d3 + 07ca976 commit 70c8a33
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,6 @@
### [ [>](https://github.com/svg/svgo/tree/v1.3.2) ] 1.3.2 / 30.10.2019
* Fixed TypeError: Cannot set property 'multipassCount' of undefined

### [ [>](https://github.com/svg/svgo/tree/v1.3.1) ] 1.3.1 / 29.10.2019
* Updated CSSO version to 4.0.2 fixing the issue with empty semicolons ";;" in styles (thanks to @strarsis and @lahmatiy).
* `prefixIds` plugin now runs only once with `--multipass` option (by @strarsis).
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -197,6 +197,7 @@ Arguments:
* as a Sketch plugin - [svgo-compressor](https://github.com/BohemianCoding/svgo-compressor)
* as macOS app - [Image Shrinker](https://image-shrinker.com)
* as a Rollup plugin - [rollup-plugin-svgo](https://github.com/porsager/rollup-plugin-svgo)
* as a Figma plugin - [Advanced SVG Export](https://www.figma.com/c/plugin/782713260363070260/Advanced-SVG-Export)

## Backers

Expand Down
1 change: 1 addition & 0 deletions README.ru.md
Expand Up @@ -197,6 +197,7 @@ $ [sudo] npm install -g svgo
* как плагин для Sketch - [svgo-compressor](https://github.com/BohemianCoding/svgo-compressor)
* в виде приложения macOS - [Image Shrinker](https://image-shrinker.com)
* как плагин для Rollup - [rollup-plugin-svgo](https://github.com/porsager/rollup-plugin-svgo)
* как плагин для Figma - [Advanced SVG Export](https://www.figma.com/c/plugin/782713260363070260/Advanced-SVG-Export)

## Лицензия и копирайты

Expand Down
5 changes: 4 additions & 1 deletion lib/svgo.js
Expand Up @@ -22,6 +22,9 @@ 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 Down Expand Up @@ -74,7 +77,7 @@ SVGO.prototype._optimizeOnce = function(svgstr, info, callback) {
/**
* The factory that creates a content item with the helper methods.
*
* @param {Object} data which passed to jsAPI constructor
* @param {Object} data which is passed to jsAPI constructor
* @returns {JSAPI} content item
*/
SVGO.prototype.createContentItem = function(data) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "svgo",
"version": "1.3.1",
"version": "1.3.2",
"description": "Nodejs-based tool for optimizing SVG vector graphics files",
"keywords": [
"svgo",
Expand Down
11 changes: 7 additions & 4 deletions plugins/cleanupIDs.js
Expand Up @@ -58,15 +58,18 @@ exports.fn = function(data, params) {

// quit if <style> or <script> present ('force' param prevents quitting)
if (!params.force) {
if (item.isElem(styleOrScript)) {
var isNotEmpty = Boolean(item.content);
if (item.isElem(styleOrScript) && isNotEmpty) {
hasStyleOrScript = true;
continue;
}

// Don't remove IDs if the whole SVG consists only of defs.
if (item.isElem('defs') && item.parentNode.isElem('svg')) {
if (item.isElem('svg')) {
var hasDefsOnly = true;
for (var j = i + 1; j < items.content.length; j++) {
if (items.content[j].isElem()) {

for (var j = 0; j < item.content.length; j++) {
if (!item.content[j].isElem('defs')) {
hasDefsOnly = false;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/convertPathData.js
Expand Up @@ -514,7 +514,7 @@ function filters(path, params) {
instruction == prev.instruction.toLowerCase() &&
(
(instruction != 'h' && instruction != 'v') ||
(prev.data[0] >= 0) == (item.data[0] >= 0)
(prev.data[0] >= 0) == (data[0] >= 0)
)) {
prev.data[0] += data[0];
if (instruction != 'h' && instruction != 'v') {
Expand Down
31 changes: 31 additions & 0 deletions plugins/prefixIds.js
Expand Up @@ -113,6 +113,35 @@ var addPrefixToUrlAttr = function(attr) {
attr.value = 'url(' + idPrefixed + ')';
};

// prefixes begin/end attribute value
var addPrefixToBeginEndAttr = function(attr) {
if (!attrNotEmpty(attr)) {
return;
}

var parts = attr.value.split('; ').map(function(val) {
val = val.trim();

if (val.endsWith('.end') || val.endsWith('.start')) {
var idPostfix = val.split('.'),
id = idPostfix[0],
postfix = idPostfix[1];

var idPrefixed = prefixId(`#${id}`);

if (!idPrefixed) {
return val;
}

idPrefixed = idPrefixed.slice(1);
return `${idPrefixed}.${postfix}`;
} else {
return val;
}
});

attr.value = parts.join('; ');
};

/**
* Prefixes identifiers
Expand Down Expand Up @@ -238,6 +267,8 @@ exports.fn = function(node, opts, extra) {
addPrefixToUrlAttr(node.attrs[referencesProp]);
}

addPrefixToBeginEndAttr(node.attrs.begin);
addPrefixToBeginEndAttr(node.attrs.end);

return node;
};
4 changes: 2 additions & 2 deletions test/plugins/cleanupIDs.02.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions test/plugins/cleanupIDs.19.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions test/plugins/cleanupIDs.20.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions test/plugins/convertPathData.04.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions test/plugins/prefixIds.10.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 70c8a33

Please sign in to comment.