Skip to content

Commit

Permalink
Fixes #989 - edge case in removing unused at-rules.
Browse files Browse the repository at this point in the history
When a value has `!important` in it then it should be stripped bare
before matching to the list of at-rules.
  • Loading branch information
jakubpawlowicz committed Mar 5, 2018
1 parent c7fc1d1 commit 870664f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions History.md
Expand Up @@ -10,6 +10,7 @@
==================

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

[4.1.9 / 2017-09-19](https://github.com/jakubpawlowicz/clean-css/compare/v4.1.8...v4.1.9)
==================
Expand Down
13 changes: 8 additions & 5 deletions lib/optimizer/level-2/remove-unused-at-rules.js
Expand Up @@ -8,10 +8,13 @@ var Token = require('../../tokenizer/token');
var animationNameRegex = /^(\-moz\-|\-o\-|\-webkit\-)?animation-name$/;
var animationRegex = /^(\-moz\-|\-o\-|\-webkit\-)?animation$/;
var keyframeRegex = /^@(\-moz\-|\-o\-|\-webkit\-)?keyframes /;
var importantRegex = /\s*!important$/;
var optionalMatchingQuotesRegex = /^(['"]?)(.*)\1$/;

function removeQuotes(value) {
return value.replace(optionalMatchingQuotesRegex, '$2');
function normalize(value) {
return value
.replace(optionalMatchingQuotesRegex, '$2')
.replace(importantRegex, '');
}

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

if (property[1][1] == 'font-family') {
match = removeQuotes(property[2][1].toLowerCase());
match = normalize(property[2][1].toLowerCase());
atRules[match] = atRules[match] || [];
atRules[match].push(token);
break;
Expand All @@ -139,7 +142,7 @@ function markFontFacesAsUsed(atRules) {
component = wrappedProperty.components[6];

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

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

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

if (normalizedMatch in atRules) {
delete atRules[normalizedMatch];
Expand Down
4 changes: 4 additions & 0 deletions test/optimizer/level-2/remove-unused-at-rules-test.js
Expand Up @@ -80,6 +80,10 @@ vows.describe('remove unused at rules')
'@font-face{font-family:test}.block{font:16px test!important}',
'@font-face{font-family:test}.block{font:16px test!important}'
],
'one used as font-family with !important': [
'@font-face{font-family:test}.block{font-family:test!important}',
'@font-face{font-family:test}.block{font-family:test!important}'
],
'one used declaration in another @font-face': [
'@font-face{font-family:test;font-weight:normal}@font-face{font-family:test;font-weight:bold}',
''
Expand Down

0 comments on commit 870664f

Please sign in to comment.