org.directwebremoting.util
Class JavascriptUtil

java.lang.Object
  extended by org.directwebremoting.util.JavascriptUtil

public class JavascriptUtil
extends java.lang.Object

Various Javascript code utilities. The escape classes were taken from jakarta-commons-lang which in turn borrowed from Turbine and other projects. The list of authors below is almost certainly far too long, but I'm not sure who really wrote these methods.

Author:
Joe Walker [joe at getahead dot ltd dot uk], Henri Yandell, Alexander Day Chaffee, Antony Riley, Helge Tesgaard, Sean Brown, Gary Gregory, Phil Steitz, Pete Gieser

Constructor Summary
JavascriptUtil()
           
 
Method Summary
static java.lang.String escapeJavaScript(java.lang.String str)
          Escapes the characters in a String using JavaScript String rules.
static boolean isReservedWord(java.lang.String name)
          Check to see if the given word is reserved or a bad idea in any known version of JavaScript.
static java.lang.String unescapeJavaScript(java.lang.String str)
          Unescapes any JavaScript literals found in the String.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JavascriptUtil

public JavascriptUtil()
Method Detail

escapeJavaScript

public static java.lang.String escapeJavaScript(java.lang.String str)

Escapes the characters in a String using JavaScript String rules.

Escapes any values it finds into their JavaScript String form. Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)

So a tab becomes the characters '\\' and 't'.

The only difference between Java strings and JavaScript strings is that in JavaScript, a single quote must be escaped.

Example:

 input string: He didn't say, "Stop!"
 output string: He didn\'t say, \"Stop!\"
 

Parameters:
str - String to escape values in, may be null
Returns:
String with escaped values, null if null string input

unescapeJavaScript

public static java.lang.String unescapeJavaScript(java.lang.String str)

Unescapes any JavaScript literals found in the String.

For example, it will turn a sequence of '\' and 'n' into a newline character, unless the '\' is preceded by another '\'.

Parameters:
str - the String to unescape, may be null
Returns:
A new unescaped String, null if null string input

isReservedWord

public static boolean isReservedWord(java.lang.String name)
Check to see if the given word is reserved or a bad idea in any known version of JavaScript.

Parameters:
name - The word to check
Returns:
false if the word is not reserved

Copyright 2008