mirror of
https://github.com/svg/svgo.git
synced 2025-08-06 04:22:39 +03:00
make passes to be independent from each other
This commit is contained in:
41
lib/svgo.js
41
lib/svgo.js
@@ -24,6 +24,33 @@ var SVGO = module.exports = function(config) {
|
|||||||
|
|
||||||
SVGO.prototype.optimize = function(svgstr, callback) {
|
SVGO.prototype.optimize = function(svgstr, callback) {
|
||||||
|
|
||||||
|
var _this = this,
|
||||||
|
config = this.config,
|
||||||
|
maxPassCount = config.multipass ? 10 : 1,
|
||||||
|
counter = 0,
|
||||||
|
prevResultSize = Number.POSITIVE_INFINITY,
|
||||||
|
optimizeOnceCallback = function(svgjs) {
|
||||||
|
|
||||||
|
if (svgjs.error) {
|
||||||
|
callback(svgjs);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (++counter < maxPassCount && svgjs.data.length < prevResultSize) {
|
||||||
|
prevResultSize = svgjs.data.length;
|
||||||
|
_this._optimizeOnce(svgjs.data, optimizeOnceCallback);
|
||||||
|
} else {
|
||||||
|
callback(svgjs);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
_this._optimizeOnce(svgstr, optimizeOnceCallback);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
SVGO.prototype._optimizeOnce = function(svgstr, callback) {
|
||||||
|
|
||||||
var config = this.config;
|
var config = this.config;
|
||||||
|
|
||||||
SVG2JS(svgstr, function(svgjs) {
|
SVG2JS(svgstr, function(svgjs) {
|
||||||
@@ -33,24 +60,14 @@ SVGO.prototype.optimize = function(svgstr, callback) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var svg = { data : null },
|
svgjs = PLUGINS(svgjs, config.plugins);
|
||||||
maxIterationCount = config.multipass ? 10 : 1,
|
|
||||||
counter = 0,
|
|
||||||
prevResult;
|
|
||||||
|
|
||||||
do {
|
callback(JS2SVG(svgjs, config.js2svg));
|
||||||
prevResult = svg;
|
|
||||||
svgjs = PLUGINS(svgjs, config.plugins);
|
|
||||||
svg = JS2SVG(svgjs, config.js2svg);
|
|
||||||
} while(++counter < maxIterationCount && prevResult.data !== svg.data);
|
|
||||||
|
|
||||||
callback(svg);
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The factory that creates a content item with the helper methods.
|
* The factory that creates a content item with the helper methods.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user