HTMLInputElement-impl.js
27.1 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
"use strict";
const conversions = require("webidl-conversions");
const DOMException = require("domexception");
const Event = require("../generated/Event");
const FileList = require("../generated/FileList");
const HTMLElementImpl = require("./HTMLElement-impl").implementation;
const idlUtils = require("../generated/utils");
const DefaultConstraintValidationImpl =
require("../constraint-validation/DefaultConstraintValidation-impl").implementation;
const ValidityState = require("../generated/ValidityState");
const { mixin } = require("../../utils");
const { domSymbolTree, cloningSteps } = require("../helpers/internal-constants");
const { closest } = require("../helpers/traversal");
const { getLabelsForLabelable } = require("../helpers/form-controls");
const {
isDisabled,
isValidEmailAddress,
isValidAbsoluteURL,
sanitizeValueByType
} = require("../helpers/form-controls");
const {
parseFloatingPointNumber,
asciiCaseInsensitiveMatch,
splitOnCommas
} = require("../helpers/strings");
const {
parseDateString,
parseLocalDateAndTimeString,
parseMonthString,
parseTimeString,
parseWeekString
} = require("../helpers/dates-and-times");
const filesSymbol = Symbol("files");
const selectAllowedTypes = new Set([
"text", "search", "tel", "url", "password", "email", "date", "month", "week",
"time", "datetime-local", "color", "file", "number"
]);
const variableLengthSelectionAllowedTypes = new Set(["text", "search", "tel", "url", "password"]);
const maxMinStepTypes = new Set(["date", "month", "week", "time", "datetime-local", "number", "range", "datetime"]);
// https://html.spec.whatwg.org/multipage/input.html#concept-input-apply
const applicableTypesForAttribute = {
max: maxMinStepTypes,
min: maxMinStepTypes,
step: maxMinStepTypes,
pattern: new Set(["text", "search", "tel", "url", "email", "password"])
};
function allowSelect(type) {
return selectAllowedTypes.has(type.toLowerCase());
}
function allowVariableLengthSelection(type) {
return variableLengthSelectionAllowedTypes.has(type.toLowerCase());
}
const valueAttributeDefaultMode = new Set(["hidden", "submit", "image", "reset", "button"]);
const valueAttributeDefaultOnMode = new Set(["checkbox", "radio"]);
function valueAttributeMode(type) {
if (valueAttributeDefaultMode.has(type)) {
return "default";
}
if (valueAttributeDefaultOnMode.has(type)) {
return "default/on";
}
if (type === "file") {
return "filename";
}
return "value";
}
// Necessary because Date.UTC() treats year within [0, 99] as [1900, 1999].
function getUTCMs(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, millisecond = 0) {
if (year > 99 || year < 0) {
return Date.UTC(year, month - 1, day, hour, minute, second, millisecond);
}
const d = new Date(0);
d.setUTCFullYear(year);
d.setUTCMonth(month - 1);
d.setUTCDate(day);
d.setUTCHours(hour);
d.setUTCMinutes(minute);
d.setUTCSeconds(second, millisecond);
return d.valueOf();
}
const dayOfWeekRelMondayLUT = [-1, 0, 1, 2, 3, -3, -2];
const convertStringToNumberByTypeMap = new Map([
[
// https://html.spec.whatwg.org/multipage/input.html#date-state-(type=date):concept-input-value-string-number
"date", input => {
const date = parseDateString(input);
if (date === null) {
return NaN;
}
return getUTCMs(date.year, date.month, date.day);
}
],
[
// https://html.spec.whatwg.org/multipage/input.html#month-state-(type=month):concept-input-value-string-number
"month", input => {
const date = parseMonthString(input);
if (date === null) {
return NaN;
}
return (date.year - 1970) * 12 + (date.month - 1);
}
],
[
// https://html.spec.whatwg.org/multipage/input.html#week-state-(type=week):concept-input-value-string-number
"week", input => {
const date = parseWeekString(input);
if (date === null) {
return NaN;
}
const dateObj = new Date(getUTCMs(date.year));
// An HTML week starts on Monday, while 0 represents Sunday. Account for such.
const dayOfWeekRelMonday = dayOfWeekRelMondayLUT[dateObj.getUTCDay()];
return dateObj.setUTCDate(1 + 7 * (date.week - 1) - dayOfWeekRelMonday);
}
],
[
// https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time):concept-input-value-string-number
"time", input => {
const time = parseTimeString(input);
if (time === null) {
return NaN;
}
return ((time.hour * 60 + time.minute) * 60 + time.second) * 1000 + time.millisecond;
}
],
[
// https://html.spec.whatwg.org/multipage/input.html#local-date-and-time-state-(type=datetime-local):concept-input-value-string-number
"datetime-local", input => {
const dateAndTime = parseLocalDateAndTimeString(input);
if (dateAndTime === null) {
return NaN;
}
const { date: { year, month, day }, time: { hour, minute, second, millisecond } } = dateAndTime;
// Doesn't quite matter whether or not UTC is used, since the offset from 1970-01-01 local time is returned.
return getUTCMs(year, month, day, hour, minute, second, millisecond);
}
],
// https://html.spec.whatwg.org/multipage/input.html#number-state-(type=number):concept-input-value-string-number
["number", parseFloatingPointNumber],
// https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range):concept-input-value-string-number
["range", parseFloatingPointNumber]
]);
class HTMLInputElementImpl extends HTMLElementImpl {
constructor(args, privateData) {
super(args, privateData);
this._selectionStart = this._selectionEnd = 0;
this._selectionDirection = "none";
this._value = null;
this._dirtyValue = false;
this._checkedness = false;
this._dirtyCheckedness = false;
// This is used to implement the canceled activation steps for radio inputs:
// "The canceled activation steps consist of setting the checkedness and the element's indeterminate IDL
// attribute back to the values they had before the pre-click activation steps were run."
this._preCheckedRadioState = null;
this.indeterminate = false;
this._customValidityErrorMessage = "";
this._labels = null;
}
// https://html.spec.whatwg.org/multipage/input.html#concept-input-value-string-number
get _convertStringToNumber() {
return convertStringToNumberByTypeMap.get(this.type);
}
_getValue() {
return this._value;
}
_preClickActivationSteps() {
if (this.type === "checkbox") {
this.checked = !this.checked;
} else if (this.type === "radio") {
this._preCheckedRadioState = this.checked;
this.checked = true;
}
}
_canceledActivationSteps() {
if (this.type === "checkbox") {
this.checked = !this.checked;
} else if (this.type === "radio") {
if (this._preCheckedRadioState !== null) {
this.checked = this._preCheckedRadioState;
this._preCheckedRadioState = null;
}
}
}
_activationBehavior() {
if (isDisabled(this)) {
return;
}
if (this.type === "checkbox" || (this.type === "radio" && !this._preCheckedRadioState)) {
const inputEvent = Event.createImpl(["input", { isTrusted: true, bubbles: true, cancelable: true }], {});
this.dispatchEvent(inputEvent);
const changeEvent = Event.createImpl(["change", { bubbles: true, cancelable: true }], {});
this.dispatchEvent(changeEvent);
} else if (this.type === "submit") {
const { form } = this;
if (form) {
form._doSubmit();
}
}
}
_attrModified(name) {
const wrapper = idlUtils.wrapperForImpl(this);
if (!this._dirtyValue && name === "value") {
this._value = sanitizeValueByType(this, wrapper.defaultValue);
}
if (!this._dirtyCheckedness && name === "checked") {
this._checkedness = wrapper.defaultChecked;
if (this._checkedness) {
this._removeOtherRadioCheckedness();
}
}
if (name === "name" || name === "type") {
if (this._checkedness) {
this._removeOtherRadioCheckedness();
}
}
super._attrModified.apply(this, arguments);
}
_formReset() {
const wrapper = idlUtils.wrapperForImpl(this);
this._value = sanitizeValueByType(this, wrapper.defaultValue);
this._dirtyValue = false;
this._checkedness = wrapper.defaultChecked;
this._dirtyCheckedness = false;
if (this._checkedness) {
this._removeOtherRadioCheckedness();
}
}
_changedFormOwner() {
if (this._checkedness) {
this._removeOtherRadioCheckedness();
}
}
get _otherRadioGroupElements() {
const wrapper = idlUtils.wrapperForImpl(this);
const root = this._radioButtonGroupRoot;
if (!root) {
return [];
}
const result = [];
const name = wrapper.name.toLowerCase();
const descendants = domSymbolTree.treeIterator(root);
for (const candidate of descendants) {
if (candidate._radioButtonGroupRoot !== root) {
continue;
}
const candidateWrapper = idlUtils.wrapperForImpl(candidate);
if (!candidateWrapper.name || candidateWrapper.name.toLowerCase() !== name) {
continue;
}
if (candidate !== this) {
result.push(candidate);
}
}
return result;
}
_removeOtherRadioCheckedness() {
for (const radioGroupElement of this._otherRadioGroupElements) {
radioGroupElement._checkedness = false;
}
}
get _radioButtonGroupRoot() {
const wrapper = idlUtils.wrapperForImpl(this);
if (this.type !== "radio" || !wrapper.name) {
return null;
}
let e = domSymbolTree.parent(this);
while (e) {
// root node of this home sub tree
// or the form element we belong to
if (!domSymbolTree.parent(e) || e.nodeName.toUpperCase() === "FORM") {
return e;
}
e = domSymbolTree.parent(e);
}
return null;
}
_isRadioGroupChecked() {
if (this.checked) {
return true;
}
return this._otherRadioGroupElements.some(radioGroupElement => radioGroupElement.checked);
}
get labels() {
return getLabelsForLabelable(this);
}
get form() {
return closest(this, "form");
}
get checked() {
return this._checkedness;
}
set checked(checked) {
this._checkedness = Boolean(checked);
this._dirtyCheckedness = true;
if (this._checkedness) {
this._removeOtherRadioCheckedness();
}
}
get value() {
switch (valueAttributeMode(this.type)) {
// https://html.spec.whatwg.org/multipage/input.html#dom-input-value-value
case "value":
return this._value !== null ? this._value : "";
// https://html.spec.whatwg.org/multipage/input.html#dom-input-value-default
case "default": {
const attr = this.getAttribute("value");
return attr !== null ? attr : "";
}
// https://html.spec.whatwg.org/multipage/input.html#dom-input-value-default-on
case "default/on": {
const attr = this.getAttribute("value");
return attr !== null ? attr : "on";
}
// https://html.spec.whatwg.org/multipage/input.html#dom-input-value-filename
case "filename":
return this.files.length ? "C:\\fakepath\\" + this.files[0].name : "";
default:
throw new Error("jsdom internal error: unknown value attribute mode");
}
}
set value(val) {
switch (valueAttributeMode(this.type)) {
// https://html.spec.whatwg.org/multipage/input.html#dom-input-value-value
case "value": {
const oldValue = this._value;
if (val === null) {
this._value = null;
} else {
this._value = sanitizeValueByType(this, String(val));
}
this._dirtyValue = true;
if (oldValue !== this._value) {
this._selectionStart = 0;
this._selectionEnd = 0;
this._selectionDirection = "none";
}
break;
}
// https://html.spec.whatwg.org/multipage/input.html#dom-input-value-default
// https://html.spec.whatwg.org/multipage/input.html#dom-input-value-default-on
case "default":
case "default/on":
this.setAttribute("value", val);
break;
// https://html.spec.whatwg.org/multipage/input.html#dom-input-value-filename
case "filename":
if (val === "") {
this.files.length = 0;
} else {
throw new DOMException("This input element accepts a filename, which may only be programmatically set to " +
"the empty string.", "InvalidStateError");
}
break;
default:
throw new Error("jsdom internal error: unknown value attribute mode");
}
}
get files() {
if (this.type === "file") {
this[filesSymbol] = this[filesSymbol] || FileList.createImpl();
} else {
this[filesSymbol] = null;
}
return this[filesSymbol];
}
set files(value) {
if (this.type === "file" && value !== null) {
this[filesSymbol] = value;
}
}
get type() {
const type = this.getAttribute("type");
return type ? type.toLowerCase() : "text";
}
set type(type) {
this.setAttribute("type", type);
}
_dispatchSelectEvent() {
const event = this._ownerDocument.createEvent("HTMLEvents");
event.initEvent("select", true, true);
this.dispatchEvent(event);
}
_getValueLength() {
return typeof this.value === "string" ? this.value.length : 0;
}
select() {
if (!allowSelect(this.type)) {
return;
}
this._selectionStart = 0;
this._selectionEnd = this._getValueLength();
this._selectionDirection = "none";
this._dispatchSelectEvent();
}
get selectionStart() {
if (!allowVariableLengthSelection(this.type)) {
return null;
}
return this._selectionStart;
}
set selectionStart(start) {
if (!allowVariableLengthSelection(this.type)) {
throw new DOMException("The object is in an invalid state.", "InvalidStateError");
}
this.setSelectionRange(start, Math.max(start, this._selectionEnd), this._selectionDirection);
}
get selectionEnd() {
if (!allowVariableLengthSelection(this.type)) {
return null;
}
return this._selectionEnd;
}
set selectionEnd(end) {
if (!allowVariableLengthSelection(this.type)) {
throw new DOMException("The object is in an invalid state.", "InvalidStateError");
}
this.setSelectionRange(this._selectionStart, end, this._selectionDirection);
}
get selectionDirection() {
if (!allowVariableLengthSelection(this.type)) {
return null;
}
return this._selectionDirection;
}
set selectionDirection(dir) {
if (!allowVariableLengthSelection(this.type)) {
throw new DOMException("The object is in an invalid state.", "InvalidStateError");
}
this.setSelectionRange(this._selectionStart, this._selectionEnd, dir);
}
setSelectionRange(start, end, dir) {
if (!allowVariableLengthSelection(this.type)) {
throw new DOMException("The object is in an invalid state.", "InvalidStateError");
}
this._selectionEnd = Math.min(end, this._getValueLength());
this._selectionStart = Math.min(start, this._selectionEnd);
this._selectionDirection = dir === "forward" || dir === "backward" ? dir : "none";
this._dispatchSelectEvent();
}
setRangeText(repl, start, end, selectionMode = "preserve") {
if (!allowVariableLengthSelection(this.type)) {
throw new DOMException("The object is in an invalid state.", "InvalidStateError");
}
if (arguments.length < 2) {
start = this._selectionStart;
end = this._selectionEnd;
} else if (start > end) {
throw new DOMException("The index is not in the allowed range.", "IndexSizeError");
}
start = Math.min(start, this._getValueLength());
end = Math.min(end, this._getValueLength());
const val = this.value;
let selStart = this._selectionStart;
let selEnd = this._selectionEnd;
this.value = val.slice(0, start) + repl + val.slice(end);
const newEnd = start + this.value.length;
if (selectionMode === "select") {
this.setSelectionRange(start, newEnd);
} else if (selectionMode === "start") {
this.setSelectionRange(start, start);
} else if (selectionMode === "end") {
this.setSelectionRange(newEnd, newEnd);
} else { // preserve
const delta = repl.length - (end - start);
if (selStart > end) {
selStart += delta;
} else if (selStart > start) {
selStart = start;
}
if (selEnd > end) {
selEnd += delta;
} else if (selEnd > start) {
selEnd = newEnd;
}
this.setSelectionRange(selStart, selEnd);
}
}
set maxLength(value) {
if (value < 0) {
throw new DOMException("The index is not in the allowed range.", "IndexSizeError");
}
this.setAttribute("maxlength", String(value));
}
get maxLength() {
if (!this.hasAttribute("maxlength")) {
return 524288; // stole this from chrome
}
return parseInt(this.getAttribute("maxlength"));
}
set minLength(value) {
if (value < 0) {
throw new DOMException("The index is not in the allowed range.", "IndexSizeError");
}
this.setAttribute("minlength", String(value));
}
get minLength() {
if (!this.hasAttribute("minlength")) {
return 0;
}
return parseInt(this.getAttribute("minlength"));
}
get size() {
if (!this.hasAttribute("size")) {
return 20;
}
return parseInt(this.getAttribute("size"));
}
set size(value) {
if (value <= 0) {
throw new DOMException("The index is not in the allowed range.", "IndexSizeError");
}
this.setAttribute("size", String(value));
}
get src() {
return conversions.USVString(this.getAttribute("src"));
}
set src(value) {
this.setAttribute("src", value);
}
// https://html.spec.whatwg.org/multipage/input.html#the-min-and-max-attributes
get _minimum() {
let min = this._defaultMinimum;
const attr = this.getAttribute("min");
const convertStringToNumber = this._convertStringToNumber;
if (attr !== null && convertStringToNumber !== undefined) {
const parsed = convertStringToNumber(attr);
if (!isNaN(parsed)) {
min = parsed;
}
}
return min;
}
get _maximum() {
let max = this._defaultMaximum;
const attr = this.getAttribute("max");
const convertStringToNumber = this._convertStringToNumber;
if (attr !== null && convertStringToNumber !== undefined) {
const parsed = convertStringToNumber(attr);
if (!isNaN(parsed)) {
max = parsed;
}
}
return max;
}
get _defaultMinimum() {
if (this.type === "range") {
return 0;
}
return null;
}
get _defaultMaximum() {
if (this.type === "range") {
return 100;
}
return null;
}
get _parsedValue() {
const converter = this._convertStringToNumber;
if (converter !== undefined) {
return converter(this.value);
}
return this.value;
}
// https://html.spec.whatwg.org/multipage/input.html#attr-input-step
get _step() {
let step = this._defaultStep;
if (this.hasAttribute("step") && !asciiCaseInsensitiveMatch(this.getAttribute("step"), "any")) {
const parsedStep = parseFloatingPointNumber(this.getAttribute("step"));
if (!isNaN(parsedStep) && parsedStep > 0) {
step = parsedStep;
}
}
return step;
}
// https://html.spec.whatwg.org/multipage/input.html#concept-input-step-scale
get _stepScaleFactor() {
const dayInMilliseconds = 24 * 60 * 60 * 1000;
switch (this.type) {
case "week":
return 7 * dayInMilliseconds;
case "date":
return dayInMilliseconds;
case "datetime-local":
case "datetime":
case "time":
return 1000;
}
return 1;
}
// https://html.spec.whatwg.org/multipage/input.html#concept-input-step-default
get _defaultStep() {
if (this.type === "datetime-local" || this.type === "datetime" || this.type === "time") {
return 60;
}
return 1;
}
// https://html.spec.whatwg.org/multipage/input.html#concept-input-min-zero
get _stepBase() {
const parseAttribute = attributeName => parseFloatingPointNumber(this.getAttribute(attributeName));
if (this.hasAttribute("min")) {
const min = parseAttribute("min");
if (!isNaN(min)) {
return min;
}
}
if (this.hasAttribute("value")) {
const value = parseAttribute("value");
if (!isNaN(value)) {
return value;
}
}
return this._defaultStepBase;
}
// https://html.spec.whatwg.org/multipage/input.html#concept-input-step-default-base
get _defaultStepBase() {
if (this.type === "week") {
// The start of week 1970-W01
return 259200000;
}
return 0;
}
_attributeApplies(attribute) {
return applicableTypesForAttribute[attribute].has(this.type);
}
_barredFromConstraintValidationSpecialization() {
// https://html.spec.whatwg.org/multipage/input.html#hidden-state-(type=hidden)
// https://html.spec.whatwg.org/multipage/input.html#reset-button-state-(type=reset)
// https://html.spec.whatwg.org/multipage/input.html#button-state-(type=button)
const willNotValidateTypes = new Set(["hidden", "reset", "button"]);
// https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly
const readOnly = this.hasAttribute("readonly");
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled
return willNotValidateTypes.has(this.type) || readOnly;
}
get validity() {
if (!this._validity) {
this._validity = ValidityState.createImpl(this, {
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-being-missing
valueMissing: () => {
if (!this.hasAttribute("required")) {
return false;
}
if (this.type === "checkbox") {
// https://html.spec.whatwg.org/multipage/input.html#checkbox-state-(type=checkbox)
// Constraint validation: If the element is required and its checkedness is
// false, then the element is suffering from being missing.
return !this.checked;
} else if (this.type === "radio") {
// https://html.spec.whatwg.org/multipage/input.html#radio-button-state-(type=radio)
// Constraint validation: If an element in the radio button group is required,
// and all of the input elements in the radio button group have a checkedness
// that is false, then the element is suffering from being missing.
return !this._isRadioGroupChecked();
}
return this.value === "";
},
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-being-too-long
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-maxlength
// jsdom has no way at the moment to emulate a user interaction, so tooLong/tooShort have
// to be set to false.
tooLong: () => false,
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-being-too-short
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-minlength
tooShort: () => false,
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-an-overflow
// https://html.spec.whatwg.org/multipage/input.html#attr-input-max
rangeOverflow: () => this._attributeApplies("max") && this._maximum !== null &&
this._parsedValue > this._maximum,
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-an-underflow
// https://html.spec.whatwg.org/multipage/input.html#attr-input-min
rangeUnderflow: () => this._attributeApplies("min") && this._minimum !== null &&
this._parsedValue < this._minimum,
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-a-pattern-mismatch
patternMismatch: () => {
if (!this.hasAttribute("pattern") || !this._attributeApplies("pattern") || this.value === "") {
return false;
}
let regExp;
try {
regExp = new RegExp(this.getAttribute("pattern"), "u");
} catch (e) {
return false;
}
if (this.type === "email" && this.hasAttribute("multiple")) {
return splitOnCommas(this.value).every(value => regExp.test(value));
}
return !regExp.test(this.value);
},
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-a-step-mismatch
// https://html.spec.whatwg.org/multipage/input.html#attr-input-step
stepMismatch: () => {
// Constraint validation: When the element has an allowed value step, and the result of applying
// the algorithm to convert a string to a number to the string given by the element's value is a
// number, and that number subtracted from the step base is not an integral multiple of the
// allowed value step, the element is suffering from a step mismatch.
if (!this._attributeApplies("step")) {
return false;
}
const step = parseFloatingPointNumber(this.getAttribute("step"));
if (isNaN(step) || step <= 0) {
return false;
}
let number = this._parsedValue;
if (isNaN(number) || this.value === "") {
return false;
}
if (this._type === "month") {
number = parseMonthString(this.value).month - 1;
}
return number % (this._stepBase - (this._step * this._stepScaleFactor)) !== 0;
},
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-a-type-mismatch
typeMismatch: () => {
if (this.value === "") {
return false;
}
if (this.type === "email") {
// https://html.spec.whatwg.org/multipage/input.html#e-mail-state-(type=email)
// Constraint validation [multiple=false]: While the value of the element is neither the empty
// string nor a single valid e - mail address, the element is suffering from a type mismatch.
// Constraint validation [multiple=true]: While the value of the element is not a valid e-mail address list,
// the element is suffering from a type mismatch.
return !isValidEmailAddress(this.value, this.hasAttribute("multiple"));
} else if (this.type === "url") {
// https://html.spec.whatwg.org/multipage/input.html#url-state-(type=url)
// Constraint validation: While the value of the element is neither the empty string
// nor a valid absolute URL, the element is suffering from a type mismatch.
return !isValidAbsoluteURL(this.value);
}
return false;
}
});
}
return this._validity;
}
[cloningSteps](copy, node) {
copy._value = node._value;
copy._checkedness = node._checkedness;
copy._dirtyValue = node._dirtyValue;
copy._dirtyCheckedness = node._dirtyCheckedness;
}
}
mixin(HTMLInputElementImpl.prototype, DefaultConstraintValidationImpl.prototype);
module.exports = {
implementation: HTMLInputElementImpl
};