-
-
Notifications
You must be signed in to change notification settings - Fork 260
/
Copy path-guard-for-maxlength.ts
52 lines (49 loc) · 1.42 KB
/
-guard-for-maxlength.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
import { FormControl } from './-is-form-control';
// ref: https://html.spec.whatwg.org/multipage/input.html#concept-input-apply
const constrainedInputTypes = [
'text',
'search',
'url',
'tel',
'email',
'password',
];
/**
@private
@param {Element} element - the element to check
@returns {boolean} `true` when the element should constrain input by the maxlength attribute, `false` otherwise
*/
function isMaxLengthConstrained(
element: Element
): element is HTMLInputElement | HTMLTextAreaElement {
return (
!!Number(element.getAttribute('maxLength')) &&
(element instanceof HTMLTextAreaElement ||
(element instanceof HTMLInputElement &&
constrainedInputTypes.indexOf(element.type) > -1))
);
}
/**
* @private
* @param {Element} element - the element to check
* @param {string} text - the text being added to element
* @param {string} testHelper - the test helper context the guard is called from (for Error message)
* @throws if `element` has `maxlength` & `value` exceeds `maxlength`
*/
export default function guardForMaxlength(
element: FormControl,
text: string,
testHelper: string
): void {
const maxlength = element.getAttribute('maxlength');
if (
isMaxLengthConstrained(element) &&
maxlength &&
text &&
text.length > Number(maxlength)
) {
throw new Error(
`Can not \`${testHelper}\` with text: '${text}' that exceeds maxlength: '${maxlength}'.`
);
}
}