Skip to content

Commit 0059efb

Browse files
authored
fix(material/input): error for some input types on iOS (#22749)
`MatInput` has a `keyup` listener which calls `setSelectionRange` in order to work around an issue in iOS. The problem is that some input types don't allow the selection range to be updated and they throw an error if somebody tries to do it. These changes avoid the errors by detecting whether the input supports setting the selection range. Fixes #22726.
1 parent 70c050c commit 0059efb

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/material/input/input.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,15 @@ export class MatInput extends _MatInputBase implements MatFormFieldControl<any>,
266266
if (_platform.IOS) {
267267
ngZone.runOutsideAngular(() => {
268268
_elementRef.nativeElement.addEventListener('keyup', (event: Event) => {
269-
let el = event.target as HTMLInputElement;
270-
if (!el.value && !el.selectionStart && !el.selectionEnd) {
269+
const el = event.target as HTMLInputElement;
270+
271+
// Note: We specifically check for 0, rather than `!el.selectionStart`, because the two
272+
// indicate different things. If the value is 0, it means that the caret is at the start
273+
// of the input, whereas a value of `null` means that the input doesn't support
274+
// manipulating the selection range. Inputs that don't support setting the selection range
275+
// will throw an error so we want to avoid calling `setSelectionRange` on them. See:
276+
// https://html.spec.whatwg.org/multipage/input.html#do-not-apply
277+
if (!el.value && el.selectionStart === 0 && el.selectionEnd === 0) {
271278
// Note: Just setting `0, 0` doesn't fix the issue. Setting
272279
// `1, 1` fixes it for the first time that you type text and
273280
// then hold delete. Toggling to `1, 1` and then back to

0 commit comments

Comments
 (0)