Skip to content

Commit

Permalink
Fixed form input with name="action" or name="method" breaking submiss…
Browse files Browse the repository at this point in the history
…ion. Fixes #1203
  • Loading branch information
mpscholten committed Nov 8, 2021
1 parent 61f716d commit 1998c19
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/IHP/static/helpers.js
Expand Up @@ -166,6 +166,11 @@ window.submitForm = function (form, possibleClickedButton) {
return;
}

// We cannot use `form.action` here because there could be a <input name="action"/>
// See https://github.com/digitallyinduced/ihp/issues/1203
var formAction = form.getAttribute('action');
var formMethod = form.getAttribute('method');

var request = new XMLHttpRequest();
request.responseType = 'document';
request.overrideMimeType('text/html');
Expand All @@ -187,15 +192,15 @@ window.submitForm = function (form, possibleClickedButton) {
);
transitionToNewPage(request.response);
Turbolinks.clearCache();
if (request.responseURL !== form.action) {
if (request.responseURL !== formAction) {
history.pushState({ turbolinks: true }, '', request.responseURL);
};
var turbolinkLoadEvent = new CustomEvent('turbolinks:load');
document.dispatchEvent(turbolinkLoadEvent);
} else {
window.liveReloadPaused = true;

if (request.responseURL !== form.action) {
if (request.responseURL !== formAction) {
history.pushState({}, '', request.responseURL);
window.onpopstate = function (event) {
window.location.reload();
Expand Down Expand Up @@ -240,26 +245,26 @@ window.submitForm = function (form, possibleClickedButton) {

var hasFileInputs = form.querySelector('input[type="file"]');
if (hasFileInputs) {
request.open(form.method, form.action, true);
request.open(formMethod, formAction, true);
request.send(formData);
} else {
var parameters = [];

if (form.method.toUpperCase() === 'GET') {
var url = new URL(form.action);
if (formMethod.toUpperCase() === 'GET') {
var url = new URL(formAction);
for (var pair of formData.entries()) {
url.searchParams.set(pair[0], pair[1]);
}

request.open(form.method, url.toString(), true);
request.open(formMethod, url.toString(), true);
} else {
for (var pair of formData.entries()) {
parameters.push(
encodeURIComponent(pair[0]) + '=' + encodeURIComponent(pair[1])
);
}

request.open(form.method, form.action, true);
request.open(formMethod, formAction, true);
}

request.setRequestHeader(
Expand Down

0 comments on commit 1998c19

Please sign in to comment.