You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by rg...@apache.org on 2007/07/27 16:56:12 UTC

svn commit: r560258 - in /struts/struts2/trunk/core/src: main/java/org/apache/struts2/components/ main/resources/template/simple/ test/java/org/apache/struts2/views/jsp/ui/ test/resources/org/apache/struts2/views/jsp/ui/

Author: rgielen
Date: Fri Jul 27 07:56:11 2007
New Revision: 560258

URL: http://svn.apache.org/viewvc?view=rev&rev=560258
Log:
WW-2007:
Fixing problems with generated javascript idetitifiers containing unescaped id parameter value

Added:
    struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/ComboBox-4.txt
Modified:
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/UIBean.java
    struts/struts2/trunk/core/src/main/resources/template/simple/combobox.ftl
    struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/ComboBoxTest.java

Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/UIBean.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/UIBean.java?view=diff&rev=560258&r1=560257&r2=560258
==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/UIBean.java (original)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/UIBean.java Fri Jul 27 07:56:11 2007
@@ -902,7 +902,8 @@
 
     /**
      * 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 @@
      * @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.

Modified: struts/struts2/trunk/core/src/main/resources/template/simple/combobox.ftl
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/template/simple/combobox.ftl?view=diff&rev=560258&r1=560257&r2=560258
==============================================================================
--- struts/struts2/trunk/core/src/main/resources/template/simple/combobox.ftl (original)
+++ struts/struts2/trunk/core/src/main/resources/template/simple/combobox.ftl Fri Jul 27 07:56:11 2007
@@ -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>

Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/ComboBoxTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/ComboBoxTest.java?view=diff&rev=560258&r1=560257&r2=560258
==============================================================================
--- struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/ComboBoxTest.java (original)
+++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/ComboBoxTest.java Fri Jul 27 07:56:11 2007
@@ -134,4 +134,26 @@
 
         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"));
+    }
+
 }

Added: struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/ComboBox-4.txt
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/ComboBox-4.txt?view=auto&rev=560258
==============================================================================
--- struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/ComboBox-4.txt (added)
+++ struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/ComboBox-4.txt Fri Jul 27 07:56:11 2007
@@ -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>