[aklug] Re: Removing newlines from minified javascript

From: Michael Fowler <michael@shoebox.net>
Date: Wed Aug 08 2012 - 17:04:28 AKDT

On Wed, Aug 08, 2012 at 11:05:23AM -0800, Tim Johnson wrote:
> FYI: I am a former C programmer, but years removed. C *should*
> recognize a newline as ascii 10. C would also recognize an escaped
> newline as '\n' or ascii 92, ascii 110 (I think).

I was speaking of a literal newline, not an escape sequence. It appears
JavaScript requires you to escape newlines in strings; for example, this
is valid code (with surrounding test case):

<html>
<head><script type="text/javascript">
function t() {
    alert(
        "hi\
        how are you?\
        good!"
    );
}
</script></head>
<body><input type="button" onclick="t()" value="Go!"></body>
</html>

> As far as I know jslint will throw an error on an actual embedded
> newline as "Unclosed string". I have thought about patching jsmin.
> I note that I mispoke about jsmin's handling of newlines. It
> actually appears to remove about 90% of newlines.

If jslint will refuse to accept the above code then you don't need to
worry. If it doesn't, then your blind replacement of newlines could
change the meaning of your code. If you remove newlines entirely it
will get worse.

--
Michael Fowler
www.shoebox.net
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Wed Aug 8 17:04:50 2012

This archive was generated by hypermail 2.1.8 : Wed Aug 08 2012 - 17:04:50 AKDT