mirror of
https://github.com/svg/svgo.git
synced 2025-07-31 07:44:22 +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) {
|
||||
|
||||
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;
|
||||
|
||||
SVG2JS(svgstr, function(svgjs) {
|
||||
@ -33,24 +60,14 @@ SVGO.prototype.optimize = function(svgstr, callback) {
|
||||
return;
|
||||
}
|
||||
|
||||
var svg = { data : null },
|
||||
maxIterationCount = config.multipass ? 10 : 1,
|
||||
counter = 0,
|
||||
prevResult;
|
||||
svgjs = PLUGINS(svgjs, config.plugins);
|
||||
|
||||
do {
|
||||
prevResult = svg;
|
||||
svgjs = PLUGINS(svgjs, config.plugins);
|
||||
svg = JS2SVG(svgjs, config.js2svg);
|
||||
} while(++counter < maxIterationCount && prevResult.data !== svg.data);
|
||||
|
||||
callback(svg);
|
||||
callback(JS2SVG(svgjs, config.js2svg));
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* The factory that creates a content item with the helper methods.
|
||||
*
|
||||
|
Reference in New Issue
Block a user