Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template strings do not respect ascii_only option #5910

Closed
PurkkaKoodari opened this issue Aug 7, 2024 · 3 comments · Fixed by #5911
Closed

Template strings do not respect ascii_only option #5910

PurkkaKoodari opened this issue Aug 7, 2024 · 3 comments · Fixed by #5911

Comments

@PurkkaKoodari
Copy link

I'm working with some modern JS code in an environment that doesn't handle control characters well, but the source code contains them in template strings. UglifyJS is outputting them raw even with ascii_only enabled.

Uglify version (uglifyjs -V)

uglify-js 3.19.1

JavaScript input

[`\0\f\x1f\u263a`, "\0\f\x1f\u263a"]

The uglifyjs CLI command executed or minify() options used.

npx uglifyjs --compress --output-opts ascii_only --comments UserScript --expression

JavaScript output or error produced.

(with non-printable bytes written out:)

[`<00><0c><1f>☺`,"\0\f\x1f\u263a"]

Potential solution

I think this would be solved by just adding to_utf8 calls to the output for AST_Template. It did resolve this in my local test, but I'm not familiar with the codebase.

UglifyJS/lib/output.js

Lines 1786 to 1798 in 2255074

DEFPRINT(AST_Template, function(output) {
var self = this;
if (self.tag) self.tag.print(output);
output.print("`");
for (var i = 0; i < self.expressions.length; i++) {
output.print(self.strings[i]);
output.print("${");
self.expressions[i].print(output);
output.print("}");
}
output.print(self.strings[i]);
output.print("`");
});

     DEFPRINT(AST_Template, function(output) {
         var self = this;
         if (self.tag) self.tag.print(output);
         output.print("`");
         for (var i = 0; i < self.expressions.length; i++) {
-            output.print(self.strings[i]);
+            output.print(output.to_utf8(self.strings[i]));
             output.print("${");
             self.expressions[i].print(output);
             output.print("}");
         }
-        output.print(self.strings[i]);
+        output.print(output.to_utf8(self.strings[i]));
         output.print("`");
     });
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Aug 7, 2024
@alexlamsl
Copy link
Collaborator

Interesting use case − I didn't think people using latest versions of ECMAScript would still be struggling with Unicode 😉

@PurkkaKoodari
Copy link
Author

Interesting use case − I didn't think people using latest versions of ECMAScript would still be struggling with Unicode 😉

Unicode isn't a problem, but null bytes in source code aren't the favorite of certain software 😁

@alexlamsl
Copy link
Collaborator

Patch released in uglify-js@3.19.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants