WW-5168 Fixes disabled attribute across all the Javatemplate tags

This commit is contained in:
Lukasz Lenart
2022-02-10 18:42:10 +01:00
parent b1e9eae01c
commit 1c3cab2e6f
16 changed files with 103 additions and 63 deletions
@@ -384,9 +384,8 @@ public class Component {
return expression;
}
} else {
expression = stripExpression(expression);
return getStack().findValue(expression, toType, throwExceptionOnELFailure);
String strippedExpression = stripExpression(expression);
return getStack().findValue(strippedExpression, toType, throwExceptionOnELFailure);
}
}
@@ -702,11 +702,13 @@ public abstract class UIBean extends Component {
}
if (requiredLabel != null) {
addParameter("required", findValue(requiredLabel, Boolean.class));
Object parsedValue = findValue(requiredLabel, Boolean.class);
addParameter("required", parsedValue == null ? Boolean.valueOf(requiredLabel) : parsedValue);
}
if (disabled != null) {
addParameter("disabled", findValue(disabled, Boolean.class));
Object parsedValue = findValue(disabled, Boolean.class);
addParameter("disabled", parsedValue == null ? Boolean.valueOf(disabled) : parsedValue);
}
if (tabindex != null) {
@@ -886,9 +888,9 @@ public abstract class UIBean extends Component {
this.addParameter("tooltipDelay", findString(this.tooltipDelay));
if (this.javascriptTooltip != null) {
Boolean jsTooltips = (Boolean) findValue(this.javascriptTooltip, Boolean.class);
Object jsTooltips = findValue(this.javascriptTooltip, Boolean.class);
//TODO use a Boolean model when tooltipConfig is dropped
this.addParameter("jsTooltipEnabled", jsTooltips.toString());
this.addParameter("jsTooltipEnabled", jsTooltips == null ? this.javascriptTooltip : jsTooltips.toString());
if (form != null)
form.addParameter("hasTooltip", jsTooltips);
@@ -968,7 +970,7 @@ public abstract class UIBean extends Component {
// 1] UI component's tooltipConfig attribute OR
// 2] <param name="tooltip" value="" /> param tag value attribute
result = new LinkedHashMap<>((Map) tooltipConfigObj);
result = new LinkedHashMap<String, String>((Map) tooltipConfigObj);
} else if (tooltipConfigObj instanceof String) {
// we get this if its configured using
@@ -28,7 +28,7 @@ import java.util.LinkedHashMap;
*/
public class Attributes extends LinkedHashMap<String, String> {
private static final long serialVersionUID = 4103241472140545630L;
private static final long serialVersionUID = 4103241472140545630L;
public Attributes add(String key, String value) {
return add(key, value, true);
@@ -74,15 +74,17 @@ public class Attributes extends LinkedHashMap<String, String> {
* @return this
*/
public Attributes addIfTrue(String attrName, Object paramValue) {
if (paramValue != null) {
if ((paramValue instanceof Boolean && ((Boolean) paramValue).booleanValue()) ||
(Boolean.valueOf(paramValue.toString()).booleanValue())) {
put(attrName, attrName);
}
if (paramValue != null && isTrue(paramValue)) {
put(attrName, attrName);
}
return this;
}
private boolean isTrue(Object paramValue) {
return (paramValue instanceof Boolean && (Boolean) paramValue)
|| (Boolean.parseBoolean(paramValue.toString()));
}
/**
* Add a key/value pair to the attributes, if the value is null, it will be set as an empty string.
* Value is html encoded.
@@ -37,12 +37,14 @@ public class AnchorHandler extends AbstractTagHandler implements TagGenerator {
Attributes attrs = new Attributes();
attrs.addIfExists("name", params.get("name"))
.addIfExists("id", params.get("id"))
.addIfExists("class", params.get("cssClass"))
.addIfExists("style", params.get("cssStyle"))
.addIfExists("href", params.get("href"), false)
.addIfExists("title", params.get("title"))
.addIfExists("tabindex", params.get("tabindex"));
.addIfExists("id", params.get("id"))
.addIfExists("class", params.get("cssClass"))
.addIfExists("style", params.get("cssStyle"))
.addIfExists("href", params.get("href"), false)
.addIfTrue("disabled", params.get("disabled"))
.addIfExists("title", params.get("title"))
.addIfExists("tabindex", params.get("tabindex"));
start("a", attrs);
String body = (String) params.get("body");
Boolean escapeHtmlBody = (Boolean) params.get("escapeHtmlBody");
@@ -40,9 +40,13 @@ public class LinkHandler extends AbstractTagHandler implements TagGenerator {
.addIfExists("referrerpolicy", params.get("referrerpolicy"))
.addIfExists("type", params.get("type"))
.addIfExists("as", params.get("as"))
.addIfExists("disabled", params.get("disabled"))
.addIfExists("title", params.get("title"));
// see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-disabled
if ("stylesheet".equals(params.get("rel"))) {
attrs.addIfTrue("disabled", params.get("disabled"));
}
start("link", attrs);
end("link");
}
@@ -18,9 +18,9 @@
*/
package org.apache.struts2.views.java.simple;
import org.apache.struts2.views.java.TagGenerator;
import org.apache.struts2.views.java.Attributes;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.views.java.Attributes;
import org.apache.struts2.views.java.TagGenerator;
import java.io.IOException;
import java.util.Map;
@@ -34,12 +34,13 @@ public class ResetHandler extends AbstractTagHandler implements TagGenerator {
boolean isButton = "button".equals(params.get("type"));
attrs.addDefaultToEmpty("name", params.get("name"))
.add("type", "reset")
.addIfExists("value", params.get("nameValue"))
.addIfExists("tabindex", params.get("tabindex"))
.addIfExists("id", params.get("id"))
.addIfExists("class", params.get("cssClass"))
.addIfExists("style", params.get("cssStyle"));
.add("type", "reset")
.addIfExists("value", params.get("nameValue"))
.addIfTrue("disabled", params.get("disabled"))
.addIfExists("tabindex", params.get("tabindex"))
.addIfExists("id", params.get("id"))
.addIfExists("class", params.get("cssClass"))
.addIfExists("style", params.get("cssStyle"));
if (!isButton)
attrs.addIfExists("title", params.get("title"));
@@ -43,7 +43,7 @@ public class AnchorTest extends AbstractTest {
theme.renderTag(getTagName(), context);
theme.renderTag(getTagName() + "-close", context);
String output = writer.getBuffer().toString();
String expected = s("<a name='name_' id='id_' class='class' style='style' href='http://sometest.com?ab=10' title='title' tabindex='1'></a>");
String expected = s("<a name='name_' id='id_' class='class' style='style' href='http://sometest.com?ab=10' disabled='disabled' title='title' tabindex='1'></a>");
assertEquals(expected, output);
}
@@ -29,7 +29,7 @@ public class CheckboxTest extends AbstractCommonAttributesTest {
public void testRenderCheckbox() {
tag.setName("name_");
tag.setDisabled("true");
tag.setDisabled("false");
tag.setTabindex("1");
tag.setId("id_");
tag.setCssClass("class");
@@ -60,14 +60,13 @@ public class CheckboxTest extends AbstractCommonAttributesTest {
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String output = writer.getBuffer().toString();
String expected = s("<input type='checkbox' name='name_' value='xyz' tabindex='1' id='id_' class='class' style='style' title='title'></input>");
String expected = s("<input type='checkbox' name='name_' value='xyz' disabled='disabled' tabindex='1' id='id_' class='class' style='style' title='title'></input>");
assertEquals(expected, output);
}
public void testRenderCheckboxWithNameValue() {
tag.setName("name_");
tag.setValue("%{someValue}");
tag.setDisabled("true");
tag.evaluateParams();
map.putAll(tag.getParameters());
@@ -43,7 +43,7 @@ public class FileTest extends AbstractCommonAttributesTest {
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String output = writer.getBuffer().toString();
String expected = s("<input name='name' type='file' size='10' value='val1' accept='accept_' tabindex='1' id='id1' class='class1' style='style1' title='title'></input>");
String expected = s("<input name='name' type='file' size='10' value='val1' disabled='disabled' accept='accept_' tabindex='1' id='id1' class='class1' style='style1' title='title'></input>");
assertEquals(expected, output);
}
@@ -41,7 +41,7 @@ public class HiddenTest extends AbstractTest {
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String output = writer.getBuffer().toString();
String expected = s("<input name='name' type='hidden' value='val1' id='id1' class='class1' style='style1'></input>");
String expected = s("<input name='name' type='hidden' value='val1' disabled='disabled' id='id1' class='class1' style='style1'></input>");
assertEquals(expected, output);
}
@@ -31,7 +31,7 @@ public class LinkTest extends AbstractTest{
private static final String NONCE_VAL = "r4andom";
public void testRenderScriptTag() {
public void testRenderLinkTag() {
tag.setHref("testhref");
tag.setHreflang("test");
tag.setRel("module");
@@ -41,27 +41,60 @@ public class LinkTest extends AbstractTest{
tag.setCrossorigin("same-origin");
tag.setType("anonymous");
tag.setAs("test");
tag.setDisabled("disabled_");
tag.setDisabled("true");
tag.setTitle("test");
tag.evaluateParams();
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String s = writer.getBuffer().toString();
String output = writer.getBuffer().toString();
assertTrue("Incorrect href attribute for link tag", s.contains("href=\"testhref\""));
assertTrue("Incorrect hreflang attribute for link tag", s.contains("hreflang=\"test\""));
assertTrue("Incorrect rel attribute for link tag", s.contains("rel=\"module\""));
assertTrue("Incorrect media attribute for link tag", s.contains("media=\"foo\""));
assertTrue("Incorrect referrerpolicy attribute for link tag", s.contains("referrerpolicy=\"test\""));
assertTrue("Incorrect sizes attribute for link tag", s.contains("sizes=\"foo\""));
assertTrue("Incorrect crossorigin attribute for link tag", s.contains("crossorigin=\"same-origin\""));
assertTrue("Incorrect type attribute for link tag", s.contains("type=\"anonymous\""));
assertTrue("Incorrect as attribute for link tag", s.contains("as=\"test\""));
assertTrue("Non-existent disabled attribute for link tag", s.contains("disabled=\"disabled_\""));
assertTrue("Incorrect title attribute for link tag", s.contains("title=\"test\""));
assertTrue("Incorrect nonce attribute for link tag", s.contains("nonce=\"" + NONCE_VAL+"\""));
assertTrue("Incorrect href attribute for link tag", output.contains(s("href='testhref'")));
assertTrue("Incorrect hreflang attribute for link tag", output.contains(s("hreflang='test'")));
assertTrue("Incorrect rel attribute for link tag", output.contains(s("rel='module'")));
assertTrue("Incorrect media attribute for link tag", output.contains(s("media='foo'")));
assertTrue("Incorrect referrerpolicy attribute for link tag", output.contains(s("referrerpolicy='test'")));
assertTrue("Incorrect sizes attribute for link tag", output.contains(s("sizes='foo'")));
assertTrue("Incorrect crossorigin attribute for link tag", output.contains(s("crossorigin='same-origin'")));
assertTrue("Incorrect type attribute for link tag", output.contains(s("type='anonymous'")));
assertTrue("Incorrect as attribute for link tag", output.contains(s("as='test'")));
assertFalse("Non-existent disabled attribute for link tag", output.contains(s("disabled='disabled'")));
assertTrue("Incorrect title attribute for link tag", output.contains(s("title='test'")));
assertTrue("Incorrect nonce attribute for link tag", output.contains(s("nonce='" + NONCE_VAL+"'")));
}
public void testRenderLinkTagAsStylesheet() {
tag.setHref("testhref");
tag.setHreflang("test");
tag.setRel("stylesheet");
tag.setMedia("foo");
tag.setReferrerpolicy("test");
tag.setSizes("foo");
tag.setCrossorigin("same-origin");
tag.setType("anonymous");
tag.setAs("test");
tag.setDisabled("true");
tag.setTitle("test");
tag.evaluateParams();
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String output = writer.getBuffer().toString();
assertTrue("Incorrect href attribute for link tag", output.contains(s("href='testhref'")));
assertTrue("Incorrect hreflang attribute for link tag", output.contains(s("hreflang='test'")));
assertTrue("Incorrect rel attribute for link tag", output.contains(s("rel='stylesheet'")));
assertTrue("Incorrect media attribute for link tag", output.contains(s("media='foo'")));
assertTrue("Incorrect referrerpolicy attribute for link tag", output.contains(s("referrerpolicy='test'")));
assertTrue("Incorrect sizes attribute for link tag", output.contains(s("sizes='foo'")));
assertTrue("Incorrect crossorigin attribute for link tag", output.contains(s("crossorigin='same-origin'")));
assertTrue("Incorrect type attribute for link tag", output.contains(s("type='anonymous'")));
assertTrue("Incorrect as attribute for link tag", output.contains(s("as='test'")));
assertTrue("Incorrect disabled attribute for link tag", output.contains(s("disabled='disabled'")));
assertTrue("Incorrect title attribute for link tag", output.contains(s("title='test'")));
assertTrue("Incorrect nonce attribute for link tag", output.contains(s("nonce='" + NONCE_VAL+"'")));
}
@Override
protected UIBean getUIBean() throws Exception {
return tag;
@@ -24,11 +24,10 @@ import org.apache.struts2.components.Password;
import org.apache.struts2.components.UIBean;
public class PasswordTest extends AbstractCommonAttributesTest {
private Password tag;
private boolean showPassword;
public void testRenderPassword() throws Exception {
this.showPassword = false;
super.setUp();
this.tag = new Password(stack, request, response);
@@ -47,12 +46,11 @@ public class PasswordTest extends AbstractCommonAttributesTest {
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String output = writer.getBuffer().toString();
String expected = s("<input name='name' type='password' size='10' tabindex='1' id='id1' class='class1' style='style1' title='title'></input>");
String expected = s("<input name='name' type='password' size='10' disabled='disabled' tabindex='1' id='id1' class='class1' style='style1' title='title'></input>");
assertEquals(expected, output);
}
public void testRenderPasswordShowIt() throws Exception {
this.showPassword = true;
super.setUp();
this.tag = new Password(stack, request, response);
@@ -71,7 +69,7 @@ public class PasswordTest extends AbstractCommonAttributesTest {
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String output = writer.getBuffer().toString();
String expected = s("<input value='val1' name='name' type='password' size='10' tabindex='1' id='id1' class='class1' style='style1' title='title'></input>");
String expected = s("<input value='val1' name='name' type='password' size='10' disabled='disabled' tabindex='1' id='id1' class='class1' style='style1' title='title'></input>");
assertEquals(expected, output);
}
@@ -45,7 +45,7 @@ public class SelectTest extends AbstractCommonAttributesTest {
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String output = writer.getBuffer().toString();
String expected = s("<select name='name_' size='10' tabindex='1' id='id_' class='class' style='style' title='title'></select>");
String expected = s("<select name='name_' size='10' disabled='disabled' tabindex='1' id='id_' class='class' style='style' title='title'></select>");
assertEquals(expected, output);
}
@@ -47,7 +47,7 @@ public class SubmitTest extends AbstractCommonAttributesTest {
map.putAll(tag.getParameters());
theme.renderTag(getTagName() + "-close", context);
String output = writer.getBuffer().toString();
String expected = s("<button name='name' type='submit' value='val1' tabindex='1' id='id1' class='class1' style='style1'><span>hey hey hey, here I go now</span></button>");
String expected = s("<button name='name' type='submit' value='val1' disabled='disabled' tabindex='1' id='id1' class='class1' style='style1'><span>hey hey hey, here I go now</span></button>");
assertEquals(expected, output);
}
@@ -69,7 +69,7 @@ public class SubmitTest extends AbstractCommonAttributesTest {
theme.renderTag(getTagName(), context);
theme.renderTag(getTagName() + "-close", context);
String output = writer.getBuffer().toString();
String expected = s("<button name='name' type='submit' value='val1' tabindex='1' id='id1' class='class1' style='style1'>Just as soon as I belong, than its time I disappear</button>");
String expected = s("<button name='name' type='submit' value='val1' disabled='disabled' tabindex='1' id='id1' class='class1' style='style1'>Just as soon as I belong, than its time I disappear</button>");
assertEquals(expected, output);
}
@@ -91,7 +91,7 @@ public class SubmitTest extends AbstractCommonAttributesTest {
theme.renderTag(getTagName(), context);
theme.renderTag(getTagName() + "-close", context);
String output = writer.getBuffer().toString();
String expected = s("<input name='name' type='submit' value='val1' tabindex='1' id='id1' class='class1' style='style1'></input>");
String expected = s("<input name='name' type='submit' value='val1' disabled='disabled' tabindex='1' id='id1' class='class1' style='style1'></input>");
assertEquals(expected, output);
}
@@ -112,7 +112,7 @@ public class SubmitTest extends AbstractCommonAttributesTest {
theme.renderTag(getTagName(), context);
theme.renderTag(getTagName() + "-close", context);
String output = writer.getBuffer().toString();
String expected = s("<input name='name' type='submit' value='val1' tabindex='1' id='id1' class='class1' style='style1'></input>");
String expected = s("<input name='name' type='submit' value='val1' disabled='disabled' tabindex='1' id='id1' class='class1' style='style1'></input>");
assertEquals(expected, output);
}
@@ -44,7 +44,7 @@ public class TextAreaTest extends AbstractCommonAttributesTest {
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String output = writer.getBuffer().toString();
String expected = s("<textarea name='name' cols='2' rows='1' tabindex='1' id='id1' class='class1' style='style1' title='title'>val1</textarea>");
String expected = s("<textarea name='name' cols='2' rows='1' disabled='disabled' tabindex='1' id='id1' class='class1' style='style1' title='title'>val1</textarea>");
assertEquals(expected, output);
}
@@ -63,7 +63,7 @@ public class TextAreaTest extends AbstractCommonAttributesTest {
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String output = writer.getBuffer().toString();
String expected = s("<textarea name='' cols='' rows='' tabindex='1' id='id1' class='class1' style='style1' title='title'>val1</textarea>");
String expected = s("<textarea name='' cols='' rows='' disabled='disabled' tabindex='1' id='id1' class='class1' style='style1' title='title'>val1</textarea>");
assertEquals(expected, output);
}
@@ -44,7 +44,7 @@ public class TextFieldTest extends AbstractCommonAttributesTest {
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String output = writer.getBuffer().toString();
String expected = s("<input type='text' name='name' size='10' maxlength='11' value='val1' tabindex='1' id='id1' class='class1' style='style1' title='title'></input>");
String expected = s("<input type='text' name='name' size='10' maxlength='11' value='val1' disabled='disabled' tabindex='1' id='id1' class='class1' style='style1' title='title'></input>");
assertEquals(expected, output);
}