-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathform-helper.js
200 lines (183 loc) · 4.81 KB
/
form-helper.js
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
import { cssEscape } from '../../helpers/dom.js';
const formElements = {
button: true,
fieldset: true,
input: true,
object: true,
output: true,
select: true,
textarea: true
};
export const isElement = (node) => node && node.nodeType === Node.ELEMENT_NODE;
export const isCustomElement = (node) => isElement(node) && node.nodeName.indexOf('-') !== -1;
export const isCustomFormElement = (node) => isCustomElement(node) && !!node.formAssociated;
export const isNativeFormElement = (node) => {
if (!isElement(node)) {
return false;
}
const nodeName = node.nodeName.toLowerCase();
return !!formElements[nodeName];
};
const _findFormElementsHelper = (ele, eles, isFormElementPredicate, visitChildrenPredicate) => {
if (isNativeFormElement(ele) || isCustomFormElement(ele) || isFormElementPredicate(ele)) {
eles.push(ele);
}
if (visitChildrenPredicate(ele)) {
for (const child of ele.children) {
_findFormElementsHelper(child, eles, isFormElementPredicate, visitChildrenPredicate);
}
}
};
export const findFormElements = (root, isFormElementPredicate = () => false, visitChildrenPredicate = () => true) => {
const eles = [];
_findFormElementsHelper(root, eles, isFormElementPredicate, visitChildrenPredicate);
return eles;
};
const _tryGetLabelElement = ele => {
if (ele.labels && ele.labels.length > 0) {
return ele.labels[0];
}
const rootNode = ele.getRootNode();
if (!rootNode || rootNode === document) {
return null;
}
const parent = ele.parentElement;
if (parent && ele.parentElement.tagName === 'LABEL') {
return parent;
}
if (ele.id) {
const rootNode = ele.getRootNode();
return rootNode.querySelector(`label[for="${cssEscape(ele.id)}"]`);
}
return null;
};
export const tryGetLabelText = (ele) => {
const labelElement = _tryGetLabelElement(ele);
if (labelElement) {
const labelText = [...labelElement.childNodes]
.filter(node => node.nodeType === Node.TEXT_NODE)
.reduce((acc, node) => acc + node.textContent, '')
.trim();
if (labelText) {
return labelText;
}
}
if (ele.hasAttribute('aria-label')) {
const labelText = ele.getAttribute('aria-label');
if (labelText) {
return labelText;
}
}
if (ele.hasAttribute('aria-labelledby')) {
const labelledby = ele.getAttribute('aria-labelledby');
const ids = labelledby.split(' ');
const root = ele.getRootNode();
for (const id of ids) {
const label = root.getElementById(id);
if (label) {
const labelText = label.textContent.trim();
if (labelText) {
return labelText;
}
}
}
}
if (ele.hasAttribute('title')) {
const labelText = ele.getAttribute('title');
if (labelText) {
return labelText;
}
}
const tagName = ele.nodeName.toLowerCase();
if (tagName === 'button' && ele.textContent) {
const labelText = ele.textContent.trim();
if (labelText) {
return labelText;
}
}
if (tagName === 'input') {
if (ele.type === 'button' || ele.type === 'submit' || ele.type === 'reset' && ele.value) {
const labelText = ele.value;
if (labelText) {
return labelText;
}
}
if (ele.type === 'image') {
const labelText = ele.alt;
if (labelText) {
return labelText;
}
}
}
return null;
};
const _getCustomFormElementData = (node) => {
if (node.formValue instanceof Object) {
return { ...node.formValue };
}
if (node.name) {
return { [node.name]: node.formValue };
}
return {};
};
const _hasFormData = (node, submitter) => {
if (node.disabled) {
return false;
}
const tagName = node.nodeName.toLowerCase();
if (tagName === 'button' && node !== submitter) {
return false;
}
if (tagName === 'input') {
const type = node.getAttribute('type');
if ((type === 'checkbox' || type === 'radio') && !node.checked) {
return false;
}
if (type === 'submit' && node !== submitter) {
return false;
}
if (type === 'reset') {
return false;
}
}
if (tagName === 'object' || tagName === 'output') {
return false;
}
return true;
};
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#constructing-form-data-set
export const getFormElementData = (node, submitter) => {
const eleData = {};
if (!_hasFormData(node, submitter)) {
return eleData;
}
const tagName = node.nodeName.toLowerCase();
if (isCustomFormElement(node)) {
return _getCustomFormElementData(node);
}
const name = node.getAttribute('name');
if (!name) {
return eleData;
}
const type = node.getAttribute('type');
if (tagName === 'input' && type === 'file') {
eleData[name] = node.files;
return eleData;
}
eleData[name] = node.value;
return eleData;
};
export const flattenMap = (map) => {
const flattened = new Map();
for (const [key, val] of map) {
if (val instanceof Map) {
const subMap = flattenMap(val);
for (const [nestedKey, nestedVal] of subMap) {
flattened.set(nestedKey, nestedVal);
}
} else {
flattened.set(key, val);
}
}
return flattened;
};