Escape all ASCII control characters within strings when using ascii_only.
authorkzc <zaxxon2011@gmail.com>
Thu, 24 Mar 2016 15:51:54 +0000 (11:51 -0400)
committerkzc <zaxxon2011@gmail.com>
Thu, 24 Mar 2016 15:51:54 +0000 (11:51 -0400)
Fixes #1017.

Tab characters within strings are now output as `\t` in all output modes.

lib/output.js
test/compress/ascii.js [new file with mode: 0644]

index a8c45a0..a59066f 100644 (file)
@@ -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 (file)
index 0000000..5c6b3b8
--- /dev/null
@@ -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\'}'
+}
+