diff --git a/lib/output.js b/lib/output.js index 7f7e74df..1c0a6bb6 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\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; }); diff --git a/test/compress/ascii.js b/test/compress/ascii.js index 3c7cc5cf..2933bec7 100644 --- a/test/compress/ascii.js +++ b/test/compress/ascii.js @@ -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\'}' } diff --git a/test/mocha/string-literal.js b/test/mocha/string-literal.js index aca915b7..2d292a85 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";"\\x07";'] ]; for (var test in tests) {