Changes behavior of `--keep-line-breaks`/`keepBreaks` option to keep breaks after...
authorGoalSmashers <jakub@goalsmashers.com>
Mon, 28 Oct 2013 11:43:05 +0000 (12:43 +0100)
committerGoalSmashers <jakub@goalsmashers.com>
Sun, 3 Nov 2013 08:48:43 +0000 (09:48 +0100)
* Old behavior allowed line breaks within selector, like 'a\np{}'.

History.md
lib/clean.js
lib/selectors/optimizer.js
lib/selectors/tokenizer.js
test/unit-test.js

index 9c21858..8c0851b 100644 (file)
@@ -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
 ==================
index 0ab6eea..94ad804 100644 (file)
@@ -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() {
index 3395437..21db102 100644 (file)
@@ -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 {
index 8fab940..160404f 100644 (file)
@@ -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;
index b8bde26..e1d36f0 100644 (file)
@@ -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}",