Invisible control characters escaped per default (retry) #3265

This commit is contained in:
Yusuke Kawasaki 2018-10-07 22:51:24 +09:00
parent ea999b0e92
commit 5b8c62d1b4
3 changed files with 8 additions and 5 deletions

View File

@ -111,10 +111,9 @@ function OutputStream(options) {
var OUTPUT = "";
var to_utf8 = options.ascii_only ? function(str, identifier) {
return str.replace(/[\u0000-\u001f\u007f-\uffff]/g, function(ch) {
return str.replace(/[\u0080-\uffff]/g, function(ch) {
var code = ch.charCodeAt(0).toString(16);
if (code.length <= 2 && !identifier) {
while (code.length < 2) code = "0" + code;
return "\\x" + code;
} else {
while (code.length < 4) code = "0" + code;
@ -136,7 +135,7 @@ function OutputStream(options) {
function make_string(str, quote) {
var dq = 0, sq = 0;
str = str.replace(/[\\\b\f\n\r\v\t\x22\x27\u2028\u2029\0\ufeff]/g,
str = str.replace(/[\\\0-\x1f\x22\x27\x7f\u2028\u2029\ufeff]/g,
function(s, i) {
switch (s) {
case '"': ++dq; return '"';
@ -153,6 +152,10 @@ function OutputStream(options) {
case "\ufeff": return "\\ufeff";
case "\0":
return /[0-9]/.test(str.charAt(i+1)) ? "\\x00" : "\\0";
default:
var code = s.charCodeAt(0).toString(16);
if (code.length < 2) code = "0" + code;
return "\\x" + code;
}
return s;
});

View File

@ -31,5 +31,5 @@ ascii_only_false: {
"\x20\x21\x22\x23 ... \x7d\x7e\x7f\x80\x81 ... \xfe\xff\u0fff\uffff";
}
}
expect_exact: 'function f(){return"\\x000\\x001\\x007\\x008\\0"+"\\0\x01\x02\x03\x04\x05\x06\x07\\b\\t\\n\\v\\f\\r\x0e\x0f"+"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"+\' !"# ... }~\x7f\x80\x81 ... \xfe\xff\u0fff\uffff\'}'
expect_exact: 'function f(){return"\\x000\\x001\\x007\\x008\\0"+"\\0\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\b\\t\\n\\v\\f\\r\\x0e\\x0f"+"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f"+\' !"# ... }~\\x7f\x80\x81 ... \xfe\xff\u0fff\uffff\'}'
}

View File

@ -65,7 +65,7 @@ describe("String literals", function() {
['"\\008"', '"\\x008";'],
['"\\0008"', '"\\x008";'],
['"use strict" === "use strict";\n"\\76";', '"use strict"==="use strict";">";'],
['"use\\\n strict";\n"\\07";', ';"use strict";"\07";']
['"use\\\n strict";\n"\\07";', ';"use strict";"\\x07";']
];
for (var test in tests) {