Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore quotes when checking @font-face use #972

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/optimizer/level-2/remove-unused-at-rules.js
Expand Up @@ -8,6 +8,11 @@ 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 optionalMatchingQuotesRegex = /^(['"]?)(.*)\1$/;

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

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

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

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

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

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

if (normalizedMatch in atRules) {
delete atRules[normalizedMatch];
Expand Down
8 changes: 8 additions & 0 deletions test/optimizer/level-2/remove-unused-at-rules-test.js
Expand Up @@ -40,10 +40,18 @@ vows.describe('remove unused at rules')
'@font-face{font-family:test}.block{font-family:test}',
'@font-face{font-family:test}.block{font-family:test}'
],
'one used quoted declaration in font-family': [
'@font-face{font-family:"test test"}.block{font-family:"test test"}',
'@font-face{font-family:"test test"}.block{font-family:"test test"}'
],
'one used declaration in font-family with different case': [
'@font-face{font-family:test}.block{font-family:Test}',
'@font-face{font-family:test}.block{font-family:Test}'
],
'one used quoted declaration in font-family with different quotes': [
'@font-face{font-family:"test test"}.block{font-family:\'test test\'}',
'@font-face{font-family:"test test"}.block{font-family:\'test test\'}'
],
'one used declaration in multi-valued font-family': [
'@font-face{font-family:test}.block{font-family:Arial,test,sans-serif}',
'@font-face{font-family:test}.block{font-family:Arial,test,sans-serif}'
Expand Down