Skip to content

Commit

Permalink
WW-3920 adds support for scripting events
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1436637 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
lukaszlenart committed Jan 21, 2013
1 parent 9b951fc commit 62ee263
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ public void testRenderAnchor() {
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();
Expand Down

0 comments on commit 62ee263

Please sign in to comment.