Fixes #471 - preserves correct order after restructuring.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 27 Feb 2015 19:29:08 +0000 (19:29 +0000)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 27 Feb 2015 19:29:47 +0000 (19:29 +0000)
Restructured content should not end up before important comments or
a charset.

History.md
lib/selectors/optimizers/advanced.js
test/fixtures/big-min.css
test/selectors/optimizer-test.js

index a93b46f..4c5ba7e 100644 (file)
@@ -2,6 +2,7 @@
 ==================
 
 * Fixed issue [#470](https://github.com/jakubpawlowicz/clean-css/issues/470) - negative padding removal.
+* Fixed issue [#471](https://github.com/jakubpawlowicz/clean-css/issues/471) - correct order after restructuring.
 
 [3.1.0 / 2015-02-26](https://github.com/jakubpawlowicz/clean-css/compare/v3.0.10...3.1.0)
 ==================
index 310a855..67ad133 100644 (file)
@@ -632,7 +632,16 @@ AdvancedOptimizer.prototype.restructure = function (tokens) {
     }
   }
 
-  var position = tokens[0] && tokens[0].kind == 'at-rule' && tokens[0].value.indexOf('@charset') === 0 ? 1 : 0;
+  var position = 0;
+  var isCharsetFirst = tokens[0] && tokens[0].kind == 'at-rule' && tokens[0].value.indexOf('@charset') === 0;
+  var isCommentFirst = !isCharsetFirst && tokens[0] && tokens[0].kind == 'text' && tokens[0].value.indexOf('__ESCAPED_COMMENT_SPECIAL') === 0;
+  var isCommentSecond = isCharsetFirst && tokens[1] && tokens[1].kind == 'text' && tokens[1].value.indexOf('__ESCAPED_COMMENT_SPECIAL') === 0;
+
+  if (isCharsetFirst || isCommentFirst)
+    position = 1;
+  if (isCommentSecond)
+    position = 2;
+
   for (i = 0; i < movedProperties.length; i++) {
     dropPropertiesAt(position, movedProperties[i]);
   }
index 71ce174..c5381b2 100644 (file)
@@ -1,8 +1,8 @@
+/*! normalize.css 2012-01-31T16:06 UTC - http://github.com/necolas/normalize.css */
 small,sub,sup{font-size:75%}
 .alpha,.ie .une_normale .liste_carre_999.liste_une .ie_impair,.liste_carre_999.liste_une li:nth-child(2n+3){clear:left}
 .clear,.clearfix:after,.deplier{visibility:hidden}
 .btn:hover,.btn_abo:hover,.btn_fonce:hover,.btn_petit:hover,.conteneur_pagination .next:hover,.conteneur_pagination .prev:hover{-webkit-transition:background-position .1s linear;-moz-transition:background-position .1s linear;-ms-transition:background-position .1s linear}
-/*! normalize.css 2012-01-31T16:06 UTC - http://github.com/necolas/normalize.css */
 article,aside,details,figcaption,figure,footer,header,hgroup,nav,section{display:block}
 audio,canvas,video{display:inline-block}
 [hidden],audio:not([controls]){display:none}
index 716fef5..80ac530 100644 (file)
@@ -3,6 +3,7 @@ var assert = require('assert');
 var SelectorsOptimizer = require('../../lib/selectors/optimizer');
 var Stringifier = require('../../lib/selectors/stringifier');
 var Compatibility = require('../../lib/utils/compatibility');
+var SourceTracker = require('../../lib/utils/source-tracker');
 
 function optimizerContext(group, specs, options) {
   var stringifier = new Stringifier(false, function (data) { return data; });
@@ -12,10 +13,13 @@ function optimizerContext(group, specs, options) {
   options.shorthandCompacting = true;
   options.restructuring = true;
   options.compatibility = new Compatibility(options.compatibility).toOptions();
+  var outerContext = {
+    sourceTracker: new SourceTracker()
+  };
 
   function optimized(target) {
     return function (source) {
-      assert.equal(new SelectorsOptimizer(options).process(source, stringifier).styles, target);
+      assert.equal(new SelectorsOptimizer(options, outerContext).process(source, stringifier).styles, target);
     };
   }
 
@@ -183,6 +187,14 @@ vows.describe(SelectorsOptimizer)
       'not in vendored keyframes': [
         '@-moz-keyframes test{0%{transform:scale3d(1,1,1);opacity:1}100%{transform:scale3d(.5,.5,.5);opacity:1}}',
         '@-moz-keyframes test{0%{transform:scale3d(1,1,1);opacity:1}100%{transform:scale3d(.5,.5,.5);opacity:1}}'
+      ],
+      'with important comment': [
+        '__ESCAPED_COMMENT_SPECIAL_CLEAN_CSS0__a{width:100px}div{color:red}.one{display:block}.two{display:inline;color:red}',
+        '__ESCAPED_COMMENT_SPECIAL_CLEAN_CSS0__.two,div{color:red}a{width:100px}.one{display:block}.two{display:inline}'
+      ],
+      'with important comment and charset': [
+        '@charset "utf-8";__ESCAPED_COMMENT_SPECIAL_CLEAN_CSS0__a{width:100px}div{color:red}.one{display:block}.two{display:inline;color:red}',
+        '@charset "utf-8";__ESCAPED_COMMENT_SPECIAL_CLEAN_CSS0__.two,div{color:red}a{width:100px}.one{display:block}.two{display:inline}'
       ]
     }, { advanced: true })
   )