Skip to content
This repository was archived by the owner on Dec 11, 2017. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion spec/Typing.Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ describe("Typing Specifications", function() {
expect(caret.begin).toEqual(1);
expect(caret.end).toEqual(1);
});
});
});

describe("when there are converters in place apply converters at each typing ",function(){
beforeEach(function(){
runs(function(){
input
.mask("a/a",{converters:{'a':function(c){return c.toUpperCase();}}})
.focus()
});
waits(1);
runs(function(){
input
.mashKeys("e")
.mashKeys("e")
});
})
it("should apply converter (converter is just c.toUpperCase())",function(){
expect(input).toHaveValue("E/E");
});
});
});
});
16 changes: 13 additions & 3 deletions src/jquery.maskedinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ $.mask = {
},
autoclear: true,
dataName: "rawMaskFn",
placeholder: '_'
placeholder: '_',
converters: {}
};

$.fn.extend({
Expand Down Expand Up @@ -78,25 +79,29 @@ $.fn.extend({
settings = $.extend({
autoclear: $.mask.autoclear,
placeholder: $.mask.placeholder, // Load default placeholder
converters: $.mask.converters,
completed: null
}, settings);


defs = $.mask.definitions;
tests = [];
converters = [];
partialPosition = len = mask.length;
firstNonMaskPos = null;

$.each(mask.split(""), function(i, c) {
if (c == '?') {
len--;
partialPosition = i;
} else if (defs[c]) {
tests.push(new RegExp(defs[c]));
converters.push(settings.converters[c]);
if (firstNonMaskPos === null) {
firstNonMaskPos = tests.length - 1;
}
} else {
converters.push(null);
tests.push(null);
}
});
Expand Down Expand Up @@ -239,6 +244,7 @@ $.fn.extend({
if (p < len) {
c = String.fromCharCode(k);
if (tests[p].test(c)) {
c = applyConverter(c,p);
shiftR(p);

buffer[p] = c;
Expand Down Expand Up @@ -276,20 +282,24 @@ $.fn.extend({

function writeBuffer() { input.val(buffer.join('')); }

function applyConverter(c,testIndex){
if(converters[testIndex]) return converters[testIndex].call(this,c)
return c;
}
function checkVal(allow) {
//try to place characters where they belong
var test = input.val(),
lastMatch = -1,
i,
c,
pos;

for (i = 0, pos = 0; i < len; i++) {
if (tests[i]) {
buffer[i] = settings.placeholder;
while (pos++ < test.length) {
c = test.charAt(pos - 1);
if (tests[i].test(c)) {
c = applyConverter(c,i);
buffer[i] = c;
lastMatch = i;
break;
Expand Down