From 3395d93711eca4f231a66a83fc557cc0581ef4b2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=B3natan=20N=C3=BA=C3=B1ez?= Date: Mon, 14 Mar 2016 23:24:23 +0100 Subject: [PATCH] Don't skip !important rule with capital letters 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 | 21 ++++++++++++++------- lib/tokenizer/extract-properties.js | 11 ++++++++--- test/integration-test.js | 8 ++++++++ 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/properties/wrap-for-optimizing.js b/lib/properties/wrap-for-optimizing.js index 4de3c644..cb12a2b3 100644 --- a/lib/properties/wrap-for-optimizing.js +++ b/lib/properties/wrap-for-optimizing.js @@ -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) { diff --git a/lib/tokenizer/extract-properties.js b/lib/tokenizer/extract-properties.js index 1805a843..9a55eb5d 100644 --- a/lib/tokenizer/extract-properties.js +++ b/lib/tokenizer/extract-properties.js @@ -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; diff --git a/test/integration-test.js b/test/integration-test.js index 3c1f09c0..4ea1d867 100644 --- a/test/integration-test.js +++ b/test/integration-test.js @@ -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 }', + '' ] }) ) -- 2.34.1