Skip to content

Commit 0cd194a

Browse files
committed
feat(display): remove fakepath
1 parent 857e891 commit 0cd194a

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/eventHandlers.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@ import { findFirstChildNode, restoreDefaultText } from './util'
22
import Selector from './selector'
33

44
const fileApi = !!window.File
5+
const FAKE_PATH = 'fakepath'
6+
const FAKE_PATH_SEPARATOR = '\\'
57

68
const getSelectedFiles = (input) => {
79
if (input.hasAttribute('multiple') && fileApi) {
8-
const files = [].slice.call(input.files)
10+
return [].slice.call(input.files)
911
.map((file) => file.name)
12+
.join(', ')
13+
}
14+
15+
if (input.value.indexOf(FAKE_PATH) !== -1) {
16+
const splittedValue = input.value.split(FAKE_PATH_SEPARATOR)
1017

11-
return files.join(', ')
18+
return splittedValue[splittedValue.length - 1]
1219
}
1320

1421
return input.value

tests/units/eventHandlers.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ describe('eventHandlers.js', function () {
4040
input.dispatchEvent(new Event('change'))
4141
})
4242

43+
it('should remove fakepath if found', function (done) {
44+
bsCustomFileInput.init()
45+
46+
var label = document.querySelector('.custom-file-label')
47+
48+
input.addEventListener('change', function () {
49+
expect(label.innerHTML).equal('myFakeFile.exe')
50+
done()
51+
})
52+
53+
Object.defineProperty(input, 'value', {
54+
value: 'C:\\fakepath\\myFakeFile.exe',
55+
})
56+
57+
input.dispatchEvent(new Event('change'))
58+
})
59+
4360
it('should change the label when files are selected', function (done) {
4461
bsCustomFileInput.init()
4562

0 commit comments

Comments
 (0)