Fixing problems with generated javascript idetitifiers containing unescaped id parameter value

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@560258 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
René Gielen
2007-07-27 14:56:11 +00:00
parent 44aeb64064
commit 0875a59f33
4 changed files with 50 additions and 10 deletions
@@ -902,7 +902,8 @@ public abstract class UIBean extends Component {
/**
* Create HTML id element for the component and populate this component parmaeter
* map.
* map. Additionally, a parameter named escapedId is populated which contains the found id value filtered by
* {@link #escape(String)}, needed eg. for naming Javascript identifiers based on the id value.
*
* The order is as follows :-
* <ol>
@@ -914,21 +915,24 @@ public abstract class UIBean extends Component {
* @param form
*/
protected void populateComponentHtmlId(Form form) {
String tryId;
if (id != null) {
// this check is needed for backwards compatibility with 2.1.x
if (altSyntax()) {
addParameter("id", findString(id));
tryId = findString(id);
} else {
addParameter("id", id);
tryId = id;
}
} else if (form != null) {
addParameter("id", form.getParameters().get("id") + "_"
+ escape(name != null ? findString(name) : null));
tryId = form.getParameters().get("id") + "_"
+ escape(name != null ? findString(name) : null);
} else {
addParameter("id", escape(name != null ? findString(name) : null));
tryId = escape(name != null ? findString(name) : null);
}
addParameter("id", tryId);
addParameter("escapedId", escape(tryId));
}
/**
* Get's the id for referencing element.
* @return the id for referencing element.
@@ -21,11 +21,11 @@
*/
-->
<script type="text/javascript">
function autoPopulate_${parameters.id?html}(targetElement) {
function autoPopulate_${parameters.escapedId?html}(targetElement) {
<#if parameters.headerKey?exists && parameters.headerValue?exists>
if (targetElement.options[targetElement.selectedIndex].value == '${parameters.headerKey?html}') {
return;
}
}
</#if>
<#if parameters.emptyOption?default(false)>
if (targetElement.options[targetElement.selectedIndex].value == '') {
@@ -38,7 +38,7 @@
<#include "/${parameters.templateDir}/simple/text.ftl" />
<br />
<#if parameters.list?exists>
<select onChange="autoPopulate_${parameters.id?html}(this);"<#rt/>
<select onChange="autoPopulate_${parameters.escapedId?html}(this);"<#rt/>
<#if parameters.disabled?default(false)>
disabled="disabled"<#rt/>
</#if>
@@ -134,4 +134,26 @@ public class ComboBoxTest extends AbstractUITagTest {
verify(ComboBoxTag.class.getResource("ComboBox-3.txt"));
}
public void testJsCallNamingUsesEscapedId() throws Exception {
TestAction testAction = (TestAction) action;
testAction.setFoo("hello");
ArrayList collection = new ArrayList();
collection.add("foo");
testAction.setCollection(collection);
ComboBoxTag tag = new ComboBoxTag();
tag.setPageContext(pageContext);
tag.setLabel("mylabel");
tag.setName("foo");
tag.setId("cb.bc");
tag.setList("collection");
tag.doStartTag();
tag.doEndTag();
verify(ComboBoxTag.class.getResource("ComboBox-4.txt"));
}
}
@@ -0,0 +1,14 @@
<tr>
<td class="tdLabel"><label for="cb.bc" class="label">mylabel:</label></td>
<td>
<script type="text/javascript">
function autoPopulate_cb_bc(targetElement) {
targetElement.form.elements['foo'].value=targetElement.options[targetElement.selectedIndex].value;
}
</script>
<input type="text" name="foo" value="hello" id="cb.bc"/><br/>
<select onChange="autoPopulate_cb_bc(this);">
<option value="foo">foo</option>
</select>
</td>
</tr>