corrected javadoc Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/c0e08569 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/c0e08569 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/c0e08569
Branch: refs/heads/master Commit: c0e0856921a13996c72451d43a6fe0456d61da77 Parents: 6540f02 Author: svenmeier <[email protected]> Authored: Thu Dec 13 15:00:28 2012 +0100 Committer: svenmeier <[email protected]> Committed: Thu Dec 13 15:00:28 2012 +0100 ---------------------------------------------------------------------- .../wicket/core/util/string/JavaScriptUtils.java | 48 +++++++------- .../core/util/string/JavaScriptUtilsTest.java | 31 +++++++++- 2 files changed, 54 insertions(+), 25 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/c0e08569/wicket-core/src/main/java/org/apache/wicket/core/util/string/JavaScriptUtils.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/string/JavaScriptUtils.java b/wicket-core/src/main/java/org/apache/wicket/core/util/string/JavaScriptUtils.java index 42d9274..c4862aa 100644 --- a/wicket-core/src/main/java/org/apache/wicket/core/util/string/JavaScriptUtils.java +++ b/wicket-core/src/main/java/org/apache/wicket/core/util/string/JavaScriptUtils.java @@ -22,36 +22,36 @@ import org.apache.wicket.util.string.Strings; /** * Provide some helpers to write javascript related tags to the response object. - * + * * @author Juergen Donnerstag */ public class JavaScriptUtils { - /** Script open tag */ - public final static String SCRIPT_OPEN_TAG = "<script type=\"text/javascript\">\n/*<![CDATA[*/\n"; - - /** Script close tag */ - public final static String SCRIPT_CLOSE_TAG = "\n/*]]>*/\n</script>\n"; - /** - * Script open tag. If this tag is changed, also update Wicket.Head.Contributor.processScript() - * function from wicket-ajax.js + * Prefix for JavaScript CDATA content. If this is changed, also update + * Wicket.Head.Contributor.processScript() function from wicket-ajax-jquery.js */ public final static String SCRIPT_CONTENT_PREFIX = "\n/*<![CDATA[*/\n"; /** - * Script close tag. If this tag is changed, also update Wicket.Head.Contributor.processScript() - * function from wicket-ajax.js + * Suffix for JavaScript CDATA content. If this is changed, also update + * Wicket.Head.Contributor.processScript() function from wicket-ajax-jquery.js */ public final static String SCRIPT_CONTENT_SUFFIX = "\n/*]]>*/\n"; + /** Script open tag including content prefix */ + public final static String SCRIPT_OPEN_TAG = "<script type=\"text/javascript\">" + + SCRIPT_CONTENT_PREFIX; + + /** Script close tag including content suffix */ + public final static String SCRIPT_CLOSE_TAG = SCRIPT_CONTENT_SUFFIX + "</script>\n"; /** The response object */ private final Response response; /** * Construct. - * + * * @param response * The response object * @param id @@ -64,7 +64,7 @@ public class JavaScriptUtils /** * Constructor without id for backward compatibility - * + * * @param response * The response object */ @@ -77,9 +77,9 @@ public class JavaScriptUtils /** * Escape single and double quotes so that they can be part of e.g. an alert call. - * + * * Note: JSON values need to escape only the double quote, so this method wont help. - * + * * @param input * the JavaScript which needs to be escaped * @return Escaped version of the input @@ -97,7 +97,7 @@ public class JavaScriptUtils /** * Write a reference to a javascript file to the response object - * + * * @param response * The HTTP response * @param url @@ -113,7 +113,7 @@ public class JavaScriptUtils /** * Write a reference to a javascript file to the response object - * + * * @param response * The HTTP response * @param url @@ -150,7 +150,7 @@ public class JavaScriptUtils /** * Write a reference to a javascript file to the response object - * + * * @param response * The HTTP response * @param url @@ -163,7 +163,7 @@ public class JavaScriptUtils /** * Write the simple text to the response object surrounded by a script tag. - * + * * @param response * The HTTP: response * @param text @@ -174,13 +174,13 @@ public class JavaScriptUtils public static void writeJavaScript(final Response response, final CharSequence text, String id) { writeOpenTag(response, id); - response.write(text); + response.write(Strings.replaceAll(text, "</", "<\\/")); writeCloseTag(response); } /** * Write the simple text to the response object surrounded by a script tag. - * + * * @param response * The HTTP: response * @param text @@ -192,7 +192,7 @@ public class JavaScriptUtils } /** - * + * * @param response * @param id */ @@ -208,7 +208,7 @@ public class JavaScriptUtils } /** - * + * * @param response */ public static void writeOpenTag(final Response response) @@ -217,7 +217,7 @@ public class JavaScriptUtils } /** - * + * * @param response */ public static void writeCloseTag(final Response response) http://git-wip-us.apache.org/repos/asf/wicket/blob/c0e08569/wicket-core/src/test/java/org/apache/wicket/core/util/string/JavaScriptUtilsTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/core/util/string/JavaScriptUtilsTest.java b/wicket-core/src/test/java/org/apache/wicket/core/util/string/JavaScriptUtilsTest.java index c984a0c..2dfb148 100644 --- a/wicket-core/src/test/java/org/apache/wicket/core/util/string/JavaScriptUtilsTest.java +++ b/wicket-core/src/test/java/org/apache/wicket/core/util/string/JavaScriptUtilsTest.java @@ -27,6 +27,7 @@ public class JavaScriptUtilsTest extends Assert { /** * https://issues.apache.org/jira/browse/WICKET-4546 + * * @throws Exception */ @Test @@ -39,6 +40,34 @@ public class JavaScriptUtilsTest extends Assert String charset = "some&bad%%charset"; JavaScriptUtils.writeJavaScriptUrl(response, url, id, defer, charset); - assertEquals("<script type=\"text/javascript\" id=\"some&bad%id\" defer=\"defer\" charset=\"some&bad%%charset\" src=\"some/url;jsessionid=1234?p1=v1&p2=v2\"></script>\n", response.toString()); + assertEquals( + "<script type=\"text/javascript\" id=\"some&bad%id\" defer=\"defer\" charset=\"some&bad%%charset\" src=\"some/url;jsessionid=1234?p1=v1&p2=v2\"></script>\n", + response.toString()); + } + + /** + */ + @Test + public void writeJavaScript() + { + StringResponse response = new StringResponse(); + JavaScriptUtils.writeJavaScript(response, + "var message = 'Scripts are written to the <script></script> tag'"); + + assertEquals("<script type=\"text/javascript\" >\n" // + + "/*<![CDATA[*/\n" // + + "var message = 'Scripts are written to the <script><\\/script> tag'\n" // + + "/*]]>*/\n"// + + "</script>\n", response.toString()); + } + + /** + */ + @Test + public void scriptTag() + { + assertEquals("<script type=\"text/javascript\">\n/*<![CDATA[*/\n", + JavaScriptUtils.SCRIPT_OPEN_TAG); + assertEquals("\n/*]]>*/\n</script>\n", JavaScriptUtils.SCRIPT_CLOSE_TAG); } }
