Permalink
Browse files

Fix trailing format char bugs for persistent false.

1 parent ff4d23b commit 1239bc92510f7b58a0e7442adf28f6bf7d2d5ada @mickeyreiss mickeyreiss committed Mar 18, 2014
Showing with 71 additions and 22 deletions.
  1. +17 −6 lib/formatter.js
  2. +1 −1 lib/formatter.min.js
  3. +17 −6 lib/jquery.formatter.js
  4. +1 −1 lib/jquery.formatter.min.js
  5. +17 −6 src/formatter.js
  6. +18 −2 test/formatter.js
View
@@ -245,8 +245,17 @@ Formatter.prototype._processKey = function (chars, delKey,ingoreCaret) {
// or Backspace and not at start
} else if (delKey && this.sel.begin - 1 >= 0) {
- this.val = utils.removeChars(this.val, this.sel.end -1, this.sel.end);
- this.delta = -1;
+
+ // Always have a delta of at least -1 for the character being deleted.
+ this.delta -= 1;
+
+ // Count number of additional format chars to be deleted. (A group of multiple format chars should be deleted like one value char.)
+ while (this.chars[this.focus-1]) {
+ this.delta--;
+ this.focus--;
+ }
+
+ this.val = utils.removeChars(this.val, this.sel.end + this.delta, this.sel.end);
// or Backspace and at start - exit
} else if (delKey) {
@@ -310,6 +319,7 @@ Formatter.prototype._formatValue = function (ignoreCaret) {
// Validate inputs
this._validateInpts();
+
// Add formatted characters
this._addChars();
@@ -407,11 +417,12 @@ Formatter.prototype._addChars = function () {
this.focus++;
}
} else {
- // Avoid caching val.length, as it changes during manipulations
+ // Avoid caching val.length and this.focus, as they may change in _addChar.
for (var j = 0; j <= this.val.length; j++) {
- // When moving backwards there are some race conditions where we
- // dont want to add the character
- if (this.delta <= 0 && (j == this.focus)) { return true; }
+ // When moving backwards, i.e. delting characters, don't add format characters past focus point.
+ if ( (this.delta <= 0 && j === this.focus && this.chars[j] === undefined) || (this.focus === 0) ) { return true; }
+
+ // Place character in current position of the formatted string.
this._addChar(j);
}
}
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -241,8 +241,17 @@ Formatter.prototype._processKey = function (chars, delKey,ingoreCaret) {
// or Backspace and not at start
} else if (delKey && this.sel.begin - 1 >= 0) {
- this.val = utils.removeChars(this.val, this.sel.end -1, this.sel.end);
- this.delta = -1;
+
+ // Always have a delta of at least -1 for the character being deleted.
+ this.delta -= 1;
+
+ // Count number of additional format chars to be deleted. (A group of multiple format chars should be deleted like one value char.)
+ while (this.chars[this.focus-1]) {
+ this.delta--;
+ this.focus--;
+ }
+
+ this.val = utils.removeChars(this.val, this.sel.end + this.delta, this.sel.end);
// or Backspace and at start - exit
} else if (delKey) {
@@ -306,6 +315,7 @@ Formatter.prototype._formatValue = function (ignoreCaret) {
// Validate inputs
this._validateInpts();
+
// Add formatted characters
this._addChars();
@@ -403,11 +413,12 @@ Formatter.prototype._addChars = function () {
this.focus++;
}
} else {
- // Avoid caching val.length, as it changes during manipulations
+ // Avoid caching val.length and this.focus, as they may change in _addChar.
for (var j = 0; j <= this.val.length; j++) {
- // When moving backwards there are some race conditions where we
- // dont want to add the character
- if (this.delta <= 0 && (j == this.focus)) { return true; }
+ // When moving backwards, i.e. delting characters, don't add format characters past focus point.
+ if ( (this.delta <= 0 && j === this.focus && this.chars[j] === undefined) || (this.focus === 0) ) { return true; }
+
+ // Place character in current position of the formatted string.
this._addChar(j);
}
}
Oops, something went wrong.

0 comments on commit 1239bc9

Please sign in to comment.