See #842 - removes HTML comments from selectors.
authorAlex Lam S.L <alexlamsl@gmail.com>
Sun, 8 Jan 2017 10:36:26 +0000 (11:36 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Sun, 8 Jan 2017 11:12:56 +0000 (12:12 +0100)
Kudos to @alexlamsl for the fix.

Why:

* Apparently anything wrapped between HTML comments still gets
  processed by browsers.

lib/optimizer/tidy-rules.js
test/optimizer/basic-test.js

index a57fec7..9b6c725 100644 (file)
@@ -1,6 +1,7 @@
 var Marker = require('../tokenizer/marker');
 var formatPosition = require('../utils/format-position');
 
+var HTML_COMMENT_PATTERN = /^(?:(?:<!--|-->)\s*)+/;
 var RELATION_PATTERN = /[>\+~]/;
 var WHITESPACE_PATTERN = /\s/;
 
@@ -142,10 +143,17 @@ function tidyRules(rules, removeUnsupported, adjacentSpace, beautify, warnings)
   var list = [];
   var repeated = [];
 
+  function removeHTMLComment(rule, match) {
+    warnings.push('HTML comment \'' + match + '\' at ' + formatPosition(rule[2][0]) + '. Removing.');
+    return '';
+  }
+
   for (var i = 0, l = rules.length; i < l; i++) {
     var rule = rules[i];
     var reduced = rule[1];
 
+    reduced = reduced.replace(HTML_COMMENT_PATTERN, removeHTMLComment.bind(null, rule));
+
     if (hasInvalidCharacters(reduced)) {
       warnings.push('Invalid selector \'' + rule[1] + '\' at ' + formatPosition(rule[2][0]) + '. Ignoring.');
       continue;
index 654d9ae..bf6b79a 100644 (file)
@@ -121,6 +121,10 @@ vows.describe('simple optimizations')
         'body;{body}',
         ''
       ],
+      'html comments': [
+        '<!-- a{color:red} --> p{color:red} <!--div{color:red}--> ',
+        'a{color:red}p{color:red}div{color:red}'
+      ],
       'missing semicolon and brace in the middle': [
         'body{color:red a{color:blue;}',
         ''