From 07bb7262d02547cef5ffaca71d49c86640a1df15 Mon Sep 17 00:00:00 2001 From: kzc Date: Thu, 24 Mar 2016 11:51:54 -0400 Subject: [PATCH] Escape all ASCII control characters within strings when using ascii_only. Fixes #1017. Tab characters within strings are now output as `\t` in all output modes. --- lib/output.js | 11 ++++++----- test/compress/ascii.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 test/compress/ascii.js diff --git a/lib/output.js b/lib/output.js index a8c45a0f..a59066fc 100644 --- a/lib/output.js +++ b/lib/output.js @@ -74,7 +74,7 @@ function OutputStream(options) { var OUTPUT = ""; function to_ascii(str, identifier) { - return str.replace(/[\u0080-\uffff]/g, function(ch) { + return str.replace(/[\u0000-\u001f\u007f-\uffff]/g, function(ch) { var code = ch.charCodeAt(0).toString(16); if (code.length <= 2 && !identifier) { while (code.length < 2) code = "0" + code; @@ -90,16 +90,17 @@ function OutputStream(options) { var dq = 0, sq = 0; str = str.replace(/[\\\b\f\n\r\v\t\x22\x27\u2028\u2029\0\ufeff]/g, function(s){ switch (s) { + case '"': ++dq; return '"'; + case "'": ++sq; return "'"; case "\\": return "\\\\"; - case "\b": return "\\b"; - case "\f": return "\\f"; case "\n": return "\\n"; case "\r": return "\\r"; + case "\t": return "\\t"; + case "\b": return "\\b"; + case "\f": return "\\f"; case "\x0B": return options.screw_ie8 ? "\\v" : "\\x0B"; case "\u2028": return "\\u2028"; case "\u2029": return "\\u2029"; - case '"': ++dq; return '"'; - case "'": ++sq; return "'"; case "\0": return "\\x00"; case "\ufeff": return "\\ufeff"; } diff --git a/test/compress/ascii.js b/test/compress/ascii.js new file mode 100644 index 00000000..5c6b3b8e --- /dev/null +++ b/test/compress/ascii.js @@ -0,0 +1,32 @@ +ascii_only_true: { + options = {} + beautify = { + ascii_only : true, + beautify : false, + } + input: { + function f() { + return "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" + + "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" + + "\x20\x21\x22\x23 ... \x7d\x7e\x7f\x80\x81 ... \xfe\xff\u0fff\uffff"; + } + } + expect_exact: 'function f(){return"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\b\\t\\n\\x0B\\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: { + options = {} + beautify = { + ascii_only : false, + beautify : false, + } + input: { + function f() { + return "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" + + "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" + + "\x20\x21\x22\x23 ... \x7d\x7e\x7f\x80\x81 ... \xfe\xff\u0fff\uffff"; + } + } + expect_exact: 'function f(){return"\\x00\x01\x02\x03\x04\x05\x06\x07\\b\\t\\n\\x0B\\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\'}' +} + -- 2.34.1