diff --git a/lib/index.js b/lib/index.js index 3c7c31f..3c7eab9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -299,24 +299,32 @@ InputMask.prototype.backspace = function backspace() { var selectionBefore = copy(this.selection) var valueBefore = this.getValue() - // No range selected - work on the character preceding the cursor - if (this.selection.start === this.selection.end) { - if (this.pattern.isEditableIndex(this.selection.start - 1)) { - this.value[this.selection.start - 1] = this.placeholderChar - } - this.selection.start-- - this.selection.end-- - } // Range selected - delete characters and leave the cursor at the start of the selection - else { + if (this.selection.start !== this.selection.end) { var end = this.selection.end - 1 - while (end >= this.selection.start) { + while (end > this.selection.start) { if (this.pattern.isEditableIndex(end)) { this.value[end] = this.placeholderChar } end-- } - this.selection.end = this.selection.start + + this.selection.start = this.selection.end = (this.selection.start + 1); + } + // No range selected - work on the character preceding the cursor + if (this.selection.start === this.selection.end) { + var current = this.selection.start - 1; + var deleted = false; + while (current >= this.pattern.firstEditableIndex && (!this.pattern.isEditableIndex(current) || !deleted)) { + if(this.pattern.isEditableIndex(current)){ + this.value[current] = this.placeholderChar; + deleted = true; + } + + current--; + } + + this.selection.start = this.selection.end = current + 1; } // History