From: Jakub Pawlowicz Date: Mon, 6 Oct 2014 09:35:48 +0000 (+0100) Subject: Rewrites optimizer's #rebuild as a simple loop. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=3a224cb9c8598be0d454e19bbc5a62aeb38a9377;p=clean-css.git Rewrites optimizer's #rebuild as a simple loop. --- diff --git a/lib/selectors/optimizer.js b/lib/selectors/optimizer.js index 7b13c8af..9c6e21f0 100644 --- a/lib/selectors/optimizer.js +++ b/lib/selectors/optimizer.js @@ -11,27 +11,30 @@ function SelectorsOptimizer(options, context) { function rebuild(tokens, keepBreaks, isFlatBlock) { var joinCharacter = isFlatBlock ? ';' : (keepBreaks ? lineBreak : ''); - - return tokens - .map(function (token) { - if (typeof token === 'string') - return token; - // TODO: broken due to joining/splitting - if (token.body.length === 0 || (token.body.length == 1 && token.body[0] === '')) - return ''; - - if (token.block) { - var body = rebuild(token.body, keepBreaks, token.isFlatBlock); - return body.length > 0 ? - token.block + '{' + body + '}' : - ''; - } else { - return token.selector.join(',') + '{' + token.body.join(';') + '}'; - } - }) - .filter(function (value) { return value.length > 0; }) - .join(joinCharacter) - .trim(); + var parts = []; + + for (var i = 0, l = tokens.length; i < l; i++) { + var token = tokens[i]; + + if (typeof token === 'string') { + parts.push(token); + continue; + } + + // TODO: broken due to joining/splitting + if (token.body.length === 0 || (token.body.length == 1 && token.body[0] === '')) + continue; + + if (token.block) { + var body = rebuild(token.body, keepBreaks, token.isFlatBlock); + if (body.length > 0) + parts.push(token.block + '{' + body + '}'); + } else { + parts.push(token.selector.join(',') + '{' + token.body.join(';') + '}'); + } + } + + return parts.join(joinCharacter); } SelectorsOptimizer.prototype.process = function (data) { @@ -41,7 +44,7 @@ SelectorsOptimizer.prototype.process = function (data) { if (!this.options.noAdvanced) new AdvancedOptimizer(this.options, this.context).optimize(tokens); - return rebuild(tokens, this.options.keepBreaks, false); + return rebuild(tokens, this.options.keepBreaks, false).trim(); }; module.exports = SelectorsOptimizer;