Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript parse error on '\u2028' unicode character

Whenever I use the \u2028 character literal in my javascript source with the content type set to "text/html; charset=utf-8" I get a javascript parse errors.

Example:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"    "http://www.w3.org/TR/html4/strict.dtd">  <html lang="en"> <head>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">     <title>json</title>      <script type="text/javascript" charset="utf-8">     var string = '
    ';     </script> </head> <body>  </body> </html> 

If the <meta http-equiv> is left out everything works as expected. I've tested this on Safari and Firefox, both exhibit the same problem.

Any ideas on why this is happening and how to properly fix this (without removing the encoding)?

Edit: After some more research, the specific problem was that the problem character was returned using JSONP. This was then interpreted by the browser, which reads u2028 as a newline and throws an error about an invalid newline in a string.

like image 312
klaaspieter Avatar asked Jun 03 '10 11:06

klaaspieter


1 Answers

Yes, it's a feature of the JavaScript language, documented in the ECMAScript standard (3rd edition section 7.3), that the U+2028 and U+2029 characters count as line endings. Consequently a JavaScript parser will treat any unencoded U+2028/9 character in the same way as a newline. Since you can't put a newline inside a string literal, you get a syntax error.

This is an unfortunate oversight in the design of JSON: it is not actually a proper subset of JavaScript. Raw U+2028/9 characters are valid in string literals in JSON, and will be accepted by JSON.parse, but not so in JavaScript itself.

Hence it is only safe to generate JavaScript code using a JSON parser if you're sure it explicitly \u-escapes those characters. Some do, some don't; many \u-escape all non-ASCII characters, which avoids the problem.

like image 166
bobince Avatar answered Sep 28 '22 01:09

bobince