diff --git a/src/app/App.js b/src/app/App.js index 60a446b..e184116 100644 --- a/src/app/App.js +++ b/src/app/App.js @@ -450,6 +450,7 @@ class App extends EventEmitter { this.isNavigationPending = false; this.pendingNavigate = null; globals.capturedFormElement = null; + globals.capturedFormButtonElement = null; console.log('Navigation done'); } @@ -653,6 +654,7 @@ class App extends EventEmitter { } globals.capturedFormElement = event.capturedFormElement; + globals.capturedFormButtonElement = event.capturedFormButtonElement; var navigateFailed = false; try { @@ -804,6 +806,12 @@ class App extends EventEmitter { return; } event.capturedFormElement = form; + const buttonSelector = 'button:not([type]),button[type=submit],input[type=submit]'; + if (dom.match(globals.document.activeElement, buttonSelector)) { + event.capturedFormButtonElement = globals.document.activeElement; + } else { + event.capturedFormButtonElement = form.querySelector(buttonSelector); + } this.maybeNavigate_(form.action, event); } diff --git a/src/screen/RequestScreen.js b/src/screen/RequestScreen.js index ca5af0f..4a5334a 100644 --- a/src/screen/RequestScreen.js +++ b/src/screen/RequestScreen.js @@ -193,17 +193,14 @@ class RequestScreen extends Screen { * @inheritDoc */ load(path) { - var cache = this.getCache(); + const cache = this.getCache(); if (core.isDefAndNotNull(cache)) { return CancellablePromise.resolve(cache); } - - var body = null; - var httpMethod = this.httpMethod; - - var headers = new MultiMap(); + let body = null; + let httpMethod = this.httpMethod; + const headers = new MultiMap(); Object.keys(this.httpHeaders).forEach(header => headers.add(header, this.httpHeaders[header])); - if (globals.capturedFormElement) { body = new FormData(globals.capturedFormElement); httpMethod = RequestScreen.POST; @@ -211,8 +208,8 @@ class RequestScreen extends Screen { headers.add('If-None-Match', '"0"'); } } - - var requestPath = this.formatLoadPath(path); + this.maybeAppendSubmitButtonValue_(body); + const requestPath = this.formatLoadPath(path); return Ajax .request(requestPath, httpMethod, body, headers, null, this.timeout) .then(xhr => { @@ -237,6 +234,18 @@ class RequestScreen extends Screen { }); } + /** + * Adds aditional data to the body of the request in case a submit button + * is captured during form submission. + * @param {!FormData} body The FormData containing the request body. + */ + maybeAppendSubmitButtonValue_(body) { + const button = globals.capturedFormButtonElement; + if (body && button && button.name && !button.disabled) { + body.append(button.name, button.value); + } + } + /** * The following method tries to extract the response url value by checking * the custom response header 'X-Request-URL' if proper value is not present diff --git a/test/screen/RequestScreen.js b/test/screen/RequestScreen.js index 3f64599..78e0dc5 100644 --- a/test/screen/RequestScreen.js +++ b/test/screen/RequestScreen.js @@ -221,6 +221,49 @@ describe('RequestScreen', function() { this.requests[0].respond(200); }); + it('should add submit input button value into request FormData', (done) => { + globals.capturedFormElement = globals.document.createElement('form'); + const submitButton = globals.document.createElement('input'); + submitButton.name = 'submitButton'; + submitButton.value = 'Send'; + globals.capturedFormElement.appendChild(submitButton); + var screen = new RequestScreen(); + screen.load('/url').then(() => { + let submitButtonValue; + for (let pair of screen.getRequest().requestBody) { + if (pair[0] === submitButton.name) { + submitButtonValue = pair[1]; + } + } + assert.strictEqual(submitButton.value, submitButtonValue); + globals.capturedFormElement = null; + done(); + }); + this.requests[0].respond(200); + }); + + it('should not add disabled submit input button value into request FormData', (done) => { + globals.capturedFormElement = globals.document.createElement('form'); + const submitButton = globals.document.createElement('input'); + submitButton.name = 'submitButton'; + submitButton.value = 'Send'; + submitButton.disabled = true; + globals.capturedFormElement.appendChild(submitButton); + var screen = new RequestScreen(); + screen.load('/url').then(() => { + let submitButtonValue; + for (let pair of screen.getRequest().requestBody) { + if (pair[0] === submitButton.name) { + submitButtonValue = pair[1]; + } + } + assert.notOk(null, submitButtonValue); + globals.capturedFormElement = null; + done(); + }); + this.requests[0].respond(200); + }); + it('should not cache get requests on ie browsers', (done) => { UA.testUserAgent('MSIE'); // Simulates ie user agent var url = '/url';