aboutsummaryrefslogtreecommitdiff
path: root/lib/output.js
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2017-07-23 12:38:21 +0800
committerGitHub <noreply@github.com>2017-07-23 12:38:21 +0800
commit6a5e74b44e65811b2152f72aeec8df3f75457663 (patch)
treed597de5711f351ce1051e91d1e7894d3116f245d /lib/output.js
parent54446341eec4c77552d6ea60419ae940f401c16a (diff)
downloadtracifyjs-6a5e74b44e65811b2152f72aeec8df3f75457663.tar.gz
tracifyjs-6a5e74b44e65811b2152f72aeec8df3f75457663.zip
unescape surrogate pairs only (#2246)
fixes #2242
Diffstat (limited to 'lib/output.js')
-rw-r--r--lib/output.js19
1 files changed, 11 insertions, 8 deletions
diff --git a/lib/output.js b/lib/output.js
index edb8d182..4c873f10 100644
--- a/lib/output.js
+++ b/lib/output.js
@@ -109,7 +109,7 @@ function OutputStream(options) {
var current_pos = 0;
var OUTPUT = "";
- function to_ascii(str, identifier) {
+ var to_utf8 = options.ascii_only ? function(str, identifier) {
return str.replace(/[\u0000-\u001f\u007f-\uffff]/g, function(ch) {
var code = ch.charCodeAt(0).toString(16);
if (code.length <= 2 && !identifier) {
@@ -120,6 +120,12 @@ function OutputStream(options) {
return "\\u" + code;
}
});
+ } : function(str) {
+ return str.replace(/[\ud800-\udbff](?![\udc00-\udfff])/g, function(ch) {
+ return "\\u" + ch.charCodeAt(0).toString(16);
+ }).replace(/(^|[^\ud800-\udbff])([\udc00-\udfff])/g, function(match, prefix, ch) {
+ return prefix + "\\u" + ch.charCodeAt(0).toString(16);
+ });
};
function make_string(str, quote) {
@@ -150,7 +156,7 @@ function OutputStream(options) {
function quote_double() {
return '"' + str.replace(/\x22/g, '\\"') + '"';
}
- if (options.ascii_only) str = to_ascii(str);
+ str = to_utf8(str);
switch (options.quote_style) {
case 1:
return quote_single();
@@ -175,8 +181,7 @@ function OutputStream(options) {
function make_name(name) {
name = name.toString();
- if (options.ascii_only)
- name = to_ascii(name, true);
+ name = to_utf8(name, true);
return name;
};
@@ -433,7 +438,7 @@ function OutputStream(options) {
last : function() { return last },
semicolon : semicolon,
force_semicolon : force_semicolon,
- to_ascii : to_ascii,
+ to_utf8 : to_utf8,
print_name : function(name) { print(make_name(name)) },
print_string : function(str, quote, escape_directive) {
var encoded = encode_string(str, quote);
@@ -1318,9 +1323,7 @@ function OutputStream(options) {
if (regexp.raw_source) {
str = "/" + regexp.raw_source + str.slice(str.lastIndexOf("/"));
}
- if (output.option("ascii_only")) {
- str = output.to_ascii(str);
- }
+ str = output.to_utf8(str);
output.print(str);
var p = output.parent();
if (p instanceof AST_Binary && /^in/.test(p.operator) && p.left === self)