-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
FormData.ts
243 lines (223 loc) · 6.01 KB
/
FormData.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import Blob from '../file/Blob.js';
import * as PropertySymbol from '../PropertySymbol.js';
import File from '../file/File.js';
import HTMLInputElement from '../nodes/html-input-element/HTMLInputElement.js';
import HTMLFormElement from '../nodes/html-form-element/HTMLFormElement.js';
import HTMLFormControlsCollection from '../nodes/html-form-element/HTMLFormControlsCollection.js';
import RadioNodeList from '../nodes/html-form-element/RadioNodeList.js';
type FormDataEntry = {
name: string;
value: string | File;
};
const SUBMITTABLE_ELEMENTS = ['BUTTON', 'INPUT', 'OBJECT', 'SELECT', 'TEXTAREA'];
/**
* FormData.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/FormData
*/
export default class FormData implements Iterable<[string, string | File]> {
#entries: FormDataEntry[] = [];
/**
* Constructor.
*
* @param [form] Form.
*/
constructor(form?: HTMLFormElement) {
if (form) {
for (const name of Object.keys(
(<HTMLFormControlsCollection>form[PropertySymbol.elements])[PropertySymbol.namedItems]
)) {
let radioNodeList = (<HTMLFormControlsCollection>form[PropertySymbol.elements])[
PropertySymbol.namedItems
][name];
if (
radioNodeList[0][PropertySymbol.tagName] === 'INPUT' &&
(radioNodeList[0].type === 'checkbox' || radioNodeList[0].type === 'radio')
) {
const newRadioNodeList = new RadioNodeList();
for (const node of radioNodeList) {
if ((<HTMLInputElement>node).checked) {
newRadioNodeList.push(node);
break;
}
}
radioNodeList = newRadioNodeList;
}
for (const node of radioNodeList) {
if (node.name && SUBMITTABLE_ELEMENTS.includes(node[PropertySymbol.tagName])) {
if (node[PropertySymbol.tagName] === 'INPUT' && node.type === 'file') {
if ((<HTMLInputElement>node)[PropertySymbol.files].length === 0) {
this.append(node.name, new File([], '', { type: 'application/octet-stream' }));
} else {
for (const file of (<HTMLInputElement>node)[PropertySymbol.files]) {
this.append(node.name, file);
}
}
} else if ((<HTMLInputElement>node).value) {
this.append(node.name, (<HTMLInputElement>node).value);
}
}
}
}
}
}
/**
* For each.
*
* @param callback Callback.
*/
public forEach(callback: (value: string | File, key: string, thisArg: FormData) => void): void {
for (const entry of this.#entries) {
callback.call(this, entry.value, entry.name, this);
}
}
/**
* Appends a new value onto an existing key.
*
* @param name Name.
* @param value Value.
* @param [filename] Filename.
*/
public append(name: string, value: string | Blob | File, filename?: string): void {
this.#entries.push({
name,
value: this.#parseValue(value, filename)
});
}
/**
* Removes a value.
*
* @param name Name.
*/
public delete(name: string): void {
const newEntries: FormDataEntry[] = [];
for (const entry of this.#entries) {
if (entry.name !== name) {
newEntries.push(entry);
}
}
this.#entries = newEntries;
}
/**
* Returns value.
*
* @param name Name.
* @returns Value.
*/
public get(name: string): string | File | null {
for (const entry of this.#entries) {
if (entry.name === name) {
return entry.value;
}
}
return null;
}
/**
* Returns all values associated with the given name.
*
* @param name Name.
* @returns Values.
*/
public getAll(name: string): Array<string | File> {
const values: Array<string | File> = [];
for (const entry of this.#entries) {
if (entry.name === name) {
values.push(entry.value);
}
}
return values;
}
/**
* Returns whether a FormData object contains a certain key.
*
* @param name Name.
* @returns "true" if the FormData object contains the key.
*/
public has(name: string): boolean {
for (const entry of this.#entries) {
if (entry.name === name) {
return true;
}
}
return false;
}
/**
* Sets a new value for an existing key inside a FormData object, or adds the key/value if it does not already exist.
*
* @param name Name.
* @param value Value.
* @param [filename] Filename.
*/
public set(name: string, value: string | Blob | File, filename?: string): void {
for (const entry of this.#entries) {
if (entry.name === name) {
entry.value = this.#parseValue(value, filename);
return;
}
}
this.append(name, value);
}
/**
* Returns an iterator, allowing you to go through all keys of the key/value pairs contained in this object.
*
* @returns Iterator.
*/
public *keys(): IterableIterator<string> {
for (const entry of this.#entries) {
yield entry.name;
}
}
/**
* Returns an iterator, allowing you to go through all values of the key/value pairs contained in this object.
*
* @returns Iterator.
*/
public *values(): IterableIterator<string | File> {
for (const entry of this.#entries) {
yield entry.value;
}
}
/**
* Returns an iterator, allowing you to go through all key/value pairs contained in this object.
*
* @returns Iterator.
*/
public *entries(): IterableIterator<[string, string | File]> {
for (const entry of this.#entries) {
yield [entry.name, entry.value];
}
}
/**
* Iterator.
*
* @returns Iterator.
*/
public *[Symbol.iterator](): IterableIterator<[string, string | File]> {
for (const entry of this.#entries) {
yield [entry.name, entry.value];
}
}
/**
* Parses a value.
*
* @param value Value.
* @param [filename] Filename.
* @returns Parsed value.
*/
#parseValue(value: string | Blob | File, filename?: string): string | File {
if (value instanceof Blob && !(value instanceof File)) {
const file = new File([], 'blob', { type: value.type });
file[PropertySymbol.buffer] = value[PropertySymbol.buffer];
return file;
}
if (value instanceof File) {
if (filename) {
const file = new File([], filename, { type: value.type, lastModified: value.lastModified });
file[PropertySymbol.buffer] = value[PropertySymbol.buffer];
return file;
}
return value;
}
return String(value);
}
}