Author: nbelaevski
Date: 2010-09-01 10:49:53 -0400 (Wed, 01 Sep 2010)
New Revision: 19075
Modified:
branches/RF-7560/core/api/src/main/java/org/ajax4jsf/javascript/JSEncoder.java
branches/RF-7560/core/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java
Log:
https://jira.jboss.org/browse/RF-9285
Modified: branches/RF-7560/core/api/src/main/java/org/ajax4jsf/javascript/JSEncoder.java
===================================================================
---
branches/RF-7560/core/api/src/main/java/org/ajax4jsf/javascript/JSEncoder.java 2010-09-01
14:18:00 UTC (rev 19074)
+++
branches/RF-7560/core/api/src/main/java/org/ajax4jsf/javascript/JSEncoder.java 2010-09-01
14:49:53 UTC (rev 19075)
@@ -31,13 +31,14 @@
// private char APOSTROPHE[] = { '\\', '\'' };
private static final char[] ENCODE_HEX = "0123456789ABCDEF".toCharArray();
- private static final char[] ENCODE_APOS = "\\'".toCharArray();
private static final char[] ENCODE_QUOT = "\\\"".toCharArray();
private static final char[] ENCODE_LF = "\\n".toCharArray();
+ private static final char[] ENCODE_BC = "\\b".toCharArray();
private static final char[] ENCODE_FF = "\\f".toCharArray();
private static final char[] ENCODE_CR = "\\r".toCharArray();
private static final char[] ENCODE_TAB = "\\t".toCharArray();
private static final char[] ENCODE_BS = "\\\\".toCharArray();
+ private static final char[] ENCODE_FS = "\\/".toCharArray();
// private static final char ENCODE_ESC[] = "\\e".toCharArray();
@@ -47,32 +48,34 @@
public JSEncoder() {}
/**
- * Return true or false wether this encoding can encode the specified
+ * Return true or false whether this encoding/format can encode the specified
* character or not.
* <p>
* This method will return true for the following character range: <br />
* <code>
- * <nobr>#x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD]</nobr>
+ * <nobr>\b | \f | \t | \r | \n | " | \ | / | [#x20-#xD7FF] |
[#xE000-#xFFFD]</nobr>
* </code>
* </p>
*
* @see <a
href="http://www.w3.org/TR/REC-xml#charsets">W3C XML 1.0
</a>
+ * @see <a
href="http://json.org/">JSON.org</a>
*/
public boolean compile(char c) {
- if ((c == 0x09) || // [\t]
- (c == 0x0a) || // [\n]
- (c == 0x0d) || // [\r](c == 0x22) || // ["]
- (c == 0x22) || // ["]
- (c == 0x27) || // [']
- (c == 0x5c) || // [\]
- (c == 0x03) || // [esc]
- (c == ']') || // ] - to avoid conflicts
in CDATA
- (c == '<') || // - escape HTML markup
characters
- (c == '>') || // - HTML
- (c == '&') || // - HTML
- (c == '-') || // - HTML comments
- (c < 0x20) || // See
<
http://www.w3.org/TR/REC-xml#charsets>
- ((c > 0xd7ff) && (c < 0xe000)) ||
(c > 0xfffd) || (c > 0xff)) {
+ if ((c == '\b') ||
+ (c == '\f') |
+ (c == '\t') ||
+ (c == '\n') ||
+ (c == '\r') ||
+ (c == '"') ||
+ (c == '\\') ||
+ (c == '/') ||
+ (c == ']') || // ] - to avoid
conflicts in CDATA
+ (c == '<') || // - escape HTML
markup characters
+ (c == '>') || // - HTML
+ (c == '&') || // - HTML
+ (c == '-') || // - HTML comments
+ (c < 0x20) || // See
<
http://www.w3.org/TR/REC-xml#charsets>
+ ((c > 0xd7ff) && (c < 0xe000))
|| (c > 0xfffd) || (c > 0xff)) {
return false;
}
@@ -85,39 +88,36 @@
*/
public char[] encode(char c) {
switch (c) {
- case 0x03 :
- return ENCODE_FF; // (>) [>]
+ case '\b' :
+ return ENCODE_BC;
+
+ case '\f' :
+ return ENCODE_FF;
- case 0x09 :
- return ENCODE_TAB; // (>) [>]
+ case '\t' :
+ return ENCODE_TAB;
- case 0x0a :
- return ENCODE_LF; // (>) [>]
+ case '\n' :
+ return ENCODE_LF;
- case 0x0d :
- return ENCODE_CR; // (>) [>]
+ case '\r' :
+ return ENCODE_CR;
- case 0x22 :
- return ENCODE_QUOT; // (") ["]
+ case '"' :
+ return ENCODE_QUOT;
- case 0x27 :
- return ENCODE_APOS; // (') [']
+ case '\\' :
+ return ENCODE_BS;
- case 0x5c :
- return ENCODE_BS; // (<) [<]
-
+ case '/' :
+ return ENCODE_FS;
+
default : {
- if (c > 0xff) {
- char[] ret = {
- '\\', 'u', ENCODE_HEX[c >> 0xc & 0xf],
ENCODE_HEX[c >> 0x8 & 0xf], ENCODE_HEX[c >> 0x4 & 0xf],
- ENCODE_HEX[c & 0xf]
- };
+ char[] ret = {
+ '\\', 'u', ENCODE_HEX[c >> 0xc & 0xf],
ENCODE_HEX[c >> 0x8 & 0xf], ENCODE_HEX[c >> 0x4 & 0xf],
+ ENCODE_HEX[c & 0xf]
+ };
- return ret;
- }
-
- char[] ret = {'\\', 'x', ENCODE_HEX[c >> 0x4 &
0xf], ENCODE_HEX[c & 0xf]};
-
return ret;
}
}
Modified:
branches/RF-7560/core/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java
===================================================================
---
branches/RF-7560/core/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java 2010-09-01
14:18:00 UTC (rev 19074)
+++
branches/RF-7560/core/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java 2010-09-01
14:49:53 UTC (rev 19075)
@@ -123,9 +123,9 @@
* Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
*/
public void testStringToScript() {
- Object obj = "foo";
+ Object obj = "f \b\r\t\f\n\"'\\/ oo";
- assertEquals("\"foo\"", ScriptUtils.toScript(obj));
+ assertEquals("\"f \\b\\r\\t\\f\\n\\\"'\\\\\\/ oo\"",
ScriptUtils.toScript(obj));
}
/**
@@ -260,8 +260,8 @@
public void testAddEncoded() {
StringBuilder buff = new StringBuilder();
- ScriptUtils.addEncoded(buff, "foo\"\'");
- assertEquals("foo\\\"\\\'", buff.toString());
+ ScriptUtils.addEncoded(buff, "foo");
+ assertEquals("foo", buff.toString());
}
/**