HoriSun / closure-compiler

Automatically exported from code.google.com/p/closure-compiler
0 stars 0 forks source link

Using --charset 'utf-8', \u2028 and \u2029 are not escaped. #718

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. echo 'alert("\u2028\u2029")' | java -jar compiler.jar --charset 'utf-8'

What is the expected output? What do you see instead?

I expect `alert("\u2028\u2029")`, but instead I get `alert("  ")` (a literal 
\u2028 and \u2029 character).

What version of the product are you using? On what operating system?

brad:~/Downloads$ java -jar compiler.jar --version
Closure Compiler (http://code.google.com/closure/compiler)
Version: 20120430 (revision 1918)
Built on: 2012/04/30 18:02

Original issue reported on code.google.com by DunbarB2 on 2 May 2012 at 11:22

GoogleCodeExporter commented 9 years ago
brad:~/Downloads$ java -version
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b04-415-11M3635)
Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01-415, mixed mode)

Using OSX 10.7.3

Original comment by DunbarB2 on 2 May 2012 at 11:23

GoogleCodeExporter commented 9 years ago
Note:  This is only with --charset 'utf-8' and does not manifest when not using 
that flag.

Original comment by DunbarB2 on 2 May 2012 at 11:24

GoogleCodeExporter commented 9 years ago
Proposed fix:

diff --git a/src/com/google/javascript/jscomp/CodeGenerator.java 
b/src/com/google/javascript/jscomp/CodeGenerator.java
index 4143577..49699b0 100644
--- a/src/com/google/javascript/jscomp/CodeGenerator.java
+++ b/src/com/google/javascript/jscomp/CodeGenerator.java
@@ -1019,6 +1019,8 @@ class CodeGenerator {
         case '\\': sb.append(backslashEscape); break;
         case '\"': sb.append(doublequoteEscape); break;
         case '\'': sb.append(singlequoteEscape); break;
+        case '\u2028': sb.append("\\u2028"); break;
+        case '\u2029': sb.append("\\u2029"); break;
         case '>':                       // Break --> into --\> or ]]> into ]]\>
           if (i >= 2 &&
               ((s.charAt(i - 1) == '-' && s.charAt(i - 2) == '-') ||

Original comment by DunbarB2 on 2 May 2012 at 12:00

GoogleCodeExporter commented 9 years ago
One more note:  The reason for the patch is that \u2028 and \u2029 cannot be 
included in javascript string literals since they are valid line terminators.  
This cropped up in an issue reported to underscorejs in which a string literal 
containing these was not escaped and caused a syntax error in compiled code.  
Relevant links below.

http://es5.github.com/#x7.3
https://github.com/documentcloud/underscore/issues/579

Original comment by DunbarB2 on 2 May 2012 at 1:34

GoogleCodeExporter commented 9 years ago
thanks for the report! have you filled out a CLA? see:
http://code.google.com/p/closure-compiler/source/browse/trunk/CONTRIBUTORS

Original comment by Nicholas.J.Santos on 2 May 2012 at 6:37

GoogleCodeExporter commented 9 years ago
I have now.  :)

Original comment by DunbarB2 on 2 May 2012 at 8:36

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r1947.

Original comment by `` on 3 May 2012 at 5:57

GoogleCodeExporter commented 9 years ago
Fantastic!  Thanks Nicholas!

Original comment by DunbarB2 on 3 May 2012 at 6:17

GoogleCodeExporter commented 9 years ago
Issue 761 has been merged into this issue.

Original comment by chadkill...@missouristate.edu on 22 Jun 2012 at 12:15

GoogleCodeExporter commented 9 years ago
Issue 761 has been merged into this issue.

Original comment by Nicholas.J.Santos on 22 Jun 2012 at 3:33