@conform-to/dom 0.4.0-pre.3 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -4,17 +4,14 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
function ownKeys(object, enumerableOnly) {
|
|
6
6
|
var keys = Object.keys(object);
|
|
7
|
-
|
|
8
7
|
if (Object.getOwnPropertySymbols) {
|
|
9
8
|
var symbols = Object.getOwnPropertySymbols(object);
|
|
10
9
|
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
|
11
10
|
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
12
11
|
})), keys.push.apply(keys, symbols);
|
|
13
12
|
}
|
|
14
|
-
|
|
15
13
|
return keys;
|
|
16
14
|
}
|
|
17
|
-
|
|
18
15
|
function _objectSpread2(target) {
|
|
19
16
|
for (var i = 1; i < arguments.length; i++) {
|
|
20
17
|
var source = null != arguments[i] ? arguments[i] : {};
|
|
@@ -24,10 +21,8 @@ function _objectSpread2(target) {
|
|
|
24
21
|
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
25
22
|
});
|
|
26
23
|
}
|
|
27
|
-
|
|
28
24
|
return target;
|
|
29
25
|
}
|
|
30
|
-
|
|
31
26
|
function _defineProperty(obj, key, value) {
|
|
32
27
|
if (key in obj) {
|
|
33
28
|
Object.defineProperty(obj, key, {
|
|
@@ -39,7 +34,6 @@ function _defineProperty(obj, key, value) {
|
|
|
39
34
|
} else {
|
|
40
35
|
obj[key] = value;
|
|
41
36
|
}
|
|
42
|
-
|
|
43
37
|
return obj;
|
|
44
38
|
}
|
|
45
39
|
|
package/index.js
CHANGED
|
@@ -12,32 +12,25 @@ function getFormElements(form) {
|
|
|
12
12
|
}
|
|
13
13
|
function getPaths(name) {
|
|
14
14
|
var pattern = /(\w*)\[(\d+)\]/;
|
|
15
|
-
|
|
16
15
|
if (!name) {
|
|
17
16
|
return [];
|
|
18
17
|
}
|
|
19
|
-
|
|
20
18
|
return name.split('.').flatMap(key => {
|
|
21
19
|
var matches = pattern.exec(key);
|
|
22
|
-
|
|
23
20
|
if (!matches) {
|
|
24
21
|
return key;
|
|
25
22
|
}
|
|
26
|
-
|
|
27
23
|
if (matches[1] === '') {
|
|
28
24
|
return Number(matches[2]);
|
|
29
25
|
}
|
|
30
|
-
|
|
31
26
|
return [matches[1], Number(matches[2])];
|
|
32
27
|
});
|
|
33
28
|
}
|
|
34
29
|
function getFormData(form, submitter) {
|
|
35
30
|
var payload = new FormData(form);
|
|
36
|
-
|
|
37
31
|
if (submitter !== null && submitter !== void 0 && submitter.name) {
|
|
38
32
|
payload.append(submitter.name, submitter.value);
|
|
39
33
|
}
|
|
40
|
-
|
|
41
34
|
return payload;
|
|
42
35
|
}
|
|
43
36
|
function getName(paths) {
|
|
@@ -45,11 +38,9 @@ function getName(paths) {
|
|
|
45
38
|
if (typeof path === 'number') {
|
|
46
39
|
return "".concat(name, "[").concat(path, "]");
|
|
47
40
|
}
|
|
48
|
-
|
|
49
41
|
if (name === '' || path === '') {
|
|
50
42
|
return [name, path].join('');
|
|
51
43
|
}
|
|
52
|
-
|
|
53
44
|
return [name, path].join('.');
|
|
54
45
|
}, '');
|
|
55
46
|
}
|
|
@@ -64,11 +55,9 @@ function hasError(error, name) {
|
|
|
64
55
|
}
|
|
65
56
|
function setFormError(form, submission) {
|
|
66
57
|
var firstErrorByName = Object.fromEntries([...submission.error].reverse());
|
|
67
|
-
|
|
68
58
|
for (var element of form.elements) {
|
|
69
59
|
if (isFieldElement(element)) {
|
|
70
60
|
var error = firstErrorByName[element.name];
|
|
71
|
-
|
|
72
61
|
if (typeof error !== 'undefined' || shouldValidate(submission, element.name)) {
|
|
73
62
|
element.setCustomValidity(error !== null && error !== void 0 ? error : '');
|
|
74
63
|
}
|
|
@@ -80,10 +69,8 @@ function setValue(target, paths, valueFn) {
|
|
|
80
69
|
var lastIndex = length - 1;
|
|
81
70
|
var index = -1;
|
|
82
71
|
var pointer = target;
|
|
83
|
-
|
|
84
72
|
while (pointer != null && ++index < length) {
|
|
85
73
|
var _pointer$key;
|
|
86
|
-
|
|
87
74
|
var key = paths[index];
|
|
88
75
|
var next = paths[index + 1];
|
|
89
76
|
var newValue = index != lastIndex ? (_pointer$key = pointer[key]) !== null && _pointer$key !== void 0 ? _pointer$key : typeof next === 'number' ? [] : {} : valueFn(pointer[key]);
|
|
@@ -111,20 +98,16 @@ function requestValidate(form, field) {
|
|
|
111
98
|
}
|
|
112
99
|
function getFormElement(element) {
|
|
113
100
|
var form = element instanceof HTMLFormElement ? element : element === null || element === void 0 ? void 0 : element.form;
|
|
114
|
-
|
|
115
101
|
if (!form) {
|
|
116
102
|
return null;
|
|
117
103
|
}
|
|
118
|
-
|
|
119
104
|
return form;
|
|
120
105
|
}
|
|
121
106
|
function focusFirstInvalidField(form) {
|
|
122
107
|
var currentFocus = document.activeElement;
|
|
123
|
-
|
|
124
108
|
if (!isFieldElement(currentFocus) || currentFocus.tagName !== 'BUTTON' || currentFocus.form !== form) {
|
|
125
109
|
return;
|
|
126
110
|
}
|
|
127
|
-
|
|
128
111
|
for (var field of form.elements) {
|
|
129
112
|
if (isFieldElement(field)) {
|
|
130
113
|
// Focus on the first non button field
|
|
@@ -137,11 +120,9 @@ function focusFirstInvalidField(form) {
|
|
|
137
120
|
}
|
|
138
121
|
function getSubmissionType(name) {
|
|
139
122
|
var prefix = 'conform/';
|
|
140
|
-
|
|
141
123
|
if (!name.startsWith(prefix) || name.length <= prefix.length) {
|
|
142
124
|
return null;
|
|
143
125
|
}
|
|
144
|
-
|
|
145
126
|
return name.slice(prefix.length);
|
|
146
127
|
}
|
|
147
128
|
function parse(payload) {
|
|
@@ -151,20 +132,16 @@ function parse(payload) {
|
|
|
151
132
|
value: {},
|
|
152
133
|
error: []
|
|
153
134
|
};
|
|
154
|
-
|
|
155
135
|
try {
|
|
156
136
|
var _loop = function _loop(name, value) {
|
|
157
137
|
var submissionType = getSubmissionType(name);
|
|
158
|
-
|
|
159
138
|
if (submissionType) {
|
|
160
139
|
if (typeof value !== 'string') {
|
|
161
140
|
throw new Error('The conform command could not be used on a file input');
|
|
162
141
|
}
|
|
163
|
-
|
|
164
142
|
if (hasCommand) {
|
|
165
143
|
throw new Error('The conform command could only be set on a button');
|
|
166
144
|
}
|
|
167
|
-
|
|
168
145
|
submission = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, submission), {}, {
|
|
169
146
|
type: submissionType,
|
|
170
147
|
intent: value
|
|
@@ -176,16 +153,13 @@ function parse(payload) {
|
|
|
176
153
|
if (prev) {
|
|
177
154
|
throw new Error('Entry with the same name is not supported');
|
|
178
155
|
}
|
|
179
|
-
|
|
180
156
|
return value;
|
|
181
157
|
});
|
|
182
158
|
}
|
|
183
159
|
};
|
|
184
|
-
|
|
185
160
|
for (var [name, value] of payload.entries()) {
|
|
186
161
|
_loop(name, value);
|
|
187
162
|
}
|
|
188
|
-
|
|
189
163
|
switch (submission.type) {
|
|
190
164
|
case 'list':
|
|
191
165
|
submission = handleList(submission);
|
|
@@ -194,17 +168,14 @@ function parse(payload) {
|
|
|
194
168
|
} catch (e) {
|
|
195
169
|
submission.error.push(['', e instanceof Error ? e.message : 'Invalid payload received']);
|
|
196
170
|
}
|
|
197
|
-
|
|
198
171
|
return submission;
|
|
199
172
|
}
|
|
200
173
|
function parseListCommand(data) {
|
|
201
174
|
try {
|
|
202
175
|
var command = JSON.parse(data);
|
|
203
|
-
|
|
204
176
|
if (typeof command.type !== 'string' || !['prepend', 'append', 'replace', 'remove', 'reorder'].includes(command.type)) {
|
|
205
177
|
throw new Error('Unsupported list command type');
|
|
206
178
|
}
|
|
207
|
-
|
|
208
179
|
return command;
|
|
209
180
|
} catch (error) {
|
|
210
181
|
throw new Error("Invalid list command: \"".concat(data, "\"; ").concat(error));
|
|
@@ -217,47 +188,38 @@ function updateList(list, command) {
|
|
|
217
188
|
list.unshift(command.payload.defaultValue);
|
|
218
189
|
break;
|
|
219
190
|
}
|
|
220
|
-
|
|
221
191
|
case 'append':
|
|
222
192
|
{
|
|
223
193
|
list.push(command.payload.defaultValue);
|
|
224
194
|
break;
|
|
225
195
|
}
|
|
226
|
-
|
|
227
196
|
case 'replace':
|
|
228
197
|
{
|
|
229
198
|
list.splice(command.payload.index, 1, command.payload.defaultValue);
|
|
230
199
|
break;
|
|
231
200
|
}
|
|
232
|
-
|
|
233
201
|
case 'remove':
|
|
234
202
|
list.splice(command.payload.index, 1);
|
|
235
203
|
break;
|
|
236
|
-
|
|
237
204
|
case 'reorder':
|
|
238
205
|
list.splice(command.payload.to, 0, ...list.splice(command.payload.from, 1));
|
|
239
206
|
break;
|
|
240
|
-
|
|
241
207
|
default:
|
|
242
208
|
throw new Error('Unknown list command received');
|
|
243
209
|
}
|
|
244
|
-
|
|
245
210
|
return list;
|
|
246
211
|
}
|
|
247
212
|
function handleList(submission) {
|
|
248
213
|
var _submission$intent;
|
|
249
|
-
|
|
250
214
|
if (submission.type !== 'list') {
|
|
251
215
|
return submission;
|
|
252
216
|
}
|
|
253
|
-
|
|
254
217
|
var command = parseListCommand((_submission$intent = submission.intent) !== null && _submission$intent !== void 0 ? _submission$intent : '');
|
|
255
218
|
var paths = getPaths(command.scope);
|
|
256
219
|
setValue(submission.value, paths, list => {
|
|
257
220
|
if (!Array.isArray(list)) {
|
|
258
221
|
throw new Error('The list command can only be applied to a list');
|
|
259
222
|
}
|
|
260
|
-
|
|
261
223
|
return updateList(list, command);
|
|
262
224
|
});
|
|
263
225
|
return submission;
|
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
function ownKeys(object, enumerableOnly) {
|
|
2
2
|
var keys = Object.keys(object);
|
|
3
|
-
|
|
4
3
|
if (Object.getOwnPropertySymbols) {
|
|
5
4
|
var symbols = Object.getOwnPropertySymbols(object);
|
|
6
5
|
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
|
7
6
|
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
8
7
|
})), keys.push.apply(keys, symbols);
|
|
9
8
|
}
|
|
10
|
-
|
|
11
9
|
return keys;
|
|
12
10
|
}
|
|
13
|
-
|
|
14
11
|
function _objectSpread2(target) {
|
|
15
12
|
for (var i = 1; i < arguments.length; i++) {
|
|
16
13
|
var source = null != arguments[i] ? arguments[i] : {};
|
|
@@ -20,10 +17,8 @@ function _objectSpread2(target) {
|
|
|
20
17
|
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
21
18
|
});
|
|
22
19
|
}
|
|
23
|
-
|
|
24
20
|
return target;
|
|
25
21
|
}
|
|
26
|
-
|
|
27
22
|
function _defineProperty(obj, key, value) {
|
|
28
23
|
if (key in obj) {
|
|
29
24
|
Object.defineProperty(obj, key, {
|
|
@@ -35,7 +30,6 @@ function _defineProperty(obj, key, value) {
|
|
|
35
30
|
} else {
|
|
36
31
|
obj[key] = value;
|
|
37
32
|
}
|
|
38
|
-
|
|
39
33
|
return obj;
|
|
40
34
|
}
|
|
41
35
|
|
package/module/index.js
CHANGED
|
@@ -8,32 +8,25 @@ function getFormElements(form) {
|
|
|
8
8
|
}
|
|
9
9
|
function getPaths(name) {
|
|
10
10
|
var pattern = /(\w*)\[(\d+)\]/;
|
|
11
|
-
|
|
12
11
|
if (!name) {
|
|
13
12
|
return [];
|
|
14
13
|
}
|
|
15
|
-
|
|
16
14
|
return name.split('.').flatMap(key => {
|
|
17
15
|
var matches = pattern.exec(key);
|
|
18
|
-
|
|
19
16
|
if (!matches) {
|
|
20
17
|
return key;
|
|
21
18
|
}
|
|
22
|
-
|
|
23
19
|
if (matches[1] === '') {
|
|
24
20
|
return Number(matches[2]);
|
|
25
21
|
}
|
|
26
|
-
|
|
27
22
|
return [matches[1], Number(matches[2])];
|
|
28
23
|
});
|
|
29
24
|
}
|
|
30
25
|
function getFormData(form, submitter) {
|
|
31
26
|
var payload = new FormData(form);
|
|
32
|
-
|
|
33
27
|
if (submitter !== null && submitter !== void 0 && submitter.name) {
|
|
34
28
|
payload.append(submitter.name, submitter.value);
|
|
35
29
|
}
|
|
36
|
-
|
|
37
30
|
return payload;
|
|
38
31
|
}
|
|
39
32
|
function getName(paths) {
|
|
@@ -41,11 +34,9 @@ function getName(paths) {
|
|
|
41
34
|
if (typeof path === 'number') {
|
|
42
35
|
return "".concat(name, "[").concat(path, "]");
|
|
43
36
|
}
|
|
44
|
-
|
|
45
37
|
if (name === '' || path === '') {
|
|
46
38
|
return [name, path].join('');
|
|
47
39
|
}
|
|
48
|
-
|
|
49
40
|
return [name, path].join('.');
|
|
50
41
|
}, '');
|
|
51
42
|
}
|
|
@@ -60,11 +51,9 @@ function hasError(error, name) {
|
|
|
60
51
|
}
|
|
61
52
|
function setFormError(form, submission) {
|
|
62
53
|
var firstErrorByName = Object.fromEntries([...submission.error].reverse());
|
|
63
|
-
|
|
64
54
|
for (var element of form.elements) {
|
|
65
55
|
if (isFieldElement(element)) {
|
|
66
56
|
var error = firstErrorByName[element.name];
|
|
67
|
-
|
|
68
57
|
if (typeof error !== 'undefined' || shouldValidate(submission, element.name)) {
|
|
69
58
|
element.setCustomValidity(error !== null && error !== void 0 ? error : '');
|
|
70
59
|
}
|
|
@@ -76,10 +65,8 @@ function setValue(target, paths, valueFn) {
|
|
|
76
65
|
var lastIndex = length - 1;
|
|
77
66
|
var index = -1;
|
|
78
67
|
var pointer = target;
|
|
79
|
-
|
|
80
68
|
while (pointer != null && ++index < length) {
|
|
81
69
|
var _pointer$key;
|
|
82
|
-
|
|
83
70
|
var key = paths[index];
|
|
84
71
|
var next = paths[index + 1];
|
|
85
72
|
var newValue = index != lastIndex ? (_pointer$key = pointer[key]) !== null && _pointer$key !== void 0 ? _pointer$key : typeof next === 'number' ? [] : {} : valueFn(pointer[key]);
|
|
@@ -107,20 +94,16 @@ function requestValidate(form, field) {
|
|
|
107
94
|
}
|
|
108
95
|
function getFormElement(element) {
|
|
109
96
|
var form = element instanceof HTMLFormElement ? element : element === null || element === void 0 ? void 0 : element.form;
|
|
110
|
-
|
|
111
97
|
if (!form) {
|
|
112
98
|
return null;
|
|
113
99
|
}
|
|
114
|
-
|
|
115
100
|
return form;
|
|
116
101
|
}
|
|
117
102
|
function focusFirstInvalidField(form) {
|
|
118
103
|
var currentFocus = document.activeElement;
|
|
119
|
-
|
|
120
104
|
if (!isFieldElement(currentFocus) || currentFocus.tagName !== 'BUTTON' || currentFocus.form !== form) {
|
|
121
105
|
return;
|
|
122
106
|
}
|
|
123
|
-
|
|
124
107
|
for (var field of form.elements) {
|
|
125
108
|
if (isFieldElement(field)) {
|
|
126
109
|
// Focus on the first non button field
|
|
@@ -133,11 +116,9 @@ function focusFirstInvalidField(form) {
|
|
|
133
116
|
}
|
|
134
117
|
function getSubmissionType(name) {
|
|
135
118
|
var prefix = 'conform/';
|
|
136
|
-
|
|
137
119
|
if (!name.startsWith(prefix) || name.length <= prefix.length) {
|
|
138
120
|
return null;
|
|
139
121
|
}
|
|
140
|
-
|
|
141
122
|
return name.slice(prefix.length);
|
|
142
123
|
}
|
|
143
124
|
function parse(payload) {
|
|
@@ -147,20 +128,16 @@ function parse(payload) {
|
|
|
147
128
|
value: {},
|
|
148
129
|
error: []
|
|
149
130
|
};
|
|
150
|
-
|
|
151
131
|
try {
|
|
152
132
|
var _loop = function _loop(name, value) {
|
|
153
133
|
var submissionType = getSubmissionType(name);
|
|
154
|
-
|
|
155
134
|
if (submissionType) {
|
|
156
135
|
if (typeof value !== 'string') {
|
|
157
136
|
throw new Error('The conform command could not be used on a file input');
|
|
158
137
|
}
|
|
159
|
-
|
|
160
138
|
if (hasCommand) {
|
|
161
139
|
throw new Error('The conform command could only be set on a button');
|
|
162
140
|
}
|
|
163
|
-
|
|
164
141
|
submission = _objectSpread2(_objectSpread2({}, submission), {}, {
|
|
165
142
|
type: submissionType,
|
|
166
143
|
intent: value
|
|
@@ -172,16 +149,13 @@ function parse(payload) {
|
|
|
172
149
|
if (prev) {
|
|
173
150
|
throw new Error('Entry with the same name is not supported');
|
|
174
151
|
}
|
|
175
|
-
|
|
176
152
|
return value;
|
|
177
153
|
});
|
|
178
154
|
}
|
|
179
155
|
};
|
|
180
|
-
|
|
181
156
|
for (var [name, value] of payload.entries()) {
|
|
182
157
|
_loop(name, value);
|
|
183
158
|
}
|
|
184
|
-
|
|
185
159
|
switch (submission.type) {
|
|
186
160
|
case 'list':
|
|
187
161
|
submission = handleList(submission);
|
|
@@ -190,17 +164,14 @@ function parse(payload) {
|
|
|
190
164
|
} catch (e) {
|
|
191
165
|
submission.error.push(['', e instanceof Error ? e.message : 'Invalid payload received']);
|
|
192
166
|
}
|
|
193
|
-
|
|
194
167
|
return submission;
|
|
195
168
|
}
|
|
196
169
|
function parseListCommand(data) {
|
|
197
170
|
try {
|
|
198
171
|
var command = JSON.parse(data);
|
|
199
|
-
|
|
200
172
|
if (typeof command.type !== 'string' || !['prepend', 'append', 'replace', 'remove', 'reorder'].includes(command.type)) {
|
|
201
173
|
throw new Error('Unsupported list command type');
|
|
202
174
|
}
|
|
203
|
-
|
|
204
175
|
return command;
|
|
205
176
|
} catch (error) {
|
|
206
177
|
throw new Error("Invalid list command: \"".concat(data, "\"; ").concat(error));
|
|
@@ -213,47 +184,38 @@ function updateList(list, command) {
|
|
|
213
184
|
list.unshift(command.payload.defaultValue);
|
|
214
185
|
break;
|
|
215
186
|
}
|
|
216
|
-
|
|
217
187
|
case 'append':
|
|
218
188
|
{
|
|
219
189
|
list.push(command.payload.defaultValue);
|
|
220
190
|
break;
|
|
221
191
|
}
|
|
222
|
-
|
|
223
192
|
case 'replace':
|
|
224
193
|
{
|
|
225
194
|
list.splice(command.payload.index, 1, command.payload.defaultValue);
|
|
226
195
|
break;
|
|
227
196
|
}
|
|
228
|
-
|
|
229
197
|
case 'remove':
|
|
230
198
|
list.splice(command.payload.index, 1);
|
|
231
199
|
break;
|
|
232
|
-
|
|
233
200
|
case 'reorder':
|
|
234
201
|
list.splice(command.payload.to, 0, ...list.splice(command.payload.from, 1));
|
|
235
202
|
break;
|
|
236
|
-
|
|
237
203
|
default:
|
|
238
204
|
throw new Error('Unknown list command received');
|
|
239
205
|
}
|
|
240
|
-
|
|
241
206
|
return list;
|
|
242
207
|
}
|
|
243
208
|
function handleList(submission) {
|
|
244
209
|
var _submission$intent;
|
|
245
|
-
|
|
246
210
|
if (submission.type !== 'list') {
|
|
247
211
|
return submission;
|
|
248
212
|
}
|
|
249
|
-
|
|
250
213
|
var command = parseListCommand((_submission$intent = submission.intent) !== null && _submission$intent !== void 0 ? _submission$intent : '');
|
|
251
214
|
var paths = getPaths(command.scope);
|
|
252
215
|
setValue(submission.value, paths, list => {
|
|
253
216
|
if (!Array.isArray(list)) {
|
|
254
217
|
throw new Error('The list command can only be applied to a list');
|
|
255
218
|
}
|
|
256
|
-
|
|
257
219
|
return updateList(list, command);
|
|
258
220
|
});
|
|
259
221
|
return submission;
|
package/package.json
CHANGED