WW-3920 adds support for scripting events

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1436637 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Lukasz Lenart
2013-01-21 21:47:13 +00:00
parent 9b951fcd73
commit 62ee2636af
2 changed files with 40 additions and 14 deletions
@@ -20,36 +20,40 @@
*/
package org.apache.struts2.views.java.simple;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.views.java.Attributes;
import org.apache.struts2.views.java.TagGenerator;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.util.Map;
public class AnchorHandler extends AbstractTagHandler implements TagGenerator {
public void generate() throws IOException {
//all rendering must happend at the end of the tag, so we can support nested params
Map<String, Object> params = context.getParameters();
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"));
start("a", attrs);
}
public static class CloseHandler extends AbstractTagHandler implements TagGenerator {
public void generate() throws IOException {
Map<String, Object> params = context.getParameters();
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"));
start("a", attrs);
String body = (String) params.get("body");
if (StringUtils.isNotEmpty(body))
if (StringUtils.isNotEmpty(body)) {
characters(body, false);
}
end("a");
}
}
}
@@ -46,6 +46,28 @@ public class AnchorTest extends AbstractTest {
assertEquals(expected, output);
}
public void testRenderScriptingEvents() {
tag.setName("name_");
tag.setOnclick("alert('click')");
tag.setOnchange("alert('change)");
tag.setOnfocus("alert('focus')");
tag.setOnselect("alert('select')");
tag.setOndblclick("alert('dbclick')");
tag.setOnkeydown("alert('keydown')");
tag.setOnkeypress("alert('keypress')");
tag.setHref("http://sometest.com?ab=10");
tag.evaluateParams();
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
theme.renderTag(getTagName() + "-close", context);
String output = writer.getBuffer().toString();
String expected = "<a name=\"name_\" id=\"name_\" href=\"http://sometest.com?ab=10\" onclick=\"alert('click')\" " +
"ondblclick=\"alert('dbclick')\" onfocus=\"alert('focus')\" onkeypress=\"alert('keypress')\" " +
"onkeydown=\"alert('keydown')\" onselect=\"alert('select')\" onchange=\"alert('change)\"></a>";
assertEquals(expected, output);
}
@Override
protected void setUp() throws Exception {
super.setUp();