question: private static String javaScriptEscapeHelper ( String s , boolean escapeToAscii ) { StringBuilder sb = new StringBuilder ( s . length ( ) * 9 / 8 ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { char c = s . charAt ( i ) ; switch ( c ) { case '\n' : sb . append ( "\\n" ) ; break ; case '\r' : sb . append ( "\\r" ) ; break ; case '\t' : sb . append ( "\\t" ) ; break ; case '\\' : sb . append ( "\\\\" ) ; break ; case '\"' : sb . append ( "\\\"" ) ; break ; case '\'' : sb . append ( "\\\'" ) ; break ; case '=' : appendHexJavaScriptRepresentation ( sb , c ) ; break ; case '<' : case '/' : boolean isUnsafe = false ; for ( String tag : UNSAFE_TAGS ) { if ( s . regionMatches ( true , i + 1 , tag , 0 , tag . length ( ) ) ) { isUnsafe = true ; break ; } } if ( isUnsafe ) { appendHexJavaScriptRepresentation ( sb , c ) ; } else { sb . append ( c ) ; } break ; case '>' : if ( sb . length ( ) > 0 && sb . charAt ( sb . length ( ) - 1 ) == '-' ) { sb . append ( '\\' ) ; } sb . append ( c ) ; break ; case ' ' : sb . append ( "\\u2028" ) ; break ; case ' ' : sb . append ( "\\u2029" ) ; break ; default : if ( c >= 128 && escapeToAscii ) { appendHexJavaScriptRepresentation ( sb , c ) ; } else { sb . append ( c ) ; } } } return sb . toString ( ) ; }
target:  Helper for javaScriptEscape and javaScriptEscapeToAscii