Skip to content

Commit

Permalink
Merge pull request #746 from JoniJnm/patch-1
Browse files Browse the repository at this point in the history
Don't skip !important rule with capital letters
  • Loading branch information
jakubpawlowicz committed Mar 26, 2016
2 parents 5e253a5 + 3395d93 commit 43072cc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
21 changes: 14 additions & 7 deletions lib/properties/wrap-for-optimizing.js
Original file line number Diff line number Diff line change
@@ -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 = '!';
Expand Down Expand Up @@ -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';
Expand All @@ -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) {
Expand Down
11 changes: 8 additions & 3 deletions lib/tokenizer/extract-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions test/integration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }',
''
]
})
)
Expand Down

0 comments on commit 43072cc

Please sign in to comment.