Skip to content

Commit 90613f2

Browse files
committed
Properly parse unicode characters in strings
This fixes the parsing of unicode strings that are 1 or 6 hex characters long. Extends the tests to support a third element in examples, which would be the re-stringified example, since a unicode character might be stringified in a different way than the original code, and still be valid. fixes webpack-contrib/css-loader#133 fixes #12
1 parent db76c76 commit 90613f2

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

lib/parseValues.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ function endSpacingMatch(match) {
2323
}
2424

2525
function unescapeString(content) {
26-
return content.replace(/\\([a-fA-F0-9]{2,5}|.)/g, function(escaped) {
27-
if(escaped.length > 2) {
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-
}
26+
return content.replace(/\\(?:([a-fA-F0-9]{1,6})|(.))/g, function(all, unicode, otherCharacter) {
27+
if (otherCharacter) {
28+
return otherCharacter;
29+
}
30+
31+
var C = parseInt(unicode, 16);
32+
if(C < 0x10000) {
33+
return String.fromCharCode(C);
3534
} else {
36-
return escaped.substr(1);
35+
return String.fromCharCode(Math.floor((C - 0x10000) / 0x400) + 0xD800) +
36+
String.fromCharCode((C - 0x10000) % 0x400 + 0xDC00);
3737
}
3838
});
3939
}

test/stringifyValues.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe("stringifyValues", function() {
1010
Object.keys(testCases).forEach(function(testCase) {
1111
it("should stringify values " + testCase, function() {
1212
var input = testCases[testCase][1];
13-
var expected = testCases[testCase][0];
13+
var expected = testCases[testCase][2] || testCases[testCase][0];
1414
assert.deepEqual(Tokenizer.stringifyValues(input), expected);
1515
});
1616
});

test/test-cases-values.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,20 @@ module.exports = {
175175
"\"\\1F50E\"",
176176
singleValue([
177177
{ type: "string", stringType: "\"", value: "\ud83d\udd0e" }
178-
])
178+
]),
179+
],
180+
"escaped unicode 5 (extra short)": [
181+
"\"\\A\"",
182+
singleValue([
183+
{ type: "string", stringType: "\"", value: "\u000A" }
184+
]),
185+
],
186+
"escaped unicode 6 (full length)": [
187+
"\"\\00000A\"",
188+
singleValue([
189+
{ type: "string", stringType: "\"", value: "\u000A" }
190+
]),
191+
"\"\\A\""
179192
],
180193
"nested-item-with append": [
181194
"linear-gradient(45deg) 25%",

0 commit comments

Comments
 (0)