/** * This method takes a string and outputs a JavaScript string literal. The * data is surrounded with quotes, and any contained characters that need to * be escaped are mapped onto their escape sequence. * * Assumptions: We are targeting a version of JavaScript that that is later * than 1.3 that supports unicode strings. */ public static function escapeString($toEscape) { //TODO: implement $charVector = ''; $charVector .= JS_QUOTE_CHAR; $n = strlen($toEscape); $i = 0; while ($i < $n) { $bytes = 0; $c = CharacterUtil::ordUTF8($toEscape, $i, $bytes); $i += $bytes; if (intval($c) < NUMBER_OF_JS_ESCAPED_CHARS && isset(ServerSerializationStreamWriter::$JS_CHARS_ESCAPED[chr($c)])) { $charVector .= JS_ESCAPE_CHAR; $charVector .= ServerSerializationStreamWriter::$JS_CHARS_ESCAPED[chr($c)]; } else { if (ServerSerializationStreamWriter::needsUnicodeEscape($c)) { $charVector .= JS_ESCAPE_CHAR; ServerSerializationStreamWriter::unicodeEscape($c, &$charVector); //$charVector.= JS_ESCAPE_CHAR.'x'. strtoupper(dechex($c)); } else { $charVector .= CharacterUtil::chrUTF8($c); } } } $charVector .= JS_QUOTE_CHAR; return $charVector; }