Commits

Anonymous committed e01c62b

WW-2555 Add javaScriptEscape attribute to propertyTag

Thanks to Jelmer Kuperus for the patch.

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1880e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits ffcfdd7

Comments (0)

Files changed (2)

src/java/com/opensymphony/xwork2/util/TextUtils.java

 
         return str.toString();
     }
+
+    /**
+     * Escape a String into a JavaScript-compatible String.
+     *
+     * @param s the String to escape.
+     * @return the escaped string
+     */
+    public final static String escapeJavaScript(String s) {
+        s = noNull(s);
+        StringBuffer str = new StringBuffer();
+
+        for (int j = 0; j < s.length(); j++) {
+            char c = s.charAt(j);
+            switch (c) {
+                case '\t':
+                    str.append("\\t");
+                    break;
+                case '\b':
+                    str.append("\\b");
+                    break;
+                case '\n':
+                    str.append("\\n");
+                    break;
+                case '\f':
+                    str.append("\\f");
+                    break;
+                case '\r':
+                    str.append("\\r");
+                    break;
+                case '\\':
+                    str.append("\\\\");
+                    break;
+                case '"':
+                    str.append("\\\"");
+                    break;
+                case '\'':
+                    str.append("\\'");
+                    break;
+                case '/':
+                    str.append("\\/");
+                    break;
+                default:
+                    str.append(c);
+            }
+        }
+        return str.toString();
+    }
+
     
     /**
      * Join an Iteration of Strings together.

src/test/com/opensymphony/xwork2/util/TextUtilsTest.java

  */
 public class TextUtilsTest extends XWorkTestCase {
 
+	public void testJavaScriptEscapeNoEscapes() {
+        assertEquals("", TextUtils.escapeJavaScript(null));
+        assertEquals("", TextUtils.escapeJavaScript(""));
+        assertEquals("   ", TextUtils.escapeJavaScript("   "));
+        assertEquals("Hello World", TextUtils.escapeJavaScript("Hello World"));
+	}
+	
+	public void testJavaScriptEscape() {
+		assertEquals("\\t", TextUtils.escapeJavaScript("\t"));
+		assertEquals("\\b", TextUtils.escapeJavaScript("\b"));
+		assertEquals("\\n", TextUtils.escapeJavaScript("\n"));
+		assertEquals("\\f", TextUtils.escapeJavaScript("\f"));
+		assertEquals("\\r", TextUtils.escapeJavaScript("\r"));
+		assertEquals("\\\\", TextUtils.escapeJavaScript("\\"));
+		assertEquals("\\\"", TextUtils.escapeJavaScript("\""));
+		assertEquals("\\'", TextUtils.escapeJavaScript("'"));
+		assertEquals("<\\/script", TextUtils.escapeJavaScript("</script"));
+	}
+	
     public void testHtmlEncodeNoHTML() {
         assertEquals("", TextUtils.htmlEncode(null));
         assertEquals("", TextUtils.htmlEncode(""));