Author: musachy Date: Mon Dec 1 10:24:52 2008 New Revision: 722159 URL: http://svn.apache.org/viewvc?rev=722159&view=rev Log: WW-255 Add escapeJavaScript attribute to propertyTag
Thanks to Jelmer Kuperus for patch. Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Property.java struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/PropertyTag.java struts/struts2/trunk/core/src/site/resources/tags/property.html struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/PropertyTagTest.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Property.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Property.java?rev=722159&r1=722158&r2=722159&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Property.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Property.java Mon Dec 1 10:24:52 2008 @@ -99,6 +99,7 @@ private String defaultValue; private String value; private boolean escape = true; + private boolean escapeJavaScript = false; @StrutsTagAttribute(description="The default value to be used if <u>value</u> attribute is null") public void setDefault(String defaultValue) { @@ -110,6 +111,11 @@ this.escape = escape; } + @StrutsTagAttribute(description="Whether to escape Javascript", type="Boolean", defaultValue="false") + public void setEscapeJavaScript(boolean escapeJavaScript) { + this.escapeJavaScript = escapeJavaScript; + } + @StrutsTagAttribute(description="Value to be displayed", type="Object", defaultValue="<top of stack>") public void setValue(String value) { this.value = value; @@ -150,10 +156,13 @@ } private String prepare(String value) { + String result = value; if (escape) { - return TextUtils.htmlEncode(value); - } else { - return value; + result = TextUtils.htmlEncode(result); } + if (escapeJavaScript) { + result = TextUtils.escapeJavaScript(result); + } + return result; } } Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/PropertyTag.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/PropertyTag.java?rev=722159&r1=722158&r2=722159&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/PropertyTag.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/PropertyTag.java Mon Dec 1 10:24:52 2008 @@ -40,6 +40,7 @@ private String defaultValue; private String value; private boolean escape = true; + private boolean escapeJavaScript = false; public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res) { return new Property(stack); @@ -52,6 +53,7 @@ tag.setDefault(defaultValue); tag.setValue(value); tag.setEscape(escape); + tag.setEscapeJavaScript(escapeJavaScript); } public void setDefault(String defaultValue) { @@ -62,6 +64,10 @@ this.escape = escape; } + public void setEscapeJavaScript(boolean escapeJavaScript) { + this.escapeJavaScript = escapeJavaScript; + } + public void setValue(String value) { this.value = value; } Modified: struts/struts2/trunk/core/src/site/resources/tags/property.html URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/site/resources/tags/property.html?rev=722159&r1=722158&r2=722159&view=diff ============================================================================== --- struts/struts2/trunk/core/src/site/resources/tags/property.html (original) +++ struts/struts2/trunk/core/src/site/resources/tags/property.html Mon Dec 1 10:24:52 2008 @@ -50,6 +50,14 @@ <td align="left" valign="top"> Whether to escape HTML</td> </tr> <tr> + <td align="left" valign="top">escapeJavaScript</td> + <td align="left" valign="top">false</td> + <td align="left" valign="top">false</td> + <td align="left" valign="top">false</td> + <td align="left" valign="top">Boolean</td> + <td align="left" valign="top">Whether to escape Javascript</td> + </tr> + <tr> <td align="left" valign="top">value</td> <td align="left" valign="top">false</td> <td align="left" valign="top"><top of stack></td> Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/PropertyTagTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/PropertyTagTest.java?rev=722159&r1=722158&r2=722159&view=diff ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/PropertyTagTest.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/PropertyTagTest.java Mon Dec 1 10:24:52 2008 @@ -193,6 +193,36 @@ pageContext.verify(); } + public void testEscapeJavaScript() throws Exception { + // setups + initDispatcher(new HashMap() {{ put(StrutsConstants.STRUTS_TAG_ALTSYNTAX, "true");}}); + + Foo foo = new Foo(); + foo.setTitle("\t\b\n\f\r\"\'/\\"); + stack.push(foo); + + MockJspWriter jspWriter = new MockJspWriter(); + jspWriter.setExpectedData("Foo is: \\t\\b\\n\\f\\r\\\"\\\'\\/\\\\"); + + MockPageContext pageContext = new MockPageContext(); + pageContext.setJspWriter(jspWriter); + pageContext.setRequest(request); + + // test + {PropertyTag tag = new PropertyTag(); + tag.setEscape(false); + tag.setEscapeJavaScript(true); + tag.setPageContext(pageContext); + tag.setValue("%{toString()}"); + tag.doStartTag(); + tag.doEndTag();} + + // verify test + request.verify(); + jspWriter.verify(); + pageContext.verify(); + } + public void testWithAltSyntax2() throws Exception { // setups initDispatcher(new HashMap() {{ put(StrutsConstants.STRUTS_TAG_ALTSYNTAX, "true");}});