From c31e40813147f4de4e3ad5f4b0eecbf1f4c96091 Mon Sep 17 00:00:00 2001 From: Yusuke Kawasaki Date: Sun, 7 Oct 2018 22:51:24 +0900 Subject: [PATCH] Invisible control characters escaped per default #3265 --- lib/output.js | 9 ++++++--- test/compress/ascii.js | 4 ++-- test/mocha/string-literal.js | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/output.js b/lib/output.js index 7f7e74df..b2ec1945 100644 --- a/lib/output.js +++ b/lib/output.js @@ -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\0\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 c = s.charCodeAt(0); + if (c < 8) return "\\0" + c; + return "\\x" + (c < 16 ? "0" : "") + c.toString(16); } return s; }); diff --git a/test/compress/ascii.js b/test/compress/ascii.js index 3c7cc5cf..1563b2f2 100644 --- a/test/compress/ascii.js +++ b/test/compress/ascii.js @@ -13,7 +13,7 @@ ascii_only_true: { "\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\\01\\02\\03\\04\\05\\06\\07\\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\'}' } ascii_only_false: { @@ -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\\01\\02\\03\\04\\05\\06\\07\\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\'}' } diff --git a/test/mocha/string-literal.js b/test/mocha/string-literal.js index aca915b7..3c7c6428 100644 --- a/test/mocha/string-literal.js +++ b/test/mocha/string-literal.js @@ -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";"\\07";'] ]; for (var test in tests) {