Skip to content

Commit 4c13af2

Browse files
duxorNepipenkoIgor
authored andcommitted
fix: limit selection to actual input length (JsDaddy#655)
1 parent dbc41b9 commit 4c13af2

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

projects/ngx-mask-lib/src/lib/mask.directive.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,11 @@ export class MaskDirective implements ControlValueAccessor, OnChanges {
284284
el.selectionStart = this._maskService.prefix.length;
285285
return;
286286
}
287+
288+
/** select only inserted text */
289+
if ((el.selectionEnd as number) > this._getActualInputLength()) {
290+
el.selectionEnd = this._getActualInputLength();
291+
}
287292
}
288293

289294
// tslint:disable-next-line: cyclomatic-complexity
@@ -348,6 +353,12 @@ export class MaskDirective implements ControlValueAccessor, OnChanges {
348353
this._inputValue.length - this.suffix.length < (el.selectionStart as number)
349354
) {
350355
el.setSelectionRange(this._inputValue.length - this.suffix.length, this._inputValue.length);
356+
} else if (
357+
(e.keyCode === 65 && e.ctrlKey === true) || // Ctrl+ A
358+
(e.keyCode === 65 && e.metaKey === true) // Cmd + A (Mac)
359+
) {
360+
el.setSelectionRange(0, this._getActualInputLength());
361+
e.preventDefault();
351362
}
352363
this._maskService.selStart = el.selectionStart;
353364
this._maskService.selEnd = el.selectionEnd;
@@ -450,4 +461,9 @@ export class MaskDirective implements ControlValueAccessor, OnChanges {
450461
}
451462
return null;
452463
}
464+
465+
private _getActualInputLength() {
466+
return this._maskService.actualValue.length ||
467+
this._maskService.actualValue.length + this._maskService.prefix.length;
468+
}
453469
}

0 commit comments

Comments
 (0)