Skip to content

Properly parse unicode characters in strings #13

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

Merged
merged 1 commit into from
Jul 20, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions lib/parseValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ function endSpacingMatch(match) {
}

function unescapeString(content) {
return content.replace(/\\([a-fA-F0-9]{2,5}|.)/g, function(escaped) {
if(escaped.length > 2) {
var C = parseInt(escaped.substr(1), 16);
if(C < 0x10000) {
return String.fromCharCode(C);
} else {
return String.fromCharCode(Math.floor((C - 0x10000) / 0x400) + 0xD800) +
String.fromCharCode((C - 0x10000) % 0x400 + 0xDC00);
}
return content.replace(/\\(?:([a-fA-F0-9]{1,6})|(.))/g, function(all, unicode, otherCharacter) {
if (otherCharacter) {
return otherCharacter;
}

var C = parseInt(unicode, 16);
if(C < 0x10000) {
return String.fromCharCode(C);
} else {
return escaped.substr(1);
return String.fromCharCode(Math.floor((C - 0x10000) / 0x400) + 0xD800) +
String.fromCharCode((C - 0x10000) % 0x400 + 0xDC00);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion test/stringifyValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe("stringifyValues", function() {
Object.keys(testCases).forEach(function(testCase) {
it("should stringify values " + testCase, function() {
var input = testCases[testCase][1];
var expected = testCases[testCase][0];
var expected = testCases[testCase][2] || testCases[testCase][0];
assert.deepEqual(Tokenizer.stringifyValues(input), expected);
});
});
Expand Down
15 changes: 14 additions & 1 deletion test/test-cases-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,20 @@ module.exports = {
"\"\\1F50E\"",
singleValue([
{ type: "string", stringType: "\"", value: "\ud83d\udd0e" }
])
]),
],
"escaped unicode 5 (extra short)": [
"\"\\A\"",
singleValue([
{ type: "string", stringType: "\"", value: "\u000A" }
]),
],
"escaped unicode 6 (full length)": [
"\"\\00000A\"",
singleValue([
{ type: "string", stringType: "\"", value: "\u000A" }
]),
"\"\\A\""
],
"nested-item-with append": [
"linear-gradient(45deg) 25%",
Expand Down