Skip to content

Commit

Permalink
Includes submit button value into FormData. See liferay#199
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Basto committed Mar 15, 2017
1 parent 1a6401f commit ed0ea26
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ class App extends EventEmitter {
this.isNavigationPending = false;
this.pendingNavigate = null;
globals.capturedFormElement = null;
globals.capturedFormButtonElement = null;
console.log('Navigation done');
}

Expand Down Expand Up @@ -653,6 +654,7 @@ class App extends EventEmitter {
}

globals.capturedFormElement = event.capturedFormElement;
globals.capturedFormButtonElement = event.capturedFormButtonElement;

var navigateFailed = false;
try {
Expand Down Expand Up @@ -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);
}

Expand Down
27 changes: 18 additions & 9 deletions src/screen/RequestScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,26 +193,23 @@ 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;
if (UA.isIeOrEdge) {
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 => {
Expand All @@ -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
Expand Down
43 changes: 43 additions & 0 deletions test/screen/RequestScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit ed0ea26

Please sign in to comment.