Skip to content

Commit 86cff48

Browse files
committed
Fixed last of failing tests around shifting with some awful code.
1 parent 26da479 commit 86cff48

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

src/jquery.maskedinput.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
break
7575
}
7676
buffer.splice(i, 1);
77-
var result= this.apply(buffer.join(''), i);
77+
var result= this.apply(buffer.join(''), i, true);
7878
result.pos=i;
7979
return result;
8080
}
@@ -86,15 +86,17 @@
8686
break
8787
}
8888
buffer.splice(i, 1);
89-
var result=this.apply(buffer.join(''), i);
89+
var result=this.apply(buffer.join(''), i, true);
9090
result.pos=i;
9191
return result;
9292
}
9393

94-
FixedWidthMask.prototype.apply = function(input, caretPosition){
94+
FixedWidthMask.prototype.apply = function(inputString, caretPosition, doShift){
9595
if(caretPosition == null)
9696
caretPosition = this.length;
97-
var buffer=[],
97+
98+
var input=inputString.split(''),
99+
buffer=[],
98100
raw=[],
99101
lastMatch = -1,
100102
i,
@@ -108,17 +110,50 @@
108110
buffer.push(this.settings.placeholder);
109111

110112
while (pos++ < input.length) {
111-
c = input.charAt(pos - 1);
113+
c = input[pos - 1];
112114
if (action.test(c)) {
113115
buffer[i] = c;
114-
raw.push(c)
116+
raw.push(c);
115117
lastMatch = i;
116118
break;
119+
}else if(doShift){
120+
//TODO: The following is awful and needs to be refactored.
121+
var tests=$.map(this.tests.slice(i + 1),function(test, offset){
122+
var index = pos - 1 + offset;
123+
if(test.test && input[index] != null){
124+
return {regex:test,char:input[index]};
125+
}
126+
});
127+
128+
if(tests.length){
129+
var newInput = [],canShift = true;
130+
tests.unshift({regex: action});
131+
for(var j = 1; j < tests.length; j++){
132+
if(!tests[j-1].regex.test(tests[j].char)){
133+
canShift = false;
134+
break;
135+
}
136+
newInput.push(tests[j].char);
137+
}
138+
}
139+
140+
if(canShift){
141+
//Everything to the right can shift left and still match.
142+
input = newInput;
143+
buffer[i] = input[0];
144+
pos = 1;
145+
}else{
146+
//Retry current char at next position leaving a blank.
147+
pos--;
148+
}
149+
//Only allow shift attempt to happen once.
150+
doShift = false;
151+
break;
117152
}
118153
}
119154
} else {
120155
buffer.push(action);
121-
if(action === input.charAt(pos) && i !== this.partialPosition) {
156+
if(action === input[pos] && i !== this.partialPosition) {
122157
pos++;
123158
}
124159
}

0 commit comments

Comments
 (0)