escape unicode characters - fixes #154

This commit is contained in:
Sebastian McKenzie
2014-11-14 02:17:06 +11:00
parent 3ee51dae1a
commit 2f4e0c3361
3 changed files with 12 additions and 1 deletions

View File

@@ -74,7 +74,16 @@ exports.Literal = function (node) {
var val = node.value;
var type = typeof val;
if (type === "boolean" || type === "number" || type === "string") {
if (type === "string") {
val = JSON.stringify(val);
// escape unicode characters
val = val.replace(/[\u007f-\uffff]/g, function(c) {
return "\\u" + ("0000" + c.charCodeAt(0).toString(16)).slice(-4);
});
this.push(val);
} else if (type === "boolean" || type === "number") {
this.push(JSON.stringify(val));
} else if (node.regex) {
this.push("/" + node.regex.pattern + "/" + node.regex.flags);