Skip to content

Commit

Permalink
WW-5022 Pass escapeHtmlBody flag to JavaTemplates tags
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Jan 7, 2022
1 parent 7a69652 commit effe687
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 21 deletions.
4 changes: 3 additions & 1 deletion core/src/main/java/org/apache/struts2/components/Anchor.java
Expand Up @@ -112,6 +112,8 @@ protected void evaluateExtraParams() {
addParameter("href", ensureAttributeSafelyNotEscaped(builtHref));
}
}

addParameter("escapeHtmlBody", escapeHtmlBody);
}

@Inject(StrutsConstants.STRUTS_URL_INCLUDEPARAMS)
Expand Down Expand Up @@ -264,7 +266,7 @@ public void setForceAddSchemeHostAndPort(boolean forceAddSchemeHostAndPort) {
urlProvider.setForceAddSchemeHostAndPort(forceAddSchemeHostAndPort);
}

@StrutsTagAttribute(description = "Specifies whether to HTML-escape the tag body or not", type = "Boolean", defaultValue = "true")
@StrutsTagAttribute(description = "Specifies whether to HTML-escape the tag body or not", type = "Boolean", defaultValue = "false")
public void setEscapeHtmlBody(boolean escapeHtmlBody) {
this.escapeHtmlBody = escapeHtmlBody;
}
Expand Down
7 changes: 5 additions & 2 deletions core/src/main/java/org/apache/struts2/components/Submit.java
Expand Up @@ -85,8 +85,11 @@ public void evaluateParams() {
public void evaluateExtraParams() {
super.evaluateExtraParams();

if (src != null)
if (src != null) {
addParameter("src", findString(src));
}

addParameter("escapeHtmlBody", escapeHtmlBody);
}

/**
Expand All @@ -103,7 +106,7 @@ public void setSrc(String src) {
this.src = src;
}

@StrutsTagAttribute(description = "Specifies whether to HTML-escape the tag body or not", type = "Boolean", defaultValue = "true")
@StrutsTagAttribute(description = "Specifies whether to HTML-escape the tag body or not", type = "Boolean", defaultValue = "false")
public void setEscapeHtmlBody(boolean escapeHtmlBody) {
this.escapeHtmlBody = escapeHtmlBody;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/site/resources/tags/a-attributes.html
Expand Up @@ -112,7 +112,7 @@
<tr>
<td align="left" valign="top">escapeHtmlBody</td>
<td align="left" valign="top">false</td>
<td align="left" valign="top">true</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">Specifies whether to HTML-escape the tag body or not</td>
Expand Down
2 changes: 1 addition & 1 deletion core/src/site/resources/tags/submit-attributes.html
Expand Up @@ -88,7 +88,7 @@
<tr>
<td align="left" valign="top">escapeHtmlBody</td>
<td align="left" valign="top">false</td>
<td align="left" valign="top">true</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">Specifies whether to HTML-escape the tag body or not</td>
Expand Down
Expand Up @@ -22,14 +22,15 @@
import org.apache.logging.log4j.LogManager;

/**
* Default implementation of TagHandlerFactory
* Default implementation of TagHandlerFactory
*/
public class DefaultTagHandlerFactory implements TagHandlerFactory {
private static final Logger LOG = LogManager.getLogger(DefaultTagHandlerFactory.class);

private Class tagHandlerClass;

public DefaultTagHandlerFactory(Class tagHandlerClass) {
private static final Logger LOG = LogManager.getLogger(DefaultTagHandlerFactory.class);

private final Class<?> tagHandlerClass;

public DefaultTagHandlerFactory(Class<?> tagHandlerClass) {
this.tagHandlerClass = tagHandlerClass;
}

Expand All @@ -41,7 +42,7 @@ public TagHandler create(TagHandler next) {
} catch (Exception e) {
LOG.error("Failed to instantiate tag handler class [{}]", tagHandlerClass.getName(), e);
}

return null;
}

Expand Down
Expand Up @@ -45,8 +45,10 @@ public void generate() throws IOException {
.addIfExists("tabindex", params.get("tabindex"));
start("a", attrs);
String body = (String) params.get("body");
if (StringUtils.isNotEmpty(body))
characters(body, false);
Boolean escapeHtmlBody = (Boolean) params.get("escapeHtmlBody");
if (StringUtils.isNotEmpty(body)) {
characters(body, escapeHtmlBody);
}
end("a");
}
}
Expand Down
Expand Up @@ -65,13 +65,13 @@ public SimpleTheme() {
setName("simple");
}

private class FactoryList extends ArrayList<TagHandlerFactory> {
private static class FactoryList extends ArrayList<TagHandlerFactory> {

private static final long serialVersionUID = -1551895041394434032L;

public FactoryList(Class... classes) {
public FactoryList(Class<?>... classes) {
super();
for (Class cls : classes) {
for (Class<?> cls : classes) {
add(new DefaultTagHandlerFactory(cls));
}
add(new DefaultTagHandlerFactory(XHTMLTagSerializer.class));
Expand Down
Expand Up @@ -73,6 +73,7 @@ public static class CloseHandler extends AbstractTagHandler implements TagGenera
public void generate() throws IOException {
Map<String, Object> params = context.getParameters();
String body = (String) params.get("body");
Boolean escapeHtmlBody = (Boolean) params.get("escapeHtmlBody");

String type = StringUtils.defaultString((String) params.get("type"), "input");
if ("button".equals(type)) {
Expand All @@ -81,16 +82,19 @@ public void generate() throws IOException {
characters(body, false);
else if (params.containsKey("label")) {
String label = (String) params.get("label");
if (StringUtils.isNotEmpty(label))
characters(label, false);
if (StringUtils.isNotEmpty(label)) {
characters(label, escapeHtmlBody);
}
}
end("button");
} else if ("image".equals(type)) {
if (StringUtils.isNotEmpty(body))
characters(body, false);
if (StringUtils.isNotEmpty(body)) {
characters(body, escapeHtmlBody);
}
end("input");
} else
} else {
end("input");
}
}
}
}
Expand Up @@ -69,6 +69,42 @@ public void testRenderScriptingEvents() {
assertEquals(expected, output);
}

public void testEnableEscapeBody() {
tag.setName("name_");
tag.setHref("http://sometest.com?ab=10");
tag.setEscapeHtmlBody(true);
tag.evaluateParams();

map.putAll(tag.getParameters());
context.getParameters().put("body", s("<i class='i-image'/>"));

theme.renderTag(getTagName(), context);
theme.renderTag(getTagName() + "-close", context);

String output = writer.getBuffer().toString();
String expected = s("<a name='name_' id='name_' href='http://sometest.com?ab=10'>&lt;i class=&quot;i-image&quot;/&gt;</a>");

assertEquals(expected, output);
}

public void testDefaultDisabledEscapeBody() {
tag.setName("name_");
tag.setHref("http://sometest.com?ab=10");
//tag.setEscapeHtmlBody(true);
tag.evaluateParams();

map.putAll(tag.getParameters());
context.getParameters().put("body", s("<i class='i-image'/>"));

theme.renderTag(getTagName(), context);
theme.renderTag(getTagName() + "-close", context);

String output = writer.getBuffer().toString();
String expected = s("<a name='name_' id='name_' href='http://sometest.com?ab=10'><i class='i-image'/></a>");

assertEquals(expected, output);
}

@Override
protected void setUp() throws Exception {
super.setUp();
Expand Down

0 comments on commit effe687

Please sign in to comment.