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

Drop special behavior for legacy browsers #19

Merged
merged 2 commits into from Feb 4, 2019
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
19 changes: 5 additions & 14 deletions cssesc.js
Expand Up @@ -16,8 +16,8 @@ var merge = function merge(options, defaults) {
return result;
};

var regexAnySingleEscape = /[ -,\.\/;-@\[-\^`\{-~]/;
var regexSingleEscape = /[ -,\.\/;-@\[\]\^`\{-~]/;
var regexAnySingleEscape = /[ -,\.\/:-@\[-\^`\{-~]/;
var regexSingleEscape = /[ -,\.\/:-@\[\]\^`\{-~]/;
var regexAlwaysEscape = /['"\\]/;
var regexExcessiveSpaces = /(^|\\+)?(\\[A-F0-9]{1,6})\x20(?![a-fA-F0-9\x20])/g;

Expand Down Expand Up @@ -60,13 +60,8 @@ var cssesc = function cssesc(string, options) {
} else {
value = '\\' + codePoint.toString(16).toUpperCase() + ' ';
}
// Note: `:` could be escaped as `\:`, but that fails in IE < 8.
} else if (/[\t\n\f\r\x0B:]/.test(character)) {
if (!isIdentifier && character == ':') {
value = character;
} else {
value = '\\' + codePoint.toString(16).toUpperCase() + ' ';
}
} else if (/[\t\n\f\r\x0B]/.test(character)) {
value = '\\' + codePoint.toString(16).toUpperCase() + ' ';
} else if (character == '\\' || !isIdentifier && (character == '"' && quote == character || character == '\'' && quote == character) || isIdentifier && regexSingleEscape.test(character)) {
value = '\\' + character;
} else {
Expand All @@ -77,11 +72,7 @@ var cssesc = function cssesc(string, options) {
}

if (isIdentifier) {
if (/^_/.test(output)) {
// Prevent IE6 from ignoring the rule altogether (in case this is for an
// identifier used as a selector)
output = '\\_' + output.slice(1);
} else if (/^-[-\d]/.test(output)) {
if (/^-[-\d]/.test(output)) {
output = '\\-' + output.slice(1);
} else if (/\d/.test(firstChar)) {
output = '\\3' + firstChar + ' ' + output.slice(1);
Expand Down
15 changes: 3 additions & 12 deletions src/cssesc.js
Expand Up @@ -61,13 +61,8 @@ const cssesc = (string, options) => {
} else {
value = '\\' + codePoint.toString(16).toUpperCase() + ' ';
}
// Note: `:` could be escaped as `\:`, but that fails in IE < 8.
} else if (/[\t\n\f\r\x0B:]/.test(character)) {
if (!isIdentifier && character == ':') {
value = character;
} else {
value = '\\' + codePoint.toString(16).toUpperCase() + ' ';
}
} else if (/[\t\n\f\r\x0B]/.test(character)) {
value = '\\' + codePoint.toString(16).toUpperCase() + ' ';
} else if (
character == '\\' ||
(
Expand All @@ -88,11 +83,7 @@ const cssesc = (string, options) => {
}

if (isIdentifier) {
if (/^_/.test(output)) {
// Prevent IE6 from ignoring the rule altogether (in case this is for an
// identifier used as a selector)
output = '\\_' + output.slice(1);
} else if (/^-[-\d]/.test(output)) {
if (/^-[-\d]/.test(output)) {
output = '\\-' + output.slice(1);
} else if (/\d/.test(firstChar)) {
output = '\\3' + firstChar + ' ' + output.slice(1);
Expand Down
2 changes: 1 addition & 1 deletion src/data.js
Expand Up @@ -4,7 +4,7 @@ var fs = require('fs');
// Characters with special meaning in CSS, except for quotes and backslashes
// (they get a separate regex)
var set = regenerate().add(
' ', '!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '.', '/', ';', '<',
' ', '!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '.', '/', ';', '<', ':',
'=', '>', '?', '@', '[', ']', '^', '`', '{', '|', '}', '~', '"', '\'', '\\'
);

Expand Down
10 changes: 5 additions & 5 deletions tests/tests.js
Expand Up @@ -48,12 +48,12 @@ describe('common usage', () => {
assert.equal(
cssesc('foo:bar', { 'isIdentifier': false }),
'foo:bar',
'foo:bar (avoid `\\:` for IE < 8 compatibility) with `isIdentifier: false`'
'foo:bar with `isIdentifier: false`'
);
assert.equal(
cssesc('foo:bar', { 'isIdentifier': true }),
'foo\\3A bar',
'foo:bar (avoid `\\:` for IE < 8 compatibility) with `isIdentifier: true`'
'foo\\:bar',
'foo:bar with `isIdentifier: true`'
);
assert.equal(
cssesc('_foo_bar', { 'isIdentifier': false }),
Expand All @@ -62,8 +62,8 @@ describe('common usage', () => {
);
assert.equal(
cssesc('_foo_bar', { 'isIdentifier': true }),
'\\_foo_bar',
'_foo_bar (escape leading `_` for IE6 compatibility) with `isIdentifier: true`'
'_foo_bar',
'_foo_bar with `isIdentifier: true`'
);
assert.equal(
cssesc('a\t\n\v\f\rb'),
Expand Down