Skip to content

Commit 8be4084

Browse files
Fixes #989 - edge case in removing unused at-rules.
When a value has `!important` in it then it should be stripped bare before matching to the list of at-rules.
1 parent 21a5df0 commit 8be4084

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

History.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
==================
33

44
* Fixed issue [#988](https://github.com/jakubpawlowicz/clean-css/issues/988) - edge case in dropping default animation-duration.
5+
* Fixed issue [#989](https://github.com/jakubpawlowicz/clean-css/issues/989) - edge case in removing unused at rules.
56

67
[4.1.9 / 2017-09-19](https://github.com/jakubpawlowicz/clean-css/compare/v4.1.8...v4.1.9)
78
==================

lib/optimizer/level-2/remove-unused-at-rules.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ var Token = require('../../tokenizer/token');
88
var animationNameRegex = /^(\-moz\-|\-o\-|\-webkit\-)?animation-name$/;
99
var animationRegex = /^(\-moz\-|\-o\-|\-webkit\-)?animation$/;
1010
var keyframeRegex = /^@(\-moz\-|\-o\-|\-webkit\-)?keyframes /;
11+
var importantRegex = /\s*!important$/;
1112
var optionalMatchingQuotesRegex = /^(['"]?)(.*)\1$/;
1213

13-
function removeQuotes(value) {
14-
return value.replace(optionalMatchingQuotesRegex, '$2');
14+
function normalize(value) {
15+
return value
16+
.replace(optionalMatchingQuotesRegex, '$2')
17+
.replace(importantRegex, '');
1518
}
1619

1720
function removeUnusedAtRules(tokens, context) {
@@ -112,7 +115,7 @@ function matchFontFace(token, atRules) {
112115
property = token[2][i];
113116

114117
if (property[1][1] == 'font-family') {
115-
match = removeQuotes(property[2][1].toLowerCase());
118+
match = normalize(property[2][1].toLowerCase());
116119
atRules[match] = atRules[match] || [];
117120
atRules[match].push(token);
118121
break;
@@ -139,7 +142,7 @@ function markFontFacesAsUsed(atRules) {
139142
component = wrappedProperty.components[6];
140143

141144
for (j = 0, m = component.value.length; j < m; j++) {
142-
normalizedMatch = removeQuotes(component.value[j][1].toLowerCase());
145+
normalizedMatch = normalize(component.value[j][1].toLowerCase());
143146

144147
if (normalizedMatch in atRules) {
145148
delete atRules[normalizedMatch];
@@ -151,7 +154,7 @@ function markFontFacesAsUsed(atRules) {
151154

152155
if (property[1][1] == 'font-family') {
153156
for (j = 2, m = property.length; j < m; j++) {
154-
normalizedMatch = removeQuotes(property[j][1].toLowerCase());
157+
normalizedMatch = normalize(property[j][1].toLowerCase());
155158

156159
if (normalizedMatch in atRules) {
157160
delete atRules[normalizedMatch];

test/optimizer/level-2/remove-unused-at-rules-test.js

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ vows.describe('remove unused at rules')
8080
'@font-face{font-family:test}.block{font:16px test!important}',
8181
'@font-face{font-family:test}.block{font:16px test!important}'
8282
],
83+
'one used as font-family with !important': [
84+
'@font-face{font-family:test}.block{font-family:test!important}',
85+
'@font-face{font-family:test}.block{font-family:test!important}'
86+
],
8387
'one used declaration in another @font-face': [
8488
'@font-face{font-family:test;font-weight:normal}@font-face{font-family:test;font-weight:bold}',
8589
''

0 commit comments

Comments
 (0)