Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix The change event shouldn't be raised on an upload input if the value wasn't changed after setFilesToUpload `` (close #1844) #1979

Merged
merged 6 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/client/sandbox/upload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import INTERNAL_PROPS from '../../../processing/dom/internal-properties';
import SandboxBase from '../base';
import UploadInfoManager from './info-manager';
import { isFileInput } from '../../utils/dom';
import { isIE, isFirefox, version as browserVersion } from '../../utils/browser';
import { isIE, isFirefox, isChrome, isMacPlatform, isSafari, version as browserVersion } from '../../utils/browser';
import { stopPropagation, preventDefault } from '../../utils/event';
import { get as getSandboxBackup } from '../backup';
import nativeMethods from '../native-methods';
Expand Down Expand Up @@ -104,6 +104,28 @@ export default class UploadSandbox extends SandboxBase {
return value;
}

// GH-1844
static _needToRaiseChangeEvent (filesToUpload, currentFiles) : boolean {
if (isFirefox || (isMacPlatform && isChrome || isSafari))
return true;

for (const file of filesToUpload) {
let found = false;

for (const currentFile of currentFiles) {
if (file.name === currentFile.name) {
found = true;
break;
}
}

if (found === false)
return true;
}

return false;
}
Farfurix marked this conversation as resolved.
Show resolved Hide resolved

doUpload (input: HTMLInputElement, filePaths: string | Array<string>) {
const currentInfoManager = UploadSandbox._getCurrentInfoManager(input);

Expand All @@ -113,10 +135,16 @@ export default class UploadSandbox extends SandboxBase {
.then(filesInfo => UploadInfoManager.prepareFileListWrapper(filesInfo))
.then(data => {
if (!data.errs.length) {
const value = UploadInfoManager.formatValue(filePaths);
const value = UploadInfoManager.formatValue(filePaths);
const inputInfo = currentInfoManager.getUploadInfo(input);

currentInfoManager.setUploadInfo(input, data.fileList, value);
this._riseChangeEvent(input);

/*eslint-disable no-restricted-properties*/
if (!inputInfo || data.fileList.length !== inputInfo.files.length ||
UploadSandbox._needToRaiseChangeEvent(data.fileList, inputInfo.files))
this._riseChangeEvent(input);
/*eslint-enable no-restricted-properties*/
}

return data.errs;
Expand Down
46 changes: 45 additions & 1 deletion test/client/fixtures/sandbox/upload-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ var INTERNAL_PROPS = hammerhead.get('../processing/dom/internal-properties');

var Promise = hammerhead.Promise;
var transport = hammerhead.transport;
var browserUtils = hammerhead.utils.browser;
var uploadSandbox = hammerhead.sandbox.upload;
var infoManager = hammerhead.sandbox.upload.infoManager;
var eventSimulator = hammerhead.sandbox.event.eventSimulator;
var nativeMethods = hammerhead.nativeMethods;

var browserUtils = hammerhead.utils.browser;
var isChrome = browserUtils.isChrome;
var isFirefox = browserUtils.isFirefox;
var isSafari = browserUtils.isSafari;
var isMacPlatform = browserUtils.isMacPlatform;

// ----- Server API mock ---------
// Virtual file system:
// - file.txt
Expand Down Expand Up @@ -538,6 +543,45 @@ asyncTest('change event', function () {
uploadSandbox.doUpload(fileInput, './file.txt');
});

test('change event in the case of the same file/files selection (GH-1844)', function () {
var needToRaiseChangeEvent = isFirefox || (isMacPlatform && isChrome || isSafari);
var assertionsCount = needToRaiseChangeEvent ? 4 : 0;

expect(assertionsCount);

var fileInput = $('<input type="file" name="test" id="777">')[0];

var changeHandler = function () {
ok(needToRaiseChangeEvent);
};

return uploadSandbox.doUpload(fileInput, './file.txt')
.then(function () {
fileInput.onchange = changeHandler;

return uploadSandbox.doUpload(fileInput, './file.txt');
})
.then(function () {
return uploadSandbox.doUpload(fileInput, ['./file.txt']);
})
.then(function () {
fileInput.onchange = null;

return uploadSandbox.doUpload(fileInput, ['folder/file.png', './file.txt']);
})
.then(function () {
fileInput.onchange = changeHandler;

return uploadSandbox.doUpload(fileInput, ['folder/file.png', './file.txt']);
})
.then(function () {
return uploadSandbox.doUpload(fileInput, ['./file.txt', 'folder/file.png']);
})
.then(function () {
fileInput.onchange = null;
});
});

test('multi-select files', function () {
var fileInput = $('<input type="file" name="test" id="id">')[0];
var value = '';
Expand Down