UglifyJS icon indicating copy to clipboard operation
UglifyJS copied to clipboard

Issue with compressing escaped UTF8 characters

Open lehni opened this issue 14 years ago • 25 comments

I am running into an issue when attempting to compress Acorn.js or Esprima.js for browser use:

Both libraries contain regular expressions that are full of escaped UTF8 characters (Look for NonAsciiIdentifierStart in Esprima, for example).

UglifyJS reads these, resolves the escaped characters, and then outputs them to real UTF8 characters.

Loading this compressed library on WebKit then causes an exception:

Invalid regular expression: range out of order in character class

Using UglifyJS1, I could pass --ascii to the compressor, solving this issue, but leading to an increased file-size of the result (about 4kb more, since all non-ascii chars would now be escaped).

In UglifyJS2, when passing -b ascii_only=true, I do not get any compressed output anymore. I believe, since the beautifier is activated, -c is ignored.

But Ideally, I would like an option that would allow me to just preserve these strings in their current form, including all escaping that they contain.

Depending on the JS parser in use, this might be hard to implement though.

What do you suggest?

lehni avatar Nov 23 '12 21:11 lehni

Using -b ascii_only=true works, but -b automatically implies “beautify”. One way around this is -b ascii_only=true,beautify=false. I'll add a separate --ascii-only flag, since I imagine it will be a common need, but in the mean time you can use this hack (it'll continue to work).

mishoo avatar Nov 24 '12 08:11 mishoo

That's good to know! But what do you think of an option to preserve the strings as they are? Escaping all unicode chars will unnecessarily increase the size of these libraries by 4kb otherwise.

lehni avatar Nov 24 '12 08:11 lehni

That's more tricky because we don't store any information about how the actual string/regexp was written. Perhaps --regexp-ascii-only would be an acceptable compromise?

mishoo avatar Nov 24 '12 08:11 mishoo

In this particular case, the regexps are constructed from strings, so I don't think that would work?

lehni avatar Nov 24 '12 08:11 lehni

Indeed, it won't help then.. If I think about it, in Acorn, except for those long regexps there's probably no other place where Unicode chars are used literally—which means that even with an option to keep literals as they are in the original source, you won't get smaller output. So I still don't see a good reason to implement this.

Here's another suggestion. I am assuming you compress multiple files and would like the ascii_only to apply only to Acorn. I could add support for per-file compression options, specified in the source file. So you'd add to acorn.js for example:

// this is a no-op statement, UglifyJS will drop it from the output
UglifyJS: ({
    codegen: {
        ascii_only: true
    }
});

This isn't a trivial change either :( but perhaps it would be best in the long run.

mishoo avatar Nov 24 '12 08:11 mishoo

Would it not be possible to keep the original source in the AST and when asked for output these instead of the newly constructed source?

rvanvelzen avatar Nov 26 '12 18:11 rvanvelzen

It's true that in Acorn, the size would not differ, since all unicode-chars are also escaped in the original file already.

As to the suggested change to allow configuring UglifyJS differently per file: This is not necessary for Paper.js, since we minify the library at the end, so we are just specifying "-b ascii_only=true,beautify=false" there now, and that works for us.

Feel free to close this bug, if you consider it a "wont fix".

lehni avatar Dec 25 '12 15:12 lehni

Feel free to close this bug, if you consider it a "wont fix".

I don't; in fact I'd like to add an option to keep literals as in the original source, at some point, so let's keep the ticket open.

mishoo avatar Dec 25 '12 23:12 mishoo

This problem exists also for ES5-Shim/Sham and the workaround works. Thanks.

kriskowal avatar May 06 '13 22:05 kriskowal

The problem exists when minifying SockJS as well: https://github.com/sockjs/sockjs-client/blob/3f7e5f4ef5673dc15d3d3db890bc82a725f0c91d/lib/utils.js#L182-L211

They solve it in a pre-minification step here: https://github.com/sockjs/sockjs-client/blob/master/bin/render.coffee#L33-L38

However when I use their non-minified source and run my own minification i of course get in trouble. Especially IE does not like this transformation:

var foo = {
    "\ufff0": "\\ufff0",
    "\ufff1": "\\ufff1"
};

to

var foo={"￰":"\\ufff0","￱":"\\ufff1"};

IE sees both property names as the same, firing a warning: multiple definitions of a property not allowed in strict mode

Munter avatar May 21 '13 16:05 Munter

While it's a good feature to have to preserve utf-8 as is in uglify, for those who arrived at this page looking for solution to the "Invalid regular expression: range out of order in character class", you can at least add charset="utf-8" in your script tag's attribute, and also make sure you have in your html mark up to get rid of the error.

michaelwsk avatar Oct 14 '13 00:10 michaelwsk

Do I understand correctly that the regexp problem only occurs when files aren't served with the right encoding information? I've just added another fat unicode regexp to one of my libraries (to recognize extending characters in CodeMirror), and it'd be great if users have the option of using the UTF8 encoding of this regexp (which will be about three time smaller than the escaped version), somehow.

UglifyJS seems to always use the original source for regexps now, but unless there are browser issues preventing this from working, I would like some way to force it to spit out the unescaped version of the regexp somehow.

marijnh avatar Jan 09 '14 22:01 marijnh

@marijnh the way a literal regexp goes out is via its toString() method. While UglifyJS does no (un)escaping (except when ascii_only is passed to the code generator), it seems possible that some particular implementation of RegExp::toString can modify the output. Can you send a test case?

mishoo avatar Jan 09 '14 23:01 mishoo

I'm testing on node (v0.11.9), the following program:

var string = "\u00e9"; var re = /\u00e9/;

gets compressed to

var string="é";var re=/\u00e9/;

i.e. the string does get unescaped, but the encoding for the regexp is preserved.

What I'm mostly curious about is what the intended behavior is, and why it is that way.

marijnh avatar Jan 09 '14 23:01 marijnh

Got it. Seems it was always this way for regexps. I'm trying out unescaping them, but it's not valid for all cases, for example if I unescape the following regexp: /^[ \n\t\x0C\u2028\u2029\xA0]+/ I get an "invalid regexp" error in NodeJS. I'm not sure what other cases there are... do you happen to know how to decide what characters to replace and what to remain escaped?

mishoo avatar Jan 10 '14 08:01 mishoo

\u2029 and \u2029 count as line separators in JavaScript, so they can not appear unescaped in regexps or strings.

marijnh avatar Jan 10 '14 08:01 marijnh

Pushed it. I hope there aren't other invalid cases... :)

mishoo avatar Jan 10 '14 08:01 mishoo

Should be okay—those two (along with \r and \n) are the only line separators.

marijnh avatar Jan 10 '14 08:01 marijnh

There's at least one case missing: '/'. UglifyJS itself uses this in

if (options.inline_script)
    ret = ret.replace(/<\x2fscript([>\/\t\n\f\r ])/gi, "<\\/script$1");

However, even when 47 is added to the code list, minifying UglifyJS itself (exported via uglifyjs --self) seems to produce invalid JS.

lautis avatar Jan 18 '14 10:01 lautis

@lautis Good point, thanks. Fixed. I'll publish a new version.

mishoo avatar Jan 18 '14 10:01 mishoo