From: Jakub Pawlowicz Date: Sun, 10 Jun 2012 20:43:43 +0000 (+0200) Subject: Added better stateless processing (when calling CleanCSS#process directly or passing... X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=0755ce1d44a8453e06f86f41d464e960e1d81345;p=clean-css.git Added better stateless processing (when calling CleanCSS#process directly or passing it around). --- diff --git a/lib/clean.js b/lib/clean.js index 3fa03f04..3a3bfaeb 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -8,17 +8,17 @@ var CleanCSS = { yellow: '#ff0' }, - specialComments: [], - contentBlocks: [], - process: function(data, options) { - var self = this, - replace = function(pattern, replacement) { - if (typeof arguments[0] == 'function') - arguments[0](); - else - data = data.replace.apply(data, arguments); - }; + var context = { + specialComments: [], + contentBlocks: [] + }; + var replace = function(pattern, replacement) { + if (typeof arguments[0] == 'function') + arguments[0](); + else + data = data.replace.apply(data, arguments); + }; options = options || {}; @@ -37,12 +37,12 @@ var CleanCSS = { // strip comments one by one replace(function stripComments() { - data = self.stripComments(data); + data = CleanCSS._stripComments(context, data); }); // replace content: with a placeholder replace(function stripContent() { - data = self.stripContent(data); + data = CleanCSS._stripContent(context, data); }); replace(/;\s*;+/g, ';') // whitespace between semicolons & multiple semicolons @@ -102,8 +102,8 @@ var CleanCSS = { replace(/calc\([^\}]+\}/g, function(match) { return match.replace(/\+/g, ' + '); }); - replace(/__CSSCOMMENT__/g, function() { return self.specialComments.shift(); }); - replace(/__CSSCONTENT__/g, function() { return self.contentBlocks.shift(); }); + replace(/__CSSCOMMENT__/g, function() { return context.specialComments.shift(); }); + replace(/__CSSCONTENT__/g, function() { return context.contentBlocks.shift(); }); return data.trim() // trim spaces at beginning and end }, @@ -111,7 +111,7 @@ var CleanCSS = { // Strips special comments (/*! ... */) by replacing them by __CSSCOMMENT__ marker // for further restoring. Plain comments are removed. It's done by scanning datq using // String#indexOf scanning instead of regexps to speed up the process. - stripComments: function(data) { + _stripComments: function(context, data) { var tempData = [], nextStart = 0, nextEnd = 0, @@ -125,7 +125,7 @@ var CleanCSS = { tempData.push(data.substring(cursor, nextStart)) if (data[nextStart + 2] == '!') { // in case of special comments, replace them with a placeholder - this.specialComments.push(data.substring(nextStart, nextEnd + 2)); + context.specialComments.push(data.substring(nextStart, nextEnd + 2)); tempData.push('__CSSCOMMENT__'); } cursor = nextEnd + 2; @@ -139,7 +139,7 @@ var CleanCSS = { // Strips content tags by replacing them by __CSSCONTENT__ marker // for further restoring. It's done via string scanning instead of // regexps to speed up the process. - stripContent: function(data) { + _stripContent: function(context, data) { var tempData = [], nextStart = 0, nextEnd = 0, @@ -188,7 +188,7 @@ var CleanCSS = { tempData.push(data.substring(cursor, nextStart - 1)); tempData.push('__CSSCONTENT__'); - this.contentBlocks.push(data.substring(nextStart - 1, nextEnd + 1)); + context.contentBlocks.push(data.substring(nextStart - 1, nextEnd + 1)); cursor = nextEnd + 1; } diff --git a/test/custom-test.js b/test/custom-test.js new file mode 100644 index 00000000..ca863f7f --- /dev/null +++ b/test/custom-test.js @@ -0,0 +1,17 @@ +var vows = require('vows'), + assert = require('assert'), + CleanCSS = require('../index'); + +vows.describe('clean-custom') + .addBatch({ + 'imported as function': { + topic: function() { + return CleanCSS.process; + }, + 'should process CSS correctly': function(process) { + assert.equal(process('a{ color: #f00; }'), 'a{color:red}'); + } + } + }) + .export(module) +