Author: pedro Date: Fri Feb 18 02:02:51 2011 New Revision: 1071860 URL: http://svn.apache.org/viewvc?rev=1071860&view=rev Log: - preventing the submit link input name from being encoded in form action - removing duplicated code in AbstractSubmitLink - hidden input name changed to be root form relative Issue: WICKET-3438
Added: wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/stateless/StatelessFormUrlTest.java Modified: wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/AbstractSubmitLink.java wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java Modified: wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/AbstractSubmitLink.java URL: http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/AbstractSubmitLink.java?rev=1071860&r1=1071859&r2=1071860&view=diff ============================================================================== --- wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/AbstractSubmitLink.java (original) +++ wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/AbstractSubmitLink.java Fri Feb 18 02:02:51 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; import org.apache.wicket.version.undo.Change; /** @@ -162,28 +159,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 (inputName.equals("submit")) - { - inputName.prepend(Component.PATH_SEPARATOR); - } - return inputName.toString(); + return Form.getRootFormRelativeId(this); } } Modified: wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java URL: http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java?rev=1071860&r1=1071859&r2=1071860&view=diff ============================================================================== --- wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java (original) +++ wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java Fri Feb 18 02:02:51 2011 @@ -56,6 +56,7 @@ import org.apache.wicket.request.target. 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; @@ -977,6 +978,11 @@ public class Form<T> extends WebMarkupCo } }); parameters.remove(getHiddenFieldId()); + if (submittingComponent instanceof AbstractSubmitLink) + { + AbstractSubmitLink submitLink = (AbstractSubmitLink)submittingComponent; + parameters.remove(submitLink.getInputName()); + } } } @@ -1596,7 +1602,7 @@ public class Form<T> extends WebMarkupCo } else { - formId = getId(); + formId = Form.getRootFormRelativeId(this); } return getInputNamePrefix() + formId + "_hf_0"; } @@ -2420,4 +2426,41 @@ public class Form<T> extends WebMarkupCo "if (typeof(Wicket)=='undefined') { Wicket={}; } if (typeof(Wicket.Forms)=='undefined') { Wicket.Forms={}; }", 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 (inputName.equals("submit")) + { + inputName.prepend(Component.PATH_SEPARATOR); + } + return inputName.toString(); + } } Modified: wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java URL: http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java?rev=1071860&r1=1071859&r2=1071860&view=diff ============================================================================== --- wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java (original) +++ wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java Fri Feb 18 02:02:51 2011 @@ -33,7 +33,6 @@ import org.apache.wicket.Component; import org.apache.wicket.IConverterLocator; import org.apache.wicket.Localizer; import org.apache.wicket.MarkupContainer; -import org.apache.wicket.Page; import org.apache.wicket.WicketRuntimeException; import org.apache.wicket.markup.ComponentTag; import org.apache.wicket.model.IModel; @@ -41,7 +40,6 @@ import org.apache.wicket.model.IProperty import org.apache.wicket.util.convert.ConversionException; import org.apache.wicket.util.convert.IConverter; import org.apache.wicket.util.lang.Classes; -import org.apache.wicket.util.string.PrependingStringBuffer; import org.apache.wicket.util.string.StringList; import org.apache.wicket.util.string.Strings; import org.apache.wicket.util.string.interpolator.MapVariableInterpolator; @@ -798,38 +796,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 (inputName.equals("submit")) - { - inputName.prepend(Component.PATH_SEPARATOR); - } + final 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/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/stateless/StatelessFormUrlTest.java URL: http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/stateless/StatelessFormUrlTest.java?rev=1071860&view=auto ============================================================================== --- wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/stateless/StatelessFormUrlTest.java (added) +++ wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/stateless/StatelessFormUrlTest.java Fri Feb 18 02:02:51 2011 @@ -0,0 +1,69 @@ +/* + * 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.PageParameters; +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.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.getServletResponse().getDocument().contains("submitLink=x")); + } + + /** + */ + public static class TestPage extends WebPage implements IMarkupResourceStreamProvider + { + + /** + * @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>"); + } + + } +}