Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #675 from capricorn86/task/494-typeerror-right-han…
…d-side-of-instanceof-is-not-an-object-filelist #494@minor: Adds support for FileList to HTMLInputElement.
- Loading branch information
Showing
8 changed files
with
96 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/happy-dom/src/nodes/html-input-element/FileList.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import File from '../../file/File'; | ||
import IFileList from './IFileList'; | ||
|
||
/** | ||
* FileList. | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileList | ||
*/ | ||
export default class FileList extends Array implements IFileList<File> { | ||
/** | ||
* Constructor. | ||
*/ | ||
constructor() { | ||
super(0); | ||
} | ||
|
||
/** | ||
* Returns `Symbol.toStringTag`. | ||
* | ||
* @returns `Symbol.toStringTag`. | ||
*/ | ||
public get [Symbol.toStringTag](): string { | ||
return this.constructor.name; | ||
} | ||
|
||
/** | ||
* Returns item by index. | ||
* | ||
* @param index Index. | ||
* @returns Item. | ||
*/ | ||
public item(index: number): File | null { | ||
return this[index] || null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/happy-dom/src/nodes/html-input-element/IFileList.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* NodeList. | ||
*/ | ||
export default interface IFileList<T> extends Array<T> { | ||
/** | ||
* Returns item by index. | ||
* | ||
* @param index Index. | ||
*/ | ||
item(index: number): T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/happy-dom/test/nodes/html-input-element/FileList.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import Window from '../../../src/window/Window'; | ||
import IWindow from '../../../src/window/Window'; | ||
import IDocument from '../../../src/nodes/document/IDocument'; | ||
import File from '../../../src/file/File'; | ||
import IHTMLInputElement from '../../../src/nodes/html-input-element/IHTMLInputElement'; | ||
|
||
describe('FileList', () => { | ||
let window: IWindow; | ||
let document: IDocument; | ||
|
||
beforeEach(() => { | ||
window = new Window(); | ||
document = window.document; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
describe('item()', () => { | ||
it('Returns file at index.', () => { | ||
const element = <IHTMLInputElement>document.createElement('input'); | ||
const file1 = new File([''], 'file.txt'); | ||
const file2 = new File([''], 'file2.txt'); | ||
|
||
element.files.push(file1); | ||
element.files.push(file2); | ||
|
||
expect(element.files.item(0)).toBe(file1); | ||
expect(element.files.item(1)).toBe(file2); | ||
}); | ||
}); | ||
}); |