This is an automated email from the ASF dual-hosted git repository. svenmeier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/wicket.git
The following commit(s) were added to refs/heads/master by this push: new 4a8b25c WICKET-6642 revert SubmitLink to request Form url 4a8b25c is described below commit 4a8b25c3693dbd28ff79256bcabb331294f2294a Author: Sven Meier <svenme...@apache.org> AuthorDate: Mon Mar 11 10:14:48 2019 +0100 WICKET-6642 revert SubmitLink to request Form url and use findSubmittingButton once again --- .../request/handler/logger/ListenerLogData.java | 2 +- .../org/apache/wicket/markup/html/form/Form.java | 47 +++++++++++-- .../apache/wicket/markup/html/form/SubmitLink.java | 13 +--- .../wicket/markup/html/form/SubmitLinkTest.java | 76 ++++++++++++++++++++++ .../wicket/stateless/StatelessFormUrlTest.java | 3 +- 5 files changed, 123 insertions(+), 18 deletions(-) diff --git a/wicket-core/src/main/java/org/apache/wicket/core/request/handler/logger/ListenerLogData.java b/wicket-core/src/main/java/org/apache/wicket/core/request/handler/logger/ListenerLogData.java index ad83c3a..c0f4165 100644 --- a/wicket-core/src/main/java/org/apache/wicket/core/request/handler/logger/ListenerLogData.java +++ b/wicket-core/src/main/java/org/apache/wicket/core/request/handler/logger/ListenerLogData.java @@ -113,7 +113,7 @@ public class ListenerLogData extends PageLogData final IRequestableComponent component = pageAndComponentProvider.getComponent(); if (component instanceof Form) { - final IFormSubmitter submitter = ((Form<?>)component).findSubmittingButton(); + final IFormSubmitter submitter = ((Form<?>)component).findSubmitter(); return submitter instanceof Component ? (Component)submitter : null; } return null; diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java index 5bc90d6..ce5bb5c 100644 --- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java +++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java @@ -423,12 +423,14 @@ public class Form<T> extends WebMarkupContainer } /** + * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT USE IT! + * <p> * Gets the IFormSubmittingComponent which submitted this form. * * @return The component which submitted this form, or null if the processing was not triggered * by a registered IFormSubmittingComponent */ - public final IFormSubmitter findSubmittingButton() + public final IFormSubmittingComponent findSubmitter() { IFormSubmittingComponent submittingComponent = getPage().visitChildren( IFormSubmittingComponent.class, new IVisitor<Component, IFormSubmittingComponent>() @@ -506,7 +508,7 @@ public class Form<T> extends WebMarkupContainer } /** - * Generate a piece of JavaScript that submits the form to the given URL. + * Generate a piece of JavaScript that submits the form to the given URL of an {@link IRequestListener}. * * Warning: This code should only be called in the rendering phase for form components inside * the form because it uses the css/javascript id of the form which can be stored in the markup. @@ -527,7 +529,8 @@ public class Form<T> extends WebMarkupContainer // parameter must be sent as hidden field, as it would be ignored in the action URL int i = action.indexOf('?'); - if (i != -1) { + if (i != -1) + { writeParamsAsHiddenFields(Strings.split(action.substring(i + 1), '&'), buffer); action = action.substring(0, i); @@ -542,6 +545,40 @@ public class Form<T> extends WebMarkupContainer } /** + * Generate a piece of JavaScript that submits the form with the given {@link IFormSubmittingComponent}. + * + * @param submitter + * the submitter + * @return the javascript code that submits the form. + * + * @see #findSubmitter() + */ + public final CharSequence getJsForSubmitter(IFormSubmittingComponent submitter) + { + Form<?> root = getRootForm(); + + String param = submitter.getInputName() + "=x"; + + AppendingStringBuffer buffer = new AppendingStringBuffer(); + buffer.append(String.format("var f = document.getElementById('%s');", root.getMarkupId())); + if (root.encodeUrlInHiddenFields()) + { + buffer.append(String.format("document.getElementById('%s').innerHTML += '", root.getHiddenFieldsId())); + + writeParamsAsHiddenFields(new String[] {param}, buffer); + + buffer.append("';"); + } + else + { + String action = root.getActionUrl().toString(); + buffer.append("f.action += '" + (action.indexOf('?') > -1 ? '&' : '?') + param + "';"); + } + buffer.append("f.submit();"); + return buffer; + } + + /** * Gets the maximum size for uploads. If null, the setting * {@link org.apache.wicket.settings.ApplicationSettings#getDefaultMaximumUploadSize()} is used. * @@ -736,10 +773,10 @@ public class Form<T> extends WebMarkupContainer // Tells FormComponents that a new user input has come inputChanged(); - // First, see if the processing was triggered by a Wicket IFormSubmittingComponent + // First, see if the processing was triggered by a IFormSubmittingComponent if (submitter == null) { - submitter = findSubmittingButton(); + submitter = findSubmitter(); if (submitter instanceof IFormSubmittingComponent) { diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/SubmitLink.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/SubmitLink.java index f7f181b..30cbe8b 100644 --- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/SubmitLink.java +++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/SubmitLink.java @@ -16,10 +16,8 @@ */ package org.apache.wicket.markup.html.form; -import org.apache.wicket.IRequestListener; import org.apache.wicket.markup.ComponentTag; import org.apache.wicket.model.IModel; -import org.apache.wicket.request.mapper.parameter.PageParameters; /** * A link which can be used exactly like a Button to submit a Form. The onclick of the link will use @@ -73,7 +71,7 @@ import org.apache.wicket.request.mapper.parameter.PageParameters; * @author Eelco Hillenius * */ -public class SubmitLink extends AbstractSubmitLink implements IRequestListener +public class SubmitLink extends AbstractSubmitLink { private static final long serialVersionUID = 1L; @@ -214,8 +212,7 @@ public class SubmitLink extends AbstractSubmitLink implements IRequestListener script.append("if (typeof ff.onsubmit === 'function' && ff.onsubmit() == false) return false;"); } - CharSequence url = urlForListener(new PageParameters()); - script.append(root.getJsForListenerUrl(url)); + script.append(root.getJsForSubmitter(this)); script.append("return false;"); return script; @@ -226,12 +223,6 @@ public class SubmitLink extends AbstractSubmitLink implements IRequestListener } } - @Override - public void onRequest() - { - getForm().onFormSubmitted(this); - } - /** * @see org.apache.wicket.markup.html.form.IFormSubmittingComponent#onError() */ diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/SubmitLinkTest.java b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/SubmitLinkTest.java new file mode 100644 index 0000000..2860796 --- /dev/null +++ b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/SubmitLinkTest.java @@ -0,0 +1,76 @@ +/* + * 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.markup.html.form; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.apache.wicket.MarkupContainer; +import org.apache.wicket.markup.IMarkupResourceStreamProvider; +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.util.resource.IResourceStream; +import org.apache.wicket.util.resource.StringResourceStream; +import org.apache.wicket.util.tester.WicketTestCase; +import org.junit.jupiter.api.Test; + +/** + * Test for {@link SubmitLink}. + */ +class SubmitLinkTest extends WicketTestCase +{ + /** + * WICKET-6642 + */ + @Test + void getFormSubmitter() + { + TestPage testPage = new TestPage(); + tester.startPage(testPage); + + tester.clickLink("form:link"); + } + + /** */ + public static class TestPage extends WebPage implements IMarkupResourceStreamProvider + { + private static final long serialVersionUID = 1L; + Form<Void> form; + SubmitLink link; + + /** */ + TestPage() + { + add(form = new Form<>("form") + { + protected void onSubmit() + { + super.onSubmit(); + + assertEquals(link, findSubmitter()); + } + }); + form.add(link = new SubmitLink("link")); + } + + @Override + public IResourceStream getMarkupResourceStream(MarkupContainer container, + Class<?> containerClass) + { + return new StringResourceStream("<html><body>" + + "<form wicket:id=\"form\"><a wicket:id=\"link\"></a></form></body></html>"); + } + } +} diff --git a/wicket-core/src/test/java/org/apache/wicket/stateless/StatelessFormUrlTest.java b/wicket-core/src/test/java/org/apache/wicket/stateless/StatelessFormUrlTest.java index 5a6aea9..ef30976 100644 --- a/wicket-core/src/test/java/org/apache/wicket/stateless/StatelessFormUrlTest.java +++ b/wicket-core/src/test/java/org/apache/wicket/stateless/StatelessFormUrlTest.java @@ -16,6 +16,7 @@ */ package org.apache.wicket.stateless; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import org.apache.wicket.MarkupContainer; @@ -58,7 +59,7 @@ class StatelessFormUrlTest extends WicketTestCase void submitLinkInputNameNotEncodedIntoFormAction() { tester.executeUrl("?0-1.IFormSubmitListener-form&text=newValue&submitLink=x"); - assertFalse(tester.getLastResponseAsString().contains("submitLink=x")); + assertEquals("./?-1.-form", tester.getTagById("form1").getAttribute("action")); } /**