From: GoalSmashers Date: Sun, 17 Nov 2013 14:33:13 +0000 (+0100) Subject: Skips empty removal if advanced processing is enabled. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=9746d712faed97581f6e89e87cc837643413b6d2;p=clean-css.git Skips empty removal if advanced processing is enabled. * Since advanced processing tokenizes data we can rebuild it without empty elements. --- diff --git a/History.md b/History.md index 3edec0cb..385cdb59 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) ================== +* Skips empty removal if advanced processing is enabled. * Fixed issue [#161](https://github.com/GoalSmashers/clean-css/issues/161) - improves tokenizer performance. * Fixed issue [#163](https://github.com/GoalSmashers/clean-css/issues/163) - round pixels to 2nd decimal place. * Fixed issue [#165](https://github.com/GoalSmashers/clean-css/issues/165) - extra space after trailing parenthesis. diff --git a/lib/clean.js b/lib/clean.js index 262c2b39..bb277b2d 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -303,9 +303,11 @@ module.exports = function(options) { data.replace(new RegExp('@charset [^;]+;(' + lineBreak + ')?', 'g'), '').trim(); }); - replace(function removeEmptySelectors() { - data = new EmptyRemoval(data).process(); - }); + if (options.noAdvanced) { + replace(function removeEmptySelectors() { + data = new EmptyRemoval(data).process(); + }); + } // trim spaces at beginning and end data = data.trim(); diff --git a/lib/selectors/optimizer.js b/lib/selectors/optimizer.js index 307057a4..5b018bea 100644 --- a/lib/selectors/optimizer.js +++ b/lib/selectors/optimizer.js @@ -200,17 +200,25 @@ module.exports = function Optimizer(data, options) { }; var rebuild = function(tokens) { - return (Array.isArray(tokens) ? tokens : [tokens]) - .map(function(token) { - if (typeof token == 'string') - return token; - - if (token.block) - return token.block + '{' + rebuild(token.body) + '}'; - else - return token.selector + '{' + token.body + '}'; - }) - .join(options.keepBreaks ? options.lineBreak : ''); + var rebuilt = []; + + tokens = Array.isArray(tokens) ? tokens : [tokens]; + for (var i = 0, l = tokens.length; i < l; i++) { + var token = tokens[i]; + + if (typeof token == 'string') { + rebuilt.push(token); + continue; + } + + var name = token.block || token.selector; + var body = token.block ? rebuild(token.body) : token.body; + + if (body.length > 0) + rebuilt.push(name + '{' + body + '}'); + } + + return rebuilt.join(options.keepBreaks ? options.lineBreak : ''); }; return { diff --git a/test/unit-test.js b/test/unit-test.js index 06b2f3af..5fa9bebd 100644 --- a/test/unit-test.js +++ b/test/unit-test.js @@ -949,6 +949,16 @@ title']{display:block}", '' ] }), + 'empty with disabled advanced optimizations': cssContext({ + 'selector': [ + 'a{}p{}', + '' + ], + 'media': [ + '@media screen{}', + '' + ] + }, { noAdvanced: true }), '@import': cssContext({ 'empty': [ "@import url();",