Skip to content

Commit 5aa6180

Browse files
committed
fixes issues with escaping of chars
fixes webpack-contrib/css-loader#87 fixes #5
1 parent ea9ca39 commit 5aa6180

File tree

4 files changed

+27
-21
lines changed

4 files changed

+27
-21
lines changed

lib/parseValues.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@ function endSpacingMatch(match) {
2323
}
2424

2525
function unescapeString(content) {
26-
return content.replace(/\\([a-fA-F0-9]{4}|.)/g, function(escaped) {
26+
return content.replace(/\\([a-fA-F0-9]{2,5}|.)/g, function(escaped) {
2727
if(escaped.length > 2) {
28-
return String.fromCharCode(parseInt(escaped.substr(1), 16));
28+
var C = parseInt(escaped.substr(1), 16);
29+
if(C < 0x10000) {
30+
return String.fromCharCode(C);
31+
} else {
32+
return String.fromCharCode(Math.floor((C - 0x10000) / 0x400) + 0xD800) +
33+
String.fromCharCode((C - 0x10000) % 0x400 + 0xDC00);
34+
}
2935
} else {
3036
return escaped.substr(1);
3137
}

lib/stringifyValues.js

+4-17
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11
"use strict";
22

3+
var cssesc = require("cssesc");
4+
35
var stringify;
46

57
function escape(str, stringType) {
6-
return str.replace(/["'\\\x80-\uFFFF]/g, function(match) {
7-
switch(match) {
8-
case "\"":
9-
if(stringType === "\"") {
10-
return "\\\"";
11-
}
12-
return match;
13-
case "'":
14-
if(stringType === "'") {
15-
return "\\'";
16-
}
17-
return match;
18-
case "\\":
19-
return "\\\\";
20-
default:
21-
return "\\" + (0x10000 + match.charCodeAt(0)).toString(16).substr(1);
22-
}
8+
return cssesc(str, {
9+
quotes: stringType === "\"" ? "double" : "single"
2310
});
2411
}
2512

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
},
3232
"homepage": "https://github.com/css-modules/css-selector-tokenizer",
3333
"dependencies": {
34+
"cssesc": "^0.1.0",
3435
"fastparse": "^1.1.1"
3536
},
3637
"devDependencies": {

test/test-cases-values.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,29 @@ module.exports = {
125125
}
126126
],
127127
"escaped unicode": [
128-
"'\\f0e3\\\\\\'\"'",
128+
"'\\F0E3\\\\\\'\"'",
129129
singleValue([
130130
{ type: "string", stringType: "'", value: "\uf0e3\\'\"" }
131131
])
132132
],
133133
"escaped unicode 2": [
134-
"\"\\f0e3\\\\'\\\"\"",
134+
"\"\\F0E3\\\\'\\\"\"",
135135
singleValue([
136136
{ type: "string", stringType: "\"", value: "\uf0e3\\'\"" }
137137
])
138138
],
139+
"escaped unicode 3 (short)": [
140+
"\"\\10\"",
141+
singleValue([
142+
{ type: "string", stringType: "\"", value: "\u0010" }
143+
])
144+
],
145+
"escaped unicode 4 (surrogate pair)": [
146+
"\"\\1F50E\"",
147+
singleValue([
148+
{ type: "string", stringType: "\"", value: "\ud83d\udd0e" }
149+
])
150+
],
139151
"nested-item-with append": [
140152
"linear-gradient(45deg) 25%",
141153
singleValue([

0 commit comments

Comments
 (0)