From: GoalSmashers Date: Mon, 28 Oct 2013 11:43:05 +0000 (+0100) Subject: Changes behavior of `--keep-line-breaks`/`keepBreaks` option to keep breaks after... X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=1a83a296bf1a75859d6fec1cd46333ecd48a46a4;p=clean-css.git Changes behavior of `--keep-line-breaks`/`keepBreaks` option to keep breaks after trailing braces only. * Old behavior allowed line breaks within selector, like 'a\np{}'. --- diff --git a/History.md b/History.md index 9c21858b..8c0851ba 100644 --- a/History.md +++ b/History.md @@ -11,6 +11,7 @@ * Adds basic optimizer removing duplicate selectors from a list. * Adds merging duplicate properties within a single selector's body. * Adds merging adjacent selectors within a scope (single and multiple ones). +* Changes behavior of `--keep-line-breaks`/`keepBreaks` option to keep breaks after trailing braces only. 1.1.7 / 2013-10-28 ================== diff --git a/lib/clean.js b/lib/clean.js index 0ab6eea4..94ad8045 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -142,8 +142,7 @@ var CleanCSS = { }); // line breaks - if (!options.keepBreaks) - replace(/[\r]?\n/g, ' '); + replace(/[\r]?\n/g, ' '); // multiple whitespace replace(/[\t ]+/g, ' '); @@ -248,7 +247,7 @@ var CleanCSS = { }); replace(function optimizeSelectors() { - data = new SelectorsOptimizer(data).process(); + data = new SelectorsOptimizer(data, options.keepBreaks, lineBreak).process(); }); replace(function restoreUrls() { @@ -278,7 +277,7 @@ var CleanCSS = { // reattach first charset and remove all subsequent data = firstCharset + (options.keepBreaks ? lineBreak : '') + - data.replace(new RegExp('@charset [^;]+;(' + lineBreak + ')?', 'g'), ''); + data.replace(new RegExp('@charset [^;]+;(' + lineBreak + ')?', 'g'), '').trim(); }); replace(function removeEmptySelectors() { diff --git a/lib/selectors/optimizer.js b/lib/selectors/optimizer.js index 33954377..21db1025 100644 --- a/lib/selectors/optimizer.js +++ b/lib/selectors/optimizer.js @@ -1,6 +1,6 @@ var Tokenizer = require('./tokenizer'); -module.exports = function Optimizer(data) { +module.exports = function Optimizer(data, keepBreaks, lineBreak) { var stripRepeats = function(selectors) { var plain = []; selectors = selectors.split(','); @@ -132,7 +132,7 @@ module.exports = function Optimizer(data) { else return token.selector + '{' + token.body + '}'; }) - .join(''); + .join(keepBreaks ? lineBreak : ''); }; return { diff --git a/lib/selectors/tokenizer.js b/lib/selectors/tokenizer.js index 8fab9401..160404f2 100644 --- a/lib/selectors/tokenizer.js +++ b/lib/selectors/tokenizer.js @@ -66,7 +66,7 @@ module.exports = function Tokenizer(data) { context.cursor = nextEnd + 1; } else { nextEnd = data.indexOf('{', nextSpecial + 1); - var block = data.substring(context.cursor, nextEnd); + var block = data.substring(context.cursor, nextEnd).trim(); var isFlat = fragment.indexOf('@font-face') === 0; oldMode = context.mode; @@ -84,7 +84,7 @@ module.exports = function Tokenizer(data) { context.cursor = nextEnd + 2; } else if (what == 'bodyStart') { - var selector = data.substring(context.cursor, nextSpecial); + var selector = data.substring(context.cursor, nextSpecial).trim(); oldMode = context.mode; context.cursor = nextSpecial + 1; diff --git a/test/unit-test.js b/test/unit-test.js index b8bde264..e1d36f0b 100644 --- a/test/unit-test.js +++ b/test/unit-test.js @@ -163,19 +163,23 @@ vows.describe('clean-units').addBatch({ 'line breaks': cssContext({ 'line breaks': [ 'div\na\r\n{width:500px}', - 'div' + lineBreak + 'a' + lineBreak + '{width:500px}' + 'div a{width:500px}' ], 'line breaks #2': [ 'div\na\r\n,p{width:500px}', - 'div' + lineBreak + 'a' + lineBreak + ',p{width:500px}' + 'div a,p{width:500px}' ], 'multiple line breaks #2': [ 'div \r\n\r\na\r\n,p{width:500px}', - 'div' + lineBreak + 'a' + lineBreak + ',p{width:500px}' + 'div a,p{width:500px}' ], 'line breaks with whitespace lines': [ 'div \n \t\n \na\r\n, p { width:500px }', - 'div' + lineBreak + 'a' + lineBreak + ',p{width:500px}' + 'div a,p{width:500px}' + ], + 'line breaks with multiple selectors': [ + 'p{width:500px}a{color:red}span{font-style:italic}', + 'p{width:500px}' + lineBreak + 'a{color:red}' + lineBreak + 'span{font-style:italic}' ], 'charset not at beginning': [ "a{ color: #f10; }\n@charset 'utf-8';\nb { font-weight: bolder}",