Author: nbelaevski
Date: 2010-05-14 10:17:37 -0400 (Fri, 14 May 2010)
New Revision: 17053
Modified:
root/core/trunk/api/src/main/java/org/ajax4jsf/javascript/ScriptUtils.java
root/core/trunk/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java
Log:
https://jira.jboss.org/jira/browse/RF-8625
Modified: root/core/trunk/api/src/main/java/org/ajax4jsf/javascript/ScriptUtils.java
===================================================================
--- root/core/trunk/api/src/main/java/org/ajax4jsf/javascript/ScriptUtils.java 2010-05-14
14:17:22 UTC (rev 17052)
+++ root/core/trunk/api/src/main/java/org/ajax4jsf/javascript/ScriptUtils.java 2010-05-14
14:17:37 UTC (rev 17053)
@@ -29,6 +29,7 @@
import java.io.Writer;
import java.lang.reflect.Array;
import java.text.MessageFormat;
+import java.util.Arrays;
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.Iterator;
@@ -50,11 +51,19 @@
private static final Logger LOG = RichfacesLogger.UTIL.getLogger();
+ private static final char[] CSS_SELECTOR_CHARS_TO_ESCAPE =
createSortedCharArray("#;&,.+*~':\"!^$[]()=>|/");
+
/**
* This is utility class, don't instantiate.
*/
private ScriptUtils() {}
+ private static char[] createSortedCharArray(String s) {
+ char[] cs = s.toCharArray();
+ Arrays.sort(cs);
+ return cs;
+ }
+
private static void writeScriptToStream(Writer writer, Object obj, Map<Object,
Boolean> cycleBusterMap) throws IOException {
Boolean cycleBusterValue = cycleBusterMap.put(obj, Boolean.TRUE);
@@ -356,7 +365,7 @@
*
* @param property -
* value of property returned from
- * {@link UIComponent#getAttributes()}
+ * {@link javax.faces.component.UIComponent#getAttributes()}
* @return true for setted property, false otherthise.
*/
public static boolean isValidProperty(Object property) {
@@ -386,4 +395,44 @@
}
return true;
}
+
+ /**
+ * <p>
+ * Escapes CSS meta-characters in string according to
+ * <a
href="http://api.jquery.com/category/selectors/">jQuery
selectors</a> document.
+ * </p>
+ *
+ * @param s {@link String} to escape meta-characters in
+ * @return string with escaped characters.
+ */
+ public static String escapeCSSMetachars(String s) {
+ if (s == null || s.length() == 0) {
+ return s;
+ }
+
+ StringBuilder builder = new StringBuilder();
+
+ int start = 0;
+ int idx = 0;
+
+ int length = s.length();
+
+ for (; idx < length; idx++) {
+ char c = s.charAt(idx);
+
+ int searchIdx = Arrays.binarySearch(CSS_SELECTOR_CHARS_TO_ESCAPE, c);
+ if (searchIdx >= 0) {
+ builder.append(s.substring(start, idx));
+
+ builder.append("\\");
+ builder.append(c);
+
+ start = idx + 1;
+ }
+ }
+
+ builder.append(s.substring(start, idx));
+
+ return builder.toString();
+ }
}
Modified: root/core/trunk/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java
===================================================================
---
root/core/trunk/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java 2010-05-14
14:17:22 UTC (rev 17052)
+++
root/core/trunk/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java 2010-05-14
14:17:37 UTC (rev 17053)
@@ -46,6 +46,7 @@
import org.easymock.Capture;
import org.easymock.CaptureType;
import org.jboss.test.faces.mock.MockFacesEnvironment;
+import org.junit.Test;
/**
* @author shura
@@ -448,4 +449,21 @@
dehydrate(ScriptUtils.toScript(parent)));
}
+ @Test
+ public void testEscapeStringForCSSSelector() throws Exception {
+ assertNull(ScriptUtils.escapeCSSMetachars(null));
+ assertEquals("", ScriptUtils.escapeCSSMetachars(""));
+
+ assertEquals("test",
ScriptUtils.escapeCSSMetachars("test"));
+ assertEquals("test\\.string",
ScriptUtils.escapeCSSMetachars("test.string"));
+ assertEquals("test\\.\\.string",
ScriptUtils.escapeCSSMetachars("test..string"));
+
+ assertEquals("some\\.test\\.string",
ScriptUtils.escapeCSSMetachars("some.test.string"));
+
+ assertEquals("\\#test",
ScriptUtils.escapeCSSMetachars("#test"));
+ assertEquals("\\#\\#test",
ScriptUtils.escapeCSSMetachars("##test"));
+
+ assertEquals("test\\#",
ScriptUtils.escapeCSSMetachars("test#"));
+ assertEquals("test\\#\\#",
ScriptUtils.escapeCSSMetachars("test##"));
+ }
}