/
form-controls.js
272 lines (239 loc) · 9.42 KB
/
form-controls.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"use strict";
const {
isValidFloatingPointNumber,
isValidSimpleColor,
parseFloatingPointNumber,
stripLeadingAndTrailingASCIIWhitespace,
stripNewlines,
splitOnCommas
} = require("./strings");
const {
isValidDateString,
isValidMonthString,
isValidTimeString,
isValidWeekString,
parseLocalDateAndTimeString,
serializeNormalizedDateAndTime
} = require("./dates-and-times");
const whatwgURL = require("whatwg-url");
const NodeList = require("../generated/NodeList");
const { domSymbolTree } = require("./internal-constants");
const { closest, firstChildWithLocalName } = require("./traversal");
const NODE_TYPE = require("../node-type");
const { HTML_NS } = require("./namespaces");
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-fe-disabled
exports.isDisabled = formControl => {
if (formControl.localName === "button" || formControl.localName === "input" || formControl.localName === "select" ||
formControl.localName === "textarea") {
if (formControl.hasAttributeNS(null, "disabled")) {
return true;
}
}
let e = formControl.parentNode;
while (e) {
if (e.localName === "fieldset" && e.hasAttributeNS(null, "disabled")) {
const firstLegendElementChild = firstChildWithLocalName(e, "legend");
if (!firstLegendElementChild || !firstLegendElementChild.contains(formControl)) {
return true;
}
}
e = e.parentNode;
}
return false;
};
// https://html.spec.whatwg.org/multipage/forms.html#category-listed
const listedElements = new Set(["button", "fieldset", "input", "object", "output", "select", "textarea"]);
exports.isListed = formControl => listedElements.has(formControl._localName) && formControl.namespaceURI === HTML_NS;
// https://html.spec.whatwg.org/multipage/forms.html#category-submit
const submittableElements = new Set(["button", "input", "object", "select", "textarea"]);
exports.isSubmittable = formControl => {
return submittableElements.has(formControl._localName) && formControl.namespaceURI === HTML_NS;
};
// https://html.spec.whatwg.org/multipage/forms.html#concept-submit-button
const submitButtonInputTypes = new Set(["submit", "image"]);
exports.isSubmitButton = formControl => {
return ((formControl._localName === "input" && submitButtonInputTypes.has(formControl.type)) ||
(formControl._localName === "button" && formControl.type === "submit")) &&
formControl.namespaceURI === HTML_NS;
};
// https://html.spec.whatwg.org/multipage/forms.html#concept-button
const buttonInputTypes = new Set([...submitButtonInputTypes, "reset", "button"]);
exports.isButton = formControl => {
return ((formControl._localName === "input" && buttonInputTypes.has(formControl.type)) ||
formControl._localName === "button") &&
formControl.namespaceURI === HTML_NS;
};
exports.normalizeToCRLF = string => {
return string.replace(/\r([^\n])/g, "\r\n$1")
.replace(/\r$/, "\r\n")
.replace(/([^\r])\n/g, "$1\r\n")
.replace(/^\n/, "\r\n");
};
exports.isLabelable = node => {
// labelable logic defined at: https://html.spec.whatwg.org/multipage/forms.html#category-label
if (node.nodeType !== NODE_TYPE.ELEMENT_NODE) {
return false;
}
switch (node.tagName) {
case "BUTTON":
case "METER":
case "OUTPUT":
case "PROGRESS":
case "SELECT":
case "TEXTAREA":
return true;
case "INPUT":
return node.type !== "hidden";
}
return false;
};
exports.getLabelsForLabelable = labelable => {
if (!exports.isLabelable(labelable)) {
return null;
}
if (!labelable._labels) {
const root = labelable.getRootNode({});
labelable._labels = NodeList.create(root._globalObject, [], {
element: root,
query: () => {
const nodes = [];
for (const descendant of domSymbolTree.treeIterator(root)) {
if (descendant.control === labelable) {
nodes.push(descendant);
}
}
return nodes;
}
});
}
return labelable._labels;
};
// https://html.spec.whatwg.org/multipage/forms.html#valid-e-mail-address
exports.isValidEmailAddress = (emailAddress, multiple = false) => {
const emailAddressRegExp = new RegExp("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9]" +
"(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}" +
"[a-zA-Z0-9])?)*$");
// A valid e-mail address list is a set of comma-separated tokens, where each token is itself
// a valid e - mail address.To obtain the list of tokens from a valid e - mail address list,
// an implementation must split the string on commas.
if (multiple) {
return splitOnCommas(emailAddress).every(value => emailAddressRegExp.test(value));
}
return emailAddressRegExp.test(emailAddress);
};
exports.isValidAbsoluteURL = url => {
return whatwgURL.parseURL(url) !== null;
};
exports.sanitizeValueByType = (input, val) => {
switch (input.type.toLowerCase()) {
case "password":
case "search":
case "tel":
case "text":
val = stripNewlines(val);
break;
case "color":
// https://html.spec.whatwg.org/multipage/forms.html#color-state-(type=color):value-sanitization-algorithm
val = isValidSimpleColor(val) ? val.toLowerCase() : "#000000";
break;
case "date":
// https://html.spec.whatwg.org/multipage/input.html#date-state-(type=date):value-sanitization-algorithm
if (!isValidDateString(val)) {
val = "";
}
break;
case "datetime-local": {
// https://html.spec.whatwg.org/multipage/input.html#local-date-and-time-state-(type=datetime-local):value-sanitization-algorithm
const dateAndTime = parseLocalDateAndTimeString(val);
val = dateAndTime !== null ? serializeNormalizedDateAndTime(dateAndTime) : "";
break;
}
case "email":
// https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email):value-sanitization-algorithm
// https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email):value-sanitization-algorithm-2
if (input.hasAttributeNS(null, "multiple")) {
val = val.split(",").map(token => stripLeadingAndTrailingASCIIWhitespace(token)).join(",");
} else {
val = stripNewlines(val);
val = stripLeadingAndTrailingASCIIWhitespace(val);
}
break;
case "month":
// https://html.spec.whatwg.org/multipage/input.html#month-state-(type=month):value-sanitization-algorithm
if (!isValidMonthString(val)) {
val = "";
}
break;
case "number":
// https://html.spec.whatwg.org/multipage/input.html#number-state-(type=number):value-sanitization-algorithm
// TODO: using parseFloatingPointNumber in addition to isValidFloatingPointNumber to pass number.html WPT.
// Possible spec bug.
if (!isValidFloatingPointNumber(val) || isNaN(parseFloatingPointNumber(val))) {
val = "";
}
break;
case "range":
// https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range):value-sanitization-algorithm
// TODO: using parseFloatingPointNumber in addition to isValidFloatingPointNumber to pass number.html WPT.
// Possible spec bug.
if (!isValidFloatingPointNumber(val) || isNaN(parseFloatingPointNumber(val))) {
const minimum = input._minimum;
const maximum = input._maximum;
const defaultValue = maximum < minimum ? minimum : (minimum + maximum) / 2;
val = `${defaultValue}`;
} else if (val < input._minimum) {
val = `${input._minimum}`;
} else if (val > input._maximum) {
val = `${input._maximum}`;
}
break;
case "time":
// https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time):value-sanitization-algorithm
if (!isValidTimeString(val)) {
val = "";
}
break;
case "url":
// https://html.spec.whatwg.org/multipage/forms.html#url-state-(type=url):value-sanitization-algorithm
val = stripNewlines(val);
val = stripLeadingAndTrailingASCIIWhitespace(val);
break;
case "week":
// https://html.spec.whatwg.org/multipage/input.html#week-state-(type=week):value-sanitization-algorithm
if (!isValidWeekString(val)) {
val = "";
}
}
return val;
};
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#form-owner
// TODO: The spec describes an imperative process for assigning/resetting an element's form
// owner based on activities involving form-associated elements. This simpler implementation
// instead calculates the current form owner only when the property is accessed. This is not
// sufficient to pass all the web platform tests, but is good enough for most purposes. We
// should eventually update it to use the correct version, though. See
// https://github.com/whatwg/html/issues/4050 for some discussion.
exports.formOwner = formControl => {
const formAttr = formControl.getAttributeNS(null, "form");
if (formAttr === "") {
return null;
}
if (formAttr === null) {
return closest(formControl, "form");
}
const root = formControl.getRootNode({});
let firstElementWithId;
for (const descendant of domSymbolTree.treeIterator(root)) {
if (descendant.nodeType === NODE_TYPE.ELEMENT_NODE &&
descendant.getAttributeNS(null, "id") === formAttr) {
firstElementWithId = descendant;
break;
}
}
if (firstElementWithId &&
firstElementWithId.namespaceURI === HTML_NS &&
firstElementWithId.localName === "form") {
return firstElementWithId;
}
return null;
};