Don't skip !important rule with capital letters
authorJónatan Núñez <JoniJnm@users.noreply.github.com>
Mon, 14 Mar 2016 22:24:23 +0000 (23:24 +0100)
committerJoniJnm <JoniJnm@users.noreply.github.com>
Sat, 19 Mar 2016 19:23:05 +0000 (20:23 +0100)
Fix #745

In v3.3.10 works ok, fails in v3.4.10

The problem was introduced in 96096402eaaeb4947afa7ecacba0982381a244f4

Prevent match !importantE with !important

lib/properties/wrap-for-optimizing.js
lib/tokenizer/extract-properties.js
test/integration-test.js

index 4de3c64..cb12a2b 100644 (file)
@@ -1,5 +1,8 @@
 var BACKSLASH_HACK = '\\';
-var IMPORTANT_TOKEN = '!important';
+var IMPORTANT_WORD = 'important';
+var IMPORTANT_TOKEN = '!'+IMPORTANT_WORD;
+var IMPORTANT_WORD_MATCH = new RegExp(IMPORTANT_WORD+'$', 'i');
+var IMPORTANT_TOKEN_MATCH = new RegExp(IMPORTANT_TOKEN+'$', 'i');
 var STAR_HACK = '*';
 var UNDERSCORE_HACK = '_';
 var BANG_HACK = '!';
@@ -38,9 +41,9 @@ function hackType(property) {
     type = 'underscore';
   } else if (name[0] == STAR_HACK) {
     type = 'star';
-  } else if (lastValue[0][0] == BANG_HACK && lastValue[0].indexOf('important') == -1) {
+  } else if (lastValue[0][0] == BANG_HACK && !lastValue[0].match(IMPORTANT_WORD_MATCH)) {
     type = 'bang';
-  } else if (lastValue[0].indexOf(BANG_HACK) > 0 && lastValue[0].indexOf('important') == -1) {
+  } else if (lastValue[0].indexOf(BANG_HACK) > 0 && !lastValue[0].match(IMPORTANT_WORD_MATCH)) {
     type = 'bang';
   } else if (lastValue[0].indexOf(BACKSLASH_HACK) > 0 && lastValue[0].indexOf(BACKSLASH_HACK) == lastValue[0].length - BACKSLASH_HACK.length - 1) {
     type = 'backslash';
@@ -52,14 +55,18 @@ function hackType(property) {
 }
 
 function isImportant(property) {
-  return property.length > 1 ?
-    property[property.length - 1][0].indexOf(IMPORTANT_TOKEN) > 0 :
-    false;
+  if (property.length > 1) {
+    var p = property[property.length - 1][0];
+    if (typeof(p) === 'string') {
+      return IMPORTANT_TOKEN_MATCH.test(p);
+    }
+  }
+  return false;
 }
 
 function stripImportant(property) {
   if (property.length > 0)
-    property[property.length - 1][0] = property[property.length - 1][0].replace(IMPORTANT_TOKEN, '');
+    property[property.length - 1][0] = property[property.length - 1][0].replace(IMPORTANT_TOKEN_MATCH, '');
 }
 
 function stripPrefixHack(property) {
index 1805a84..9a55eb5 100644 (file)
@@ -5,6 +5,11 @@ var FORWARD_SLASH = '/';
 
 var AT_RULE = 'at-rule';
 
+var IMPORTANT_WORD = 'important';
+var IMPORTANT_TOKEN = '!'+IMPORTANT_WORD;
+var IMPORTANT_WORD_MATCH = new RegExp('^'+IMPORTANT_WORD+'$', 'i');
+var IMPORTANT_TOKEN_MATCH = new RegExp('^'+IMPORTANT_TOKEN+'$', 'i');
+
 function selectorName(value) {
   return value[0];
 }
@@ -153,14 +158,14 @@ function extractProperties(string, selectors, context) {
       }
 
       var pos = body.length - 1;
-      if (trimmed == 'important' && body[pos][0] == '!') {
+      if (IMPORTANT_WORD_MATCH.test(trimmed) && body[pos][0] == '!') {
         context.track(trimmed);
-        body[pos - 1][0] += '!important';
+        body[pos - 1][0] += IMPORTANT_TOKEN;
         body.pop();
         continue;
       }
 
-      if (trimmed == '!important' || (trimmed == 'important' && body[pos][0][body[pos][0].length - 1] == '!')) {
+      if (IMPORTANT_TOKEN_MATCH.test(trimmed) || (IMPORTANT_WORD_MATCH.test(trimmed) && body[pos][0][body[pos][0].length - 1] == '!')) {
         context.track(trimmed);
         body[pos][0] += trimmed;
         continue;
index 3c1f09c..4ea1d86 100644 (file)
@@ -1882,6 +1882,14 @@ vows.describe('integration tests')
       'space between ! and important': [
         'body{background-color:#fff  ! important}',
         'body{background-color:#fff!important}'
+      ],
+      'Capital letters': [
+        'a{color: red !ImporTant }',
+        'a{color:red!important}'
+      ],
+      'Match word': [
+        'a{color: red !importante }',
+        ''
       ]
     })
   )