From 394e9fff83faf3cffb0f428fd0cd757e67467020 Mon Sep 17 00:00:00 2001 From: GoalSmashers Date: Tue, 10 Dec 2013 10:18:08 +0100 Subject: [PATCH] Adds an optional callback to CleanCSS#minify method. --- History.md | 1 + lib/clean.js | 6 ++++-- test/module-test.js | 24 ++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index 7894e7da..82d5d924 100644 --- a/History.md +++ b/History.md @@ -1,6 +1,7 @@ [2.1.0 / 2013-xx-xx (UNRELEASED)](https://github.com/GoalSmashers/clean-css/compare/v2.0.0...HEAD) ================== +* Adds an optional callback to minify method. * Deprecates --selectors-merge-mode / selectorsMergeMode in favor to --compatibility / compatibility. * Skips empty removal if advanced processing is enabled. * Fixed issue [#160](https://github.com/GoalSmashers/clean-css/issues/160) - re-runs optimizer until a clean pass. diff --git a/lib/clean.js b/lib/clean.js index 187aaa53..2c3b03f2 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -51,7 +51,7 @@ var CleanCSS = module.exports = function CleanCSS(options) { this.lineBreak = process.platform == 'win32' ? '\r\n' : '\n'; }; -CleanCSS.prototype.minify = function(data) { +CleanCSS.prototype.minify = function(data, callback) { var startedAt; var stats = this.stats; var options = this.options; @@ -348,5 +348,7 @@ CleanCSS.prototype.minify = function(data) { stats.minifiedSize = data.length; } - return data; + return callback ? + callback.call(this, this.context.errors.length > 0 ? this.context.errors : null, data) : + data; }; diff --git a/test/module-test.js b/test/module-test.js index b485ae56..8cce9fa3 100644 --- a/test/module-test.js +++ b/test/module-test.js @@ -40,6 +40,30 @@ vows.describe('module tests').addBatch({ delete CleanCSS.prototype.foo; } }, + 'with callback passed and no errors': { + topic: function() { + new CleanCSS().minify('a{color:#f00}', this.callback); + }, + 'should correctly set context': function() { + assert.equal(true, this instanceof CleanCSS); + }, + 'should yield no error': function(errors, minified) { + /* jshint unused: false */ + assert.equal(errors, null); + }, + 'should yield minified data': function(errors, minified) { + assert.equal(minified, 'a{color:red}'); + } + }, + 'with callback passed and one error': { + topic: function() { + new CleanCSS().minify('@import "missing.css";', this.callback); + }, + 'should yield no error and minify': function(errors, minified) { + /* jshint unused: false */ + assert.equal(errors.length, 1); + } + }, 'no debug': { topic: function() { var minifier = new CleanCSS(); -- 2.34.1