WICKET-6525 dynamic hidden fields

-use id for div containing hidden parameter fields
-getJsForListenerUrl changes hidden parameters for get requests
-reuse writeParamsAsHiddenFields in getJsForListenerUrl()


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/57ae17dc
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/57ae17dc
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/57ae17dc

Branch: refs/heads/master
Commit: 57ae17dc51cb2bde2c5cda60171d9098862d5741
Parents: 4320913
Author: Sven Meier <svenme...@apache.org>
Authored: Wed Feb 14 00:00:11 2018 +0100
Committer: Sven Meier <svenme...@apache.org>
Committed: Thu Feb 22 21:40:45 2018 +0100

----------------------------------------------------------------------
 .../apache/wicket/markup/html/form/Form.java    | 59 +++++++++++---------
 .../markup/html/form/FormMethodTestPage.java    | 13 ++++-
 .../html/form/FormMethodTestPage_expected.html  |  2 +-
 .../wicket/markup/html/form/FormTest.java       | 14 ++++-
 .../html/form/pageWithParameters_expected.html  |  2 +-
 5 files changed, 56 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/57ae17dc/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
----------------------------------------------------------------------
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 9b230c4..a9df65b 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
@@ -38,8 +38,6 @@ import org.apache.wicket.behavior.Behavior;
 import org.apache.wicket.event.IEvent;
 import org.apache.wicket.markup.ComponentTag;
 import org.apache.wicket.markup.MarkupStream;
-import org.apache.wicket.markup.head.IHeaderResponse;
-import org.apache.wicket.markup.head.JavaScriptHeaderItem;
 import org.apache.wicket.markup.html.WebMarkupContainer;
 import org.apache.wicket.markup.html.form.upload.FileUploadField;
 import org.apache.wicket.markup.html.form.validation.FormValidatorAdapter;
@@ -146,8 +144,6 @@ public class Form<T> extends WebMarkupContainer
                IRequestListener,
                IGenericComponent<T, Form<T>>
 {
-       private static final String HIDDEN_DIV_START = "<div 
style=\"width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden\">";
-
        public static final String ENCTYPE_MULTIPART_FORM_DATA = 
"multipart/form-data";
 
        /**
@@ -529,22 +525,26 @@ public class Form<T> extends WebMarkupContainer
        {
                Form<?> root = getRootForm();
 
-               StringBuilder string = new StringBuilder();
-               string.append(String.format("var f = 
document.getElementById('%s');", root.getMarkupId()));
-
+               AppendingStringBuffer buffer = new AppendingStringBuffer();
+               
                String action = url.toString();
                if (root.encodeUrlInHiddenFields()) {
+                       
buffer.append(String.format("document.getElementById('%s').innerHTML = '", 
root.getHiddenFieldsId()));
+                       
                        // parameter must be sent as hidden field, as it would 
be ignored in the action URL
                        int i = action.indexOf('?');
                        if (i != -1) {
-                               
string.append(String.format("f.getElementsByTagName('input')[0].name = '%s';", 
action.substring(i + 1)));
+                               
writeParamsAsHiddenFields(Strings.split(action.substring(i + 1), '&'), buffer);
+                               
                                action = action.substring(0, i);
                        }
+                       
+                       buffer.append("';");
                }
-
-               string.append(String.format("f.action='%s';", action));
-               string.append("f.submit();");
-               return string;
+               buffer.append(String.format("var f = 
document.getElementById('%s');", root.getMarkupId()));
+               buffer.append(String.format("f.action='%s';", action));
+               buffer.append("f.submit();");
+               return buffer;
        }
 
        /**
@@ -1194,9 +1194,8 @@ public class Form<T> extends WebMarkupContainer
        {
                AppendingStringBuffer buffer = new AppendingStringBuffer();
 
-               // div that is not visible (but not display:none either)
-               buffer.append(HIDDEN_DIV_START);
-
+               getResponse().write("<div 
style=\"width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden\">");
+               
                // add an empty textfield (otherwise IE doesn't work)
                buffer.append("<input type=\"text\" tabindex=\"-1\" 
autocomplete=\"off\"/>");
 
@@ -1209,9 +1208,8 @@ public class Form<T> extends WebMarkupContainer
                buffer.append("'); if 
(b!=null&amp;&amp;b.onclick!=null&amp;&amp;typeof(b.onclick) != 'undefined') {  
var r = Wicket.bind(b.onclick, b)(); if (r != false) b.click(); } else { 
b.click(); };  return false;\" ");
                buffer.append(" />");
 
-               // close div
-               buffer.append("</div>");
-
+               getResponse().write("</div>");
+               
                getResponse().write(buffer);
        }
 
@@ -1281,6 +1279,16 @@ public class Form<T> extends WebMarkupContainer
        }
 
        /**
+        * Returns the id which will be used for the hidden div containing all 
parameter fields.
+        * 
+        * @return the id of the hidden div
+        */
+       private final String getHiddenFieldsId()
+       {
+               return getInputNamePrefix() + getMarkupId() + "_hf_0";
+       }
+
+       /**
         * Gets the HTTP submit method that will appear in form markup. If no 
method is specified in the
         * template, "post" is the default. Note that the markup-declared HTTP 
method may not correspond
         * to the one actually used to submit the form; in an Ajax submit, for 
example, JavaScript event
@@ -1655,7 +1663,7 @@ public class Form<T> extends WebMarkupContainer
        }
 
        /**
-        * Writes the markup for the hidden input field and default button 
field if applicable to the
+        * Writes the markup for the hidden input fields and default button 
field if applicable to the
         * current response.
         */
        public final void writeHiddenFields()
@@ -1664,7 +1672,9 @@ public class Form<T> extends WebMarkupContainer
                // and have to write the url parameters as hidden fields
                if (encodeUrlInHiddenFields())
                {
-                       AppendingStringBuffer buffer = new 
AppendingStringBuffer(HIDDEN_DIV_START);
+                       getResponse().write(String.format("<div id=\"%s\" 
style=\"width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden\">",
 getHiddenFieldsId()));
+
+                       AppendingStringBuffer buffer = new 
AppendingStringBuffer();                             
 
                        String url = getActionUrl().toString();
                        int i = url.indexOf('?');
@@ -1673,8 +1683,9 @@ public class Form<T> extends WebMarkupContainer
 
                        writeParamsAsHiddenFields(params, buffer);
 
-                       buffer.append("</div>");
                        getResponse().write(buffer);
+                       
+                       getResponse().write("</div>");
                }
 
                // if a default submitting component was set, handle the 
rendering of that
@@ -2028,12 +2039,6 @@ public class Form<T> extends WebMarkupContainer
                return component.findParent(Form.class);
        }
 
-       /** {@inheritDoc} */
-       @Override
-       public void renderHead(IHeaderResponse response)
-       {
-       }
-
        /**
         * Utility method to assemble an id to distinct form components from 
different nesting levels.
         * Useful to generate input names attributes.

http://git-wip-us.apache.org/repos/asf/wicket/blob/57ae17dc/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormMethodTestPage.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormMethodTestPage.java
 
b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormMethodTestPage.java
index d6d353c..2bfbe6c 100644
--- 
a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormMethodTestPage.java
+++ 
b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormMethodTestPage.java
@@ -25,14 +25,20 @@ public class FormMethodTestPage extends WebPage
 {
        /** For serialization. */
        private static final long serialVersionUID = 1L;
+       
+       public final Form<Void> postForm;
+
+       public final Form<Void> getForm;
 
        /**
         * Construct.
         */
        public FormMethodTestPage()
        {
-               add(new Form<Void>("formpost"));
-               add(new Form<Void>("formget")
+               postForm = new Form<Void>("formpost");
+               add(postForm);
+               
+               getForm = new Form<Void>("formget")
                {
                        /** Serialization */
                        private static final long serialVersionUID = 1L;
@@ -42,6 +48,7 @@ public class FormMethodTestPage extends WebPage
                        {
                                return Form.METHOD_GET;
                        }
-               });
+               };
+               add(getForm);
        }
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/57ae17dc/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormMethodTestPage_expected.html
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormMethodTestPage_expected.html
 
b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormMethodTestPage_expected.html
index 6689a44..1dac4cc 100644
--- 
a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormMethodTestPage_expected.html
+++ 
b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormMethodTestPage_expected.html
@@ -1,6 +1,6 @@
 <html>
 <body>
        <form wicket:id="formpost" id="formpost1" method="post" 
action="./org.apache.wicket.markup.html.form.FormMethodTestPage?0-1.-formpost"></form>
-       <form wicket:id="formget" id="formget2" method="get" 
action="./org.apache.wicket.markup.html.form.FormMethodTestPage"><div 
style="width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden"><input
 type="hidden" name="0-1.-formget" value="" /></div></form>
+       <form wicket:id="formget" id="formget2" method="get" 
action="./org.apache.wicket.markup.html.form.FormMethodTestPage"><div 
id="formget2_hf_0" 
style="width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden"><input
 type="hidden" name="0-1.-formget" value="" /></div></form>
 </body>
 </html>

http://git-wip-us.apache.org/repos/asf/wicket/blob/57ae17dc/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormTest.java 
b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormTest.java
index 05344a5..4bf48cb 100644
--- a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormTest.java
+++ b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormTest.java
@@ -55,12 +55,22 @@ public class FormTest extends WicketTestCase
 
 
        /**
-        * @throws Exception
+        * WICKET-6525 / WICKET-6348
         */
        @Test
-       public void formMethodGet() throws Exception
+       public void formMethods() throws Exception
        {
                executeTest(FormMethodTestPage.class, 
"FormMethodTestPage_expected.html");
+               
+               FormMethodTestPage page = 
(FormMethodTestPage)tester.getLastRenderedPage();
+       
+               // form action contains path and query 
+               assertEquals("var f = 
document.getElementById('formpost1');f.action='path?param1&param2=val%20ue';f.submit();",
+                       
page.postForm.getJsForListenerUrl("path?param1&param2=val%20ue").toString());
+               
+               // form action must not contain query (since ignored by 
browser), put into hidden form parameters instead 
+               
assertEquals("document.getElementById('formget2_hf_0').innerHTML = '<input 
type=\"hidden\" name=\"param1\" value=\"\" /><input type=\"hidden\" 
name=\"param2\" value=\"val ue\" />';var f = 
document.getElementById('formget2');f.action='path';f.submit();",
+                       
page.getForm.getJsForListenerUrl("path?param1&param2=val%20ue").toString());
        }
 
        /**

http://git-wip-us.apache.org/repos/asf/wicket/blob/57ae17dc/wicket-core/src/test/java/org/apache/wicket/markup/html/form/pageWithParameters_expected.html
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/pageWithParameters_expected.html
 
b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/pageWithParameters_expected.html
index f4a5d2b..791508f 100644
--- 
a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/pageWithParameters_expected.html
+++ 
b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/pageWithParameters_expected.html
@@ -1,6 +1,6 @@
 <html>
 <body>
        <form wicket:id="formpost" id="formpost1" method="post" 
action="./org.apache.wicket.markup.html.form.FormMethodTestPage?0-1.-formpost&amp;first=foo&amp;second=bar"></form>
-       <form wicket:id="formget" id="formget2" method="get" 
action="./org.apache.wicket.markup.html.form.FormMethodTestPage"><div 
style="width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden"><input
 type="hidden" name="0-1.-formget" value="" /><input type="hidden" name="first" 
value="foo" /><input type="hidden" name="second" value="bar" /></div></form>
+       <form wicket:id="formget" id="formget2" method="get" 
action="./org.apache.wicket.markup.html.form.FormMethodTestPage"><div 
id="formget2_hf_0" 
style="width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden"><input
 type="hidden" name="0-1.-formget" value="" /><input type="hidden" name="first" 
value="foo" /><input type="hidden" name="second" value="bar" /></div></form>
 </body>
 </html>

Reply via email to