You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by pe...@apache.org on 2011/02/18 17:24:59 UTC

svn commit: r1072044 - in /wicket/trunk/wicket-core/src: main/java/org/apache/wicket/markup/html/form/ test/java/org/apache/wicket/stateless/

Author: pedro
Date: Fri Feb 18 16:24:59 2011
New Revision: 1072044

URL: http://svn.apache.org/viewvc?rev=1072044&view=rev
Log:
- Preventing the submit link input name from being encoded in form action. This change makes more sense in 1.4 where POST parameters are considered page parameters. But nothing stops users from send this parameter as a GET one, and nothing blocks us from start consider POST parameters as page parameters.

- Removing duplicated code in AbstractSubmitLink
- Hidden input name changed to be root form relative
Issue: WICKET-3438

Added:
    wicket/trunk/wicket-core/src/test/java/org/apache/wicket/stateless/StatelessFormUrlTest.java
Modified:
    wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractSubmitLink.java
    wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
    wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java

Modified: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractSubmitLink.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractSubmitLink.java?rev=1072044&r1=1072043&r2=1072044&view=diff
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractSubmitLink.java (original)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractSubmitLink.java Fri Feb 18 16:24:59 2011
@@ -16,11 +16,8 @@
  */
 package org.apache.wicket.markup.html.form;
 
-import org.apache.wicket.Component;
-import org.apache.wicket.Page;
 import org.apache.wicket.markup.html.link.AbstractLink;
 import org.apache.wicket.model.IModel;
-import org.apache.wicket.util.string.PrependingStringBuffer;
 
 /**
  * Abstract class for links that are capable of submitting a form.
@@ -143,28 +140,6 @@ public abstract class AbstractSubmitLink
 	 */
 	public String getInputName()
 	{
-		// TODO: This is a copy & paste from the FormComponent class.
-		String id = getId();
-		final PrependingStringBuffer inputName = new PrependingStringBuffer(id.length());
-		Component c = this;
-		while (true)
-		{
-			inputName.prepend(id);
-			c = c.getParent();
-			if (c == null || (c instanceof Form && ((Form<?>)c).isRootForm()) || c instanceof Page)
-			{
-				break;
-			}
-			inputName.prepend(Component.PATH_SEPARATOR);
-			id = c.getId();
-		}
-
-		// having input name "submit" causes problems with javascript, so we
-		// create a unique string to replace it by prepending a path separator
-		if ("submit".equals(inputName.toString()))
-		{
-			inputName.prepend(Component.PATH_SEPARATOR);
-		}
-		return inputName.toString();
+		return Form.getRootFormRelativeId(this);
 	}
 }

Modified: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java?rev=1072044&r1=1072043&r2=1072044&view=diff
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java (original)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java Fri Feb 18 16:24:59 2011
@@ -51,6 +51,7 @@ import org.apache.wicket.request.mapper.
 import org.apache.wicket.settings.IApplicationSettings;
 import org.apache.wicket.util.lang.Bytes;
 import org.apache.wicket.util.string.AppendingStringBuffer;
+import org.apache.wicket.util.string.PrependingStringBuffer;
 import org.apache.wicket.util.string.Strings;
 import org.apache.wicket.util.string.interpolator.MapVariableInterpolator;
 import org.apache.wicket.util.upload.FileUploadBase.SizeLimitExceededException;
@@ -822,6 +823,11 @@ public class Form<T> extends WebMarkupCo
 				}
 			});
 			parameters.remove(getHiddenFieldId());
+			if (submittingComponent instanceof AbstractSubmitLink)
+			{
+				AbstractSubmitLink submitLink = (AbstractSubmitLink)submittingComponent;
+				parameters.remove(submitLink.getInputName());
+			}
 		}
 	}
 
@@ -1906,4 +1912,40 @@ public class Form<T> extends WebMarkupCo
 			Form.class.getName());
 	}
 
+	/**
+	 * Utility method to assemble an id to distinct form components from diferent nesting levels.
+	 * Useful to generate input names attributes.
+	 * 
+	 * @param component
+	 * @return form relative identification string
+	 */
+	public static String getRootFormRelativeId(Component component)
+	{
+		String id = component.getId();
+		final PrependingStringBuffer inputName = new PrependingStringBuffer(id.length());
+		Component c = component;
+		while (true)
+		{
+			inputName.prepend(id);
+			c = c.getParent();
+			if (c == null || (c instanceof Form<?> && ((Form<?>)c).isRootForm()) ||
+				c instanceof Page)
+			{
+				break;
+			}
+			inputName.prepend(Component.PATH_SEPARATOR);
+			id = c.getId();
+		}
+
+		/*
+		 * having input name "submit" causes problems with JavaScript, so we create a unique string
+		 * to replace it by prepending a path separator, as this identification can be assigned to
+		 * an submit form component name
+		 */
+		if ("submit".equals(inputName.toString()))
+		{
+			inputName.prepend(Component.PATH_SEPARATOR);
+		}
+		return inputName.toString();
+	}
 }

Modified: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java?rev=1072044&r1=1072043&r2=1072044&view=diff
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java (original)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java Fri Feb 18 16:24:59 2011
@@ -32,7 +32,6 @@ import org.apache.wicket.Application;
 import org.apache.wicket.Component;
 import org.apache.wicket.IConverterLocator;
 import org.apache.wicket.Localizer;
-import org.apache.wicket.Page;
 import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.behavior.Behavior;
 import org.apache.wicket.markup.ComponentTag;
@@ -43,7 +42,6 @@ import org.apache.wicket.util.convert.IC
 import org.apache.wicket.util.lang.Args;
 import org.apache.wicket.util.lang.Classes;
 import org.apache.wicket.util.lang.WicketObjects;
-import org.apache.wicket.util.string.PrependingStringBuffer;
 import org.apache.wicket.util.string.StringList;
 import org.apache.wicket.util.string.StringValue;
 import org.apache.wicket.util.string.Strings;
@@ -737,38 +735,16 @@ public abstract class FormComponent<T> e
 	 */
 	public String getInputName()
 	{
-		// TODO: keep this in sync with AbstractSubmitLink#getInputName
-		String id = getId();
-		final PrependingStringBuffer inputName = new PrependingStringBuffer(id.length());
-		Component c = this;
-		while (true)
-		{
-			inputName.prepend(id);
-			c = c.getParent();
-			if (c == null || (c instanceof Form<?> && ((Form<?>)c).isRootForm()) ||
-				c instanceof Page)
-			{
-				break;
-			}
-			inputName.prepend(Component.PATH_SEPARATOR);
-			id = c.getId();
-		}
-
-		// having input name "submit" causes problems with javascript, so we
-		// create a unique string to replace it by prepending a path separator
-		if ("submit".equals(inputName.toString()))
-		{
-			inputName.prepend(Component.PATH_SEPARATOR);
-		}
+		String inputName = Form.getRootFormRelativeId(this);
 		Form<?> form = findParent(Form.class);
 
 		if (form != null)
 		{
-			return form.getInputNamePrefix() + inputName.toString();
+			return form.getInputNamePrefix() + inputName;
 		}
 		else
 		{
-			return inputName.toString();
+			return inputName;
 		}
 	}
 

Added: wicket/trunk/wicket-core/src/test/java/org/apache/wicket/stateless/StatelessFormUrlTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/stateless/StatelessFormUrlTest.java?rev=1072044&view=auto
==============================================================================
--- wicket/trunk/wicket-core/src/test/java/org/apache/wicket/stateless/StatelessFormUrlTest.java (added)
+++ wicket/trunk/wicket-core/src/test/java/org/apache/wicket/stateless/StatelessFormUrlTest.java Fri Feb 18 16:24:59 2011
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.stateless;
+
+import org.apache.wicket.MarkupContainer;
+import org.apache.wicket.WicketTestCase;
+import org.apache.wicket.markup.IMarkupResourceStreamProvider;
+import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.markup.html.form.StatelessForm;
+import org.apache.wicket.markup.html.form.SubmitLink;
+import org.apache.wicket.request.mapper.parameter.PageParameters;
+import org.apache.wicket.util.resource.IResourceStream;
+import org.apache.wicket.util.resource.StringResourceStream;
+
+/**
+ * @author Pedro Santos
+ */
+public class StatelessFormUrlTest extends WicketTestCase
+{
+	/**
+	 * Preventing WICKET-3438
+	 */
+	public void testSubmitLinkInputNameNotEncodedIntoFormAction()
+	{
+		tester.startPage(TestPage.class);
+		tester.clickLink("form:submitLink");
+		assertFalse(tester.getLastResponseAsString().contains("submitLink=x"));
+	}
+
+	/** */
+	public static class TestPage extends WebPage implements IMarkupResourceStreamProvider
+	{
+		/** */
+		private static final long serialVersionUID = 1L;
+
+		/**
+		 * @param pageParameters
+		 */
+		public TestPage(PageParameters pageParameters)
+		{
+			super(pageParameters);
+			StatelessForm<Void> form = new StatelessForm<Void>("form");
+			add(form);
+			SubmitLink submitLink = new SubmitLink("submitLink");
+			form.add(submitLink);
+		}
+
+		public IResourceStream getMarkupResourceStream(MarkupContainer container,
+			Class<?> containerClass)
+		{
+			return new StringResourceStream(
+				"<html><body><form wicket:id=\"form\"><a wicket:id=\"submitLink\"></a></form></body></html>");
+		}
+
+	}
+}
\ No newline at end of file