1
Vote

JavaScript Minification like as "function window.onload(){……}" problem

description

Hi
 
JavaScript Minification like as "function window.onload(){……}" problem
eg.
function window.onload(){
 document.all(""spanLevel"").innerHTML=document.all(""sHtml"").value;
}
output(loose "onload"):
function window(){ document.all(""spanLevel"").innerHTML=document.all(""sHtml"").value}
 
correct output:
function window.onload(){ document.all(""spanLevel"").innerHTML=document.all(""sHtml"").value}

comments

ronlo wrote May 13, 2011 at 4:55 PM

Well, technically that's invalid JavaScript. It may be okay in IE to have the function name be an object member operator, but it doesn't work in any other browsers I tested it in (Safari, Chrome, Firefox, and Opera). That said, since it does work in IE [learn something new every day] I'll try to get the code to output as you expect. It will still throw a syntax error, though.

ronlo wrote May 13, 2011 at 9:42 PM

This will be fixed in the 4.21 release, changeset #73357. I'll mark the bug as "fixed" once it's released.

zhxucan wrote May 14, 2011 at 4:34 AM

thank you.but whether you can auto fix it like "window.onload = function(){………………}"

ronlo wrote Jun 2, 2011 at 1:03 AM

Fixed the original issue (broken output not matching the input) in released version 4.21. I did not, however, implement the [very good] suggestion of automatically fixing the code to work in all browsers; that will have to wait for a future release. I'm going to leave this bug active for now, to reflect that new request.

abm wrote Jul 28, 2012 at 2:45 AM

Reconsidering your example:

function window.onload(){
document.all(""spanLevel"").innerHTML=document.all(""sHtml"").value;
}

It will result in

function window.onload(){document.all("").innerHTML=document.all("").value}

as you have double quotes ("") wrapping on both sides of id!!

There are 3 corrections required on this code:
  1. replace double(double(quotes)) "" with single(double(quotes)) "
  2. replace function winndow.onload(){...} with window.onload = function(){...}
  3. replace document.all with document.getElementById.... atleast in those cases where all() is parametrized (meaning its being used instead of getElementById).
Would be an excellent deal if the engine "sniff" out browser sniffing and replace it with feature detection (where its safe).

ronlo wrote Aug 1, 2012 at 7:28 PM

The double-double-quotes are still an issue: they aren't valid JavaScript. If those were single double-quote characters:
function window.onload(){
 document.all("spanLevel").innerHTML=document.all("sHtml").value;
}
Then you'd get the expected output of:
function window.onload(){document.all("spanLevel").innerHTML=document.all("sHtml").value}
But you can't double-up your quote characters and expect valid minification. I have no plans currently to support malformed strings like that, although the next version of the parser might just give up on that syntax and output it as-is.