From 6b57bbb4a09f25f584d6cf8be10f864989ac667a Mon Sep 17 00:00:00 2001 From: Victor Homyakov Date: Fri, 27 May 2011 08:40:43 -0700 Subject: [PATCH 001/116] Simplified var declaration and checkVal() code --- src/jquery.maskedinput.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index af67604..2a0f2c6 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -60,9 +60,8 @@ var defs = $.mask.definitions; var tests = []; - var partialPosition = mask.length; + var len = mask.length, partialPosition = len; var firstNonMaskPos = null; - var len = mask.length; $.each(mask.split(""), function(i, c) { if (c == '?') { @@ -205,12 +204,14 @@ lastMatch = i; } } - if (!allow && lastMatch + 1 < partialPosition) { + if (allow) { + writeBuffer(); + } else if (lastMatch + 1 < partialPosition) { input.val(""); clearBuffer(0, len); - } else if (allow || lastMatch + 1 >= partialPosition) { + } else { writeBuffer(); - if (!allow) input.val(input.val().substring(0, lastMatch + 1)); + input.val(input.val().substring(0, lastMatch + 1)); } return (partialPosition ? i : firstNonMaskPos); }; From 40218dfd0fc552bdb22f0637c028deefb7228baf Mon Sep 17 00:00:00 2001 From: "Neri J. Jakubowski Junior" Date: Thu, 28 Jun 2012 10:33:17 -0300 Subject: [PATCH 002/116] Added support to change default placeholder. use: $.mask.placeholder = '-'; P.S.This way $(element).mask('99-99',{placeholder: '_'}); is valid too --- src/jquery.maskedinput.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index af67604..a271176 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -7,7 +7,7 @@ (function($) { var pasteEventName = ($.browser.msie ? 'paste' : 'input') + ".mask"; var iPhone = (window.orientation != undefined); - + $.mask = { //Predefined character definitions definitions: { @@ -15,7 +15,8 @@ 'a': "[A-Za-z]", '*': "[A-Za-z0-9]" }, - dataName:"rawMaskFn" + dataName: "rawMaskFn", + placeholder: '_', }; $.fn.extend({ @@ -54,7 +55,7 @@ return input.data($.mask.dataName)(); } settings = $.extend({ - placeholder: "_", + placeholder: $.mask.placeholder, // Load default placeholder completed: null }, settings); From 6008114f64bd377fa11d2e5b4f5b87803efe5c0c Mon Sep 17 00:00:00 2001 From: norlin Date: Wed, 5 Sep 2012 16:30:27 +0400 Subject: [PATCH 003/116] Refactoring a bit of good code formatting --- src/jquery.maskedinput.js | 213 +++++++++++++++++++++++++------------- 1 file changed, 141 insertions(+), 72 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index af67604..f228194 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -1,12 +1,12 @@ /* Masked Input plugin for jQuery Copyright (c) 2007-@Year Josh Bush (digitalbush.com) - Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) + Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) Version: @version */ (function($) { - var pasteEventName = ($.browser.msie ? 'paste' : 'input') + ".mask"; - var iPhone = (window.orientation != undefined); + var pasteEventName = ($.browser.msie ? 'paste' : 'input') + ".mask", + iPhone = (window.orientation !== undefined); $.mask = { //Predefined character definitions @@ -21,14 +21,19 @@ $.fn.extend({ //Helper Function for Caret positioning caret: function(begin, end) { - if (this.length == 0) return; + var range; + + if (this.length === 0) { + return; + } + if (typeof begin == 'number') { - end = (typeof end == 'number') ? end : begin; + end = (typeof end === 'number') ? end : begin; return this.each(function() { if (this.setSelectionRange) { this.setSelectionRange(begin, end); } else if (this.createTextRange) { - var range = this.createTextRange(); + range = this.createTextRange(); range.collapse(true); range.moveEnd('character', end); range.moveStart('character', begin); @@ -40,17 +45,26 @@ begin = this[0].selectionStart; end = this[0].selectionEnd; } else if (document.selection && document.selection.createRange) { - var range = document.selection.createRange(); + range = document.selection.createRange(); begin = 0 - range.duplicate().moveStart('character', -100000); end = begin + range.text.length; } return { begin: begin, end: end }; } }, - unmask: function() { return this.trigger("unmask"); }, + unmask: function() { + return this.trigger("unmask"); + }, mask: function(mask, settings) { + var input, + defs, + tests, + partialPosition, + firstNonMaskPos, + len; + if (!mask && this.length > 0) { - var input = $(this[0]); + input = $(this[0]); return input.data($.mask.dataName)(); } settings = $.extend({ @@ -58,11 +72,11 @@ completed: null }, settings); - var defs = $.mask.definitions; - var tests = []; - var partialPosition = mask.length; - var firstNonMaskPos = null; - var len = mask.length; + defs = $.mask.definitions; + tests = []; + partialPosition = mask.length; + firstNonMaskPos = null; + len = mask.length; $.each(mask.split(""), function(i, c) { if (c == '?') { @@ -70,73 +84,102 @@ partialPosition = i; } else if (defs[c]) { tests.push(new RegExp(defs[c])); - if(firstNonMaskPos==null) - firstNonMaskPos = tests.length - 1; + if (firstNonMaskPos === null) { + firstNonMaskPos = tests.length - 1; + } } else { tests.push(null); } }); return this.trigger("unmask").each(function() { - var input = $(this); - var buffer = $.map(mask.split(""), function(c, i) { if (c != '?') return defs[c] ? settings.placeholder : c }); - var focusText = input.val(); + var input = $(this), + buffer = $.map( + mask.split(""), + function(c, i) { + if (c != '?') { + return defs[c] ? settings.placeholder : c; + } + }), + focusText = input.val(); function seekNext(pos) { while (++pos <= len && !tests[pos]); return pos; - }; + } + function seekPrev(pos) { while (--pos >= 0 && !tests[pos]); return pos; - }; + } function shiftL(begin,end) { - if(begin<0) - return; - for (var i = begin,j = seekNext(end); i < len; i++) { + var i, + j; + + if (begin<0) { + return; + } + + for (i = begin, j = seekNext(end); i < len; i++) { if (tests[i]) { if (j < len && tests[i].test(buffer[j])) { buffer[i] = buffer[j]; buffer[j] = settings.placeholder; - } else + } else { break; + } + j = seekNext(j); } } writeBuffer(); input.caret(Math.max(firstNonMaskPos, begin)); - }; + } function shiftR(pos) { - for (var i = pos, c = settings.placeholder; i < len; i++) { + var i, + c, + j, + t; + + for (i = pos, c = settings.placeholder; i < len; i++) { if (tests[i]) { - var j = seekNext(i); - var t = buffer[i]; + j = seekNext(i); + t = buffer[i]; buffer[i] = c; - if (j < len && tests[j].test(t)) + if (j < len && tests[j].test(t)) { c = t; - else + } else { break; + } } } - }; + } function keydownEvent(e) { - var k=e.which; + var k = e.which, + pos, + begin, + end; //backspace, delete, and escape get special treatment - if(k == 8 || k == 46 || (iPhone && k == 127)){ - var pos = input.caret(), - begin = pos.begin, - end = pos.end; - - if(end-begin==0){ - begin=k!=46?seekPrev(begin):(end=seekNext(begin-1)); - end=k==46?seekNext(end):end; + if (k === 8 || k === 46 || (iPhone && k === 127)) { + pos = input.caret(); + begin = pos.begin; + end = pos.end; + + if (end - begin === 0) { + if (k !== 46) { + begin = seekPrev(begin); + end = seekNext(begin - 1); + } else { + begin = seekNext(begin - 1); + end = seekNext(end); + } } clearBuffer(begin, end); - shiftL(begin,end-1); + shiftL(begin, end - 1); return false; } else if (k == 27) {//escape @@ -144,63 +187,77 @@ input.caret(0, checkVal()); return false; } - }; + } function keypressEvent(e) { var k = e.which, - pos = input.caret(); - if (e.ctrlKey || e.altKey || e.metaKey || k<32) {//Ignore + pos = input.caret(), + p, + c, + next; + + if (e.ctrlKey || e.altKey || e.metaKey || k < 32) {//Ignore return true; } else if (k) { - if(pos.end-pos.begin!=0){ + if (pos.end - pos.begin !== 0){ clearBuffer(pos.begin, pos.end); shiftL(pos.begin, pos.end-1); } - var p = seekNext(pos.begin - 1); + p = seekNext(pos.begin - 1); if (p < len) { - var c = String.fromCharCode(k); + c = String.fromCharCode(k); if (tests[p].test(c)) { shiftR(p); + buffer[p] = c; writeBuffer(); - var next = seekNext(p); + + next = seekNext(p); input.caret(next); - if (settings.completed && next >= len) + + if (settings.completed && next >= len) { settings.completed.call(input); + } } } return false; } - }; + } function clearBuffer(start, end) { - for (var i = start; i < end && i < len; i++) { - if (tests[i]) + var i; + for (i = start; i < end && i < len; i++) { + if (tests[i]) { buffer[i] = settings.placeholder; + } } - }; + } - function writeBuffer() { return input.val(buffer.join('')).val(); }; + function writeBuffer() { return input.val(buffer.join('')).val(); } function checkVal(allow) { //try to place characters where they belong - var test = input.val(); - var lastMatch = -1; - for (var i = 0, pos = 0; i < len; i++) { + var test = input.val(), + lastMatch = -1, + i, + c; + + for (i = 0, pos = 0; i < len; i++) { if (tests[i]) { buffer[i] = settings.placeholder; while (pos++ < test.length) { - var c = test.charAt(pos - 1); + c = test.charAt(pos - 1); if (tests[i].test(c)) { buffer[i] = c; lastMatch = i; break; } } - if (pos > test.length) + if (pos > test.length) { break; - } else if (buffer[i] == test.charAt(pos) && i!=partialPosition) { + } + } else if (buffer[i] === test.charAt(pos) && i !== partialPosition) { pos++; lastMatch = i; } @@ -210,16 +267,18 @@ clearBuffer(0, len); } else if (allow || lastMatch + 1 >= partialPosition) { writeBuffer(); - if (!allow) input.val(input.val().substring(0, lastMatch + 1)); + if (!allow) { + input.val(input.val().substring(0, lastMatch + 1)); + } } return (partialPosition ? i : firstNonMaskPos); - }; + } input.data($.mask.dataName,function(){ return $.map(buffer, function(c, i) { return tests[i]&&c!=settings.placeholder ? c : null; }).join(''); - }) + }); if (!input.attr("readonly")) input @@ -229,16 +288,25 @@ .removeData($.mask.dataName); }) .bind("focus.mask", function() { + var pos, + moveCaret; + focusText = input.val(); - var pos = checkVal(); + pos = checkVal(); writeBuffer(); - var moveCaret=function(){ - if (pos == mask.length) + moveCaret = function(){ + if (pos == mask.length) { input.caret(0, pos); - else + } else { input.caret(pos); + } }; - ($.browser.msie ? moveCaret:function(){setTimeout(moveCaret,0)})(); + + if ($.browser.msie) { + moveCaret(); + } else { + window.setTimeout(moveCaret, 0); + } }) .bind("blur.mask", function() { checkVal(); @@ -248,11 +316,12 @@ .bind("keydown.mask", keydownEvent) .bind("keypress.mask", keypressEvent) .bind(pasteEventName, function() { - setTimeout(function() { input.caret(checkVal(true)); }, 0); + setTimeout(function() { + input.caret(checkVal(true)); + }, 0); }); - checkVal(); //Perform initial check for existing values }); } }); -})(jQuery); +})(jQuery); \ No newline at end of file From 86f8a2b6cd4a40b1e297e63a59c34d12c806a6db Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Sat, 22 Dec 2012 14:27:54 -0600 Subject: [PATCH 004/116] Changes broke specs for backspace and delete. Reverting how we assign begin and end on backspace/delete. --- src/jquery.maskedinput.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index f228194..bd543b1 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -170,13 +170,8 @@ end = pos.end; if (end - begin === 0) { - if (k !== 46) { - begin = seekPrev(begin); - end = seekNext(begin - 1); - } else { - begin = seekNext(begin - 1); - end = seekNext(end); - } + begin=k!==46?seekPrev(begin):(end=seekNext(begin-1)); + end=k===46?seekNext(end):end; } clearBuffer(begin, end); shiftL(begin, end - 1); From 84df03d21ad86e485c8a4b71637da60d1aa8561b Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Sat, 22 Dec 2012 23:00:56 -0600 Subject: [PATCH 005/116] updating to latest version of jQuery. --- demo/datepicker.html | 2 +- demo/index.html | 2 +- lib/jquery-1.4.4.min.js | 167 ---------------------------------------- lib/jquery-1.8.3.min.js | 2 + spec/SpecRunner.html | 2 +- 5 files changed, 5 insertions(+), 170 deletions(-) delete mode 100644 lib/jquery-1.4.4.min.js create mode 100644 lib/jquery-1.8.3.min.js diff --git a/demo/datepicker.html b/demo/datepicker.html index 0b7ce77..95aa088 100644 --- a/demo/datepicker.html +++ b/demo/datepicker.html @@ -1,7 +1,7 @@ datepicker demo - + diff --git a/demo/index.html b/demo/index.html index c371c75..3e66b89 100644 --- a/demo/index.html +++ b/demo/index.html @@ -1,7 +1,7 @@ jQuery Mask Test - + - + From d0678709ac28afecb166fc4b2d6dd52ad5ea685b Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Sat, 22 Dec 2012 23:09:48 -0600 Subject: [PATCH 006/116] updating README to explain optional inputs --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 64828ed..833e287 100644 --- a/README.md +++ b/README.md @@ -7,4 +7,6 @@ This is a masked input plugin for the jQuery javascript library. It allows a use * a - Represents an alpha character (A-Z,a-z) * 9 - Represents a numeric character (0-9) -* \* - Represents an alphanumeric character (A-Z,a-z,0-9) \ No newline at end of file +* \* - Represents an alphanumeric character (A-Z,a-z,0-9) + +By design, this plugin will reject input which doesn't complete the mask. You can bypass this by using a '?' character at the position where you would like to consider input optional. For example, a mask of "(999) 999-9999? x99999" would require only the first 10 digits of a phone number with extension being optional. \ No newline at end of file From 8aae81ff326d1a1bd6c2c6dd9f0507c96fe821bc Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Sat, 22 Dec 2012 23:20:19 -0600 Subject: [PATCH 007/116] updating README to explain custom definitions. --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 833e287..7160405 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,7 @@ This is a masked input plugin for the jQuery javascript library. It allows a use * 9 - Represents a numeric character (0-9) * \* - Represents an alphanumeric character (A-Z,a-z,0-9) -By design, this plugin will reject input which doesn't complete the mask. You can bypass this by using a '?' character at the position where you would like to consider input optional. For example, a mask of "(999) 999-9999? x99999" would require only the first 10 digits of a phone number with extension being optional. \ No newline at end of file +If your requirements aren't met by the predefined placeholders, you can always add your own. For example, maybe you need a mask to only allow hexadecimal characters. You can add your own definition for a placeholder, say 'h', like so: `$.mask.definitions['h'] = "[A-Fa-f0-9]";` Then you can use that to mask for something like css colors in hex with a mask "#hhhhhh". + +By design, this plugin will reject input which doesn't complete the mask. You can bypass this by using a '?' character at the position where you would like to consider input optional. For example, a mask of "(999) 999-9999? x99999" would require only the first 10 digits of a phone number with extension being optional. + From f318592bbca2910c61aeb4692eeb1c50c940e097 Mon Sep 17 00:00:00 2001 From: Fagner Brack Date: Mon, 2 Jul 2012 16:08:04 -0300 Subject: [PATCH 008/116] Don't manipulate caret of hidden elements Fixes #34 --- spec/Focus.Spec.js | 17 +++++++++++++++++ src/jquery.maskedinput.js | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/spec/Focus.Spec.js b/spec/Focus.Spec.js index c59645f..1eac082 100644 --- a/spec/Focus.Spec.js +++ b/spec/Focus.Spec.js @@ -34,6 +34,23 @@ feature("Focusing A Masked Input",function(){ expect(caret.end).toEqual(1); }); }); + + + scenario("Masking a hidden input",function(){ + var error; + $(window).on("error.test",function(err){error=err;}) + given("a mask on a hidden input",function(){ + input.hide().mask("9"); + + }); + when("focusing input",function(){ + input.focus(); + }); + waits(1); + then("should not throw an error",function(){ + expect(error).toBeUndefined(); + }) + }); }); feature("Leaving A Masked Input",function(){ diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 677d2bf..13c99c8 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -24,10 +24,10 @@ caret: function(begin, end) { var range; - if (this.length === 0) { + if (this.length === 0 || this.is(":hidden")) { return; } - + if (typeof begin == 'number') { end = (typeof end === 'number') ? end : begin; return this.each(function() { From e0599b25433c41696bb831792249b1ae992039e8 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Wed, 26 Dec 2012 16:59:31 -0600 Subject: [PATCH 009/116] Cleaning whitespace from that last merge. --- spec/Focus.Spec.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/spec/Focus.Spec.js b/spec/Focus.Spec.js index 1eac082..a6a701c 100644 --- a/spec/Focus.Spec.js +++ b/spec/Focus.Spec.js @@ -35,13 +35,12 @@ feature("Focusing A Masked Input",function(){ }); }); - scenario("Masking a hidden input",function(){ var error; - $(window).on("error.test",function(err){error=err;}) + $(window).on("error.test",function(err){error=err;}) + given("a mask on a hidden input",function(){ input.hide().mask("9"); - }); when("focusing input",function(){ input.focus(); From 90ac106b977c41bed176785f8b1a192019cfcb56 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Thu, 27 Dec 2012 09:38:48 -0600 Subject: [PATCH 010/116] Applying bounds check fix from #16 close #16 close #12 --- src/jquery.maskedinput.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 13c99c8..526e71d 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -27,7 +27,7 @@ if (this.length === 0 || this.is(":hidden")) { return; } - + if (typeof begin == 'number') { end = (typeof end === 'number') ? end : begin; return this.each(function() { @@ -105,7 +105,7 @@ focusText = input.val(); function seekNext(pos) { - while (++pos <= len && !tests[pos]); + while (++pos < len && !tests[pos]); return pos; } From 884bcb87c6bf5efe6714a54b71de81067c252561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mendel=20Gusm=C3=A3o?= Date: Mon, 21 Nov 2011 17:28:55 -0200 Subject: [PATCH 011/116] Adding support for triggering 'complete' handler when pasting a valid value --- src/jquery.maskedinput.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 526e71d..3461d3c 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -312,8 +312,10 @@ .bind("keydown.mask", keydownEvent) .bind("keypress.mask", keypressEvent) .bind(pasteEventName, function() { - setTimeout(function() { - input.caret(checkVal(true)); + setTimeout(function() { + input.caret(checkVal(true)); + if (checkVal(true) == input.val().length) + settings.completed.call(input); }, 0); }); checkVal(); //Perform initial check for existing values From 4ab65878502cc618622a8f564583307605e6fbaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mendel=20Gusm=C3=A3o?= Date: Tue, 22 Nov 2011 11:50:53 -0200 Subject: [PATCH 012/116] Forgot to validate if completed handler exists --- src/jquery.maskedinput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 3461d3c..5e852c8 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -314,7 +314,7 @@ .bind(pasteEventName, function() { setTimeout(function() { input.caret(checkVal(true)); - if (checkVal(true) == input.val().length) + if (settings.completed && checkVal(true) == input.val().length) settings.completed.call(input); }, 0); }); From a70eee682c9be441383df80ae3e077395ad2e4be Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Thu, 27 Dec 2012 10:02:27 -0600 Subject: [PATCH 013/116] Adding spec to validate paste changes --- spec/Paste.Spec.js | 16 ++++++++++++++++ spec/SpecRunner.html | 2 ++ 2 files changed, 18 insertions(+) create mode 100644 spec/Paste.Spec.js diff --git a/spec/Paste.Spec.js b/spec/Paste.Spec.js new file mode 100644 index 0000000..a9ae291 --- /dev/null +++ b/spec/Paste.Spec.js @@ -0,0 +1,16 @@ +feature("Pasting", function() { + scenario('When pasting a value',function(){ + var completed=false; + given("an input with a completed callback", function(){ + input.mask("99",{completed:function(){completed=true;}}); + }); + + when("pasting",function(){ + input.val("99").trigger("paste").trigger("input"); + }); + waits(1); + then("completed callback should be called",function(){ + expect(completed).toBeTruthy(); + }); + }); +}); \ No newline at end of file diff --git a/spec/SpecRunner.html b/spec/SpecRunner.html index 347b570..30db095 100644 --- a/spec/SpecRunner.html +++ b/spec/SpecRunner.html @@ -46,6 +46,8 @@ + + From 35d69fed4832884fe4ef425d4dcb68ed59658a4d Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Thu, 27 Dec 2012 10:03:12 -0600 Subject: [PATCH 014/116] Removing duplicate call to checkVal(true) on paste close #62 --- src/jquery.maskedinput.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 5e852c8..b7414e6 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -313,8 +313,9 @@ .bind("keypress.mask", keypressEvent) .bind(pasteEventName, function() { setTimeout(function() { - input.caret(checkVal(true)); - if (settings.completed && checkVal(true) == input.val().length) + var pos=checkVal(true); + input.caret(pos); + if (settings.completed && pos == input.val().length) settings.completed.call(input); }, 0); }); From 56c1fa800ee8244da2491171d995050129a2e70f Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Thu, 27 Dec 2012 22:14:03 -0600 Subject: [PATCH 015/116] Removing use of $.browser --- src/jquery.maskedinput.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index b7414e6..77ce631 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -5,7 +5,14 @@ Version: @version */ (function($) { - var pasteEventName = ($.browser.msie ? 'paste' : 'input') + ".mask", + function getPasteEvent() { + var el = document.createElement('input'), + name = 'onpaste'; + el.setAttribute(name, ''); + return (typeof el[name] === 'function')?'paste':'input'; + } + + var pasteEventName = getPasteEvent() + ".mask", iPhone = (window.orientation !== undefined); $.mask = { @@ -297,12 +304,7 @@ input.caret(pos); } }; - - if ($.browser.msie) { - moveCaret(); - } else { - window.setTimeout(moveCaret, 0); - } + setTimeout(moveCaret, 0); }) .bind("blur.mask", function() { checkVal(); From 1cd8c804b2a993c642e092bd1d26d0840f285601 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Tue, 1 Jan 2013 20:39:07 -0600 Subject: [PATCH 016/116] fixing how we detect iphone --- src/jquery.maskedinput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 77ce631..29d8650 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -13,7 +13,7 @@ } var pasteEventName = getPasteEvent() + ".mask", - iPhone = (window.orientation !== undefined); + iPhone = !!navigator.userAgent.match(/(iphone)/i); $.mask = { //Predefined character definitions From 905a53d55f2fd2ee0f2db1900b07172a5f68a85d Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Tue, 1 Jan 2013 22:25:01 -0600 Subject: [PATCH 017/116] Fixing android problem with setting caret within keypress Closes #41 Closes #50 Closes #85 --- src/jquery.maskedinput.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 29d8650..d1a1093 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -13,7 +13,9 @@ } var pasteEventName = getPasteEvent() + ".mask", - iPhone = !!navigator.userAgent.match(/(iphone)/i); + ua = navigator.userAgent, + iPhone = /iphone/i.test(ua), + android=/android/i.test(ua); $.mask = { //Predefined character definitions @@ -217,7 +219,11 @@ writeBuffer(); next = seekNext(p); - input.caret(next); + if(android){ + setTimeout($.proxy($.fn.caret,input,next),0); + }else{ + input.caret(next); + } if (settings.completed && next >= len) { settings.completed.call(input); From 8c047cfbbdb93479bc4029726bc97c69ac6f7af4 Mon Sep 17 00:00:00 2001 From: wilsonhut Date: Wed, 9 Jan 2013 08:26:31 -0600 Subject: [PATCH 018/116] Update src/jquery.maskedinput.js timeoutId --- src/jquery.maskedinput.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index d1a1093..80d368e 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -5,6 +5,7 @@ Version: @version */ (function($) { + var caretTimeoutId; function getPasteEvent() { var el = document.createElement('input'), name = 'onpaste'; @@ -297,6 +298,7 @@ .removeData($.mask.dataName); }) .bind("focus.mask", function() { + clearTimeout(caretTimeoutId); var pos, moveCaret; @@ -310,7 +312,7 @@ input.caret(pos); } }; - setTimeout(moveCaret, 0); + caretTimeoutId = setTimeout(moveCaret, 0); }) .bind("blur.mask", function() { checkVal(); From 340a51ed6ee16312ddce8a06af7c736b572e5ce0 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Wed, 9 Jan 2013 21:27:30 -0600 Subject: [PATCH 019/116] Fixing bug where sometimes caret jumps to the end of the input on focus. Added a bit of delay between focus and caret positioning. Closes #21 --- spec/Focus.Spec.js | 4 ++-- src/jquery.maskedinput.js | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/spec/Focus.Spec.js b/spec/Focus.Spec.js index a6a701c..b7a2143 100644 --- a/spec/Focus.Spec.js +++ b/spec/Focus.Spec.js @@ -6,7 +6,7 @@ feature("Focusing A Masked Input",function(){ when("focusing",function(){ input.focus(); }); - waits(1); + waits(20); then("placeholder text should be correct",function(){ expect(input).toHaveValue('_'); }); @@ -24,7 +24,7 @@ feature("Focusing A Masked Input",function(){ when("focusing",function(){ input.focus(); }); - waits(1); + waits(20); then("placeholder text should be correct",function(){ expect(input).toHaveValue('(_)'); }); diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 80d368e..23d01b5 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -5,7 +5,6 @@ Version: @version */ (function($) { - var caretTimeoutId; function getPasteEvent() { var el = document.createElement('input'), name = 'onpaste'; @@ -16,7 +15,8 @@ var pasteEventName = getPasteEvent() + ".mask", ua = navigator.userAgent, iPhone = /iphone/i.test(ua), - android=/android/i.test(ua); + android=/android/i.test(ua), + caretTimeoutId; $.mask = { //Predefined character definitions @@ -244,7 +244,7 @@ } } - function writeBuffer() { return input.val(buffer.join('')).val(); } + function writeBuffer() { input.val(buffer.join('')); } function checkVal(allow) { //try to place characters where they belong @@ -304,15 +304,15 @@ focusText = input.val(); pos = checkVal(); - writeBuffer(); - moveCaret = function(){ + + caretTimeoutId = setTimeout(function(){ + writeBuffer(); if (pos == mask.length) { input.caret(0, pos); } else { input.caret(pos); } - }; - caretTimeoutId = setTimeout(moveCaret, 0); + }, 10); }) .bind("blur.mask", function() { checkVal(); From dfae9cdc2ad9b68108308d7a32b1529298be8de4 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Sun, 13 Jan 2013 21:16:49 -0600 Subject: [PATCH 020/116] Fixing issue where we prevent event bubbling. Closes #81 --- src/jquery.maskedinput.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 23d01b5..47012a8 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -187,11 +187,11 @@ clearBuffer(begin, end); shiftL(begin, end - 1); - return false; + e.preventDefault(); } else if (k == 27) {//escape input.val(focusText); input.caret(0, checkVal()); - return false; + e.preventDefault(); } } @@ -203,7 +203,7 @@ next; if (e.ctrlKey || e.altKey || e.metaKey || k < 32) {//Ignore - return true; + return; } else if (k) { if (pos.end - pos.begin !== 0){ clearBuffer(pos.begin, pos.end); @@ -218,8 +218,8 @@ buffer[p] = c; writeBuffer(); - next = seekNext(p); + if(android){ setTimeout($.proxy($.fn.caret,input,next),0); }else{ @@ -231,7 +231,7 @@ } } } - return false; + e.preventDefault(); } } From 944165b8c74e3867654c1952e3c1939ada60763c Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Sun, 13 Jan 2013 22:41:07 -0600 Subject: [PATCH 021/116] updating to new keymasher version Keymasher had mixed up preventDefault and stopPropagation --- spec/lib/jquery.keymasher.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/lib/jquery.keymasher.js b/spec/lib/jquery.keymasher.js index c76ce59..fdf4e0e 100644 --- a/spec/lib/jquery.keymasher.js +++ b/spec/lib/jquery.keymasher.js @@ -1,8 +1,8 @@ /* Key Masher plugin for jQuery (https://github.com/digitalBush/jquery.keymasher) - Copyright (c) 2010 Josh Bush (digitalbush.com) + Copyright (c) 2010-2013 Josh Bush (digitalbush.com) Licensed under the MIT license - Version: 0.1 + Version: 0.2 */ (function($,undefined){ @@ -51,9 +51,9 @@ up = $.extend($.Event('keyup'), modifierState, {keyCode: key.keyCode, charCode: 0, which:key.keyCode}); elm.trigger(down); - if(!down.isPropagationStopped() && !ignore){ + if(!down.isDefaultPrevented() && !ignore){ elm.trigger(press); - if(!press.isPropagationStopped()){ + if(!press.isDefaultPrevented()){ //need to do caret positioning elm.val(elm.val()+String.fromCharCode(key.charCode)); } From 2c5075f3f61268997d8ca4d24cef7977fc5554a1 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Wed, 16 Jan 2013 21:34:23 -0600 Subject: [PATCH 022/116] Adding jQuery plugin manifest --- LICENSE | 2 +- README.md | 2 +- maskedinput.jquery.json | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 maskedinput.jquery.json diff --git a/LICENSE b/LICENSE index f1fe0fc..8f4ccae 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2007-2010 Josh Bush (digitalbush.com) +Copyright (c) 2007-2013 Josh Bush (digitalbush.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation diff --git a/README.md b/README.md index 7160405..ef6f39b 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Masked Input Plugin for jQuery Overview -------- -This is a masked input plugin for the jQuery javascript library. It allows a user to more easily enter fixed width input where you would like them to enter the data in a certain format (dates,phone numbers, etc). It has been tested on Internet Explorer 6/7, Firefox 1.5/2/3, Safari, Opera, and Chrome. A mask is defined by a format made up of mask literals and mask definitions. Any character not in the definitions list below is considered a mask literal. Mask literals will be automatically entered for the user as they type and will not be able to be removed by the user.The following mask definitions are predefined: +This is a masked input plugin for the jQuery javascript library. It allows a user to more easily enter fixed width input where you would like them to enter the data in a certain format (dates,phone numbers, etc). It has been tested on Internet Explorer, Firefox, Safari, Opera, and Chrome. A mask is defined by a format made up of mask literals and mask definitions. Any character not in the definitions list below is considered a mask literal. Mask literals will be automatically entered for the user as they type and will not be able to be removed by the user.The following mask definitions are predefined: * a - Represents an alpha character (A-Z,a-z) * 9 - Represents a numeric character (0-9) diff --git a/maskedinput.jquery.json b/maskedinput.jquery.json new file mode 100644 index 0000000..151ff1e --- /dev/null +++ b/maskedinput.jquery.json @@ -0,0 +1,23 @@ +{ + "name": "maskedinput", + "title": "jQuery Masked Input", + "description": "jQuery plugin for forcing fixed width inputs to follow a certain patern.", + "keywords": ["input", "form", "mask"], + "version": "1.3", + "author": { + "name": "Josh Bush", + "url": "http://digitalbush.com" + }, + "licenses": [ + { + "type": "MIT", + "url": "https://raw.github.com/digitalBush/jquery.maskedinput/master/LICENSE" + } + ], + "bugs": "https://github.com/digitalBush/jquery.maskedinput/issues", + "homepage": "http://digitalbush.com/projects/masked-input-plugin/", + "docs": "http://digitalbush.com/projects/masked-input-plugin/#usage", + "dependencies": { + "jquery": ">=1.5" + } +} \ No newline at end of file From 9191e875a49bf09f9e07390b7ddea23c1a2379e4 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Wed, 16 Jan 2013 23:13:37 -0600 Subject: [PATCH 023/116] Ditching cake for jake and added templating for distribution output. Using new UglifyJS2 fixes #69 --- Cakefile | 36 -- Jakefile | 31 ++ src/jquery.maskedinput.js | 587 +++++++++++++------------- templates/jquery.maskedinput.template | 9 + 4 files changed, 330 insertions(+), 333 deletions(-) delete mode 100644 Cakefile create mode 100644 Jakefile create mode 100644 templates/jquery.maskedinput.template diff --git a/Cakefile b/Cakefile deleted file mode 100644 index d91b6b2..0000000 --- a/Cakefile +++ /dev/null @@ -1,36 +0,0 @@ -sys = require 'sys' -fs = require 'fs' -path = require 'path' -uglify = require 'uglify-js' -distPath='dist/' - -plugin = JSON.parse(fs.readFileSync('plugin.json','utf8')) -plugin.Year=new Date().getFullYear() - - -task 'clean','clean dist/', -> - fs.rmdir distPath - - -minify = (js)-> - uglify = require 'uglify-js' - ast = uglify.parser.parse(js) - process = uglify.uglify - ast = process.ast_mangle(ast) - ast = process.ast_squeeze(ast) - comment = uglify.parser.tokenizer(js)().comments_before[0].value; - '/*'+comment+'*/\n'+process.gen_code(ast) - -replaceTokens = (js,tokens)-> - js.replace( - /@(\w+)/g, - (match,p)-> tokens[p] - ) - -task 'compress', 'compress javascript', -> - invoke 'clean' - fs.mkdir(distPath,0755) - compressed = minify(fs.readFileSync('src/jquery.maskedinput.js','utf8')) - final=replaceTokens(compressed,plugin) - fs.writeFileSync(path.join(distPath,'jquery.maskedinput.min.js'), final) - diff --git a/Jakefile b/Jakefile new file mode 100644 index 0000000..5001cb7 --- /dev/null +++ b/Jakefile @@ -0,0 +1,31 @@ +var Handlebars=require("handlebars"), + fs = require("fs"), + path = require ("path"), + UglifyJS = require("uglify-js"), + distPath='dist/'; + +Handlebars.registerHelper('include', function(context) { + return fs.readFileSync(context,'utf8'); +}); + +function keepComment(node,comment){ + return comment.type === "comment2"; +} + +task('clean',function(){ + fs.rmdir(distPath) +}); + +task('default',['clean'], function (params) { + fs.mkdir(distPath,0755); + + var options = JSON.parse(fs.readFileSync('plugin.json','utf8')) + options.Year=new Date().getFullYear() + + var template = Handlebars.compile(fs.readFileSync('templates/jquery.maskedinput.template','utf8')); + var debugFile = path.join(distPath,'jquery.maskedinput.js'); + fs.writeFileSync(debugFile,template(options)); + + compressed = UglifyJS.minify(debugFile,{output:{comments:keepComment}}); + fs.writeFileSync(path.join(distPath,'jquery.maskedinput.min.js'), compressed.code); +}); \ No newline at end of file diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 47012a8..2228e8f 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -1,336 +1,329 @@ -/* - Masked Input plugin for jQuery - Copyright (c) 2007-@Year Josh Bush (digitalbush.com) - Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) - Version: @version -*/ -(function($) { - function getPasteEvent() { - var el = document.createElement('input'), - name = 'onpaste'; - el.setAttribute(name, ''); - return (typeof el[name] === 'function')?'paste':'input'; - } - - var pasteEventName = getPasteEvent() + ".mask", - ua = navigator.userAgent, - iPhone = /iphone/i.test(ua), - android=/android/i.test(ua), - caretTimeoutId; - - $.mask = { - //Predefined character definitions - definitions: { - '9': "[0-9]", - 'a': "[A-Za-z]", - '*': "[A-Za-z0-9]" - }, - dataName: "rawMaskFn", - placeholder: '_', - }; - - $.fn.extend({ - //Helper Function for Caret positioning - caret: function(begin, end) { - var range; - - if (this.length === 0 || this.is(":hidden")) { - return; - } +function getPasteEvent() { + var el = document.createElement('input'), + name = 'onpaste'; + el.setAttribute(name, ''); + return (typeof el[name] === 'function')?'paste':'input'; +} + +var pasteEventName = getPasteEvent() + ".mask", + ua = navigator.userAgent, + iPhone = /iphone/i.test(ua), + android=/android/i.test(ua), + caretTimeoutId; + +$.mask = { + //Predefined character definitions + definitions: { + '9': "[0-9]", + 'a': "[A-Za-z]", + '*': "[A-Za-z0-9]" + }, + dataName: "rawMaskFn", + placeholder: '_', +}; + +$.fn.extend({ + //Helper Function for Caret positioning + caret: function(begin, end) { + var range; + + if (this.length === 0 || this.is(":hidden")) { + return; + } - if (typeof begin == 'number') { - end = (typeof end === 'number') ? end : begin; - return this.each(function() { - if (this.setSelectionRange) { - this.setSelectionRange(begin, end); - } else if (this.createTextRange) { - range = this.createTextRange(); - range.collapse(true); - range.moveEnd('character', end); - range.moveStart('character', begin); - range.select(); - } - }); - } else { - if (this[0].setSelectionRange) { - begin = this[0].selectionStart; - end = this[0].selectionEnd; - } else if (document.selection && document.selection.createRange) { - range = document.selection.createRange(); - begin = 0 - range.duplicate().moveStart('character', -100000); - end = begin + range.text.length; + if (typeof begin == 'number') { + end = (typeof end === 'number') ? end : begin; + return this.each(function() { + if (this.setSelectionRange) { + this.setSelectionRange(begin, end); + } else if (this.createTextRange) { + range = this.createTextRange(); + range.collapse(true); + range.moveEnd('character', end); + range.moveStart('character', begin); + range.select(); } - return { begin: begin, end: end }; + }); + } else { + if (this[0].setSelectionRange) { + begin = this[0].selectionStart; + end = this[0].selectionEnd; + } else if (document.selection && document.selection.createRange) { + range = document.selection.createRange(); + begin = 0 - range.duplicate().moveStart('character', -100000); + end = begin + range.text.length; } - }, - unmask: function() { - return this.trigger("unmask"); - }, - mask: function(mask, settings) { - var input, - defs, - tests, - partialPosition, - firstNonMaskPos, - len; - - if (!mask && this.length > 0) { - input = $(this[0]); - return input.data($.mask.dataName)(); + return { begin: begin, end: end }; + } + }, + unmask: function() { + return this.trigger("unmask"); + }, + mask: function(mask, settings) { + var input, + defs, + tests, + partialPosition, + firstNonMaskPos, + len; + + if (!mask && this.length > 0) { + input = $(this[0]); + return input.data($.mask.dataName)(); + } + settings = $.extend({ + placeholder: $.mask.placeholder, // Load default placeholder + completed: null + }, settings); + + + defs = $.mask.definitions; + tests = []; + 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])); + if (firstNonMaskPos === null) { + firstNonMaskPos = tests.length - 1; + } + } else { + tests.push(null); } - settings = $.extend({ - placeholder: $.mask.placeholder, // Load default placeholder - completed: null - }, settings); - - - defs = $.mask.definitions; - tests = []; - 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])); - if (firstNonMaskPos === null) { - firstNonMaskPos = tests.length - 1; + }); + + return this.trigger("unmask").each(function() { + var input = $(this), + buffer = $.map( + mask.split(""), + function(c, i) { + if (c != '?') { + return defs[c] ? settings.placeholder : c; } - } else { - tests.push(null); - } - }); + }), + focusText = input.val(); - return this.trigger("unmask").each(function() { - var input = $(this), - buffer = $.map( - mask.split(""), - function(c, i) { - if (c != '?') { - return defs[c] ? settings.placeholder : c; - } - }), - focusText = input.val(); + function seekNext(pos) { + while (++pos < len && !tests[pos]); + return pos; + } - function seekNext(pos) { - while (++pos < len && !tests[pos]); - return pos; - } + function seekPrev(pos) { + while (--pos >= 0 && !tests[pos]); + return pos; + } - function seekPrev(pos) { - while (--pos >= 0 && !tests[pos]); - return pos; + function shiftL(begin,end) { + var i, + j; + + if (begin<0) { + return; } - function shiftL(begin,end) { - var i, - j; + for (i = begin, j = seekNext(end); i < len; i++) { + if (tests[i]) { + if (j < len && tests[i].test(buffer[j])) { + buffer[i] = buffer[j]; + buffer[j] = settings.placeholder; + } else { + break; + } - if (begin<0) { - return; + j = seekNext(j); } + } + writeBuffer(); + input.caret(Math.max(firstNonMaskPos, begin)); + } - for (i = begin, j = seekNext(end); i < len; i++) { - if (tests[i]) { - if (j < len && tests[i].test(buffer[j])) { - buffer[i] = buffer[j]; - buffer[j] = settings.placeholder; - } else { - break; - } - - j = seekNext(j); + function shiftR(pos) { + var i, + c, + j, + t; + + for (i = pos, c = settings.placeholder; i < len; i++) { + if (tests[i]) { + j = seekNext(i); + t = buffer[i]; + buffer[i] = c; + if (j < len && tests[j].test(t)) { + c = t; + } else { + break; } } - writeBuffer(); - input.caret(Math.max(firstNonMaskPos, begin)); } + } - function shiftR(pos) { - var i, - c, - j, - t; - - for (i = pos, c = settings.placeholder; i < len; i++) { - if (tests[i]) { - j = seekNext(i); - t = buffer[i]; - buffer[i] = c; - if (j < len && tests[j].test(t)) { - c = t; - } else { - break; - } - } + function keydownEvent(e) { + var k = e.which, + pos, + begin, + end; + + //backspace, delete, and escape get special treatment + if (k === 8 || k === 46 || (iPhone && k === 127)) { + pos = input.caret(); + begin = pos.begin; + end = pos.end; + + if (end - begin === 0) { + begin=k!==46?seekPrev(begin):(end=seekNext(begin-1)); + end=k===46?seekNext(end):end; } + clearBuffer(begin, end); + shiftL(begin, end - 1); + + e.preventDefault(); + } else if (k == 27) {//escape + input.val(focusText); + input.caret(0, checkVal()); + e.preventDefault(); } + } - function keydownEvent(e) { - var k = e.which, - pos, - begin, - end; - - //backspace, delete, and escape get special treatment - if (k === 8 || k === 46 || (iPhone && k === 127)) { - pos = input.caret(); - begin = pos.begin; - end = pos.end; - - if (end - begin === 0) { - begin=k!==46?seekPrev(begin):(end=seekNext(begin-1)); - end=k===46?seekNext(end):end; - } - clearBuffer(begin, end); - shiftL(begin, end - 1); - - e.preventDefault(); - } else if (k == 27) {//escape - input.val(focusText); - input.caret(0, checkVal()); - e.preventDefault(); + function keypressEvent(e) { + var k = e.which, + pos = input.caret(), + p, + c, + next; + + if (e.ctrlKey || e.altKey || e.metaKey || k < 32) {//Ignore + return; + } else if (k) { + if (pos.end - pos.begin !== 0){ + clearBuffer(pos.begin, pos.end); + shiftL(pos.begin, pos.end-1); } - } - function keypressEvent(e) { - var k = e.which, - pos = input.caret(), - p, - c, - next; - - if (e.ctrlKey || e.altKey || e.metaKey || k < 32) {//Ignore - return; - } else if (k) { - if (pos.end - pos.begin !== 0){ - clearBuffer(pos.begin, pos.end); - shiftL(pos.begin, pos.end-1); - } + p = seekNext(pos.begin - 1); + if (p < len) { + c = String.fromCharCode(k); + if (tests[p].test(c)) { + shiftR(p); - p = seekNext(pos.begin - 1); - if (p < len) { - c = String.fromCharCode(k); - if (tests[p].test(c)) { - shiftR(p); - - buffer[p] = c; - writeBuffer(); - next = seekNext(p); - - if(android){ - setTimeout($.proxy($.fn.caret,input,next),0); - }else{ - input.caret(next); - } - - if (settings.completed && next >= len) { - settings.completed.call(input); - } + buffer[p] = c; + writeBuffer(); + next = seekNext(p); + + if(android){ + setTimeout($.proxy($.fn.caret,input,next),0); + }else{ + input.caret(next); + } + + if (settings.completed && next >= len) { + settings.completed.call(input); } } - e.preventDefault(); } + e.preventDefault(); } + } - function clearBuffer(start, end) { - var i; - for (i = start; i < end && i < len; i++) { - if (tests[i]) { - buffer[i] = settings.placeholder; - } + function clearBuffer(start, end) { + var i; + for (i = start; i < end && i < len; i++) { + if (tests[i]) { + buffer[i] = settings.placeholder; } } + } - function writeBuffer() { input.val(buffer.join('')); } - - function checkVal(allow) { - //try to place characters where they belong - var test = input.val(), - lastMatch = -1, - i, - c; - - 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)) { - buffer[i] = c; - lastMatch = i; - break; - } - } - if (pos > test.length) { + function writeBuffer() { input.val(buffer.join('')); } + + function checkVal(allow) { + //try to place characters where they belong + var test = input.val(), + lastMatch = -1, + i, + c; + + 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)) { + buffer[i] = c; + lastMatch = i; break; } - } else if (buffer[i] === test.charAt(pos) && i !== partialPosition) { - pos++; - lastMatch = i; } + if (pos > test.length) { + break; + } + } else if (buffer[i] === test.charAt(pos) && i !== partialPosition) { + pos++; + lastMatch = i; } - if (allow) { - writeBuffer(); - } else if (lastMatch + 1 < partialPosition) { - input.val(""); - clearBuffer(0, len); - } else { - writeBuffer(); - input.val(input.val().substring(0, lastMatch + 1)); - } - return (partialPosition ? i : firstNonMaskPos); } + if (allow) { + writeBuffer(); + } else if (lastMatch + 1 < partialPosition) { + input.val(""); + clearBuffer(0, len); + } else { + writeBuffer(); + input.val(input.val().substring(0, lastMatch + 1)); + } + return (partialPosition ? i : firstNonMaskPos); + } - input.data($.mask.dataName,function(){ - return $.map(buffer, function(c, i) { - return tests[i]&&c!=settings.placeholder ? c : null; - }).join(''); - }); + input.data($.mask.dataName,function(){ + return $.map(buffer, function(c, i) { + return tests[i]&&c!=settings.placeholder ? c : null; + }).join(''); + }); - if (!input.attr("readonly")) + if (!input.attr("readonly")) + input + .one("unmask", function() { input - .one("unmask", function() { - input - .unbind(".mask") - .removeData($.mask.dataName); - }) - .bind("focus.mask", function() { - clearTimeout(caretTimeoutId); - var pos, - moveCaret; - - focusText = input.val(); - pos = checkVal(); - - caretTimeoutId = setTimeout(function(){ - writeBuffer(); - if (pos == mask.length) { - input.caret(0, pos); - } else { - input.caret(pos); - } - }, 10); - }) - .bind("blur.mask", function() { - checkVal(); - if (input.val() != focusText) - input.change(); - }) - .bind("keydown.mask", keydownEvent) - .bind("keypress.mask", keypressEvent) - .bind(pasteEventName, function() { - setTimeout(function() { - var pos=checkVal(true); - input.caret(pos); - if (settings.completed && pos == input.val().length) - settings.completed.call(input); - }, 0); - }); - checkVal(); //Perform initial check for existing values - }); - } - }); -})(jQuery); + .unbind(".mask") + .removeData($.mask.dataName); + }) + .bind("focus.mask", function() { + clearTimeout(caretTimeoutId); + var pos, + moveCaret; + + focusText = input.val(); + pos = checkVal(); + + caretTimeoutId = setTimeout(function(){ + writeBuffer(); + if (pos == mask.length) { + input.caret(0, pos); + } else { + input.caret(pos); + } + }, 10); + }) + .bind("blur.mask", function() { + checkVal(); + if (input.val() != focusText) + input.change(); + }) + .bind("keydown.mask", keydownEvent) + .bind("keypress.mask", keypressEvent) + .bind(pasteEventName, function() { + setTimeout(function() { + var pos=checkVal(true); + input.caret(pos); + if (settings.completed && pos == input.val().length) + settings.completed.call(input); + }, 0); + }); + checkVal(); //Perform initial check for existing values + }); + } +}); + diff --git a/templates/jquery.maskedinput.template b/templates/jquery.maskedinput.template new file mode 100644 index 0000000..18f6d17 --- /dev/null +++ b/templates/jquery.maskedinput.template @@ -0,0 +1,9 @@ +/* + Masked Input plugin for jQuery + Copyright (c) 2007-{{Year}} Josh Bush (digitalbush.com) + Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) + Version: {{version}} +*/ +(function($) { + {{{include "src/jquery.maskedinput.js"}}} +})(jQuery); \ No newline at end of file From 96726303dbe9a35f800cdd8fc72b369d18c5d6f7 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Wed, 16 Jan 2013 23:20:18 -0600 Subject: [PATCH 024/116] 1.3.1 --- .gitignore | 1 - demo/index.html | 2 +- dist/jquery.maskedinput.js | 338 +++++++++++++++++++++++++++++++++ dist/jquery.maskedinput.min.js | 7 + maskedinput.jquery.json | 2 +- plugin.json | 2 +- spec/SpecRunner.html | 2 +- 7 files changed, 349 insertions(+), 5 deletions(-) create mode 100644 dist/jquery.maskedinput.js create mode 100644 dist/jquery.maskedinput.min.js diff --git a/.gitignore b/.gitignore index 3260eff..bfa0621 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ .idea/* -dist/* diff --git a/demo/index.html b/demo/index.html index 3e66b89..68e5979 100644 --- a/demo/index.html +++ b/demo/index.html @@ -2,7 +2,7 @@ jQuery Mask Test - + - + From d9675b722d1c7f0b63676ea75517baf2b4f9c84c Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Wed, 16 Jan 2013 23:52:17 -0600 Subject: [PATCH 025/116] Updating to jQuery 1.9 and keymasher 0.3 --- demo/index.html | 2 +- lib/jquery-1.9.0.min.js | 4 ++++ spec/SpecRunner.html | 2 +- spec/lib/jquery.keymasher.js | 6 +++--- 4 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 lib/jquery-1.9.0.min.js diff --git a/demo/index.html b/demo/index.html index 68e5979..60d7e12 100644 --- a/demo/index.html +++ b/demo/index.html @@ -1,7 +1,7 @@ jQuery Mask Test - + - + diff --git a/spec/lib/jquery.keymasher.js b/spec/lib/jquery.keymasher.js index fdf4e0e..d8d9df5 100644 --- a/spec/lib/jquery.keymasher.js +++ b/spec/lib/jquery.keymasher.js @@ -2,7 +2,7 @@ Key Masher plugin for jQuery (https://github.com/digitalBush/jquery.keymasher) Copyright (c) 2010-2013 Josh Bush (digitalbush.com) Licensed under the MIT license - Version: 0.2 + Version: 0.3 */ (function($,undefined){ @@ -16,7 +16,7 @@ capslock:20,numlock:144,scrolllock:145,pageup:33,pagedown:34,end:35,home:36,backspace:8, insert:45, 'delete':46,pause:19,esc:27,left:37,up:38,right:39,down:40,printscreen:44}; - $.each(keys,function(index,value){ + $.each(keys.split(''),function(index,value){ var keyCode=value.charCodeAt(0),shift=shifted[index]; defs[value]={keyCode:keyCode,charCode:keyCode,shift:shift}; if(shift) @@ -75,7 +75,7 @@ type:function(){ $.each(arguments,function(index,typing){ if($.type(typing)==='string') - $.each(typing,function(index,value){queueStroke(value);}); + $.each(typing.split(''),function(index,value){queueStroke(value);}); else queueStroke(typing); }); From 75c27c9f148276ac33ddab079b68e12c103c985e Mon Sep 17 00:00:00 2001 From: Simon Timms Date: Fri, 18 Jan 2013 13:15:57 -0800 Subject: [PATCH 026/116] Create jquery.makedinput.nuspec --- dist/jquery.makedinput.nuspec | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 dist/jquery.makedinput.nuspec diff --git a/dist/jquery.makedinput.nuspec b/dist/jquery.makedinput.nuspec new file mode 100644 index 0000000..632c0c4 --- /dev/null +++ b/dist/jquery.makedinput.nuspec @@ -0,0 +1,17 @@ + + + + jQuery.MaskedInput + 1.3.1.0 + digitalBush + stimms + https://github.com/digitalBush/jquery.maskedinput/blob/master/LICENSE + http://digitalbush.com/projects/masked-input-plugin/ + false + A jQuery plugin which applies a mask to input boxes to provide both a UI hint for users as well as some rudimentary input checking. + jQuery,plugins + + + + + From a4ba9fd638079ca2c56463c9af76d2d225b56baa Mon Sep 17 00:00:00 2001 From: James Jensen Date: Thu, 14 Feb 2013 13:50:07 -0700 Subject: [PATCH 027/116] Removed extraneous comma IE7 doesn't deal well with extra commas at the end of javascript object initializers. --- src/jquery.maskedinput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 2228e8f..70747d6 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -19,7 +19,7 @@ $.mask = { '*': "[A-Za-z0-9]" }, dataName: "rawMaskFn", - placeholder: '_', + placeholder: '_' }; $.fn.extend({ From 949b2f74be43c0bae273bf1381d5d5047d2ed652 Mon Sep 17 00:00:00 2001 From: James Jensen Date: Thu, 14 Feb 2013 13:54:17 -0700 Subject: [PATCH 028/116] Declare "pos" as local variable This variable is set in the `for` loop on the next line. We should declare it locally to avoid setting a global variable. --- src/jquery.maskedinput.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 70747d6..9ad0052 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -244,7 +244,8 @@ $.fn.extend({ var test = input.val(), lastMatch = -1, i, - c; + c, + pos; for (i = 0, pos = 0; i < len; i++) { if (tests[i]) { From 8ac725a3872c6c5f983f5b07328a92b3a64a85b0 Mon Sep 17 00:00:00 2001 From: James Jensen Date: Thu, 14 Feb 2013 13:55:22 -0700 Subject: [PATCH 029/116] Removed unused variable --- src/jquery.maskedinput.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 9ad0052..749da65 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -293,8 +293,7 @@ $.fn.extend({ }) .bind("focus.mask", function() { clearTimeout(caretTimeoutId); - var pos, - moveCaret; + var pos; focusText = input.val(); pos = checkVal(); From 73b572103b3a07d5bf575005d605500004271785 Mon Sep 17 00:00:00 2001 From: Ryan Whitmire Date: Wed, 6 Mar 2013 08:36:50 -0500 Subject: [PATCH 030/116] removed trailing comma which caused errors in IE7 --- src/jquery.maskedinput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 2228e8f..70747d6 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -19,7 +19,7 @@ $.mask = { '*': "[A-Za-z0-9]" }, dataName: "rawMaskFn", - placeholder: '_', + placeholder: '_' }; $.fn.extend({ From 5ebc74d05ca3a5401ac6543279a09367a55f888b Mon Sep 17 00:00:00 2001 From: holtkamp Date: Tue, 27 Aug 2013 19:22:45 +0200 Subject: [PATCH 031/116] Properly closed mask object definition to prevent JavaScript error in IE7 Unclosed object generates a JavaScript error in IE7: "Expected identifier, string, or number" --- dist/jquery.maskedinput.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/jquery.maskedinput.js b/dist/jquery.maskedinput.js index 49a5a72..89ce020 100644 --- a/dist/jquery.maskedinput.js +++ b/dist/jquery.maskedinput.js @@ -26,7 +26,7 @@ $.mask = { '*': "[A-Za-z0-9]" }, dataName: "rawMaskFn", - placeholder: '_', + placeholder: '_' }; $.fn.extend({ @@ -335,4 +335,4 @@ $.fn.extend({ }); -})(jQuery); \ No newline at end of file +})(jQuery); From 0dea72e516f8c3443b1e321d5504421d43a5c311 Mon Sep 17 00:00:00 2001 From: Greg Burghardt Date: Wed, 9 Oct 2013 11:07:46 -0400 Subject: [PATCH 032/116] Added "autoclear" setting so invalid values persist To increase usability, add a new setting that keeps invalid values in the input field to give the user a chance to correct a mistake. If the user fat fingers the value on accident and doesn't realize it before tabbing away from the form field, the invalid value will remain so the user can correct the value rather than retyping it. - Added new setting called "autoclear", defaulting to true, to maintain existing behavior. - When autoclear=false, leave invalid values visible so the user can correct their mistake. - Added test coverage for when the mask(...) is first invoked (Init.Spec.js) - Added scenarios to Focus.Spec.js to cover new functionality - Repackaged the distribution version to include this new feature --- dist/jquery.maskedinput.js | 33 ++++++++++++++-------- dist/jquery.maskedinput.min.js | 2 +- spec/Focus.Spec.js | 32 +++++++++++++++++++++ spec/Init.Spec.js | 51 ++++++++++++++++++++++++++++++++++ spec/SpecRunner.html | 3 +- src/jquery.maskedinput.js | 27 +++++++++++++----- 6 files changed, 128 insertions(+), 20 deletions(-) create mode 100644 spec/Init.Spec.js diff --git a/dist/jquery.maskedinput.js b/dist/jquery.maskedinput.js index 89ce020..7ecfcc0 100644 --- a/dist/jquery.maskedinput.js +++ b/dist/jquery.maskedinput.js @@ -9,7 +9,7 @@ var el = document.createElement('input'), name = 'onpaste'; el.setAttribute(name, ''); - return (typeof el[name] === 'function')?'paste':'input'; + return (typeof el[name] === 'function')?'paste':'input'; } var pasteEventName = getPasteEvent() + ".mask", @@ -25,6 +25,7 @@ $.mask = { 'a': "[A-Za-z]", '*': "[A-Za-z0-9]" }, + autoclear: true, dataName: "rawMaskFn", placeholder: '_' }; @@ -79,6 +80,7 @@ $.fn.extend({ return input.data($.mask.dataName)(); } settings = $.extend({ + autoclear: $.mask.autoclear, placeholder: $.mask.placeholder, // Load default placeholder completed: null }, settings); @@ -251,7 +253,8 @@ $.fn.extend({ var test = input.val(), lastMatch = -1, i, - c; + c, + pos; for (i = 0, pos = 0; i < len; i++) { if (tests[i]) { @@ -275,8 +278,14 @@ $.fn.extend({ if (allow) { writeBuffer(); } else if (lastMatch + 1 < partialPosition) { - input.val(""); - clearBuffer(0, len); + if (settings.autoclear) { + input.val(""); + clearBuffer(0, len); + } else { + // Invalid value, but we opt to show the value to the + // user and allow them to correct their mistake. + writeBuffer(); + } } else { writeBuffer(); input.val(input.val().substring(0, lastMatch + 1)); @@ -299,12 +308,12 @@ $.fn.extend({ }) .bind("focus.mask", function() { clearTimeout(caretTimeoutId); - var pos, - moveCaret; + var pos; focusText = input.val(); + pos = checkVal(); - + caretTimeoutId = setTimeout(function(){ writeBuffer(); if (pos == mask.length) { @@ -316,23 +325,25 @@ $.fn.extend({ }) .bind("blur.mask", function() { checkVal(); + if (input.val() != focusText) input.change(); }) .bind("keydown.mask", keydownEvent) .bind("keypress.mask", keypressEvent) .bind(pasteEventName, function() { - setTimeout(function() { + setTimeout(function() { var pos=checkVal(true); - input.caret(pos); + input.caret(pos); if (settings.completed && pos == input.val().length) settings.completed.call(input); }, 0); }); - checkVal(); //Perform initial check for existing values + + checkVal(); //Perform initial check for existing values }); } }); -})(jQuery); +})(jQuery); \ No newline at end of file diff --git a/dist/jquery.maskedinput.min.js b/dist/jquery.maskedinput.min.js index 0d9ce6e..558899e 100644 --- a/dist/jquery.maskedinput.min.js +++ b/dist/jquery.maskedinput.min.js @@ -4,4 +4,4 @@ Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) Version: 1.3.1 */ -(function(e){function t(){var e=document.createElement("input"),t="onpaste";return e.setAttribute(t,""),"function"==typeof e[t]?"paste":"input"}var n,a=t()+".mask",r=navigator.userAgent,i=/iphone/i.test(r),o=/android/i.test(r);e.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},dataName:"rawMaskFn",placeholder:"_"},e.fn.extend({caret:function(e,t){var n;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof e?(t="number"==typeof t?t:e,this.each(function(){this.setSelectionRange?this.setSelectionRange(e,t):this.createTextRange&&(n=this.createTextRange(),n.collapse(!0),n.moveEnd("character",t),n.moveStart("character",e),n.select())})):(this[0].setSelectionRange?(e=this[0].selectionStart,t=this[0].selectionEnd):document.selection&&document.selection.createRange&&(n=document.selection.createRange(),e=0-n.duplicate().moveStart("character",-1e5),t=e+n.text.length),{begin:e,end:t})},unmask:function(){return this.trigger("unmask")},mask:function(t,r){var c,l,s,u,f,h;return!t&&this.length>0?(c=e(this[0]),c.data(e.mask.dataName)()):(r=e.extend({placeholder:e.mask.placeholder,completed:null},r),l=e.mask.definitions,s=[],u=h=t.length,f=null,e.each(t.split(""),function(e,t){"?"==t?(h--,u=e):l[t]?(s.push(RegExp(l[t])),null===f&&(f=s.length-1)):s.push(null)}),this.trigger("unmask").each(function(){function c(e){for(;h>++e&&!s[e];);return e}function d(e){for(;--e>=0&&!s[e];);return e}function m(e,t){var n,a;if(!(0>e)){for(n=e,a=c(t);h>n;n++)if(s[n]){if(!(h>a&&s[n].test(R[a])))break;R[n]=R[a],R[a]=r.placeholder,a=c(a)}b(),x.caret(Math.max(f,e))}}function p(e){var t,n,a,i;for(t=e,n=r.placeholder;h>t;t++)if(s[t]){if(a=c(t),i=R[t],R[t]=n,!(h>a&&s[a].test(i)))break;n=i}}function g(e){var t,n,a,r=e.which;8===r||46===r||i&&127===r?(t=x.caret(),n=t.begin,a=t.end,0===a-n&&(n=46!==r?d(n):a=c(n-1),a=46===r?c(a):a),k(n,a),m(n,a-1),e.preventDefault()):27==r&&(x.val(S),x.caret(0,y()),e.preventDefault())}function v(t){var n,a,i,l=t.which,u=x.caret();t.ctrlKey||t.altKey||t.metaKey||32>l||l&&(0!==u.end-u.begin&&(k(u.begin,u.end),m(u.begin,u.end-1)),n=c(u.begin-1),h>n&&(a=String.fromCharCode(l),s[n].test(a)&&(p(n),R[n]=a,b(),i=c(n),o?setTimeout(e.proxy(e.fn.caret,x,i),0):x.caret(i),r.completed&&i>=h&&r.completed.call(x))),t.preventDefault())}function k(e,t){var n;for(n=e;t>n&&h>n;n++)s[n]&&(R[n]=r.placeholder)}function b(){x.val(R.join(""))}function y(e){var t,n,a=x.val(),i=-1;for(t=0,pos=0;h>t;t++)if(s[t]){for(R[t]=r.placeholder;pos++a.length)break}else R[t]===a.charAt(pos)&&t!==u&&(pos++,i=t);return e?b():u>i+1?(x.val(""),k(0,h)):(b(),x.val(x.val().substring(0,i+1))),u?t:f}var x=e(this),R=e.map(t.split(""),function(e){return"?"!=e?l[e]?r.placeholder:e:void 0}),S=x.val();x.data(e.mask.dataName,function(){return e.map(R,function(e,t){return s[t]&&e!=r.placeholder?e:null}).join("")}),x.attr("readonly")||x.one("unmask",function(){x.unbind(".mask").removeData(e.mask.dataName)}).bind("focus.mask",function(){clearTimeout(n);var e;S=x.val(),e=y(),n=setTimeout(function(){b(),e==t.length?x.caret(0,e):x.caret(e)},10)}).bind("blur.mask",function(){y(),x.val()!=S&&x.change()}).bind("keydown.mask",g).bind("keypress.mask",v).bind(a,function(){setTimeout(function(){var e=y(!0);x.caret(e),r.completed&&e==x.val().length&&r.completed.call(x)},0)}),y()}))}})})(jQuery); \ No newline at end of file +!function(e){function t(){var e=document.createElement("input"),t="onpaste";return e.setAttribute(t,""),"function"==typeof e[t]?"paste":"input"}var n,a=t()+".mask",r=navigator.userAgent,i=/iphone/i.test(r),c=/android/i.test(r);e.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},e.fn.extend({caret:function(e,t){var n;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof e?(t="number"==typeof t?t:e,this.each(function(){this.setSelectionRange?this.setSelectionRange(e,t):this.createTextRange&&(n=this.createTextRange(),n.collapse(!0),n.moveEnd("character",t),n.moveStart("character",e),n.select())})):(this[0].setSelectionRange?(e=this[0].selectionStart,t=this[0].selectionEnd):document.selection&&document.selection.createRange&&(n=document.selection.createRange(),e=0-n.duplicate().moveStart("character",-1e5),t=e+n.text.length),{begin:e,end:t})},unmask:function(){return this.trigger("unmask")},mask:function(t,r){var o,l,u,s,f,h;return!t&&this.length>0?(o=e(this[0]),o.data(e.mask.dataName)()):(r=e.extend({autoclear:e.mask.autoclear,placeholder:e.mask.placeholder,completed:null},r),l=e.mask.definitions,u=[],s=h=t.length,f=null,e.each(t.split(""),function(e,t){"?"==t?(h--,s=e):l[t]?(u.push(new RegExp(l[t])),null===f&&(f=u.length-1)):u.push(null)}),this.trigger("unmask").each(function(){function o(e){for(;++e=0&&!u[e];);return e}function m(e,t){var n,a;if(!(0>e)){for(n=e,a=o(t);h>n;n++)if(u[n]){if(!(h>a&&u[n].test(R[a])))break;R[n]=R[a],R[a]=r.placeholder,a=o(a)}b(),x.caret(Math.max(f,e))}}function p(e){var t,n,a,i;for(t=e,n=r.placeholder;h>t;t++)if(u[t]){if(a=o(t),i=R[t],R[t]=n,!(h>a&&u[a].test(i)))break;n=i}}function g(e){var t,n,a,r=e.which;8===r||46===r||i&&127===r?(t=x.caret(),n=t.begin,a=t.end,0===a-n&&(n=46!==r?d(n):a=o(n-1),a=46===r?o(a):a),k(n,a),m(n,a-1),e.preventDefault()):27==r&&(x.val(S),x.caret(0,y()),e.preventDefault())}function v(t){var n,a,i,l=t.which,s=x.caret();t.ctrlKey||t.altKey||t.metaKey||32>l||l&&(0!==s.end-s.begin&&(k(s.begin,s.end),m(s.begin,s.end-1)),n=o(s.begin-1),h>n&&(a=String.fromCharCode(l),u[n].test(a)&&(p(n),R[n]=a,b(),i=o(n),c?setTimeout(e.proxy(e.fn.caret,x,i),0):x.caret(i),r.completed&&i>=h&&r.completed.call(x))),t.preventDefault())}function k(e,t){var n;for(n=e;t>n&&h>n;n++)u[n]&&(R[n]=r.placeholder)}function b(){x.val(R.join(""))}function y(e){var t,n,a,i=x.val(),c=-1;for(t=0,a=0;h>t;t++)if(u[t]){for(R[t]=r.placeholder;a++i.length)break}else R[t]===i.charAt(a)&&t!==s&&(a++,c=t);return e?b():s>c+1?r.autoclear?(x.val(""),k(0,h)):b():(b(),x.val(x.val().substring(0,c+1))),s?t:f}var x=e(this),R=e.map(t.split(""),function(e){return"?"!=e?l[e]?r.placeholder:e:void 0}),S=x.val();x.data(e.mask.dataName,function(){return e.map(R,function(e,t){return u[t]&&e!=r.placeholder?e:null}).join("")}),x.attr("readonly")||x.one("unmask",function(){x.unbind(".mask").removeData(e.mask.dataName)}).bind("focus.mask",function(){clearTimeout(n);var e;S=x.val(),e=y(),n=setTimeout(function(){b(),e==t.length?x.caret(0,e):x.caret(e)},10)}).bind("blur.mask",function(){y(),x.val()!=S&&x.change()}).bind("keydown.mask",g).bind("keypress.mask",v).bind(a,function(){setTimeout(function(){var e=y(!0);x.caret(e),r.completed&&e==x.val().length&&r.completed.call(x)},0)}),y()}))}})}(jQuery); \ No newline at end of file diff --git a/spec/Focus.Spec.js b/spec/Focus.Spec.js index b7a2143..01ddebd 100644 --- a/spec/Focus.Spec.js +++ b/spec/Focus.Spec.js @@ -50,6 +50,24 @@ feature("Focusing A Masked Input",function(){ expect(error).toBeUndefined(); }) }); + + scenario("Mask contains a partial value with autoclear set to false",function(){ + given("the input has a partial value",function(){ + input.val("1"); + }); + given("a mask with two placeholders and autoclear=false",function(){ + input.mask("99", { autoclear: false }); + }); + when("focusing on the input",function(){ + input.focus(); + }); + then("the value should be partially filled out",function(){ + expect(input).toHaveValue("1_"); + }); + then("the input partial value should remain",function(){ + expect(input).toHaveValue("1_"); + }); + }); }); feature("Leaving A Masked Input",function(){ @@ -76,6 +94,20 @@ feature("Leaving A Masked Input",function(){ expect(input).toHaveValue(""); }); }); + + scenario("Empty placeholders remaining with autoclear set to false",function(){ + given("a mask with two placeholders",function(){ + input.mask("99", { autoclear: false }); + }); + when("typing one character and blurring",function(){ + input.caret(0); + input.mashKeys("1") + input.blur(); + }); + then("value should remain visible with placeholders",function(){ + expect(input).toHaveValue("1_"); + }); + }); }); feature("Optional marker",function(){ diff --git a/spec/Init.Spec.js b/spec/Init.Spec.js new file mode 100644 index 0000000..1b64130 --- /dev/null +++ b/spec/Init.Spec.js @@ -0,0 +1,51 @@ +feature("Initializing a Mask",function(){ + scenario("An input with no value",function(){ + given("an input with no value",function(){ + input.val(""); + }); + when("setting a mask with two placeholders",function(){ + input.mask("99"); + }); + then("the value should be an empty string",function(){ + expect(input).toHaveValue(""); + }); + }); + + scenario("An input with a valid value and no placeholders remaining",function(){ + given("an input with a valid value",function(){ + input.val("5555555555"); + }); + when("setting a mask",function(){ + input.mask("(999) 999-9999"); + }); + then("the value should be intact",function(){ + expect(input).toHaveValue("(555) 555-5555"); + }); + }); + + scenario("An input with an invalid value and placeholders remaining",function(){ + given("an invalid input value",function(){ + input.val("55555555"); + }); + when("setting a mask",function(){ + input.mask("(999) 999-9999"); + }); + then("the value should be empty",function(){ + expect(input).toHaveValue(""); + }); + }); + + scenario("An input with an invalid value, placeholders remaining and autoclear set to false",function(){ + given("an invalid input value",function(){ + input.val("55555555"); + }); + when("setting a mask with autoclear set to false",function(){ + input.mask("(999) 999-9999", { autoclear: false }); + }); + then("the value be intact with placeholders visible",function(){ + expect(input).toHaveValue("(555) 555-55__"); + }); + }); + + scenario("An input with an invalid value and placeholders remaining"); +}); diff --git a/spec/SpecRunner.html b/spec/SpecRunner.html index 0339b85..6747474 100644 --- a/spec/SpecRunner.html +++ b/spec/SpecRunner.html @@ -40,7 +40,8 @@ - + + diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 749da65..6382f70 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -2,7 +2,7 @@ function getPasteEvent() { var el = document.createElement('input'), name = 'onpaste'; el.setAttribute(name, ''); - return (typeof el[name] === 'function')?'paste':'input'; + return (typeof el[name] === 'function')?'paste':'input'; } var pasteEventName = getPasteEvent() + ".mask", @@ -18,6 +18,7 @@ $.mask = { 'a': "[A-Za-z]", '*': "[A-Za-z0-9]" }, + autoclear: true, dataName: "rawMaskFn", placeholder: '_' }; @@ -72,6 +73,7 @@ $.fn.extend({ return input.data($.mask.dataName)(); } settings = $.extend({ + autoclear: $.mask.autoclear, placeholder: $.mask.placeholder, // Load default placeholder completed: null }, settings); @@ -269,8 +271,16 @@ $.fn.extend({ if (allow) { writeBuffer(); } else if (lastMatch + 1 < partialPosition) { - input.val(""); - clearBuffer(0, len); + if (settings.autoclear) { + // Invalid value. Remove it and replace it with the + // mask, which is the default behavior. + input.val(""); + clearBuffer(0, len); + } else { + // Invalid value, but we opt to show the value to the + // user and allow them to correct their mistake. + writeBuffer(); + } } else { writeBuffer(); input.val(input.val().substring(0, lastMatch + 1)); @@ -296,8 +306,9 @@ $.fn.extend({ var pos; focusText = input.val(); + pos = checkVal(); - + caretTimeoutId = setTimeout(function(){ writeBuffer(); if (pos == mask.length) { @@ -309,20 +320,22 @@ $.fn.extend({ }) .bind("blur.mask", function() { checkVal(); + if (input.val() != focusText) input.change(); }) .bind("keydown.mask", keydownEvent) .bind("keypress.mask", keypressEvent) .bind(pasteEventName, function() { - setTimeout(function() { + setTimeout(function() { var pos=checkVal(true); - input.caret(pos); + input.caret(pos); if (settings.completed && pos == input.val().length) settings.completed.call(input); }, 0); }); - checkVal(); //Perform initial check for existing values + + checkVal(); //Perform initial check for existing values }); } }); From b9c0097c176d41c9d7e3d3dd6be5fed8115abcda Mon Sep 17 00:00:00 2001 From: Greg Burghardt Date: Thu, 10 Oct 2013 13:17:49 -0400 Subject: [PATCH 033/116] Fixing default mask display with "autoclear" set to false When a mask is initialized, a text field with an empty value should show up as a blank field. When "autoclear" was set to false, it showed the mask even when the user did not have focus on the field. This fixes that issue. --- demo/index.html | 8 +++++--- dist/jquery.maskedinput.js | 5 ++++- dist/jquery.maskedinput.min.js | 2 +- spec/Init.Spec.js | 12 +++++++++++- src/jquery.maskedinput.js | 3 ++- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/demo/index.html b/demo/index.html index 60d7e12..68dfe10 100644 --- a/demo/index.html +++ b/demo/index.html @@ -2,7 +2,7 @@ jQuery Mask Test - + @@ -38,6 +39,7 @@ Eye Script~9.99 ~9.99 999 Purchase Orderaaa-999-*** Percent99% + Phone (autoclear=false)99%
diff --git a/dist/jquery.maskedinput.js b/dist/jquery.maskedinput.js index 7ecfcc0..656174f 100644 --- a/dist/jquery.maskedinput.js +++ b/dist/jquery.maskedinput.js @@ -114,6 +114,7 @@ $.fn.extend({ return defs[c] ? settings.placeholder : c; } }), + defaultBuffer = buffer.join(''), focusText = input.val(); function seekNext(pos) { @@ -278,7 +279,9 @@ $.fn.extend({ if (allow) { writeBuffer(); } else if (lastMatch + 1 < partialPosition) { - if (settings.autoclear) { + if (settings.autoclear || buffer.join('') === defaultBuffer) { + // Invalid value. Remove it and replace it with the + // mask, which is the default behavior. input.val(""); clearBuffer(0, len); } else { diff --git a/dist/jquery.maskedinput.min.js b/dist/jquery.maskedinput.min.js index 558899e..9c6056c 100644 --- a/dist/jquery.maskedinput.min.js +++ b/dist/jquery.maskedinput.min.js @@ -4,4 +4,4 @@ Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) Version: 1.3.1 */ -!function(e){function t(){var e=document.createElement("input"),t="onpaste";return e.setAttribute(t,""),"function"==typeof e[t]?"paste":"input"}var n,a=t()+".mask",r=navigator.userAgent,i=/iphone/i.test(r),c=/android/i.test(r);e.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},e.fn.extend({caret:function(e,t){var n;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof e?(t="number"==typeof t?t:e,this.each(function(){this.setSelectionRange?this.setSelectionRange(e,t):this.createTextRange&&(n=this.createTextRange(),n.collapse(!0),n.moveEnd("character",t),n.moveStart("character",e),n.select())})):(this[0].setSelectionRange?(e=this[0].selectionStart,t=this[0].selectionEnd):document.selection&&document.selection.createRange&&(n=document.selection.createRange(),e=0-n.duplicate().moveStart("character",-1e5),t=e+n.text.length),{begin:e,end:t})},unmask:function(){return this.trigger("unmask")},mask:function(t,r){var o,l,u,s,f,h;return!t&&this.length>0?(o=e(this[0]),o.data(e.mask.dataName)()):(r=e.extend({autoclear:e.mask.autoclear,placeholder:e.mask.placeholder,completed:null},r),l=e.mask.definitions,u=[],s=h=t.length,f=null,e.each(t.split(""),function(e,t){"?"==t?(h--,s=e):l[t]?(u.push(new RegExp(l[t])),null===f&&(f=u.length-1)):u.push(null)}),this.trigger("unmask").each(function(){function o(e){for(;++e=0&&!u[e];);return e}function m(e,t){var n,a;if(!(0>e)){for(n=e,a=o(t);h>n;n++)if(u[n]){if(!(h>a&&u[n].test(R[a])))break;R[n]=R[a],R[a]=r.placeholder,a=o(a)}b(),x.caret(Math.max(f,e))}}function p(e){var t,n,a,i;for(t=e,n=r.placeholder;h>t;t++)if(u[t]){if(a=o(t),i=R[t],R[t]=n,!(h>a&&u[a].test(i)))break;n=i}}function g(e){var t,n,a,r=e.which;8===r||46===r||i&&127===r?(t=x.caret(),n=t.begin,a=t.end,0===a-n&&(n=46!==r?d(n):a=o(n-1),a=46===r?o(a):a),k(n,a),m(n,a-1),e.preventDefault()):27==r&&(x.val(S),x.caret(0,y()),e.preventDefault())}function v(t){var n,a,i,l=t.which,s=x.caret();t.ctrlKey||t.altKey||t.metaKey||32>l||l&&(0!==s.end-s.begin&&(k(s.begin,s.end),m(s.begin,s.end-1)),n=o(s.begin-1),h>n&&(a=String.fromCharCode(l),u[n].test(a)&&(p(n),R[n]=a,b(),i=o(n),c?setTimeout(e.proxy(e.fn.caret,x,i),0):x.caret(i),r.completed&&i>=h&&r.completed.call(x))),t.preventDefault())}function k(e,t){var n;for(n=e;t>n&&h>n;n++)u[n]&&(R[n]=r.placeholder)}function b(){x.val(R.join(""))}function y(e){var t,n,a,i=x.val(),c=-1;for(t=0,a=0;h>t;t++)if(u[t]){for(R[t]=r.placeholder;a++i.length)break}else R[t]===i.charAt(a)&&t!==s&&(a++,c=t);return e?b():s>c+1?r.autoclear?(x.val(""),k(0,h)):b():(b(),x.val(x.val().substring(0,c+1))),s?t:f}var x=e(this),R=e.map(t.split(""),function(e){return"?"!=e?l[e]?r.placeholder:e:void 0}),S=x.val();x.data(e.mask.dataName,function(){return e.map(R,function(e,t){return u[t]&&e!=r.placeholder?e:null}).join("")}),x.attr("readonly")||x.one("unmask",function(){x.unbind(".mask").removeData(e.mask.dataName)}).bind("focus.mask",function(){clearTimeout(n);var e;S=x.val(),e=y(),n=setTimeout(function(){b(),e==t.length?x.caret(0,e):x.caret(e)},10)}).bind("blur.mask",function(){y(),x.val()!=S&&x.change()}).bind("keydown.mask",g).bind("keypress.mask",v).bind(a,function(){setTimeout(function(){var e=y(!0);x.caret(e),r.completed&&e==x.val().length&&r.completed.call(x)},0)}),y()}))}})}(jQuery); \ No newline at end of file +!function(e){function t(){var e=document.createElement("input"),t="onpaste";return e.setAttribute(t,""),"function"==typeof e[t]?"paste":"input"}var n,a=t()+".mask",r=navigator.userAgent,i=/iphone/i.test(r),c=/android/i.test(r);e.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},e.fn.extend({caret:function(e,t){var n;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof e?(t="number"==typeof t?t:e,this.each(function(){this.setSelectionRange?this.setSelectionRange(e,t):this.createTextRange&&(n=this.createTextRange(),n.collapse(!0),n.moveEnd("character",t),n.moveStart("character",e),n.select())})):(this[0].setSelectionRange?(e=this[0].selectionStart,t=this[0].selectionEnd):document.selection&&document.selection.createRange&&(n=document.selection.createRange(),e=0-n.duplicate().moveStart("character",-1e5),t=e+n.text.length),{begin:e,end:t})},unmask:function(){return this.trigger("unmask")},mask:function(t,r){var o,l,u,s,f,h;return!t&&this.length>0?(o=e(this[0]),o.data(e.mask.dataName)()):(r=e.extend({autoclear:e.mask.autoclear,placeholder:e.mask.placeholder,completed:null},r),l=e.mask.definitions,u=[],s=h=t.length,f=null,e.each(t.split(""),function(e,t){"?"==t?(h--,s=e):l[t]?(u.push(new RegExp(l[t])),null===f&&(f=u.length-1)):u.push(null)}),this.trigger("unmask").each(function(){function o(e){for(;++e=0&&!u[e];);return e}function m(e,t){var n,a;if(!(0>e)){for(n=e,a=o(t);h>n;n++)if(u[n]){if(!(h>a&&u[n].test(R[a])))break;R[n]=R[a],R[a]=r.placeholder,a=o(a)}b(),x.caret(Math.max(f,e))}}function p(e){var t,n,a,i;for(t=e,n=r.placeholder;h>t;t++)if(u[t]){if(a=o(t),i=R[t],R[t]=n,!(h>a&&u[a].test(i)))break;n=i}}function g(e){var t,n,a,r=e.which;8===r||46===r||i&&127===r?(t=x.caret(),n=t.begin,a=t.end,0===a-n&&(n=46!==r?d(n):a=o(n-1),a=46===r?o(a):a),k(n,a),m(n,a-1),e.preventDefault()):27==r&&(x.val(A),x.caret(0,y()),e.preventDefault())}function v(t){var n,a,i,l=t.which,s=x.caret();t.ctrlKey||t.altKey||t.metaKey||32>l||l&&(0!==s.end-s.begin&&(k(s.begin,s.end),m(s.begin,s.end-1)),n=o(s.begin-1),h>n&&(a=String.fromCharCode(l),u[n].test(a)&&(p(n),R[n]=a,b(),i=o(n),c?setTimeout(e.proxy(e.fn.caret,x,i),0):x.caret(i),r.completed&&i>=h&&r.completed.call(x))),t.preventDefault())}function k(e,t){var n;for(n=e;t>n&&h>n;n++)u[n]&&(R[n]=r.placeholder)}function b(){x.val(R.join(""))}function y(e){var t,n,a,i=x.val(),c=-1;for(t=0,a=0;h>t;t++)if(u[t]){for(R[t]=r.placeholder;a++i.length)break}else R[t]===i.charAt(a)&&t!==s&&(a++,c=t);return e?b():s>c+1?r.autoclear||R.join("")===S?(x.val(""),k(0,h)):b():(b(),x.val(x.val().substring(0,c+1))),s?t:f}var x=e(this),R=e.map(t.split(""),function(e){return"?"!=e?l[e]?r.placeholder:e:void 0}),S=R.join(""),A=x.val();x.data(e.mask.dataName,function(){return e.map(R,function(e,t){return u[t]&&e!=r.placeholder?e:null}).join("")}),x.attr("readonly")||x.one("unmask",function(){x.unbind(".mask").removeData(e.mask.dataName)}).bind("focus.mask",function(){clearTimeout(n);var e;A=x.val(),e=y(),n=setTimeout(function(){b(),e==t.length?x.caret(0,e):x.caret(e)},10)}).bind("blur.mask",function(){y(),x.val()!=A&&x.change()}).bind("keydown.mask",g).bind("keypress.mask",v).bind(a,function(){setTimeout(function(){var e=y(!0);x.caret(e),r.completed&&e==x.val().length&&r.completed.call(x)},0)}),y()}))}})}(jQuery); \ No newline at end of file diff --git a/spec/Init.Spec.js b/spec/Init.Spec.js index 1b64130..0bd7dea 100644 --- a/spec/Init.Spec.js +++ b/spec/Init.Spec.js @@ -47,5 +47,15 @@ feature("Initializing a Mask",function(){ }); }); - scenario("An input with an invalid value and placeholders remaining"); + scenario("An input no value and autoclear set to false", function() { + given("an input with no value",function(){ + input.val(""); + }); + when("setting a mask with autoclear set to false",function(){ + input.mask("(999) 999-9999", { autoclear: false }); + }); + then("the value should be empty",function(){ + expect(input).toHaveValue(""); + }); + }); }); diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 6382f70..060a946 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -107,6 +107,7 @@ $.fn.extend({ return defs[c] ? settings.placeholder : c; } }), + defaultBuffer = buffer.join(''), focusText = input.val(); function seekNext(pos) { @@ -271,7 +272,7 @@ $.fn.extend({ if (allow) { writeBuffer(); } else if (lastMatch + 1 < partialPosition) { - if (settings.autoclear) { + if (settings.autoclear || buffer.join('') === defaultBuffer) { // Invalid value. Remove it and replace it with the // mask, which is the default behavior. input.val(""); From e677e8da7765a4e8ad90ab7d3a9e58daa7ea723d Mon Sep 17 00:00:00 2001 From: b2k Date: Fri, 23 Aug 2013 14:11:01 -0500 Subject: [PATCH 034/116] Fix for chrome browser on Android On a Samsung Galaxy, Chrome version 28 does not throw the keypress event. Here is a fix for that. --- src/jquery.maskedinput.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 749da65..0869182 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -8,6 +8,7 @@ function getPasteEvent() { var pasteEventName = getPasteEvent() + ".mask", ua = navigator.userAgent, iPhone = /iphone/i.test(ua), + chrome = /chrome/i.test(ua), android=/android/i.test(ua), caretTimeoutId; @@ -195,6 +196,22 @@ $.fn.extend({ c, next; + if (k == 0) { + // unable to detect key pressed. Grab it from pos and adjust + // this is a failsafe for mobile chrome + // which can't detect keypress events + // reliably + if (pos.begin >= len) { + input.val(input.val().substr(0, len)); + e.preventDefault(); + return false; + } + if (pos.begin == pos.end) { + k = input.val().charCodeAt(pos.begin - 1); + pos.begin--; + pos.end--; + } + } if (e.ctrlKey || e.altKey || e.metaKey || k < 32) {//Ignore return; } else if (k) { @@ -322,8 +339,10 @@ $.fn.extend({ settings.completed.call(input); }, 0); }); + if (chrome) { + input.bind("keyup.mask", keypressEvent); + } checkVal(); //Perform initial check for existing values }); } }); - From 4791c99777c8468274e19401f5024dca0059d7e8 Mon Sep 17 00:00:00 2001 From: b2k Date: Fri, 23 Aug 2013 14:15:57 -0500 Subject: [PATCH 035/116] Add android to if statement --- src/jquery.maskedinput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 0869182..9c0d392 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -339,7 +339,7 @@ $.fn.extend({ settings.completed.call(input); }, 0); }); - if (chrome) { + if (chrome && android) { input.bind("keyup.mask", keypressEvent); } checkVal(); //Perform initial check for existing values From 0589b7ccc3c78aad5c42aefa23b1a34b4e1c6bf6 Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Mon, 28 Oct 2013 23:49:18 -0400 Subject: [PATCH 036/116] Changes the Jakefile to work with node v0.10.x Node introduced a breaking change in v0.10 where all FS module actions require a callback to be passed. This change simply passes an empty callback that will swallow any errors passed in. Basically keeping things the same as they were in node v0.8.x Read More: https://github.com/joyent/node/issues/5005 --- Jakefile | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Jakefile b/Jakefile index 5001cb7..901dc9f 100644 --- a/Jakefile +++ b/Jakefile @@ -5,27 +5,27 @@ var Handlebars=require("handlebars"), distPath='dist/'; Handlebars.registerHelper('include', function(context) { - return fs.readFileSync(context,'utf8'); + return fs.readFileSync(context,'utf8', function() { /* swallow errors */ }); }); -function keepComment(node,comment){ +function keepComment(node,comment){ return comment.type === "comment2"; } task('clean',function(){ - fs.rmdir(distPath) + fs.rmdir(distPath, function() { /* swallow errors */ }); }); task('default',['clean'], function (params) { - fs.mkdir(distPath,0755); + fs.mkdir(distPath,0755, function() { /* swallow errors */ }); - var options = JSON.parse(fs.readFileSync('plugin.json','utf8')) + var options = JSON.parse(fs.readFileSync('plugin.json','utf8', function() { /* swallow errors */ })) options.Year=new Date().getFullYear() - var template = Handlebars.compile(fs.readFileSync('templates/jquery.maskedinput.template','utf8')); + var template = Handlebars.compile(fs.readFileSync('templates/jquery.maskedinput.template','utf8', function() { /* swallow errors */ })); var debugFile = path.join(distPath,'jquery.maskedinput.js'); fs.writeFileSync(debugFile,template(options)); compressed = UglifyJS.minify(debugFile,{output:{comments:keepComment}}); fs.writeFileSync(path.join(distPath,'jquery.maskedinput.min.js'), compressed.code); -}); \ No newline at end of file +}); From 86435ffed97ea2e060f0fc8b4ae4a886a7b20ebe Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Tue, 29 Oct 2013 00:10:59 -0400 Subject: [PATCH 037/116] adds node_modules/ to gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index bfa0621..3175dd7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .idea/* + +node_modules/ From b3ce183ff41825c358b46dbe6e9fc7840bf4ef6e Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Tue, 29 Oct 2013 01:04:25 -0400 Subject: [PATCH 038/116] changes to gruntjs powered build The existing SpecRunner.html is still in /spec, I didn't see the need to change it, so it will still load the specs correctly. It will need to be updated whenever a new spec file is added, but I don't think this is too much work. The grunt build will automatically pick up new spec files and executes via the command line. Also, this will enable integration via Travis-CI so in the future we can have PRs automatically tested before we accept them. --- Jakefile | 31 --------------------------- README.md | 12 +++++++++++ gruntfile.js | 27 +++++++++++++++++++++++ package.json | 12 +++++++++++ spec/lib/setup.js | 14 ++++++++++++ src/jquery.maskedinput.js | 17 +++++++++++---- templates/jquery.maskedinput.template | 9 -------- 7 files changed, 78 insertions(+), 44 deletions(-) delete mode 100644 Jakefile create mode 100644 gruntfile.js create mode 100644 package.json create mode 100644 spec/lib/setup.js delete mode 100644 templates/jquery.maskedinput.template diff --git a/Jakefile b/Jakefile deleted file mode 100644 index 5001cb7..0000000 --- a/Jakefile +++ /dev/null @@ -1,31 +0,0 @@ -var Handlebars=require("handlebars"), - fs = require("fs"), - path = require ("path"), - UglifyJS = require("uglify-js"), - distPath='dist/'; - -Handlebars.registerHelper('include', function(context) { - return fs.readFileSync(context,'utf8'); -}); - -function keepComment(node,comment){ - return comment.type === "comment2"; -} - -task('clean',function(){ - fs.rmdir(distPath) -}); - -task('default',['clean'], function (params) { - fs.mkdir(distPath,0755); - - var options = JSON.parse(fs.readFileSync('plugin.json','utf8')) - options.Year=new Date().getFullYear() - - var template = Handlebars.compile(fs.readFileSync('templates/jquery.maskedinput.template','utf8')); - var debugFile = path.join(distPath,'jquery.maskedinput.js'); - fs.writeFileSync(debugFile,template(options)); - - compressed = UglifyJS.minify(debugFile,{output:{comments:keepComment}}); - fs.writeFileSync(path.join(distPath,'jquery.maskedinput.min.js'), compressed.code); -}); \ No newline at end of file diff --git a/README.md b/README.md index ef6f39b..bcc7370 100644 --- a/README.md +++ b/README.md @@ -13,3 +13,15 @@ If your requirements aren't met by the predefined placeholders, you can always a By design, this plugin will reject input which doesn't complete the mask. You can bypass this by using a '?' character at the position where you would like to consider input optional. For example, a mask of "(999) 999-9999? x99999" would require only the first 10 digits of a phone number with extension being optional. +Setting up your Developer Environment +------------------------------------- +jQuery Masked Input uses [NodeJS](http://www.nodejs.org) and [GruntJS](http://www.gruntjs.com) as it's developer platform and build automation tool. + +To get your environment setup correctly, you'll need nodejs version 0.8.25 or greater installed. + +Once node is installed on your system all that you need to do is install the developer dependencies and run the grunt build: + + $ npm install + $ grunt + +All of the tests for jQuery Masked Input are run using the [jasmine](http://pivotal.github.io/jasmine/) test runner. diff --git a/gruntfile.js b/gruntfile.js new file mode 100644 index 0000000..27bd038 --- /dev/null +++ b/gruntfile.js @@ -0,0 +1,27 @@ + +"use strict"; + +module.exports = function( grunt ) { + grunt.initConfig({ + jasmine: { + full: { + src: "src/**/*.js", + options: { + specs: "spec/*Spec.js", + vendor: [ + "spec/lib/matchers.js", + "spec/lib/jasmine-species/jasmine-grammar.js", + "spec/lib/setup.js", + "lib/jquery-1.9.0.min.js", + "spec/lib/jquery.keymasher.js" + ] + } + } + } + }); + + grunt.loadNpmTasks("grunt-contrib-jasmine"); + + grunt.registerTask('test', ['jasmine']); + grunt.registerTask('default', ['test']); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..bd9e406 --- /dev/null +++ b/package.json @@ -0,0 +1,12 @@ +{ + "name": "jquery.maskedinput", + "version": "1.3.0", + "author": "Josh Bush", + "description": "jQuery Masked Input Plugin", + + "devDependencies": { + "grunt": "0.4.x", + "grunt-contrib-jasmine": "0.5.x", + "grunt-contrib-watch": "0.5.x" + } +} diff --git a/spec/lib/setup.js b/spec/lib/setup.js new file mode 100644 index 0000000..9539215 --- /dev/null +++ b/spec/lib/setup.js @@ -0,0 +1,14 @@ +function importGrammar(g){ + for (var prop in g) { + if (g.hasOwnProperty(prop)) + window[prop] = g[prop]; + + } +} + +importGrammar(jasmine.grammar.FeatureStory); +importGrammar(jasmine.grammar.GWT); + +var input; +beforeEach(function(){ input = $("").appendTo("body").focus(); }); +afterEach(function(){ input.remove();}); diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 9c0d392..ef7fba8 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -1,8 +1,16 @@ +/* + Masked Input plugin for jQuery + Copyright (c) 2007-{{Year}} Josh Bush (digitalbush.com) + Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) + Version: {{version}} +*/ +(function($) { + function getPasteEvent() { var el = document.createElement('input'), name = 'onpaste'; el.setAttribute(name, ''); - return (typeof el[name] === 'function')?'paste':'input'; + return (typeof el[name] === 'function')?'paste':'input'; } var pasteEventName = getPasteEvent() + ".mask", @@ -314,7 +322,7 @@ $.fn.extend({ focusText = input.val(); pos = checkVal(); - + caretTimeoutId = setTimeout(function(){ writeBuffer(); if (pos == mask.length) { @@ -332,9 +340,9 @@ $.fn.extend({ .bind("keydown.mask", keydownEvent) .bind("keypress.mask", keypressEvent) .bind(pasteEventName, function() { - setTimeout(function() { + setTimeout(function() { var pos=checkVal(true); - input.caret(pos); + input.caret(pos); if (settings.completed && pos == input.val().length) settings.completed.call(input); }, 0); @@ -346,3 +354,4 @@ $.fn.extend({ }); } }); +})(jQuery); diff --git a/templates/jquery.maskedinput.template b/templates/jquery.maskedinput.template deleted file mode 100644 index 18f6d17..0000000 --- a/templates/jquery.maskedinput.template +++ /dev/null @@ -1,9 +0,0 @@ -/* - Masked Input plugin for jQuery - Copyright (c) 2007-{{Year}} Josh Bush (digitalbush.com) - Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) - Version: {{version}} -*/ -(function($) { - {{{include "src/jquery.maskedinput.js"}}} -})(jQuery); \ No newline at end of file From 02ad44c2bf0d9a5638b7a876f88323123312d116 Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Tue, 29 Oct 2013 21:21:05 -0400 Subject: [PATCH 039/116] removes interpolation keys from banner comment --- src/jquery.maskedinput.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index ef7fba8..5a1fc52 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -1,8 +1,8 @@ /* Masked Input plugin for jQuery - Copyright (c) 2007-{{Year}} Josh Bush (digitalbush.com) + Copyright (c) 2007-2013 Josh Bush (digitalbush.com) Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) - Version: {{version}} + Version: 1.3 */ (function($) { From b28407984d76edfd9197b4d800baae7643f90eca Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Tue, 29 Oct 2013 21:55:59 -0400 Subject: [PATCH 040/116] banner comments are generated using data from package.json when we merge #105 we should change the to load component.json instead --- dist/jquery.maskedinput.js | 466 +++++++++------------------------ dist/jquery.maskedinput.min.js | 10 +- gruntfile.js | 28 +- package.json | 5 +- src/jquery.maskedinput.js | 6 - 5 files changed, 165 insertions(+), 350 deletions(-) diff --git a/dist/jquery.maskedinput.js b/dist/jquery.maskedinput.js index 89ce020..f5a5e29 100644 --- a/dist/jquery.maskedinput.js +++ b/dist/jquery.maskedinput.js @@ -1,338 +1,132 @@ /* - Masked Input plugin for jQuery - Copyright (c) 2007-2013 Josh Bush (digitalbush.com) - Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) - Version: 1.3.1 + jQuery Masked Input Plugin + Copyright (c) 2007 - 2013 Josh Bush (digitalbush.com) + Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) + Version: 1.3.0 */ -(function($) { - function getPasteEvent() { - var el = document.createElement('input'), - name = 'onpaste'; - el.setAttribute(name, ''); - return (typeof el[name] === 'function')?'paste':'input'; -} - -var pasteEventName = getPasteEvent() + ".mask", - ua = navigator.userAgent, - iPhone = /iphone/i.test(ua), - android=/android/i.test(ua), - caretTimeoutId; - -$.mask = { - //Predefined character definitions - definitions: { - '9': "[0-9]", - 'a': "[A-Za-z]", - '*': "[A-Za-z0-9]" - }, - dataName: "rawMaskFn", - placeholder: '_' -}; - -$.fn.extend({ - //Helper Function for Caret positioning - caret: function(begin, end) { - var range; - - if (this.length === 0 || this.is(":hidden")) { - return; - } - - if (typeof begin == 'number') { - end = (typeof end === 'number') ? end : begin; - return this.each(function() { - if (this.setSelectionRange) { - this.setSelectionRange(begin, end); - } else if (this.createTextRange) { - range = this.createTextRange(); - range.collapse(true); - range.moveEnd('character', end); - range.moveStart('character', begin); - range.select(); - } - }); - } else { - if (this[0].setSelectionRange) { - begin = this[0].selectionStart; - end = this[0].selectionEnd; - } else if (document.selection && document.selection.createRange) { - range = document.selection.createRange(); - begin = 0 - range.duplicate().moveStart('character', -100000); - end = begin + range.text.length; - } - return { begin: begin, end: end }; - } - }, - unmask: function() { - return this.trigger("unmask"); - }, - mask: function(mask, settings) { - var input, - defs, - tests, - partialPosition, - firstNonMaskPos, - len; - - if (!mask && this.length > 0) { - input = $(this[0]); - return input.data($.mask.dataName)(); - } - settings = $.extend({ - placeholder: $.mask.placeholder, // Load default placeholder - completed: null - }, settings); - - - defs = $.mask.definitions; - tests = []; - 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])); - if (firstNonMaskPos === null) { - firstNonMaskPos = tests.length - 1; - } - } else { - tests.push(null); - } - }); - - return this.trigger("unmask").each(function() { - var input = $(this), - buffer = $.map( - mask.split(""), - function(c, i) { - if (c != '?') { - return defs[c] ? settings.placeholder : c; - } - }), - focusText = input.val(); - - function seekNext(pos) { - while (++pos < len && !tests[pos]); - return pos; - } - - function seekPrev(pos) { - while (--pos >= 0 && !tests[pos]); - return pos; - } - - function shiftL(begin,end) { - var i, - j; - - if (begin<0) { - return; - } - - for (i = begin, j = seekNext(end); i < len; i++) { - if (tests[i]) { - if (j < len && tests[i].test(buffer[j])) { - buffer[i] = buffer[j]; - buffer[j] = settings.placeholder; - } else { - break; - } - - j = seekNext(j); - } - } - writeBuffer(); - input.caret(Math.max(firstNonMaskPos, begin)); - } - - function shiftR(pos) { - var i, - c, - j, - t; - - for (i = pos, c = settings.placeholder; i < len; i++) { - if (tests[i]) { - j = seekNext(i); - t = buffer[i]; - buffer[i] = c; - if (j < len && tests[j].test(t)) { - c = t; - } else { - break; - } - } - } - } - - function keydownEvent(e) { - var k = e.which, - pos, - begin, - end; - - //backspace, delete, and escape get special treatment - if (k === 8 || k === 46 || (iPhone && k === 127)) { - pos = input.caret(); - begin = pos.begin; - end = pos.end; - - if (end - begin === 0) { - begin=k!==46?seekPrev(begin):(end=seekNext(begin-1)); - end=k===46?seekNext(end):end; - } - clearBuffer(begin, end); - shiftL(begin, end - 1); - - e.preventDefault(); - } else if (k == 27) {//escape - input.val(focusText); - input.caret(0, checkVal()); - e.preventDefault(); - } - } - - function keypressEvent(e) { - var k = e.which, - pos = input.caret(), - p, - c, - next; - - if (e.ctrlKey || e.altKey || e.metaKey || k < 32) {//Ignore - return; - } else if (k) { - if (pos.end - pos.begin !== 0){ - clearBuffer(pos.begin, pos.end); - shiftL(pos.begin, pos.end-1); - } - - p = seekNext(pos.begin - 1); - if (p < len) { - c = String.fromCharCode(k); - if (tests[p].test(c)) { - shiftR(p); - - buffer[p] = c; - writeBuffer(); - next = seekNext(p); - - if(android){ - setTimeout($.proxy($.fn.caret,input,next),0); - }else{ - input.caret(next); - } - - if (settings.completed && next >= len) { - settings.completed.call(input); - } - } - } - e.preventDefault(); - } - } - - function clearBuffer(start, end) { - var i; - for (i = start; i < end && i < len; i++) { - if (tests[i]) { - buffer[i] = settings.placeholder; - } - } - } - - function writeBuffer() { input.val(buffer.join('')); } - - function checkVal(allow) { - //try to place characters where they belong - var test = input.val(), - lastMatch = -1, - i, - c; - - 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)) { - buffer[i] = c; - lastMatch = i; - break; - } - } - if (pos > test.length) { - break; - } - } else if (buffer[i] === test.charAt(pos) && i !== partialPosition) { - pos++; - lastMatch = i; - } - } - if (allow) { - writeBuffer(); - } else if (lastMatch + 1 < partialPosition) { - input.val(""); - clearBuffer(0, len); - } else { - writeBuffer(); - input.val(input.val().substring(0, lastMatch + 1)); - } - return (partialPosition ? i : firstNonMaskPos); - } - - input.data($.mask.dataName,function(){ - return $.map(buffer, function(c, i) { - return tests[i]&&c!=settings.placeholder ? c : null; - }).join(''); - }); - - if (!input.attr("readonly")) - input - .one("unmask", function() { - input - .unbind(".mask") - .removeData($.mask.dataName); - }) - .bind("focus.mask", function() { - clearTimeout(caretTimeoutId); - var pos, - moveCaret; - - focusText = input.val(); - pos = checkVal(); - - caretTimeoutId = setTimeout(function(){ - writeBuffer(); - if (pos == mask.length) { - input.caret(0, pos); - } else { - input.caret(pos); - } - }, 10); - }) - .bind("blur.mask", function() { - checkVal(); - if (input.val() != focusText) - input.change(); - }) - .bind("keydown.mask", keydownEvent) - .bind("keypress.mask", keypressEvent) - .bind(pasteEventName, function() { - setTimeout(function() { - var pos=checkVal(true); - input.caret(pos); - if (settings.completed && pos == input.val().length) - settings.completed.call(input); - }, 0); - }); - checkVal(); //Perform initial check for existing values - }); - } -}); - - -})(jQuery); +!function(a) { + function b() { + var a = document.createElement("input"), b = "onpaste"; + return a.setAttribute(b, ""), "function" == typeof a[b] ? "paste" : "input"; + } + var c, d = b() + ".mask", e = navigator.userAgent, f = /iphone/i.test(e), g = /chrome/i.test(e), h = /android/i.test(e); + a.mask = { + definitions: { + "9": "[0-9]", + a: "[A-Za-z]", + "*": "[A-Za-z0-9]" + }, + dataName: "rawMaskFn", + placeholder: "_" + }, a.fn.extend({ + caret: function(a, b) { + var c; + if (0 !== this.length && !this.is(":hidden")) return "number" == typeof a ? (b = "number" == typeof b ? b : a, + this.each(function() { + this.setSelectionRange ? this.setSelectionRange(a, b) : this.createTextRange && (c = this.createTextRange(), + c.collapse(!0), c.moveEnd("character", b), c.moveStart("character", a), c.select()); + })) : (this[0].setSelectionRange ? (a = this[0].selectionStart, b = this[0].selectionEnd) : document.selection && document.selection.createRange && (c = document.selection.createRange(), + a = 0 - c.duplicate().moveStart("character", -1e5), b = a + c.text.length), { + begin: a, + end: b + }); + }, + unmask: function() { + return this.trigger("unmask"); + }, + mask: function(b, e) { + var i, j, k, l, m, n; + return !b && this.length > 0 ? (i = a(this[0]), i.data(a.mask.dataName)()) : (e = a.extend({ + placeholder: a.mask.placeholder, + completed: null + }, e), j = a.mask.definitions, k = [], l = n = b.length, m = null, a.each(b.split(""), function(a, b) { + "?" == b ? (n--, l = a) : j[b] ? (k.push(new RegExp(j[b])), null === m && (m = k.length - 1)) : k.push(null); + }), this.trigger("unmask").each(function() { + function i(a) { + for (;++a < n && !k[a]; ) ; + return a; + } + function o(a) { + for (;--a >= 0 && !k[a]; ) ; + return a; + } + function p(a, b) { + var c, d; + if (!(0 > a)) { + for (c = a, d = i(b); n > c; c++) if (k[c]) { + if (!(n > d && k[c].test(x[d]))) break; + x[c] = x[d], x[d] = e.placeholder, d = i(d); + } + u(), w.caret(Math.max(m, a)); + } + } + function q(a) { + var b, c, d, f; + for (b = a, c = e.placeholder; n > b; b++) if (k[b]) { + if (d = i(b), f = x[b], x[b] = c, !(n > d && k[d].test(f))) break; + c = f; + } + } + function r(a) { + var b, c, d, e = a.which; + 8 === e || 46 === e || f && 127 === e ? (b = w.caret(), c = b.begin, d = b.end, + 0 === d - c && (c = 46 !== e ? o(c) : d = i(c - 1), d = 46 === e ? i(d) : d), t(c, d), + p(c, d - 1), a.preventDefault()) : 27 == e && (w.val(y), w.caret(0, v()), a.preventDefault()); + } + function s(b) { + var c, d, f, g = b.which, j = w.caret(); + if (0 == g) { + if (j.begin >= n) return w.val(w.val().substr(0, n)), b.preventDefault(), !1; + j.begin == j.end && (g = w.val().charCodeAt(j.begin - 1), j.begin--, j.end--); + } + b.ctrlKey || b.altKey || b.metaKey || 32 > g || g && (0 !== j.end - j.begin && (t(j.begin, j.end), + p(j.begin, j.end - 1)), c = i(j.begin - 1), n > c && (d = String.fromCharCode(g), + k[c].test(d) && (q(c), x[c] = d, u(), f = i(c), h ? setTimeout(a.proxy(a.fn.caret, w, f), 0) : w.caret(f), + e.completed && f >= n && e.completed.call(w))), b.preventDefault()); + } + function t(a, b) { + var c; + for (c = a; b > c && n > c; c++) k[c] && (x[c] = e.placeholder); + } + function u() { + w.val(x.join("")); + } + function v(a) { + var b, c, d, f = w.val(), g = -1; + for (b = 0, d = 0; n > b; b++) if (k[b]) { + for (x[b] = e.placeholder; d++ < f.length; ) if (c = f.charAt(d - 1), k[b].test(c)) { + x[b] = c, g = b; + break; + } + if (d > f.length) break; + } else x[b] === f.charAt(d) && b !== l && (d++, g = b); + return a ? u() : l > g + 1 ? (w.val(""), t(0, n)) : (u(), w.val(w.val().substring(0, g + 1))), + l ? b : m; + } + var w = a(this), x = a.map(b.split(""), function(a) { + return "?" != a ? j[a] ? e.placeholder : a : void 0; + }), y = w.val(); + w.data(a.mask.dataName, function() { + return a.map(x, function(a, b) { + return k[b] && a != e.placeholder ? a : null; + }).join(""); + }), w.attr("readonly") || w.one("unmask", function() { + w.unbind(".mask").removeData(a.mask.dataName); + }).bind("focus.mask", function() { + clearTimeout(c); + var a; + y = w.val(), a = v(), c = setTimeout(function() { + u(), a == b.length ? w.caret(0, a) : w.caret(a); + }, 10); + }).bind("blur.mask", function() { + v(), w.val() != y && w.change(); + }).bind("keydown.mask", r).bind("keypress.mask", s).bind(d, function() { + setTimeout(function() { + var a = v(!0); + w.caret(a), e.completed && a == w.val().length && e.completed.call(w); + }, 0); + }), g && h && w.bind("keyup.mask", s), v(); + })); + } + }); +}(jQuery); \ No newline at end of file diff --git a/dist/jquery.maskedinput.min.js b/dist/jquery.maskedinput.min.js index 0d9ce6e..45baf54 100644 --- a/dist/jquery.maskedinput.min.js +++ b/dist/jquery.maskedinput.min.js @@ -1,7 +1,7 @@ /* - Masked Input plugin for jQuery - Copyright (c) 2007-2013 Josh Bush (digitalbush.com) - Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) - Version: 1.3.1 + jQuery Masked Input Plugin + Copyright (c) 2007 - 2013 Josh Bush (digitalbush.com) + Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) + Version: 1.3.0 */ -(function(e){function t(){var e=document.createElement("input"),t="onpaste";return e.setAttribute(t,""),"function"==typeof e[t]?"paste":"input"}var n,a=t()+".mask",r=navigator.userAgent,i=/iphone/i.test(r),o=/android/i.test(r);e.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},dataName:"rawMaskFn",placeholder:"_"},e.fn.extend({caret:function(e,t){var n;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof e?(t="number"==typeof t?t:e,this.each(function(){this.setSelectionRange?this.setSelectionRange(e,t):this.createTextRange&&(n=this.createTextRange(),n.collapse(!0),n.moveEnd("character",t),n.moveStart("character",e),n.select())})):(this[0].setSelectionRange?(e=this[0].selectionStart,t=this[0].selectionEnd):document.selection&&document.selection.createRange&&(n=document.selection.createRange(),e=0-n.duplicate().moveStart("character",-1e5),t=e+n.text.length),{begin:e,end:t})},unmask:function(){return this.trigger("unmask")},mask:function(t,r){var c,l,s,u,f,h;return!t&&this.length>0?(c=e(this[0]),c.data(e.mask.dataName)()):(r=e.extend({placeholder:e.mask.placeholder,completed:null},r),l=e.mask.definitions,s=[],u=h=t.length,f=null,e.each(t.split(""),function(e,t){"?"==t?(h--,u=e):l[t]?(s.push(RegExp(l[t])),null===f&&(f=s.length-1)):s.push(null)}),this.trigger("unmask").each(function(){function c(e){for(;h>++e&&!s[e];);return e}function d(e){for(;--e>=0&&!s[e];);return e}function m(e,t){var n,a;if(!(0>e)){for(n=e,a=c(t);h>n;n++)if(s[n]){if(!(h>a&&s[n].test(R[a])))break;R[n]=R[a],R[a]=r.placeholder,a=c(a)}b(),x.caret(Math.max(f,e))}}function p(e){var t,n,a,i;for(t=e,n=r.placeholder;h>t;t++)if(s[t]){if(a=c(t),i=R[t],R[t]=n,!(h>a&&s[a].test(i)))break;n=i}}function g(e){var t,n,a,r=e.which;8===r||46===r||i&&127===r?(t=x.caret(),n=t.begin,a=t.end,0===a-n&&(n=46!==r?d(n):a=c(n-1),a=46===r?c(a):a),k(n,a),m(n,a-1),e.preventDefault()):27==r&&(x.val(S),x.caret(0,y()),e.preventDefault())}function v(t){var n,a,i,l=t.which,u=x.caret();t.ctrlKey||t.altKey||t.metaKey||32>l||l&&(0!==u.end-u.begin&&(k(u.begin,u.end),m(u.begin,u.end-1)),n=c(u.begin-1),h>n&&(a=String.fromCharCode(l),s[n].test(a)&&(p(n),R[n]=a,b(),i=c(n),o?setTimeout(e.proxy(e.fn.caret,x,i),0):x.caret(i),r.completed&&i>=h&&r.completed.call(x))),t.preventDefault())}function k(e,t){var n;for(n=e;t>n&&h>n;n++)s[n]&&(R[n]=r.placeholder)}function b(){x.val(R.join(""))}function y(e){var t,n,a=x.val(),i=-1;for(t=0,pos=0;h>t;t++)if(s[t]){for(R[t]=r.placeholder;pos++a.length)break}else R[t]===a.charAt(pos)&&t!==u&&(pos++,i=t);return e?b():u>i+1?(x.val(""),k(0,h)):(b(),x.val(x.val().substring(0,i+1))),u?t:f}var x=e(this),R=e.map(t.split(""),function(e){return"?"!=e?l[e]?r.placeholder:e:void 0}),S=x.val();x.data(e.mask.dataName,function(){return e.map(R,function(e,t){return s[t]&&e!=r.placeholder?e:null}).join("")}),x.attr("readonly")||x.one("unmask",function(){x.unbind(".mask").removeData(e.mask.dataName)}).bind("focus.mask",function(){clearTimeout(n);var e;S=x.val(),e=y(),n=setTimeout(function(){b(),e==t.length?x.caret(0,e):x.caret(e)},10)}).bind("blur.mask",function(){y(),x.val()!=S&&x.change()}).bind("keydown.mask",g).bind("keypress.mask",v).bind(a,function(){setTimeout(function(){var e=y(!0);x.caret(e),r.completed&&e==x.val().length&&r.completed.call(x)},0)}),y()}))}})})(jQuery); \ No newline at end of file +!function(a){function b(){var a=document.createElement("input"),b="onpaste";return a.setAttribute(b,""),"function"==typeof a[b]?"paste":"input"}var c,d=b()+".mask",e=navigator.userAgent,f=/iphone/i.test(e),g=/chrome/i.test(e),h=/android/i.test(e);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(b,e){var i,j,k,l,m,n;return!b&&this.length>0?(i=a(this[0]),i.data(a.mask.dataName)()):(e=a.extend({placeholder:a.mask.placeholder,completed:null},e),j=a.mask.definitions,k=[],l=n=b.length,m=null,a.each(b.split(""),function(a,b){"?"==b?(n--,l=a):j[b]?(k.push(new RegExp(j[b])),null===m&&(m=k.length-1)):k.push(null)}),this.trigger("unmask").each(function(){function i(a){for(;++a=0&&!k[a];);return a}function p(a,b){var c,d;if(!(0>a)){for(c=a,d=i(b);n>c;c++)if(k[c]){if(!(n>d&&k[c].test(x[d])))break;x[c]=x[d],x[d]=e.placeholder,d=i(d)}u(),w.caret(Math.max(m,a))}}function q(a){var b,c,d,f;for(b=a,c=e.placeholder;n>b;b++)if(k[b]){if(d=i(b),f=x[b],x[b]=c,!(n>d&&k[d].test(f)))break;c=f}}function r(a){var b,c,d,e=a.which;8===e||46===e||f&&127===e?(b=w.caret(),c=b.begin,d=b.end,0===d-c&&(c=46!==e?o(c):d=i(c-1),d=46===e?i(d):d),t(c,d),p(c,d-1),a.preventDefault()):27==e&&(w.val(y),w.caret(0,v()),a.preventDefault())}function s(b){var c,d,f,g=b.which,j=w.caret();if(0==g){if(j.begin>=n)return w.val(w.val().substr(0,n)),b.preventDefault(),!1;j.begin==j.end&&(g=w.val().charCodeAt(j.begin-1),j.begin--,j.end--)}b.ctrlKey||b.altKey||b.metaKey||32>g||g&&(0!==j.end-j.begin&&(t(j.begin,j.end),p(j.begin,j.end-1)),c=i(j.begin-1),n>c&&(d=String.fromCharCode(g),k[c].test(d)&&(q(c),x[c]=d,u(),f=i(c),h?setTimeout(a.proxy(a.fn.caret,w,f),0):w.caret(f),e.completed&&f>=n&&e.completed.call(w))),b.preventDefault())}function t(a,b){var c;for(c=a;b>c&&n>c;c++)k[c]&&(x[c]=e.placeholder)}function u(){w.val(x.join(""))}function v(a){var b,c,d,f=w.val(),g=-1;for(b=0,d=0;n>b;b++)if(k[b]){for(x[b]=e.placeholder;d++f.length)break}else x[b]===f.charAt(d)&&b!==l&&(d++,g=b);return a?u():l>g+1?(w.val(""),t(0,n)):(u(),w.val(w.val().substring(0,g+1))),l?b:m}var w=a(this),x=a.map(b.split(""),function(a){return"?"!=a?j[a]?e.placeholder:a:void 0}),y=w.val();w.data(a.mask.dataName,function(){return a.map(x,function(a,b){return k[b]&&a!=e.placeholder?a:null}).join("")}),w.attr("readonly")||w.one("unmask",function(){w.unbind(".mask").removeData(a.mask.dataName)}).bind("focus.mask",function(){clearTimeout(c);var a;y=w.val(),a=v(),c=setTimeout(function(){u(),a==b.length?w.caret(0,a):w.caret(a)},10)}).bind("blur.mask",function(){v(),w.val()!=y&&w.change()}).bind("keydown.mask",r).bind("keypress.mask",s).bind(d,function(){setTimeout(function(){var a=v(!0);w.caret(a),e.completed&&a==w.val().length&&e.completed.call(w)},0)}),g&&h&&w.bind("keyup.mask",s),v()}))}})}(jQuery); \ No newline at end of file diff --git a/gruntfile.js b/gruntfile.js index 27bd038..2ec3979 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -3,6 +3,31 @@ module.exports = function( grunt ) { grunt.initConfig({ + // TODO: change to read component.json + pkg: require('./package.json'), + + uglify: { + options: { + banner: '/*\n <%= pkg.description %>\n Copyright (c) 2007 - <%= grunt.template.today("yyyy") %> <%= pkg.author %>\n Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license)\n Version: <%= pkg.version %>\n*/\n' + }, + + dev: { + options: { + beautify: true + }, + + files: { + 'dist/jquery.maskedinput.js': ['src/jquery.maskedinput.js'] + } + }, + + min: { + files: { + 'dist/jquery.maskedinput.min.js': ['src/jquery.maskedinput.js'] + } + } + }, + jasmine: { full: { src: "src/**/*.js", @@ -21,7 +46,8 @@ module.exports = function( grunt ) { }); grunt.loadNpmTasks("grunt-contrib-jasmine"); + grunt.loadNpmTasks("grunt-contrib-uglify"); grunt.registerTask('test', ['jasmine']); - grunt.registerTask('default', ['test']); + grunt.registerTask('default', ['test', 'uglify']); }; diff --git a/package.json b/package.json index bd9e406..11702b8 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,13 @@ { "name": "jquery.maskedinput", "version": "1.3.0", - "author": "Josh Bush", + "author": "Josh Bush (digitalbush.com)", "description": "jQuery Masked Input Plugin", "devDependencies": { "grunt": "0.4.x", "grunt-contrib-jasmine": "0.5.x", - "grunt-contrib-watch": "0.5.x" + "grunt-contrib-watch": "0.5.x", + "grunt-contrib-uglify": "0.2.x" } } diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 5a1fc52..a526da8 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -1,9 +1,3 @@ -/* - Masked Input plugin for jQuery - Copyright (c) 2007-2013 Josh Bush (digitalbush.com) - Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) - Version: 1.3 -*/ (function($) { function getPasteEvent() { From 1249c9c928b3c41568bdb529f7e47b0d9db332dc Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Tue, 29 Oct 2013 21:57:31 -0400 Subject: [PATCH 041/116] adds documentation about installing grunt-cli --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bcc7370..c95df69 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,9 @@ Setting up your Developer Environment ------------------------------------- jQuery Masked Input uses [NodeJS](http://www.nodejs.org) and [GruntJS](http://www.gruntjs.com) as it's developer platform and build automation tool. -To get your environment setup correctly, you'll need nodejs version 0.8.25 or greater installed. +To get your environment setup correctly, you'll need nodejs version 0.8.25 or greater installed. You'll also need to install the grunt command line tool: + + $ sudo npm install -g grunt-cli Once node is installed on your system all that you need to do is install the developer dependencies and run the grunt build: From 09ccf54932717db47c1cc253efc413d96c8e4115 Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Tue, 29 Oct 2013 22:00:03 -0400 Subject: [PATCH 042/116] uglify grunt task does not mangle uncompressed source --- dist/jquery.maskedinput.js | 183 +++++++++++++++++---------------- dist/jquery.maskedinput.min.js | 2 +- gruntfile.js | 3 +- 3 files changed, 100 insertions(+), 88 deletions(-) diff --git a/dist/jquery.maskedinput.js b/dist/jquery.maskedinput.js index f5a5e29..74e2a41 100644 --- a/dist/jquery.maskedinput.js +++ b/dist/jquery.maskedinput.js @@ -2,15 +2,15 @@ jQuery Masked Input Plugin Copyright (c) 2007 - 2013 Josh Bush (digitalbush.com) Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) - Version: 1.3.0 + Version: 1.3.1 */ -!function(a) { - function b() { - var a = document.createElement("input"), b = "onpaste"; - return a.setAttribute(b, ""), "function" == typeof a[b] ? "paste" : "input"; +!function($) { + function getPasteEvent() { + var el = document.createElement("input"), name = "onpaste"; + return el.setAttribute(name, ""), "function" == typeof el[name] ? "paste" : "input"; } - var c, d = b() + ".mask", e = navigator.userAgent, f = /iphone/i.test(e), g = /chrome/i.test(e), h = /android/i.test(e); - a.mask = { + var caretTimeoutId, pasteEventName = getPasteEvent() + ".mask", ua = navigator.userAgent, iPhone = /iphone/i.test(ua), chrome = /chrome/i.test(ua), android = /android/i.test(ua); + $.mask = { definitions: { "9": "[0-9]", a: "[A-Za-z]", @@ -18,114 +18,125 @@ }, dataName: "rawMaskFn", placeholder: "_" - }, a.fn.extend({ - caret: function(a, b) { - var c; - if (0 !== this.length && !this.is(":hidden")) return "number" == typeof a ? (b = "number" == typeof b ? b : a, + }, $.fn.extend({ + caret: function(begin, end) { + var range; + if (0 !== this.length && !this.is(":hidden")) return "number" == typeof begin ? (end = "number" == typeof end ? end : begin, this.each(function() { - this.setSelectionRange ? this.setSelectionRange(a, b) : this.createTextRange && (c = this.createTextRange(), - c.collapse(!0), c.moveEnd("character", b), c.moveStart("character", a), c.select()); - })) : (this[0].setSelectionRange ? (a = this[0].selectionStart, b = this[0].selectionEnd) : document.selection && document.selection.createRange && (c = document.selection.createRange(), - a = 0 - c.duplicate().moveStart("character", -1e5), b = a + c.text.length), { - begin: a, - end: b + this.setSelectionRange ? this.setSelectionRange(begin, end) : this.createTextRange && (range = this.createTextRange(), + range.collapse(!0), range.moveEnd("character", end), range.moveStart("character", begin), + range.select()); + })) : (this[0].setSelectionRange ? (begin = this[0].selectionStart, end = this[0].selectionEnd) : document.selection && document.selection.createRange && (range = document.selection.createRange(), + begin = 0 - range.duplicate().moveStart("character", -1e5), end = begin + range.text.length), + { + begin: begin, + end: end }); }, unmask: function() { return this.trigger("unmask"); }, - mask: function(b, e) { - var i, j, k, l, m, n; - return !b && this.length > 0 ? (i = a(this[0]), i.data(a.mask.dataName)()) : (e = a.extend({ - placeholder: a.mask.placeholder, + mask: function(mask, settings) { + var input, defs, tests, partialPosition, firstNonMaskPos, len; + return !mask && this.length > 0 ? (input = $(this[0]), input.data($.mask.dataName)()) : (settings = $.extend({ + placeholder: $.mask.placeholder, completed: null - }, e), j = a.mask.definitions, k = [], l = n = b.length, m = null, a.each(b.split(""), function(a, b) { - "?" == b ? (n--, l = a) : j[b] ? (k.push(new RegExp(j[b])), null === m && (m = k.length - 1)) : k.push(null); + }, settings), defs = $.mask.definitions, tests = [], partialPosition = len = mask.length, + firstNonMaskPos = null, $.each(mask.split(""), function(i, c) { + "?" == c ? (len--, partialPosition = i) : defs[c] ? (tests.push(new RegExp(defs[c])), + null === firstNonMaskPos && (firstNonMaskPos = tests.length - 1)) : tests.push(null); }), this.trigger("unmask").each(function() { - function i(a) { - for (;++a < n && !k[a]; ) ; - return a; + function seekNext(pos) { + for (;++pos < len && !tests[pos]; ) ; + return pos; } - function o(a) { - for (;--a >= 0 && !k[a]; ) ; - return a; + function seekPrev(pos) { + for (;--pos >= 0 && !tests[pos]; ) ; + return pos; } - function p(a, b) { - var c, d; - if (!(0 > a)) { - for (c = a, d = i(b); n > c; c++) if (k[c]) { - if (!(n > d && k[c].test(x[d]))) break; - x[c] = x[d], x[d] = e.placeholder, d = i(d); + function shiftL(begin, end) { + var i, j; + if (!(0 > begin)) { + for (i = begin, j = seekNext(end); len > i; i++) if (tests[i]) { + if (!(len > j && tests[i].test(buffer[j]))) break; + buffer[i] = buffer[j], buffer[j] = settings.placeholder, j = seekNext(j); } - u(), w.caret(Math.max(m, a)); + writeBuffer(), input.caret(Math.max(firstNonMaskPos, begin)); } } - function q(a) { - var b, c, d, f; - for (b = a, c = e.placeholder; n > b; b++) if (k[b]) { - if (d = i(b), f = x[b], x[b] = c, !(n > d && k[d].test(f))) break; - c = f; + function shiftR(pos) { + var i, c, j, t; + for (i = pos, c = settings.placeholder; len > i; i++) if (tests[i]) { + if (j = seekNext(i), t = buffer[i], buffer[i] = c, !(len > j && tests[j].test(t))) break; + c = t; } } - function r(a) { - var b, c, d, e = a.which; - 8 === e || 46 === e || f && 127 === e ? (b = w.caret(), c = b.begin, d = b.end, - 0 === d - c && (c = 46 !== e ? o(c) : d = i(c - 1), d = 46 === e ? i(d) : d), t(c, d), - p(c, d - 1), a.preventDefault()) : 27 == e && (w.val(y), w.caret(0, v()), a.preventDefault()); + function keydownEvent(e) { + var pos, begin, end, k = e.which; + 8 === k || 46 === k || iPhone && 127 === k ? (pos = input.caret(), begin = pos.begin, + end = pos.end, 0 === end - begin && (begin = 46 !== k ? seekPrev(begin) : end = seekNext(begin - 1), + end = 46 === k ? seekNext(end) : end), clearBuffer(begin, end), shiftL(begin, end - 1), + e.preventDefault()) : 27 == k && (input.val(focusText), input.caret(0, checkVal()), + e.preventDefault()); } - function s(b) { - var c, d, f, g = b.which, j = w.caret(); - if (0 == g) { - if (j.begin >= n) return w.val(w.val().substr(0, n)), b.preventDefault(), !1; - j.begin == j.end && (g = w.val().charCodeAt(j.begin - 1), j.begin--, j.end--); + function keypressEvent(e) { + var p, c, next, k = e.which, pos = input.caret(); + if (0 == k) { + if (pos.begin >= len) return input.val(input.val().substr(0, len)), e.preventDefault(), + !1; + pos.begin == pos.end && (k = input.val().charCodeAt(pos.begin - 1), pos.begin--, + pos.end--); } - b.ctrlKey || b.altKey || b.metaKey || 32 > g || g && (0 !== j.end - j.begin && (t(j.begin, j.end), - p(j.begin, j.end - 1)), c = i(j.begin - 1), n > c && (d = String.fromCharCode(g), - k[c].test(d) && (q(c), x[c] = d, u(), f = i(c), h ? setTimeout(a.proxy(a.fn.caret, w, f), 0) : w.caret(f), - e.completed && f >= n && e.completed.call(w))), b.preventDefault()); + e.ctrlKey || e.altKey || e.metaKey || 32 > k || k && (0 !== pos.end - pos.begin && (clearBuffer(pos.begin, pos.end), + shiftL(pos.begin, pos.end - 1)), p = seekNext(pos.begin - 1), len > p && (c = String.fromCharCode(k), + tests[p].test(c) && (shiftR(p), buffer[p] = c, writeBuffer(), next = seekNext(p), + android ? setTimeout($.proxy($.fn.caret, input, next), 0) : input.caret(next), settings.completed && next >= len && settings.completed.call(input))), + e.preventDefault()); } - function t(a, b) { - var c; - for (c = a; b > c && n > c; c++) k[c] && (x[c] = e.placeholder); + function clearBuffer(start, end) { + var i; + for (i = start; end > i && len > i; i++) tests[i] && (buffer[i] = settings.placeholder); } - function u() { - w.val(x.join("")); + function writeBuffer() { + input.val(buffer.join("")); } - function v(a) { - var b, c, d, f = w.val(), g = -1; - for (b = 0, d = 0; n > b; b++) if (k[b]) { - for (x[b] = e.placeholder; d++ < f.length; ) if (c = f.charAt(d - 1), k[b].test(c)) { - x[b] = c, g = b; + function checkVal(allow) { + var i, c, pos, test = input.val(), lastMatch = -1; + for (i = 0, pos = 0; len > i; i++) if (tests[i]) { + for (buffer[i] = settings.placeholder; pos++ < test.length; ) if (c = test.charAt(pos - 1), + tests[i].test(c)) { + buffer[i] = c, lastMatch = i; break; } - if (d > f.length) break; - } else x[b] === f.charAt(d) && b !== l && (d++, g = b); - return a ? u() : l > g + 1 ? (w.val(""), t(0, n)) : (u(), w.val(w.val().substring(0, g + 1))), - l ? b : m; + if (pos > test.length) break; + } else buffer[i] === test.charAt(pos) && i !== partialPosition && (pos++, lastMatch = i); + return allow ? writeBuffer() : partialPosition > lastMatch + 1 ? (input.val(""), + clearBuffer(0, len)) : (writeBuffer(), input.val(input.val().substring(0, lastMatch + 1))), + partialPosition ? i : firstNonMaskPos; } - var w = a(this), x = a.map(b.split(""), function(a) { - return "?" != a ? j[a] ? e.placeholder : a : void 0; - }), y = w.val(); - w.data(a.mask.dataName, function() { - return a.map(x, function(a, b) { - return k[b] && a != e.placeholder ? a : null; + var input = $(this), buffer = $.map(mask.split(""), function(c) { + return "?" != c ? defs[c] ? settings.placeholder : c : void 0; + }), focusText = input.val(); + input.data($.mask.dataName, function() { + return $.map(buffer, function(c, i) { + return tests[i] && c != settings.placeholder ? c : null; }).join(""); - }), w.attr("readonly") || w.one("unmask", function() { - w.unbind(".mask").removeData(a.mask.dataName); + }), input.attr("readonly") || input.one("unmask", function() { + input.unbind(".mask").removeData($.mask.dataName); }).bind("focus.mask", function() { - clearTimeout(c); - var a; - y = w.val(), a = v(), c = setTimeout(function() { - u(), a == b.length ? w.caret(0, a) : w.caret(a); + clearTimeout(caretTimeoutId); + var pos; + focusText = input.val(), pos = checkVal(), caretTimeoutId = setTimeout(function() { + writeBuffer(), pos == mask.length ? input.caret(0, pos) : input.caret(pos); }, 10); }).bind("blur.mask", function() { - v(), w.val() != y && w.change(); - }).bind("keydown.mask", r).bind("keypress.mask", s).bind(d, function() { + checkVal(), input.val() != focusText && input.change(); + }).bind("keydown.mask", keydownEvent).bind("keypress.mask", keypressEvent).bind(pasteEventName, function() { setTimeout(function() { - var a = v(!0); - w.caret(a), e.completed && a == w.val().length && e.completed.call(w); + var pos = checkVal(!0); + input.caret(pos), settings.completed && pos == input.val().length && settings.completed.call(input); }, 0); - }), g && h && w.bind("keyup.mask", s), v(); + }), chrome && android && input.bind("keyup.mask", keypressEvent), checkVal(); })); } }); diff --git a/dist/jquery.maskedinput.min.js b/dist/jquery.maskedinput.min.js index 45baf54..683f0e1 100644 --- a/dist/jquery.maskedinput.min.js +++ b/dist/jquery.maskedinput.min.js @@ -2,6 +2,6 @@ jQuery Masked Input Plugin Copyright (c) 2007 - 2013 Josh Bush (digitalbush.com) Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) - Version: 1.3.0 + Version: 1.3.1 */ !function(a){function b(){var a=document.createElement("input"),b="onpaste";return a.setAttribute(b,""),"function"==typeof a[b]?"paste":"input"}var c,d=b()+".mask",e=navigator.userAgent,f=/iphone/i.test(e),g=/chrome/i.test(e),h=/android/i.test(e);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(b,e){var i,j,k,l,m,n;return!b&&this.length>0?(i=a(this[0]),i.data(a.mask.dataName)()):(e=a.extend({placeholder:a.mask.placeholder,completed:null},e),j=a.mask.definitions,k=[],l=n=b.length,m=null,a.each(b.split(""),function(a,b){"?"==b?(n--,l=a):j[b]?(k.push(new RegExp(j[b])),null===m&&(m=k.length-1)):k.push(null)}),this.trigger("unmask").each(function(){function i(a){for(;++a=0&&!k[a];);return a}function p(a,b){var c,d;if(!(0>a)){for(c=a,d=i(b);n>c;c++)if(k[c]){if(!(n>d&&k[c].test(x[d])))break;x[c]=x[d],x[d]=e.placeholder,d=i(d)}u(),w.caret(Math.max(m,a))}}function q(a){var b,c,d,f;for(b=a,c=e.placeholder;n>b;b++)if(k[b]){if(d=i(b),f=x[b],x[b]=c,!(n>d&&k[d].test(f)))break;c=f}}function r(a){var b,c,d,e=a.which;8===e||46===e||f&&127===e?(b=w.caret(),c=b.begin,d=b.end,0===d-c&&(c=46!==e?o(c):d=i(c-1),d=46===e?i(d):d),t(c,d),p(c,d-1),a.preventDefault()):27==e&&(w.val(y),w.caret(0,v()),a.preventDefault())}function s(b){var c,d,f,g=b.which,j=w.caret();if(0==g){if(j.begin>=n)return w.val(w.val().substr(0,n)),b.preventDefault(),!1;j.begin==j.end&&(g=w.val().charCodeAt(j.begin-1),j.begin--,j.end--)}b.ctrlKey||b.altKey||b.metaKey||32>g||g&&(0!==j.end-j.begin&&(t(j.begin,j.end),p(j.begin,j.end-1)),c=i(j.begin-1),n>c&&(d=String.fromCharCode(g),k[c].test(d)&&(q(c),x[c]=d,u(),f=i(c),h?setTimeout(a.proxy(a.fn.caret,w,f),0):w.caret(f),e.completed&&f>=n&&e.completed.call(w))),b.preventDefault())}function t(a,b){var c;for(c=a;b>c&&n>c;c++)k[c]&&(x[c]=e.placeholder)}function u(){w.val(x.join(""))}function v(a){var b,c,d,f=w.val(),g=-1;for(b=0,d=0;n>b;b++)if(k[b]){for(x[b]=e.placeholder;d++f.length)break}else x[b]===f.charAt(d)&&b!==l&&(d++,g=b);return a?u():l>g+1?(w.val(""),t(0,n)):(u(),w.val(w.val().substring(0,g+1))),l?b:m}var w=a(this),x=a.map(b.split(""),function(a){return"?"!=a?j[a]?e.placeholder:a:void 0}),y=w.val();w.data(a.mask.dataName,function(){return a.map(x,function(a,b){return k[b]&&a!=e.placeholder?a:null}).join("")}),w.attr("readonly")||w.one("unmask",function(){w.unbind(".mask").removeData(a.mask.dataName)}).bind("focus.mask",function(){clearTimeout(c);var a;y=w.val(),a=v(),c=setTimeout(function(){u(),a==b.length?w.caret(0,a):w.caret(a)},10)}).bind("blur.mask",function(){v(),w.val()!=y&&w.change()}).bind("keydown.mask",r).bind("keypress.mask",s).bind(d,function(){setTimeout(function(){var a=v(!0);w.caret(a),e.completed&&a==w.val().length&&e.completed.call(w)},0)}),g&&h&&w.bind("keyup.mask",s),v()}))}})}(jQuery); \ No newline at end of file diff --git a/gruntfile.js b/gruntfile.js index 2ec3979..76bb0e8 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -13,7 +13,8 @@ module.exports = function( grunt ) { dev: { options: { - beautify: true + beautify: true, + mangle: false }, files: { From bf5b3a306baf9333bad334af7d550150616b609c Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Tue, 29 Oct 2013 22:00:13 -0400 Subject: [PATCH 043/116] corrects package version to 1.3.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 11702b8..8b3a95d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jquery.maskedinput", - "version": "1.3.0", + "version": "1.3.1", "author": "Josh Bush (digitalbush.com)", "description": "jQuery Masked Input Plugin", From debc0d1878fca126c09acdaccedae20d95a507b6 Mon Sep 17 00:00:00 2001 From: Greg Burghardt Date: Thu, 31 Oct 2013 09:00:07 -0400 Subject: [PATCH 044/116] Adding demo with optional markers and autoclear=false --- demo/index.html | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/demo/index.html b/demo/index.html index 68dfe10..8efda6a 100644 --- a/demo/index.html +++ b/demo/index.html @@ -16,7 +16,8 @@ $("#eyescript").mask("~9.99 ~9.99 999"); $("#po").mask("PO: aaa-999-***"); $("#pct").mask("99%"); - $("#phoneAutoclearFalse").mask("(999) 999-999-9999", { autoclear: false }); + $("#phoneAutoclearFalse").mask("(999) 999-9999", { autoclear: false }); + $("#phoneExtAutoclearFalse").mask("(999) 999-9999? x99999", { autoclear: false }); $("input").blur(function() { $("#info").html("Unmasked value: " + $(this).mask()); @@ -39,7 +40,8 @@ Eye Script~9.99 ~9.99 999 Purchase Orderaaa-999-*** Percent99% - Phone (autoclear=false)99% + Phone (autoclear=false)(999) 999-9999 + Phone + Ext (autoclear=false)(999) 999-9999? x99999
From 2f518e6f37df7b1bde026b4fa4a284faee686905 Mon Sep 17 00:00:00 2001 From: Greg Burghardt Date: Thu, 31 Oct 2013 09:01:19 -0400 Subject: [PATCH 045/116] Adding test coverage around masks with optional markers and autoclear=false --- spec/Focus.Spec.js | 48 +++++++++++++++++++++++++++ spec/Raw.Spec.js | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/spec/Focus.Spec.js b/spec/Focus.Spec.js index 01ddebd..f469bf5 100644 --- a/spec/Focus.Spec.js +++ b/spec/Focus.Spec.js @@ -123,6 +123,18 @@ feature("Optional marker",function(){ }); }); + scenario("Placeholders not filled to marker and autoclear = false", function() { + given("a mask with an optional marker",function(){ + input.mask("99?99", { autoclear: false }); + }); + when("typing one character and leaving",function(){ + input.mashKeys("1").blur(); + }); + then("value should be empty",function(){ + expect(input).toHaveValue("1___"); + }); + }); + scenario("Placeholders filled to marker",function(){ given("a mask with an optional marker",function(){ input.mask("99?99"); @@ -134,4 +146,40 @@ feature("Optional marker",function(){ expect(input).toHaveValue("12"); }); }); + + scenario("Placeholders filled to marker and autoclear = false", function() { + given("a mask with an optional marker",function(){ + input.mask("99?99", { autoclear: false }); + }); + when("typing two characters and leaving",function(){ + input.mashKeys("12").blur(); + }); + then("value should remain",function(){ + expect(input).toHaveValue("12"); + }); + }); + + scenario("Placeholders filled, one marker filled, and autoclear = false", function() { + given("a mask with an optional marker",function(){ + input.mask("99?99", { autoclear: false }); + }); + when("typing three characters and leaving",function(){ + input.mashKeys("123").blur(); + }); + then("value should remain",function(){ + expect(input).toHaveValue("123"); + }); + }); + + scenario("Placeholders and markers filled, and autoclear = false", function() { + given("a mask with an optional marker",function(){ + input.mask("99?99", { autoclear: false }); + }); + when("typing four characters and leaving",function(){ + input.mashKeys("1234").blur(); + }); + then("value should remain",function(){ + expect(input).toHaveValue("1234"); + }); + }); }); diff --git a/spec/Raw.Spec.js b/spec/Raw.Spec.js index 0516971..97be1f8 100644 --- a/spec/Raw.Spec.js +++ b/spec/Raw.Spec.js @@ -54,4 +54,86 @@ feature("Getting raw value",function(){ expect(input.mask()).toEqual("12"); }); }); +}); + +feature("Getting raw value with autoclear set to false", function() { + scenario("After typing",function(){ + given("an input with a mask containing a literal", function(){ + input.mask("9/9", { autoclear: false }); + }); + + when("typing all numbers",function(){ + input.mashKeys("12"); + }); + + then("raw value should be correct",function(){ + expect(input.mask()).toEqual("12"); + }); + }); + + scenario("While typing",function(){ + given("an input with a mask containing a literal", function(){ + input.mask("9/9", { autoclear: false }); + }); + + when("typing a number",function(){ + input.mashKeys("1"); + }); + + then("raw value should be correct",function(){ + expect(input.mask()).toEqual("1"); + }); + }); + + scenario("Before typing",function(){ + given("an input with a mask containing a literal", function(){ + input.mask("9/9", { autoclear: false }); + }); + + then("raw value should be correct",function(){ + expect(input.mask()).toEqual(""); + }); + }); + + scenario("After typing partial input past an optional marker",function(){ + given("an input with a mask containing a literal", function(){ + input.mask("9?99", { autoclear: false }); + }); + + when("typing a partial input",function(){ + input.mashKeys("12"); + }); + + then("raw value should be correct",function(){ + expect(input.mask()).toEqual("12"); + }); + }); + + scenario("After typing partial input",function(){ + given("an input with a mask containing a literal", function(){ + input.mask("99?99", { autoclear: false }); + }); + + when("typing a partial input",function(){ + input.mashKeys("1"); + }); + + then("raw value should be correct",function(){ + expect(input.mask()).toEqual("1"); + }); + }); + + scenario("After typing partial input up to an optional marker",function(){ + given("an input with a mask containing a literal", function(){ + input.mask("9?99", { autoclear: false }); + }); + + when("typing a partial input",function(){ + input.mashKeys("1"); + }); + + then("raw value should be correct",function(){ + expect(input.mask()).toEqual("1"); + }); + }); }); \ No newline at end of file From a4308eac7c3bffaeef6415cefec583b3230f5cae Mon Sep 17 00:00:00 2001 From: Greg Burghardt Date: Thu, 31 Oct 2013 09:47:41 -0400 Subject: [PATCH 046/116] Rebuilding distribution files after merging from upsteam/master. --- dist/jquery.maskedinput.js | 22 ++++++++++++++++++++-- dist/jquery.maskedinput.min.js | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/dist/jquery.maskedinput.js b/dist/jquery.maskedinput.js index 656174f..5c84271 100644 --- a/dist/jquery.maskedinput.js +++ b/dist/jquery.maskedinput.js @@ -15,6 +15,7 @@ var pasteEventName = getPasteEvent() + ".mask", ua = navigator.userAgent, iPhone = /iphone/i.test(ua), + chrome = /chrome/i.test(ua), android=/android/i.test(ua), caretTimeoutId; @@ -205,6 +206,22 @@ $.fn.extend({ c, next; + if (k == 0) { + // unable to detect key pressed. Grab it from pos and adjust + // this is a failsafe for mobile chrome + // which can't detect keypress events + // reliably + if (pos.begin >= len) { + input.val(input.val().substr(0, len)); + e.preventDefault(); + return false; + } + if (pos.begin == pos.end) { + k = input.val().charCodeAt(pos.begin - 1); + pos.begin--; + pos.end--; + } + } if (e.ctrlKey || e.altKey || e.metaKey || k < 32) {//Ignore return; } else if (k) { @@ -342,11 +359,12 @@ $.fn.extend({ settings.completed.call(input); }, 0); }); - + if (chrome && android) { + input.bind("keyup.mask", keypressEvent); + } checkVal(); //Perform initial check for existing values }); } }); - })(jQuery); \ No newline at end of file diff --git a/dist/jquery.maskedinput.min.js b/dist/jquery.maskedinput.min.js index 9c6056c..818d1c8 100644 --- a/dist/jquery.maskedinput.min.js +++ b/dist/jquery.maskedinput.min.js @@ -4,4 +4,4 @@ Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) Version: 1.3.1 */ -!function(e){function t(){var e=document.createElement("input"),t="onpaste";return e.setAttribute(t,""),"function"==typeof e[t]?"paste":"input"}var n,a=t()+".mask",r=navigator.userAgent,i=/iphone/i.test(r),c=/android/i.test(r);e.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},e.fn.extend({caret:function(e,t){var n;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof e?(t="number"==typeof t?t:e,this.each(function(){this.setSelectionRange?this.setSelectionRange(e,t):this.createTextRange&&(n=this.createTextRange(),n.collapse(!0),n.moveEnd("character",t),n.moveStart("character",e),n.select())})):(this[0].setSelectionRange?(e=this[0].selectionStart,t=this[0].selectionEnd):document.selection&&document.selection.createRange&&(n=document.selection.createRange(),e=0-n.duplicate().moveStart("character",-1e5),t=e+n.text.length),{begin:e,end:t})},unmask:function(){return this.trigger("unmask")},mask:function(t,r){var o,l,u,s,f,h;return!t&&this.length>0?(o=e(this[0]),o.data(e.mask.dataName)()):(r=e.extend({autoclear:e.mask.autoclear,placeholder:e.mask.placeholder,completed:null},r),l=e.mask.definitions,u=[],s=h=t.length,f=null,e.each(t.split(""),function(e,t){"?"==t?(h--,s=e):l[t]?(u.push(new RegExp(l[t])),null===f&&(f=u.length-1)):u.push(null)}),this.trigger("unmask").each(function(){function o(e){for(;++e=0&&!u[e];);return e}function m(e,t){var n,a;if(!(0>e)){for(n=e,a=o(t);h>n;n++)if(u[n]){if(!(h>a&&u[n].test(R[a])))break;R[n]=R[a],R[a]=r.placeholder,a=o(a)}b(),x.caret(Math.max(f,e))}}function p(e){var t,n,a,i;for(t=e,n=r.placeholder;h>t;t++)if(u[t]){if(a=o(t),i=R[t],R[t]=n,!(h>a&&u[a].test(i)))break;n=i}}function g(e){var t,n,a,r=e.which;8===r||46===r||i&&127===r?(t=x.caret(),n=t.begin,a=t.end,0===a-n&&(n=46!==r?d(n):a=o(n-1),a=46===r?o(a):a),k(n,a),m(n,a-1),e.preventDefault()):27==r&&(x.val(A),x.caret(0,y()),e.preventDefault())}function v(t){var n,a,i,l=t.which,s=x.caret();t.ctrlKey||t.altKey||t.metaKey||32>l||l&&(0!==s.end-s.begin&&(k(s.begin,s.end),m(s.begin,s.end-1)),n=o(s.begin-1),h>n&&(a=String.fromCharCode(l),u[n].test(a)&&(p(n),R[n]=a,b(),i=o(n),c?setTimeout(e.proxy(e.fn.caret,x,i),0):x.caret(i),r.completed&&i>=h&&r.completed.call(x))),t.preventDefault())}function k(e,t){var n;for(n=e;t>n&&h>n;n++)u[n]&&(R[n]=r.placeholder)}function b(){x.val(R.join(""))}function y(e){var t,n,a,i=x.val(),c=-1;for(t=0,a=0;h>t;t++)if(u[t]){for(R[t]=r.placeholder;a++i.length)break}else R[t]===i.charAt(a)&&t!==s&&(a++,c=t);return e?b():s>c+1?r.autoclear||R.join("")===S?(x.val(""),k(0,h)):b():(b(),x.val(x.val().substring(0,c+1))),s?t:f}var x=e(this),R=e.map(t.split(""),function(e){return"?"!=e?l[e]?r.placeholder:e:void 0}),S=R.join(""),A=x.val();x.data(e.mask.dataName,function(){return e.map(R,function(e,t){return u[t]&&e!=r.placeholder?e:null}).join("")}),x.attr("readonly")||x.one("unmask",function(){x.unbind(".mask").removeData(e.mask.dataName)}).bind("focus.mask",function(){clearTimeout(n);var e;A=x.val(),e=y(),n=setTimeout(function(){b(),e==t.length?x.caret(0,e):x.caret(e)},10)}).bind("blur.mask",function(){y(),x.val()!=A&&x.change()}).bind("keydown.mask",g).bind("keypress.mask",v).bind(a,function(){setTimeout(function(){var e=y(!0);x.caret(e),r.completed&&e==x.val().length&&r.completed.call(x)},0)}),y()}))}})}(jQuery); \ No newline at end of file +!function(e){function t(){var e=document.createElement("input"),t="onpaste";return e.setAttribute(t,""),"function"==typeof e[t]?"paste":"input"}var n,a=t()+".mask",r=navigator.userAgent,i=/iphone/i.test(r),c=/chrome/i.test(r),o=/android/i.test(r);e.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},e.fn.extend({caret:function(e,t){var n;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof e?(t="number"==typeof t?t:e,this.each(function(){this.setSelectionRange?this.setSelectionRange(e,t):this.createTextRange&&(n=this.createTextRange(),n.collapse(!0),n.moveEnd("character",t),n.moveStart("character",e),n.select())})):(this[0].setSelectionRange?(e=this[0].selectionStart,t=this[0].selectionEnd):document.selection&&document.selection.createRange&&(n=document.selection.createRange(),e=0-n.duplicate().moveStart("character",-1e5),t=e+n.text.length),{begin:e,end:t})},unmask:function(){return this.trigger("unmask")},mask:function(t,r){var l,u,s,f,d,h;return!t&&this.length>0?(l=e(this[0]),l.data(e.mask.dataName)()):(r=e.extend({autoclear:e.mask.autoclear,placeholder:e.mask.placeholder,completed:null},r),u=e.mask.definitions,s=[],f=h=t.length,d=null,e.each(t.split(""),function(e,t){"?"==t?(h--,f=e):u[t]?(s.push(new RegExp(u[t])),null===d&&(d=s.length-1)):s.push(null)}),this.trigger("unmask").each(function(){function l(e){for(;++e=0&&!s[e];);return e}function p(e,t){var n,a;if(!(0>e)){for(n=e,a=l(t);h>n;n++)if(s[n]){if(!(h>a&&s[n].test(A[a])))break;A[n]=A[a],A[a]=r.placeholder,a=l(a)}y(),R.caret(Math.max(d,e))}}function g(e){var t,n,a,i;for(t=e,n=r.placeholder;h>t;t++)if(s[t]){if(a=l(t),i=A[t],A[t]=n,!(h>a&&s[a].test(i)))break;n=i}}function v(e){var t,n,a,r=e.which;8===r||46===r||i&&127===r?(t=R.caret(),n=t.begin,a=t.end,0===a-n&&(n=46!==r?m(n):a=l(n-1),a=46===r?l(a):a),k(n,a),p(n,a-1),e.preventDefault()):27==r&&(R.val(T),R.caret(0,x()),e.preventDefault())}function b(t){var n,a,i,c=t.which,u=R.caret();if(0==c){if(u.begin>=h)return R.val(R.val().substr(0,h)),t.preventDefault(),!1;u.begin==u.end&&(c=R.val().charCodeAt(u.begin-1),u.begin--,u.end--)}t.ctrlKey||t.altKey||t.metaKey||32>c||c&&(0!==u.end-u.begin&&(k(u.begin,u.end),p(u.begin,u.end-1)),n=l(u.begin-1),h>n&&(a=String.fromCharCode(c),s[n].test(a)&&(g(n),A[n]=a,y(),i=l(n),o?setTimeout(e.proxy(e.fn.caret,R,i),0):R.caret(i),r.completed&&i>=h&&r.completed.call(R))),t.preventDefault())}function k(e,t){var n;for(n=e;t>n&&h>n;n++)s[n]&&(A[n]=r.placeholder)}function y(){R.val(A.join(""))}function x(e){var t,n,a,i=R.val(),c=-1;for(t=0,a=0;h>t;t++)if(s[t]){for(A[t]=r.placeholder;a++i.length)break}else A[t]===i.charAt(a)&&t!==f&&(a++,c=t);return e?y():f>c+1?r.autoclear||A.join("")===S?(R.val(""),k(0,h)):y():(y(),R.val(R.val().substring(0,c+1))),f?t:d}var R=e(this),A=e.map(t.split(""),function(e){return"?"!=e?u[e]?r.placeholder:e:void 0}),S=A.join(""),T=R.val();R.data(e.mask.dataName,function(){return e.map(A,function(e,t){return s[t]&&e!=r.placeholder?e:null}).join("")}),R.attr("readonly")||R.one("unmask",function(){R.unbind(".mask").removeData(e.mask.dataName)}).bind("focus.mask",function(){clearTimeout(n);var e;T=R.val(),e=x(),n=setTimeout(function(){y(),e==t.length?R.caret(0,e):R.caret(e)},10)}).bind("blur.mask",function(){x(),R.val()!=T&&R.change()}).bind("keydown.mask",v).bind("keypress.mask",b).bind(a,function(){setTimeout(function(){var e=x(!0);R.caret(e),r.completed&&e==R.val().length&&r.completed.call(R)},0)}),c&&o&&R.bind("keyup.mask",b),x()}))}})}(jQuery); \ No newline at end of file From 8250c1d12982ecd5d1f173a847c9b538221b2487 Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Thu, 31 Oct 2013 22:03:57 -0400 Subject: [PATCH 047/116] Jasmine Spec file suffix is not case sensitive --- gruntfile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gruntfile.js b/gruntfile.js index 76bb0e8..5896f86 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -33,7 +33,7 @@ module.exports = function( grunt ) { full: { src: "src/**/*.js", options: { - specs: "spec/*Spec.js", + specs: "spec/*[S|s]pec.js", vendor: [ "spec/lib/matchers.js", "spec/lib/jasmine-species/jasmine-grammar.js", From 00f855f840770bd8c1e91977266b83c72d1a614c Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Thu, 31 Oct 2013 22:08:22 -0400 Subject: [PATCH 048/116] removes spec/SpecRunner.html --- spec/SpecRunner.html | 68 -------------------------------------------- 1 file changed, 68 deletions(-) delete mode 100644 spec/SpecRunner.html diff --git a/spec/SpecRunner.html b/spec/SpecRunner.html deleted file mode 100644 index 0339b85..0000000 --- a/spec/SpecRunner.html +++ /dev/null @@ -1,68 +0,0 @@ - - - - Masked Input Plugin Tests - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - From 027f1de0b4c2561466ebbcafa1d2c558ad8236aa Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Sat, 2 Nov 2013 08:45:54 -0500 Subject: [PATCH 049/116] Adding completed callback to demo. --- demo/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/index.html b/demo/index.html index 8efda6a..d297b1e 100644 --- a/demo/index.html +++ b/demo/index.html @@ -16,7 +16,7 @@ $("#eyescript").mask("~9.99 ~9.99 999"); $("#po").mask("PO: aaa-999-***"); $("#pct").mask("99%"); - $("#phoneAutoclearFalse").mask("(999) 999-9999", { autoclear: false }); + $("#phoneAutoclearFalse").mask("(999) 999-9999", { autoclear: false, completed:function(){alert("completed autoclear!");} }); $("#phoneExtAutoclearFalse").mask("(999) 999-9999? x99999", { autoclear: false }); $("input").blur(function() { From 966d354de4368d416ecb1b6f72263d8b18930a38 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Sat, 2 Nov 2013 14:11:41 -0500 Subject: [PATCH 050/116] Trying to get CI working. --- .travis.yml | 5 +++++ package.json | 3 +++ 2 files changed, 8 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..0ce3be2 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: + - "0.10" +before_script: + - npm install -g grunt-cli \ No newline at end of file diff --git a/package.json b/package.json index 8b3a95d..2f8f240 100644 --- a/package.json +++ b/package.json @@ -9,5 +9,8 @@ "grunt-contrib-jasmine": "0.5.x", "grunt-contrib-watch": "0.5.x", "grunt-contrib-uglify": "0.2.x" + }, + "scripts": { + "test": "grunt test" } } From d71f2af2a45195a8224fa0ef993fa19cc5b35c50 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Sat, 2 Nov 2013 14:18:26 -0500 Subject: [PATCH 051/116] Adding Badge for build status --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c95df69..f4b307e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ Masked Input Plugin for jQuery ============================== - +[![Build Status](https://travis-ci.org/digitalBush/jquery.maskedinput.png)](https://travis-ci.org/digitalBush/jquery.maskedinput) Overview -------- This is a masked input plugin for the jQuery javascript library. It allows a user to more easily enter fixed width input where you would like them to enter the data in a certain format (dates,phone numbers, etc). It has been tested on Internet Explorer, Firefox, Safari, Opera, and Chrome. A mask is defined by a format made up of mask literals and mask definitions. Any character not in the definitions list below is considered a mask literal. Mask literals will be automatically entered for the user as they type and will not be able to be removed by the user.The following mask definitions are predefined: From fe622dc58ac190cfcffcadc48f61f94ccd7f139f Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Fri, 8 Nov 2013 22:49:53 -0500 Subject: [PATCH 052/116] closes #177. cleans the input when [enter] is pressed --- spec/Enter.Spec.js | 118 ++++++++++++++++++++++++++++++++++++++ src/jquery.maskedinput.js | 21 ++++--- 2 files changed, 131 insertions(+), 8 deletions(-) create mode 100644 spec/Enter.Spec.js diff --git a/spec/Enter.Spec.js b/spec/Enter.Spec.js new file mode 100644 index 0000000..025e21e --- /dev/null +++ b/spec/Enter.Spec.js @@ -0,0 +1,118 @@ +feature("Enter Key", function() { + var enterKeyEvent = $.Event('keydown.mask'); + enterKeyEvent.which = enterKeyEvent.keyCode = 13; + + story('User presses enter key after typing in some changes',function(){ + scenario("All placeholders filled",function(){ + given("a mask with two placeholders",function(){ + input.mask("99"); + }); + when("typing two characters and pressing enter",function(){ + input.mashKeys("12").trigger(enterKeyEvent); + }); + then("value should be correct",function(){ + expect(input).toHaveValue("12"); + }); + }); + + scenario("Empty placeholders remaining",function(){ + given("a mask with two placeholders",function(){ + input.mask("99"); + }); + when("typing one character and pressing enter",function(){ + input.mashKeys("1").trigger(enterKeyEvent); + }); + then("value should be empty",function(){ + expect(input).toHaveValue(""); + }); + }); + + scenario("Empty placeholders remaining with autoclear set to false",function(){ + given("a mask with two placeholders",function(){ + input.mask("99", { autoclear: false }); + }); + when("typing one character and pressing enter",function(){ + input.caret(0); + input.mashKeys("1") + input.trigger(enterKeyEvent); + }); + then("value should remain visible with placeholders",function(){ + expect(input).toHaveValue("1_"); + }); + }); + }); + + story("User presses enter key after typing in some changes and masks contain Optional Markers",function(){ + scenario("Placeholders not filled to marker",function(){ + given("a mask with an optional marker",function(){ + input.mask("99?99"); + }); + when("typing one character and leaving",function(){ + input.mashKeys("1").trigger(enterKeyEvent); + }); + then("value should be empty",function(){ + expect(input).toHaveValue(""); + }); + }); + + scenario("Placeholders not filled to marker and autoclear = false", function() { + given("a mask with an optional marker",function(){ + input.mask("99?99", { autoclear: false }); + }); + when("typing one character and leaving",function(){ + input.mashKeys("1").trigger(enterKeyEvent); + }); + then("value should be empty",function(){ + expect(input).toHaveValue("1___"); + }); + }); + + scenario("Placeholders filled to marker",function(){ + given("a mask with an optional marker",function(){ + input.mask("99?99"); + }); + when("typing two characters and leaving",function(){ + input.mashKeys("12").trigger(enterKeyEvent); + }); + then("value should remain",function(){ + expect(input).toHaveValue("12"); + }); + }); + + scenario("Placeholders filled to marker and autoclear = false", function() { + given("a mask with an optional marker",function(){ + input.mask("99?99", { autoclear: false }); + }); + when("typing two characters and leaving",function(){ + input.mashKeys("12").trigger(enterKeyEvent); + }); + then("value should remain",function(){ + expect(input).toHaveValue("12"); + }); + }); + + scenario("Placeholders filled, one marker filled, and autoclear = false", function() { + given("a mask with an optional marker",function(){ + input.mask("99?99", { autoclear: false }); + }); + when("typing three characters and leaving",function(){ + input.mashKeys("123").trigger(enterKeyEvent); + }); + then("value should remain",function(){ + expect(input).toHaveValue("123"); + }); + }); + + scenario("Placeholders and markers filled, and autoclear = false", function() { + given("a mask with an optional marker",function(){ + input.mask("99?99", { autoclear: false }); + }); + when("typing four characters and leaving",function(){ + input.mashKeys("1234").trigger(enterKeyEvent); + }); + then("value should remain",function(){ + expect(input).toHaveValue("1234"); + }); + }); + }); +}); diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index fb811db..610107b 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -167,6 +167,13 @@ $.fn.extend({ } } + function blurEvent(e) { + checkVal(); + + if (input.val() != focusText) + input.change(); + } + function keydownEvent(e) { var k = e.which, pos, @@ -187,7 +194,9 @@ $.fn.extend({ shiftL(begin, end - 1); e.preventDefault(); - } else if (k == 27) {//escape + } else if( k === 13 ) { // enter + blurEvent.call(this, e); + } else if (k === 27) { // escape input.val(focusText); input.caret(0, checkVal()); e.preventDefault(); @@ -217,9 +226,10 @@ $.fn.extend({ pos.end--; } } + if (e.ctrlKey || e.altKey || e.metaKey || k < 32) {//Ignore return; - } else if (k) { + } else if ( k && k !== 13 ) { if (pos.end - pos.begin !== 0){ clearBuffer(pos.begin, pos.end); shiftL(pos.begin, pos.end-1); @@ -338,12 +348,7 @@ $.fn.extend({ } }, 10); }) - .bind("blur.mask", function() { - checkVal(); - - if (input.val() != focusText) - input.change(); - }) + .bind("blur.mask", blurEvent) .bind("keydown.mask", keydownEvent) .bind("keypress.mask", keypressEvent) .bind(pasteEventName, function() { From ed5cae87fe6ed40c462beda63512dccf805af212 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Fri, 8 Nov 2013 22:15:09 -0600 Subject: [PATCH 053/116] Adding bower.json --- bower.json | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 bower.json diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..3e01d3b --- /dev/null +++ b/bower.json @@ -0,0 +1,18 @@ +{ + "name": "jquery.maskedinput", + "version": "1.3.1", + "main": "dist/jquery.maskedinput.min.js" + "homepage": "https://github.com/digitalBush/jquery.maskedinput", + "authors": [ + "Josh Bush " + ], + "description": "Fixed width input masking.", + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ] +} From a80efee9f230c4929a5353c54e764c624ababd23 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Fri, 8 Nov 2013 22:20:04 -0600 Subject: [PATCH 054/116] Adding dependency for jQuery. I'm not sure what the minimum version we currently support is? --- bower.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bower.json b/bower.json index 3e01d3b..4a4e10b 100644 --- a/bower.json +++ b/bower.json @@ -8,6 +8,9 @@ ], "description": "Fixed width input masking.", "license": "MIT", + "dependencies": { + "jquery": ">= 1.8.0" + }, "ignore": [ "**/.*", "node_modules", From 0538217fd0592f4cca0744ead7cedf35ba15afcb Mon Sep 17 00:00:00 2001 From: bhoekv Date: Wed, 13 Nov 2013 11:40:12 +0100 Subject: [PATCH 055/116] Fixes #171 Fixes #171 --- src/jquery.maskedinput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 610107b..fdd7941 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -341,7 +341,7 @@ $.fn.extend({ caretTimeoutId = setTimeout(function(){ writeBuffer(); - if (pos == mask.length) { + if (pos == mask.replace("?","").length) { input.caret(0, pos); } else { input.caret(pos); From 975089d2b739bec456d0e358ac84fd399ec4664b Mon Sep 17 00:00:00 2001 From: bhoekv Date: Thu, 14 Nov 2013 10:21:58 +0100 Subject: [PATCH 056/116] Test for issue #171 --- spec/Focus.Spec.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/spec/Focus.Spec.js b/spec/Focus.Spec.js index f469bf5..a0f0bfc 100644 --- a/spec/Focus.Spec.js +++ b/spec/Focus.Spec.js @@ -68,6 +68,25 @@ feature("Focusing A Masked Input",function(){ expect(input).toHaveValue("1_"); }); }); + + + scenario("Mask containing optional mask ?,function(){ + given("the input has a partial value",function(){ + input.val("99"); + }); + given("a optional mask on input",function(){ + input.mask("9?9"); + }); + when("focusing input",function(){ + input.focus(); + }); + waits(1); + then("caret position should be correct",function(){ + var caret=input.caret(); + expect(caret.begin).toEqual(0); + expect(caret.end).toEqual(2); + }); + }); }); feature("Leaving A Masked Input",function(){ From daa7455e75abe4c413170dcbed7019c299651196 Mon Sep 17 00:00:00 2001 From: bhoekv Date: Thu, 14 Nov 2013 10:34:34 +0100 Subject: [PATCH 057/116] Test issue #171 Fix program err --- spec/Focus.Spec.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/spec/Focus.Spec.js b/spec/Focus.Spec.js index a0f0bfc..9964893 100644 --- a/spec/Focus.Spec.js +++ b/spec/Focus.Spec.js @@ -68,9 +68,8 @@ feature("Focusing A Masked Input",function(){ expect(input).toHaveValue("1_"); }); }); - - - scenario("Mask containing optional mask ?,function(){ + + scenario("Mask containing optional mask ?",function(){ given("the input has a partial value",function(){ input.val("99"); }); From 23e6f693670bad821d9f56319e28886ffbeb5784 Mon Sep 17 00:00:00 2001 From: Marc Pires RJ Date: Thu, 5 Dec 2013 08:59:15 -0200 Subject: [PATCH 058/116] Patch for CSP Violation on FireFox OS --- src/jquery.maskedinput.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index fdd7941..8c35211 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -246,7 +246,13 @@ $.fn.extend({ next = seekNext(p); if(android){ - setTimeout($.proxy($.fn.caret,input,next),0); + //Path for CSP Violation on FireFox OS 1.1 + var proxy = function() + { + $.proxy($.fn.caret,input,next); + } + + setTimeout(proxy,0); }else{ input.caret(next); } From 0b67203f1b9fef3a49bfd1c4f242dd23b21fa863 Mon Sep 17 00:00:00 2001 From: sh0dow Date: Wed, 25 Dec 2013 16:32:12 +0200 Subject: [PATCH 059/116] Update jquery.maskedinput.js --- src/jquery.maskedinput.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index fdd7941..03412ad 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -328,10 +328,10 @@ $.fn.extend({ input .one("unmask", function() { input - .unbind(".mask") + .off(".mask") .removeData($.mask.dataName); }) - .bind("focus.mask", function() { + .on("focus.mask", function() { clearTimeout(caretTimeoutId); var pos; @@ -348,10 +348,10 @@ $.fn.extend({ } }, 10); }) - .bind("blur.mask", blurEvent) - .bind("keydown.mask", keydownEvent) - .bind("keypress.mask", keypressEvent) - .bind(pasteEventName, function() { + .on("blur.mask", blurEvent) + .on("keydown.mask", keydownEvent) + .on("keypress.mask", keypressEvent) + .on(pasteEventName, function() { setTimeout(function() { var pos=checkVal(true); input.caret(pos); @@ -360,7 +360,7 @@ $.fn.extend({ }, 0); }); if (chrome && android) { - input.bind("keyup.mask", keypressEvent); + input.on("keyup.mask", keypressEvent); } checkVal(); //Perform initial check for existing values }); From cbd237fb2a9b849a8638d553e2c355f0a1eeaaf2 Mon Sep 17 00:00:00 2001 From: Blair Vanderhoof Date: Fri, 17 Jan 2014 21:59:42 -0800 Subject: [PATCH 060/116] Fix missing comma --- bower.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bower.json b/bower.json index 4a4e10b..1da684a 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "jquery.maskedinput", "version": "1.3.1", - "main": "dist/jquery.maskedinput.min.js" + "main": "dist/jquery.maskedinput.min.js", "homepage": "https://github.com/digitalBush/jquery.maskedinput", "authors": [ "Josh Bush " From fe4a1553d5fab639af5861f46b1ebf422166f70d Mon Sep 17 00:00:00 2001 From: SRG Group Date: Thu, 30 Jan 2014 13:50:09 +0100 Subject: [PATCH 061/116] Small fix for inputs with "required" attributes It's a small fix. If the input has the html5 "required" attribute, the input gets the "invalid" mark from the browser, because the script changes the value of the input. With this fix it is working better maybe. --- src/jquery.maskedinput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index fdd7941..36c75e4 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -304,7 +304,7 @@ $.fn.extend({ if (settings.autoclear || buffer.join('') === defaultBuffer) { // Invalid value. Remove it and replace it with the // mask, which is the default behavior. - input.val(""); + if(input.val()) input.val(""); clearBuffer(0, len); } else { // Invalid value, but we opt to show the value to the From f20917c85138b79eae659bfd5c4259b637a2cd47 Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Mon, 24 Mar 2014 16:38:31 -0400 Subject: [PATCH 062/116] preps dist/ for release subtree branch --- dist/.gitignore | 4 ++++ bower.json => dist/bower.json | 0 2 files changed, 4 insertions(+) create mode 100644 dist/.gitignore rename bower.json => dist/bower.json (100%) diff --git a/dist/.gitignore b/dist/.gitignore new file mode 100644 index 0000000..3175dd7 --- /dev/null +++ b/dist/.gitignore @@ -0,0 +1,4 @@ + +.idea/* + +node_modules/ diff --git a/bower.json b/dist/bower.json similarity index 100% rename from bower.json rename to dist/bower.json From 7ce8fea7d0f6acdfbe449e9827dc7415a67bc1d3 Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Mon, 24 Mar 2014 16:51:03 -0400 Subject: [PATCH 063/116] adds license file to distro --- dist/LICENSE | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 dist/LICENSE diff --git a/dist/LICENSE b/dist/LICENSE new file mode 100644 index 0000000..8f4ccae --- /dev/null +++ b/dist/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2007-2013 Josh Bush (digitalbush.com) + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file From bb36aed50dd4982b6c288ca144fd40d03eb9fa00 Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Mon, 24 Mar 2014 20:23:34 -0400 Subject: [PATCH 064/116] travis ignores release branch and version tags --- .travis.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 0ce3be2..36948ce 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,4 +2,9 @@ language: node_js node_js: - "0.10" before_script: - - npm install -g grunt-cli \ No newline at end of file + - npm install -g grunt-cli + +# ignore the release branch +branches: + - release + - /\d+\.\d+(\.\d+)*/ From 32cac56cea697d651f1282453f253d2f77edffba Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Mon, 24 Mar 2014 21:25:47 -0400 Subject: [PATCH 065/116] add travis config for dist --- .travis.yml | 4 ---- dist/.travis.yml | 8 ++++++++ 2 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 dist/.travis.yml diff --git a/.travis.yml b/.travis.yml index 36948ce..562a78b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,3 @@ node_js: before_script: - npm install -g grunt-cli -# ignore the release branch -branches: - - release - - /\d+\.\d+(\.\d+)*/ diff --git a/dist/.travis.yml b/dist/.travis.yml new file mode 100644 index 0000000..5e46253 --- /dev/null +++ b/dist/.travis.yml @@ -0,0 +1,8 @@ +language: node_js +node_js: + - "0.10" + +# ignore the release branch +branches: + - release + - /\d+\.\d+(\.\d+)*/ From 1fb1565d4b1c2ccbc5baa21c0137f7e64f42be1c Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Mon, 24 Mar 2014 21:59:02 -0400 Subject: [PATCH 066/116] release branch and tags builds always pass --- dist/.travis.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/dist/.travis.yml b/dist/.travis.yml index 5e46253..68cb80f 100644 --- a/dist/.travis.yml +++ b/dist/.travis.yml @@ -2,7 +2,5 @@ language: node_js node_js: - "0.10" -# ignore the release branch -branches: - - release - - /\d+\.\d+(\.\d+)*/ +# always succeed on release branch and tags +script: /bin/true From 46b41f5c93b36ff0afca4cfb1c729ba9d0ca4ce4 Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Mon, 24 Mar 2014 23:47:57 -0400 Subject: [PATCH 067/116] adds release publishing instructions to readme --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index f4b307e..8e1454c 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,24 @@ If your requirements aren't met by the predefined placeholders, you can always a By design, this plugin will reject input which doesn't complete the mask. You can bypass this by using a '?' character at the position where you would like to consider input optional. For example, a mask of "(999) 999-9999? x99999" would require only the first 10 digits of a phone number with extension being optional. +Publishing a Release +-------------------- +jQuery Masked Input uses git tags to publish releases to [Bower](http://www.bower.io). We used to just tag directly off of master but this added a lot of cruft - the entire git directory - to our users `bower_components` directory. A lot of other libraries are nice enough to clean up their distributions and so we should too. + +So here is how you go about publishing a new release. _This assumes you've updated the version strings in the dist/bower.json and the source js_. + +``` + $ git subtree split --prefix dist --branch release + + $ git checkout release + + $ git merge -s subtree master + + $ git tag + + $ git push origin release && git push origin --tags +``` + Setting up your Developer Environment ------------------------------------- jQuery Masked Input uses [NodeJS](http://www.nodejs.org) and [GruntJS](http://www.gruntjs.com) as it's developer platform and build automation tool. From ef818cb5b26e39bf857f48c1a156028c2f7c0f59 Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Tue, 25 Mar 2014 00:22:59 -0400 Subject: [PATCH 068/116] revert changes for subtree releases Using git subtree to structure a release tag is ok, but it doesn't get rid of the need for a /dist folder, which is the main use case we were wanting to fix. Also, it adds an extra bit of complexity to our release process. --- README.md | 20 +------------------- dist/LICENSE | 22 ---------------------- dist/bower.json | 21 --------------------- 3 files changed, 1 insertion(+), 62 deletions(-) delete mode 100644 dist/LICENSE delete mode 100644 dist/bower.json diff --git a/README.md b/README.md index 8e1454c..ac4c6c0 100644 --- a/README.md +++ b/README.md @@ -13,31 +13,13 @@ If your requirements aren't met by the predefined placeholders, you can always a By design, this plugin will reject input which doesn't complete the mask. You can bypass this by using a '?' character at the position where you would like to consider input optional. For example, a mask of "(999) 999-9999? x99999" would require only the first 10 digits of a phone number with extension being optional. -Publishing a Release --------------------- -jQuery Masked Input uses git tags to publish releases to [Bower](http://www.bower.io). We used to just tag directly off of master but this added a lot of cruft - the entire git directory - to our users `bower_components` directory. A lot of other libraries are nice enough to clean up their distributions and so we should too. - -So here is how you go about publishing a new release. _This assumes you've updated the version strings in the dist/bower.json and the source js_. - -``` - $ git subtree split --prefix dist --branch release - - $ git checkout release - - $ git merge -s subtree master - - $ git tag - - $ git push origin release && git push origin --tags -``` - Setting up your Developer Environment ------------------------------------- jQuery Masked Input uses [NodeJS](http://www.nodejs.org) and [GruntJS](http://www.gruntjs.com) as it's developer platform and build automation tool. To get your environment setup correctly, you'll need nodejs version 0.8.25 or greater installed. You'll also need to install the grunt command line tool: - $ sudo npm install -g grunt-cli + $ sudo npm install -g grunt-cli Once node is installed on your system all that you need to do is install the developer dependencies and run the grunt build: diff --git a/dist/LICENSE b/dist/LICENSE deleted file mode 100644 index 8f4ccae..0000000 --- a/dist/LICENSE +++ /dev/null @@ -1,22 +0,0 @@ -Copyright (c) 2007-2013 Josh Bush (digitalbush.com) - -Permission is hereby granted, free of charge, to any person -obtaining a copy of this software and associated documentation -files (the "Software"), to deal in the Software without -restriction, including without limitation the rights to use, -copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the -Software is furnished to do so, subject to the following -conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/dist/bower.json b/dist/bower.json deleted file mode 100644 index 1da684a..0000000 --- a/dist/bower.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "jquery.maskedinput", - "version": "1.3.1", - "main": "dist/jquery.maskedinput.min.js", - "homepage": "https://github.com/digitalBush/jquery.maskedinput", - "authors": [ - "Josh Bush " - ], - "description": "Fixed width input masking.", - "license": "MIT", - "dependencies": { - "jquery": ">= 1.8.0" - }, - "ignore": [ - "**/.*", - "node_modules", - "bower_components", - "test", - "tests" - ] -} From b6141ae395e72644f3c39db37fac7cdd6fe772c2 Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Tue, 25 Mar 2014 00:24:41 -0400 Subject: [PATCH 069/116] removes gitignore and travis config from dist --- dist/.gitignore | 4 ---- dist/.travis.yml | 6 ------ 2 files changed, 10 deletions(-) delete mode 100644 dist/.gitignore delete mode 100644 dist/.travis.yml diff --git a/dist/.gitignore b/dist/.gitignore deleted file mode 100644 index 3175dd7..0000000 --- a/dist/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ - -.idea/* - -node_modules/ diff --git a/dist/.travis.yml b/dist/.travis.yml deleted file mode 100644 index 68cb80f..0000000 --- a/dist/.travis.yml +++ /dev/null @@ -1,6 +0,0 @@ -language: node_js -node_js: - - "0.10" - -# always succeed on release branch and tags -script: /bin/true From b6a0ebe9e99c5ccab47a5b66d58fd04b4c81a4ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Ro=C5=BCniecki?= Date: Thu, 27 Mar 2014 12:43:59 +0100 Subject: [PATCH 070/116] Fix caret position on Android Fixes bug introduced in 23e6f693 - function created by $.proxy was never executed. --- src/jquery.maskedinput.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index cd226c8..cdfe8a0 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -247,10 +247,9 @@ $.fn.extend({ if(android){ //Path for CSP Violation on FireFox OS 1.1 - var proxy = function() - { - $.proxy($.fn.caret,input,next); - } + var proxy = function() { + $.proxy($.fn.caret,input,next)(); + }; setTimeout(proxy,0); }else{ From af38d5128e4cd6946f107cbd2d4328bd3e8cd9a9 Mon Sep 17 00:00:00 2001 From: "Shahrukh A. Khan" Date: Thu, 24 Apr 2014 20:01:39 +0500 Subject: [PATCH 071/116] Usage Updated --- README.md | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ac4c6c0..c43c36a 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,65 @@ This is a masked input plugin for the jQuery javascript library. It allows a use * 9 - Represents a numeric character (0-9) * \* - Represents an alphanumeric character (A-Z,a-z,0-9) -If your requirements aren't met by the predefined placeholders, you can always add your own. For example, maybe you need a mask to only allow hexadecimal characters. You can add your own definition for a placeholder, say 'h', like so: `$.mask.definitions['h'] = "[A-Fa-f0-9]";` Then you can use that to mask for something like css colors in hex with a mask "#hhhhhh". +### Usage +First, include the jQuery and masked input javascript files. + +```html + + +``` + +Next, call the mask function for those items you wish to have masked. + +```html +jQuery(function($){ + $("#date").mask("99/99/9999"); + $("#phone").mask("(999) 999-9999"); + $("#tin").mask("99-9999999"); + $("#ssn").mask("999-99-9999"); +}); +``` + +Optionally, if you are not satisfied with the underscore ('_') character as a placeholder, you may pass an optional argument to the maskedinput method. + +```html +jQuery(function($){ + $("#product").mask("99/99/9999",{placeholder:" "}); +}); +``` + +Optionally, if you would like to execute a function once the mask has been completed, you can specify that function as an optional argument to the maskedinput method. + +```html +jQuery(function($){ + $("#product").mask("99/99/9999",{completed:function(){alert("You typed the following: "+this.val());}}); +}); +``` + +You can now supply your own mask definitions. +```html +jQuery(function($){ + $.mask.definitions['~']='[+-]'; + $("#eyescript").mask("~9.99 ~9.99 999"); +}); +``` + +You can have part of your mask be optional. Anything listed after '?' within the mask is considered optional user input. The common example for this is phone number + optional extension. + +```html +jQuery(function($){ + $("#phone").mask("(999) 999-9999? x99999"); +}); +``` + +If your requirements aren't met by the predefined placeholders, you can always add your own. For example, maybe you need a mask to only allow hexadecimal characters. You can add your own definition for a placeholder, say 'h', like so: `$.mask.definitions['h'] = "[A-Fa-f0-9]";` Then you can use that to mask for something like css colors in hex with a `mask "#hhhhhh"`. + +```html +jQuery(function($){ + $("#phone").mask("#hhhhhh"); +}); +``` + By design, this plugin will reject input which doesn't complete the mask. You can bypass this by using a '?' character at the position where you would like to consider input optional. For example, a mask of "(999) 999-9999? x99999" would require only the first 10 digits of a phone number with extension being optional. From 15667fed1400c29a2035ba9b0f00a2c95e1c6ace Mon Sep 17 00:00:00 2001 From: zxyang Date: Sun, 24 Aug 2014 14:36:36 -0400 Subject: [PATCH 072/116] Update jquery.maskedinput.js Add support to Android KitKat WebView. 'keypress' event is deprecated, replace with 'input' event if 'keypress' event is not supported. --- src/jquery.maskedinput.js | 731 +++++++++++++++++++------------------- 1 file changed, 365 insertions(+), 366 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index ad999e3..33c23a2 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -1,374 +1,373 @@ +/* + Masked Input plugin for jQuery + Copyright (c) 2007-2013 Josh Bush (digitalbush.com) + Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) + Version: 1.3.1 + */ (function($) { + function getPasteEvent() { + var el = document.createElement('input'), + name = 'onpaste'; + el.setAttribute(name, ''); + return (typeof el[name] === 'function') ? 'paste' : 'input'; + } + + var pasteEventName = getPasteEvent() + ".mask", + ua = navigator.userAgent, + iPhone = /iphone/i.test(ua), + android = /android/i.test(ua), + caretTimeoutId; + + $.mask = { + //Predefined character definitions + definitions: { + '9': "[0-9]", + 'a': "[A-Za-z]", + '*': "[A-Za-z0-9]" + }, + dataName: "rawMaskFn", + placeholder: '_', + }; + + $.fn.extend({ + //Helper Function for Caret positioning + caret: function(begin, end) { + var range; + + if (this.length === 0 || this.is(":hidden")) { + return; + } + + if (typeof begin == 'number') { + end = (typeof end === 'number') ? end : begin; + return this.each(function() { + if (this.setSelectionRange) { + this.setSelectionRange(begin, end); + } else if (this.createTextRange) { + range = this.createTextRange(); + range.collapse(true); + range.moveEnd('character', end); + range.moveStart('character', begin); + range.select(); + } + }); + } else { + if (this[0].setSelectionRange) { + begin = this[0].selectionStart; + end = this[0].selectionEnd; + } else if (document.selection && document.selection.createRange) { + range = document.selection.createRange(); + begin = 0 - range.duplicate().moveStart('character', -100000); + end = begin + range.text.length; + } + return {begin: begin, end: end}; + } + }, + unmask: function() { + return this.trigger("unmask"); + }, + mask: function(mask, settings) { + var input, + defs, + tests, + partialPosition, + firstNonMaskPos, + len, oldVal, bindInputEvent; + + if (!mask && this.length > 0) { + input = $(this[0]); + return input.data($.mask.dataName)(); + } + settings = $.extend({ + placeholder: $.mask.placeholder, // Load default placeholder + completed: null + }, settings); + + + defs = $.mask.definitions; + tests = []; + 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])); + if (firstNonMaskPos === null) { + firstNonMaskPos = tests.length - 1; + } + } else { + tests.push(null); + } + }); + + return this.trigger("unmask").each(function() { + var input = $(this), + buffer = $.map( + mask.split(""), + function(c, i) { + if (c != '?') { + return defs[c] ? settings.placeholder : c; + } + }), + focusText = input.val(); + + function seekNext(pos) { + while (++pos < len && !tests[pos]) + ; + return pos; + } + + function seekPrev(pos) { + while (--pos >= 0 && !tests[pos]) + ; + return pos; + } + + function shiftL(begin, end) { + var i, + j; + + if (begin < 0) { + return; + } + + for (i = begin, j = seekNext(end); i < len; i++) { + if (tests[i]) { + if (j < len && tests[i].test(buffer[j])) { + buffer[i] = buffer[j]; + buffer[j] = settings.placeholder; + } else { + break; + } -function getPasteEvent() { - var el = document.createElement('input'), - name = 'onpaste'; - el.setAttribute(name, ''); - return (typeof el[name] === 'function')?'paste':'input'; -} - -var pasteEventName = getPasteEvent() + ".mask", - ua = navigator.userAgent, - iPhone = /iphone/i.test(ua), - chrome = /chrome/i.test(ua), - android=/android/i.test(ua), - caretTimeoutId; - -$.mask = { - //Predefined character definitions - definitions: { - '9': "[0-9]", - 'a': "[A-Za-z]", - '*': "[A-Za-z0-9]" - }, - autoclear: true, - dataName: "rawMaskFn", - placeholder: '_' -}; - -$.fn.extend({ - //Helper Function for Caret positioning - caret: function(begin, end) { - var range; - - if (this.length === 0 || this.is(":hidden")) { - return; - } - - if (typeof begin == 'number') { - end = (typeof end === 'number') ? end : begin; - return this.each(function() { - if (this.setSelectionRange) { - this.setSelectionRange(begin, end); - } else if (this.createTextRange) { - range = this.createTextRange(); - range.collapse(true); - range.moveEnd('character', end); - range.moveStart('character', begin); - range.select(); - } - }); - } else { - if (this[0].setSelectionRange) { - begin = this[0].selectionStart; - end = this[0].selectionEnd; - } else if (document.selection && document.selection.createRange) { - range = document.selection.createRange(); - begin = 0 - range.duplicate().moveStart('character', -100000); - end = begin + range.text.length; - } - return { begin: begin, end: end }; - } - }, - unmask: function() { - return this.trigger("unmask"); - }, - mask: function(mask, settings) { - var input, - defs, - tests, - partialPosition, - firstNonMaskPos, - len; - - if (!mask && this.length > 0) { - input = $(this[0]); - return input.data($.mask.dataName)(); - } - settings = $.extend({ - autoclear: $.mask.autoclear, - placeholder: $.mask.placeholder, // Load default placeholder - completed: null - }, settings); - - - defs = $.mask.definitions; - tests = []; - 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])); - if (firstNonMaskPos === null) { - firstNonMaskPos = tests.length - 1; - } - } else { - tests.push(null); - } - }); - - return this.trigger("unmask").each(function() { - var input = $(this), - buffer = $.map( - mask.split(""), - function(c, i) { - if (c != '?') { - return defs[c] ? settings.placeholder : c; - } - }), - defaultBuffer = buffer.join(''), - focusText = input.val(); - - function seekNext(pos) { - while (++pos < len && !tests[pos]); - return pos; - } - - function seekPrev(pos) { - while (--pos >= 0 && !tests[pos]); - return pos; - } - - function shiftL(begin,end) { - var i, - j; - - if (begin<0) { - return; - } - - for (i = begin, j = seekNext(end); i < len; i++) { - if (tests[i]) { - if (j < len && tests[i].test(buffer[j])) { - buffer[i] = buffer[j]; - buffer[j] = settings.placeholder; - } else { - break; - } - - j = seekNext(j); - } - } - writeBuffer(); - input.caret(Math.max(firstNonMaskPos, begin)); - } - - function shiftR(pos) { - var i, - c, - j, - t; - - for (i = pos, c = settings.placeholder; i < len; i++) { - if (tests[i]) { - j = seekNext(i); - t = buffer[i]; - buffer[i] = c; - if (j < len && tests[j].test(t)) { - c = t; - } else { - break; - } - } - } - } - - function blurEvent(e) { - checkVal(); - - if (input.val() != focusText) - input.change(); - } - - function keydownEvent(e) { - var k = e.which, - pos, - begin, - end; - - //backspace, delete, and escape get special treatment - if (k === 8 || k === 46 || (iPhone && k === 127)) { - pos = input.caret(); - begin = pos.begin; - end = pos.end; - - if (end - begin === 0) { - begin=k!==46?seekPrev(begin):(end=seekNext(begin-1)); - end=k===46?seekNext(end):end; - } - clearBuffer(begin, end); - shiftL(begin, end - 1); - - e.preventDefault(); - } else if( k === 13 ) { // enter - blurEvent.call(this, e); - } else if (k === 27) { // escape - input.val(focusText); - input.caret(0, checkVal()); - e.preventDefault(); - } - } - - function keypressEvent(e) { - var k = e.which, - pos = input.caret(), - p, - c, - next; - - if (k == 0) { - // unable to detect key pressed. Grab it from pos and adjust - // this is a failsafe for mobile chrome - // which can't detect keypress events - // reliably - if (pos.begin >= len) { - input.val(input.val().substr(0, len)); - e.preventDefault(); - return false; + j = seekNext(j); } - if (pos.begin == pos.end) { - k = input.val().charCodeAt(pos.begin - 1); - pos.begin--; - pos.end--; + } + writeBuffer(); + input.caret(Math.max(firstNonMaskPos, begin)); + } + + function shiftR(pos) { + var i, + c, + j, + t; + + for (i = pos, c = settings.placeholder; i < len; i++) { + if (tests[i]) { + j = seekNext(i); + t = buffer[i]; + buffer[i] = c; + if (j < len && tests[j].test(t)) { + c = t; + } else { + break; + } } } + } - if (e.ctrlKey || e.altKey || e.metaKey || k < 32) {//Ignore - return; - } else if ( k && k !== 13 ) { - if (pos.end - pos.begin !== 0){ - clearBuffer(pos.begin, pos.end); - shiftL(pos.begin, pos.end-1); - } - - p = seekNext(pos.begin - 1); - if (p < len) { - c = String.fromCharCode(k); - if (tests[p].test(c)) { - shiftR(p); - - buffer[p] = c; - writeBuffer(); - next = seekNext(p); - - if(android){ - //Path for CSP Violation on FireFox OS 1.1 - var proxy = function() { - $.proxy($.fn.caret,input,next)(); - }; - - setTimeout(proxy,0); - }else{ - input.caret(next); - } - - if (settings.completed && next >= len) { - settings.completed.call(input); - } - } - } - e.preventDefault(); - } - } - - function clearBuffer(start, end) { - var i; - for (i = start; i < end && i < len; i++) { - if (tests[i]) { - buffer[i] = settings.placeholder; - } - } - } - - function writeBuffer() { input.val(buffer.join('')); } - - 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)) { - buffer[i] = c; - lastMatch = i; - break; - } - } - if (pos > test.length) { - break; - } - } else if (buffer[i] === test.charAt(pos) && i !== partialPosition) { - pos++; - lastMatch = i; - } - } - if (allow) { - writeBuffer(); - } else if (lastMatch + 1 < partialPosition) { - if (settings.autoclear || buffer.join('') === defaultBuffer) { - // Invalid value. Remove it and replace it with the - // mask, which is the default behavior. - if(input.val()) input.val(""); - clearBuffer(0, len); - } else { - // Invalid value, but we opt to show the value to the - // user and allow them to correct their mistake. - writeBuffer(); - } - } else { - writeBuffer(); - input.val(input.val().substring(0, lastMatch + 1)); - } - return (partialPosition ? i : firstNonMaskPos); - } - - input.data($.mask.dataName,function(){ - return $.map(buffer, function(c, i) { - return tests[i]&&c!=settings.placeholder ? c : null; - }).join(''); - }); - - if (!input.attr("readonly")) - input - .one("unmask", function() { - input - .off(".mask") - .removeData($.mask.dataName); - }) - .on("focus.mask", function() { - clearTimeout(caretTimeoutId); - var pos; - - focusText = input.val(); - - pos = checkVal(); - - caretTimeoutId = setTimeout(function(){ - writeBuffer(); - if (pos == mask.replace("?","").length) { - input.caret(0, pos); - } else { - input.caret(pos); - } - }, 10); - }) - .on("blur.mask", blurEvent) - .on("keydown.mask", keydownEvent) - .on("keypress.mask", keypressEvent) - .on(pasteEventName, function() { - setTimeout(function() { - var pos=checkVal(true); - input.caret(pos); - if (settings.completed && pos == input.val().length) - settings.completed.call(input); - }, 0); - }); - if (chrome && android) { - input.on("keyup.mask", keypressEvent); + function inputEvent(e) { + var curVal = input.val(); + var pos = input.caret(); + if (curVal.length < oldVal.length) { + // a deletion or backspace happened + checkVal(true); + pos.begin = seekPrev(pos.begin + 1); + input.caret(pos.begin, pos.begin); + } else { + pos = checkVal(true); + input.caret(pos); + } + if (settings.completed && pos == input.val().length) + settings.completed.call(input); } - checkVal(); //Perform initial check for existing values - }); - } -}); + + function keydownEvent(e) { + var k = e.which, + pos, + begin, + end; + oldVal = input.val(); + //backspace, delete, and escape get special treatment + if (k === 8 || k === 46 || (iPhone && k === 127)) { + pos = input.caret(); + begin = pos.begin; + end = pos.end; + + if (end - begin === 0) { + begin = k !== 46 ? seekPrev(begin) : (end = seekNext(begin - 1)); + end = k === 46 ? seekNext(end) : end; + } + clearBuffer(begin, end); + shiftL(begin, end - 1); + + e.preventDefault(); + } else if (k == 27) {//escape + input.val(focusText); + input.caret(0, checkVal()); + e.preventDefault(); + } + } + + function keypressEvent(e) { + + if (bindInputEvent) + { + bindInputEvent = false; + input.unbind('input.mask'); + } + ; + var k = e.which, + pos = input.caret(), + p, + c, + next; + + if (e.ctrlKey || e.altKey || e.metaKey || k < 32) {//Ignore + return; + } else if (k) { + if (pos.end - pos.begin !== 0) { + clearBuffer(pos.begin, pos.end); + shiftL(pos.begin, pos.end - 1); + } + + p = seekNext(pos.begin - 1); + if (p < len) { + c = String.fromCharCode(k); + if (tests[p].test(c)) { + shiftR(p); + + buffer[p] = c; + writeBuffer(); + next = seekNext(p); + + if (android) { + setTimeout($.proxy($.fn.caret, input, next), 0); + } else { + input.caret(next); + } + + if (settings.completed && next >= len) { + settings.completed.call(input); + } + } + } + e.preventDefault(); + } + } + + function clearBuffer(start, end) { + var i; + for (i = start; i < end && i < len; i++) { + if (tests[i]) { + buffer[i] = settings.placeholder; + } + } + } + + function writeBuffer() { + input.val(buffer.join('')); + } + + function checkVal(allow) { + //try to place characters where they belong + var test = input.val(), + lastMatch = -1, + i, + c; + + 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)) { + buffer[i] = c; + lastMatch = i; + break; + } + } + if (pos > test.length) { + break; + } + } else if (buffer[i] === test.charAt(pos) && i !== partialPosition) { + pos++; + lastMatch = i; + } + } + if (allow) { + writeBuffer(); + } else if (lastMatch + 1 < partialPosition) { + input.val(""); + clearBuffer(0, len); + } else { + writeBuffer(); + input.val(input.val().substring(0, lastMatch + 1)); + } + return (partialPosition ? i : firstNonMaskPos); + } + + input.data($.mask.dataName, function() { + return $.map(buffer, function(c, i) { + return tests[i] && c != settings.placeholder ? c : null; + }).join(''); + }); + + if (!input.attr("readonly")) + input + .one("unmask", function() { + input + .unbind(".mask") + .removeData($.mask.dataName); + }) + .bind("focus.mask", function() { + clearTimeout(caretTimeoutId); + var pos, + moveCaret; + + focusText = input.val(); + pos = checkVal(); + + caretTimeoutId = setTimeout(function() { + writeBuffer(); + if (pos == mask.length) { + input.caret(0, pos); + } else { + input.caret(pos); + } + }, 10); + }) + .bind("blur.mask", function() { + checkVal(); + if (input.val() != focusText) + input.change(); + }) + .bind("keydown.mask", keydownEvent) + .bind("keypress.mask", keypressEvent) + .bind(pasteEventName, function() { + setTimeout(function() { + var pos = checkVal(true); + input.caret(pos); + if (settings.completed && pos == input.val().length) + settings.completed.call(input); + }, 0); + }); + if (android) // bind with input event + { + bindInputEvent = true; + input.bind('input.mask', inputEvent); + } else + { + bindInputEvent = false; + } + checkVal(); //Perform initial check for existing values + }); + } + }); + + })(jQuery); From 482b0a6b02adeabcd6d0cc633596411120c819e4 Mon Sep 17 00:00:00 2001 From: zxyang Date: Sun, 24 Aug 2014 16:13:10 -0400 Subject: [PATCH 073/116] Update jquery.maskedinput.js fix caret positions --- src/jquery.maskedinput.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 33c23a2..c4eef73 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -176,11 +176,20 @@ if (curVal.length < oldVal.length) { // a deletion or backspace happened checkVal(true); - pos.begin = seekPrev(pos.begin + 1); - input.caret(pos.begin, pos.begin); + while (pos.begin > 0 && !tests[pos.begin-1]) + pos.begin--; + if (pos.begin === 0) + { + while (pos.begin < firstNonMaskPos && !tests[pos.begin]) + pos.begin++; + } + input.caret(pos.begin,pos.begin); } else { - pos = checkVal(true); - input.caret(pos); + var pos2 = checkVal(true); + while (pos.begin < len && !tests[pos.begin]) + pos.begin++; + + input.caret(pos.begin,pos.begin); } if (settings.completed && pos == input.val().length) settings.completed.call(input); From bf7f7afd91956af4972f144f3aa93d02ecd1134b Mon Sep 17 00:00:00 2001 From: zxyang Date: Sun, 24 Aug 2014 16:22:45 -0400 Subject: [PATCH 074/116] Update jquery.maskedinput.js From 2d79c1b4601f0f1fc8ad3d99277c6f09e62128cd Mon Sep 17 00:00:00 2001 From: zxyang Date: Sun, 24 Aug 2014 16:54:28 -0400 Subject: [PATCH 075/116] Update jquery.maskedinput.js Merged with the latest on the main project. --- src/jquery.maskedinput.js | 700 ++++++++++++++++++++------------------ 1 file changed, 363 insertions(+), 337 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index c4eef73..5117780 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -1,175 +1,171 @@ -/* - Masked Input plugin for jQuery - Copyright (c) 2007-2013 Josh Bush (digitalbush.com) - Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) - Version: 1.3.1 - */ (function($) { - function getPasteEvent() { - var el = document.createElement('input'), - name = 'onpaste'; - el.setAttribute(name, ''); - return (typeof el[name] === 'function') ? 'paste' : 'input'; - } - - var pasteEventName = getPasteEvent() + ".mask", - ua = navigator.userAgent, - iPhone = /iphone/i.test(ua), - android = /android/i.test(ua), - caretTimeoutId; - - $.mask = { - //Predefined character definitions - definitions: { - '9': "[0-9]", - 'a': "[A-Za-z]", - '*': "[A-Za-z0-9]" - }, - dataName: "rawMaskFn", - placeholder: '_', - }; - - $.fn.extend({ - //Helper Function for Caret positioning - caret: function(begin, end) { - var range; - - if (this.length === 0 || this.is(":hidden")) { - return; - } - - if (typeof begin == 'number') { - end = (typeof end === 'number') ? end : begin; - return this.each(function() { - if (this.setSelectionRange) { - this.setSelectionRange(begin, end); - } else if (this.createTextRange) { - range = this.createTextRange(); - range.collapse(true); - range.moveEnd('character', end); - range.moveStart('character', begin); - range.select(); - } - }); - } else { - if (this[0].setSelectionRange) { - begin = this[0].selectionStart; - end = this[0].selectionEnd; - } else if (document.selection && document.selection.createRange) { - range = document.selection.createRange(); - begin = 0 - range.duplicate().moveStart('character', -100000); - end = begin + range.text.length; - } - return {begin: begin, end: end}; - } - }, - unmask: function() { - return this.trigger("unmask"); - }, - mask: function(mask, settings) { - var input, - defs, - tests, - partialPosition, - firstNonMaskPos, - len, oldVal, bindInputEvent; - - if (!mask && this.length > 0) { - input = $(this[0]); - return input.data($.mask.dataName)(); - } - settings = $.extend({ - placeholder: $.mask.placeholder, // Load default placeholder - completed: null - }, settings); - - - defs = $.mask.definitions; - tests = []; - 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])); - if (firstNonMaskPos === null) { - firstNonMaskPos = tests.length - 1; - } - } else { - tests.push(null); - } - }); - - return this.trigger("unmask").each(function() { - var input = $(this), - buffer = $.map( - mask.split(""), - function(c, i) { - if (c != '?') { - return defs[c] ? settings.placeholder : c; - } - }), - focusText = input.val(); - - function seekNext(pos) { - while (++pos < len && !tests[pos]) - ; - return pos; - } - - function seekPrev(pos) { - while (--pos >= 0 && !tests[pos]) - ; - return pos; - } - - function shiftL(begin, end) { - var i, - j; - - if (begin < 0) { - return; - } - - for (i = begin, j = seekNext(end); i < len; i++) { - if (tests[i]) { - if (j < len && tests[i].test(buffer[j])) { - buffer[i] = buffer[j]; - buffer[j] = settings.placeholder; - } else { - break; - } - - j = seekNext(j); - } - } - writeBuffer(); - input.caret(Math.max(firstNonMaskPos, begin)); - } - function shiftR(pos) { - var i, - c, - j, - t; - - for (i = pos, c = settings.placeholder; i < len; i++) { - if (tests[i]) { - j = seekNext(i); - t = buffer[i]; - buffer[i] = c; - if (j < len && tests[j].test(t)) { - c = t; - } else { - break; - } - } - } - } +function getPasteEvent() { + var el = document.createElement('input'), + name = 'onpaste'; + el.setAttribute(name, ''); + return (typeof el[name] === 'function')?'paste':'input'; +} + +var pasteEventName = getPasteEvent() + ".mask", + ua = navigator.userAgent, + iPhone = /iphone/i.test(ua), + chrome = /chrome/i.test(ua), + android=/android/i.test(ua), + caretTimeoutId; + +$.mask = { + //Predefined character definitions + definitions: { + '9': "[0-9]", + 'a': "[A-Za-z]", + '*': "[A-Za-z0-9]" + }, + autoclear: true, + dataName: "rawMaskFn", + placeholder: '_' +}; + +$.fn.extend({ + //Helper Function for Caret positioning + caret: function(begin, end) { + var range; + + if (this.length === 0 || this.is(":hidden")) { + return; + } + + if (typeof begin == 'number') { + end = (typeof end === 'number') ? end : begin; + return this.each(function() { + if (this.setSelectionRange) { + this.setSelectionRange(begin, end); + } else if (this.createTextRange) { + range = this.createTextRange(); + range.collapse(true); + range.moveEnd('character', end); + range.moveStart('character', begin); + range.select(); + } + }); + } else { + if (this[0].setSelectionRange) { + begin = this[0].selectionStart; + end = this[0].selectionEnd; + } else if (document.selection && document.selection.createRange) { + range = document.selection.createRange(); + begin = 0 - range.duplicate().moveStart('character', -100000); + end = begin + range.text.length; + } + return { begin: begin, end: end }; + } + }, + unmask: function() { + return this.trigger("unmask"); + }, + mask: function(mask, settings) { + var input, + defs, + tests, + partialPosition, + firstNonMaskPos, + len, oldVal, bindInputEvent; + if (!mask && this.length > 0) { + input = $(this[0]); + return input.data($.mask.dataName)(); + } + settings = $.extend({ + autoclear: $.mask.autoclear, + placeholder: $.mask.placeholder, // Load default placeholder + completed: null + }, settings); + + + defs = $.mask.definitions; + tests = []; + 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])); + if (firstNonMaskPos === null) { + firstNonMaskPos = tests.length - 1; + } + } else { + tests.push(null); + } + }); + + return this.trigger("unmask").each(function() { + var input = $(this), + buffer = $.map( + mask.split(""), + function(c, i) { + if (c != '?') { + return defs[c] ? settings.placeholder : c; + } + }), + defaultBuffer = buffer.join(''), + focusText = input.val(); + + function seekNext(pos) { + while (++pos < len && !tests[pos]); + return pos; + } + + function seekPrev(pos) { + while (--pos >= 0 && !tests[pos]); + return pos; + } + + function shiftL(begin,end) { + var i, + j; + + if (begin<0) { + return; + } + + for (i = begin, j = seekNext(end); i < len; i++) { + if (tests[i]) { + if (j < len && tests[i].test(buffer[j])) { + buffer[i] = buffer[j]; + buffer[j] = settings.placeholder; + } else { + break; + } + + j = seekNext(j); + } + } + writeBuffer(); + input.caret(Math.max(firstNonMaskPos, begin)); + } + + function shiftR(pos) { + var i, + c, + j, + t; + + for (i = pos, c = settings.placeholder; i < len; i++) { + if (tests[i]) { + j = seekNext(i); + t = buffer[i]; + buffer[i] = c; + if (j < len && tests[j].test(t)) { + c = t; + } else { + break; + } + } + } + } function inputEvent(e) { var curVal = input.val(); var pos = input.caret(); @@ -194,178 +190,210 @@ if (settings.completed && pos == input.val().length) settings.completed.call(input); } - - function keydownEvent(e) { - var k = e.which, - pos, - begin, - end; + function blurEvent(e) { + checkVal(); + + if (input.val() != focusText) + input.change(); + } + + function keydownEvent(e) { + var k = e.which, + pos, + begin, + end; oldVal = input.val(); - //backspace, delete, and escape get special treatment - if (k === 8 || k === 46 || (iPhone && k === 127)) { - pos = input.caret(); - begin = pos.begin; - end = pos.end; - - if (end - begin === 0) { - begin = k !== 46 ? seekPrev(begin) : (end = seekNext(begin - 1)); - end = k === 46 ? seekNext(end) : end; - } - clearBuffer(begin, end); - shiftL(begin, end - 1); - - e.preventDefault(); - } else if (k == 27) {//escape - input.val(focusText); - input.caret(0, checkVal()); - e.preventDefault(); - } - } - - function keypressEvent(e) { - + //backspace, delete, and escape get special treatment + if (k === 8 || k === 46 || (iPhone && k === 127)) { + pos = input.caret(); + begin = pos.begin; + end = pos.end; + + if (end - begin === 0) { + begin=k!==46?seekPrev(begin):(end=seekNext(begin-1)); + end=k===46?seekNext(end):end; + } + clearBuffer(begin, end); + shiftL(begin, end - 1); + + e.preventDefault(); + } else if( k === 13 ) { // enter + blurEvent.call(this, e); + } else if (k === 27) { // escape + input.val(focusText); + input.caret(0, checkVal()); + e.preventDefault(); + } + } + + function keypressEvent(e) { if (bindInputEvent) { bindInputEvent = false; - input.unbind('input.mask'); + input.off('input.mask'); } ; - var k = e.which, - pos = input.caret(), - p, - c, - next; - - if (e.ctrlKey || e.altKey || e.metaKey || k < 32) {//Ignore - return; - } else if (k) { - if (pos.end - pos.begin !== 0) { - clearBuffer(pos.begin, pos.end); - shiftL(pos.begin, pos.end - 1); + var k = e.which, + pos = input.caret(), + p, + c, + next; + + if (k == 0) { + // unable to detect key pressed. Grab it from pos and adjust + // this is a failsafe for mobile chrome + // which can't detect keypress events + // reliably + if (pos.begin >= len) { + input.val(input.val().substr(0, len)); + e.preventDefault(); + return false; } - - p = seekNext(pos.begin - 1); - if (p < len) { - c = String.fromCharCode(k); - if (tests[p].test(c)) { - shiftR(p); - - buffer[p] = c; - writeBuffer(); - next = seekNext(p); - - if (android) { - setTimeout($.proxy($.fn.caret, input, next), 0); - } else { - input.caret(next); - } - - if (settings.completed && next >= len) { - settings.completed.call(input); - } - } + if (pos.begin == pos.end) { + k = input.val().charCodeAt(pos.begin - 1); + pos.begin--; + pos.end--; } - e.preventDefault(); } - } - - function clearBuffer(start, end) { - var i; - for (i = start; i < end && i < len; i++) { - if (tests[i]) { - buffer[i] = settings.placeholder; - } - } - } - function writeBuffer() { - input.val(buffer.join('')); - } - - function checkVal(allow) { - //try to place characters where they belong - var test = input.val(), - lastMatch = -1, - i, - c; - - 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)) { - buffer[i] = c; - lastMatch = i; - break; - } - } - if (pos > test.length) { - break; - } - } else if (buffer[i] === test.charAt(pos) && i !== partialPosition) { - pos++; - lastMatch = i; - } - } - if (allow) { - writeBuffer(); - } else if (lastMatch + 1 < partialPosition) { - input.val(""); - clearBuffer(0, len); - } else { - writeBuffer(); - input.val(input.val().substring(0, lastMatch + 1)); - } - return (partialPosition ? i : firstNonMaskPos); - } - - input.data($.mask.dataName, function() { - return $.map(buffer, function(c, i) { - return tests[i] && c != settings.placeholder ? c : null; - }).join(''); - }); - - if (!input.attr("readonly")) - input - .one("unmask", function() { - input - .unbind(".mask") - .removeData($.mask.dataName); - }) - .bind("focus.mask", function() { - clearTimeout(caretTimeoutId); - var pos, - moveCaret; - - focusText = input.val(); - pos = checkVal(); - - caretTimeoutId = setTimeout(function() { - writeBuffer(); - if (pos == mask.length) { - input.caret(0, pos); - } else { - input.caret(pos); - } - }, 10); - }) - .bind("blur.mask", function() { - checkVal(); - if (input.val() != focusText) - input.change(); - }) - .bind("keydown.mask", keydownEvent) - .bind("keypress.mask", keypressEvent) - .bind(pasteEventName, function() { - setTimeout(function() { - var pos = checkVal(true); - input.caret(pos); - if (settings.completed && pos == input.val().length) - settings.completed.call(input); - }, 0); - }); - if (android) // bind with input event + if (e.ctrlKey || e.altKey || e.metaKey || k < 32) {//Ignore + return; + } else if ( k && k !== 13 ) { + if (pos.end - pos.begin !== 0){ + clearBuffer(pos.begin, pos.end); + shiftL(pos.begin, pos.end-1); + } + + p = seekNext(pos.begin - 1); + if (p < len) { + c = String.fromCharCode(k); + if (tests[p].test(c)) { + shiftR(p); + + buffer[p] = c; + writeBuffer(); + next = seekNext(p); + + if(android){ + //Path for CSP Violation on FireFox OS 1.1 + var proxy = function() { + $.proxy($.fn.caret,input,next)(); + }; + + setTimeout(proxy,0); + }else{ + input.caret(next); + } + + if (settings.completed && next >= len) { + settings.completed.call(input); + } + } + } + e.preventDefault(); + } + } + + function clearBuffer(start, end) { + var i; + for (i = start; i < end && i < len; i++) { + if (tests[i]) { + buffer[i] = settings.placeholder; + } + } + } + + function writeBuffer() { input.val(buffer.join('')); } + + 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)) { + buffer[i] = c; + lastMatch = i; + break; + } + } + if (pos > test.length) { + break; + } + } else if (buffer[i] === test.charAt(pos) && i !== partialPosition) { + pos++; + lastMatch = i; + } + } + if (allow) { + writeBuffer(); + } else if (lastMatch + 1 < partialPosition) { + if (settings.autoclear || buffer.join('') === defaultBuffer) { + // Invalid value. Remove it and replace it with the + // mask, which is the default behavior. + if(input.val()) input.val(""); + clearBuffer(0, len); + } else { + // Invalid value, but we opt to show the value to the + // user and allow them to correct their mistake. + writeBuffer(); + } + } else { + writeBuffer(); + input.val(input.val().substring(0, lastMatch + 1)); + } + return (partialPosition ? i : firstNonMaskPos); + } + + input.data($.mask.dataName,function(){ + return $.map(buffer, function(c, i) { + return tests[i]&&c!=settings.placeholder ? c : null; + }).join(''); + }); + + if (!input.attr("readonly")) + input + .one("unmask", function() { + input + .off(".mask") + .removeData($.mask.dataName); + }) + .on("focus.mask", function() { + clearTimeout(caretTimeoutId); + var pos; + + focusText = input.val(); + + pos = checkVal(); + + caretTimeoutId = setTimeout(function(){ + writeBuffer(); + if (pos == mask.replace("?","").length) { + input.caret(0, pos); + } else { + input.caret(pos); + } + }, 10); + }) + .on("blur.mask", blurEvent) + .on("keydown.mask", keydownEvent) + .on("keypress.mask", keypressEvent) + .on(pasteEventName, function() { + setTimeout(function() { + var pos=checkVal(true); + input.caret(pos); + if (settings.completed && pos == input.val().length) + settings.completed.call(input); + }, 0); + }); + if (chrome && android) // bind with input event { bindInputEvent = true; input.bind('input.mask', inputEvent); @@ -373,10 +401,8 @@ { bindInputEvent = false; } - checkVal(); //Perform initial check for existing values - }); - } - }); - - + checkVal(); //Perform initial check for existing values + }); + } +}); })(jQuery); From adecd78d322773c845a77e2ba2addd279dad5467 Mon Sep 17 00:00:00 2001 From: Mohnish Thallavajhula Date: Mon, 25 Aug 2014 11:22:58 -0700 Subject: [PATCH 076/116] :heart: 2014 --- LICENSE | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LICENSE b/LICENSE index 8f4ccae..d66f9f9 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2007-2013 Josh Bush (digitalbush.com) +Copyright (c) 2007-2014 Josh Bush (digitalbush.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation @@ -19,4 +19,4 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file +OTHER DEALINGS IN THE SOFTWARE. From c836559765d2334064daae3ebe18291dbaa136df Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Sun, 28 Sep 2014 21:30:35 -0500 Subject: [PATCH 077/116] Bit of cleanup for #245 --- dist/jquery.maskedinput.js | 60 +++++++++++++++++++--------- dist/jquery.maskedinput.min.js | 4 +- src/jquery.maskedinput.js | 73 ++++++++++++++++------------------ 3 files changed, 77 insertions(+), 60 deletions(-) diff --git a/dist/jquery.maskedinput.js b/dist/jquery.maskedinput.js index 65ead0f..e0eeeb4 100644 --- a/dist/jquery.maskedinput.js +++ b/dist/jquery.maskedinput.js @@ -1,6 +1,6 @@ /* jQuery Masked Input Plugin - Copyright (c) 2007 - 2013 Josh Bush (digitalbush.com) + Copyright (c) 2007 - 2014 Josh Bush (digitalbush.com) Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) Version: 1.3.1 */ @@ -38,7 +38,7 @@ return this.trigger("unmask"); }, mask: function(mask, settings) { - var input, defs, tests, partialPosition, firstNonMaskPos, len; + var input, defs, tests, partialPosition, firstNonMaskPos, len, oldVal; return !mask && this.length > 0 ? (input = $(this[0]), input.data($.mask.dataName)()) : (settings = $.extend({ autoclear: $.mask.autoclear, placeholder: $.mask.placeholder, @@ -73,13 +73,28 @@ c = t; } } + function androidInputEvent() { + var curVal = input.val(), pos = input.caret(); + if (curVal.length < oldVal.length) { + for (checkVal(!0); pos.begin > 0 && !tests[pos.begin - 1]; ) pos.begin--; + if (0 === pos.begin) for (;pos.begin < firstNonMaskPos && !tests[pos.begin]; ) pos.begin++; + input.caret(pos.begin, pos.begin); + } else { + for (checkVal(!0); pos.begin < len && !tests[pos.begin]; ) pos.begin++; + input.caret(pos.begin, pos.begin); + } + settings.completed && pos == input.val().length && settings.completed.call(input); + } + function blurEvent() { + checkVal(), input.val() != focusText && input.change(); + } function keydownEvent(e) { var pos, begin, end, k = e.which; - 8 === k || 46 === k || iPhone && 127 === k ? (pos = input.caret(), begin = pos.begin, - end = pos.end, 0 === end - begin && (begin = 46 !== k ? seekPrev(begin) : end = seekNext(begin - 1), + oldVal = input.val(), 8 === k || 46 === k || iPhone && 127 === k ? (pos = input.caret(), + begin = pos.begin, end = pos.end, end - begin === 0 && (begin = 46 !== k ? seekPrev(begin) : end = seekNext(begin - 1), end = 46 === k ? seekNext(end) : end), clearBuffer(begin, end), shiftL(begin, end - 1), - e.preventDefault()) : 27 == k && (input.val(focusText), input.caret(0, checkVal()), - e.preventDefault()); + e.preventDefault()) : 13 === k ? blurEvent.call(this, e) : 27 === k && (input.val(focusText), + input.caret(0, checkVal()), e.preventDefault()); } function keypressEvent(e) { var p, c, next, k = e.which, pos = input.caret(); @@ -89,11 +104,19 @@ pos.begin == pos.end && (k = input.val().charCodeAt(pos.begin - 1), pos.begin--, pos.end--); } - e.ctrlKey || e.altKey || e.metaKey || 32 > k || k && (0 !== pos.end - pos.begin && (clearBuffer(pos.begin, pos.end), - shiftL(pos.begin, pos.end - 1)), p = seekNext(pos.begin - 1), len > p && (c = String.fromCharCode(k), - tests[p].test(c) && (shiftR(p), buffer[p] = c, writeBuffer(), next = seekNext(p), - android ? setTimeout($.proxy($.fn.caret, input, next), 0) : input.caret(next), settings.completed && next >= len && settings.completed.call(input))), - e.preventDefault()); + if (!(e.ctrlKey || e.altKey || e.metaKey || 32 > k) && k && 13 !== k) { + if (pos.end - pos.begin !== 0 && (clearBuffer(pos.begin, pos.end), shiftL(pos.begin, pos.end - 1)), + p = seekNext(pos.begin - 1), len > p && (c = String.fromCharCode(k), tests[p].test(c))) { + if (shiftR(p), buffer[p] = c, writeBuffer(), next = seekNext(p), android) { + var proxy = function() { + $.proxy($.fn.caret, input, next)(); + }; + setTimeout(proxy, 0); + } else input.caret(next); + settings.completed && next >= len && settings.completed.call(input); + } + e.preventDefault(); + } } function clearBuffer(start, end) { var i; @@ -112,7 +135,7 @@ } if (pos > test.length) break; } else buffer[i] === test.charAt(pos) && i !== partialPosition && (pos++, lastMatch = i); - return allow ? writeBuffer() : partialPosition > lastMatch + 1 ? settings.autoclear || buffer.join("") === defaultBuffer ? (input.val(""), + return allow ? writeBuffer() : partialPosition > lastMatch + 1 ? settings.autoclear || buffer.join("") === defaultBuffer ? (input.val() && input.val(""), clearBuffer(0, len)) : writeBuffer() : (writeBuffer(), input.val(input.val().substring(0, lastMatch + 1))), partialPosition ? i : firstNonMaskPos; } @@ -124,21 +147,20 @@ return tests[i] && c != settings.placeholder ? c : null; }).join(""); }), input.attr("readonly") || input.one("unmask", function() { - input.unbind(".mask").removeData($.mask.dataName); - }).bind("focus.mask", function() { + input.off(".mask").removeData($.mask.dataName); + }).on("focus.mask", function() { clearTimeout(caretTimeoutId); var pos; focusText = input.val(), pos = checkVal(), caretTimeoutId = setTimeout(function() { - writeBuffer(), pos == mask.length ? input.caret(0, pos) : input.caret(pos); + writeBuffer(), pos == mask.replace("?", "").length ? input.caret(0, pos) : input.caret(pos); }, 10); - }).bind("blur.mask", function() { - checkVal(), input.val() != focusText && input.change(); - }).bind("keydown.mask", keydownEvent).bind("keypress.mask", keypressEvent).bind(pasteEventName, function() { + }).on("blur.mask", blurEvent).on("keydown.mask", keydownEvent).on("keypress.mask", keypressEvent).on(pasteEventName, function() { setTimeout(function() { var pos = checkVal(!0); input.caret(pos), settings.completed && pos == input.val().length && settings.completed.call(input); }, 0); - }), chrome && android && input.bind("keyup.mask", keypressEvent), checkVal(); + }), chrome && android && input.off("input.mask").on("input.mask", androidInputEvent), + checkVal(); })); } }); diff --git a/dist/jquery.maskedinput.min.js b/dist/jquery.maskedinput.min.js index ff8a5e5..67889ad 100644 --- a/dist/jquery.maskedinput.min.js +++ b/dist/jquery.maskedinput.min.js @@ -1,7 +1,7 @@ /* jQuery Masked Input Plugin - Copyright (c) 2007 - 2013 Josh Bush (digitalbush.com) + Copyright (c) 2007 - 2014 Josh Bush (digitalbush.com) Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) Version: 1.3.1 */ -!function(a){function b(){var a=document.createElement("input"),b="onpaste";return a.setAttribute(b,""),"function"==typeof a[b]?"paste":"input"}var c,d=b()+".mask",e=navigator.userAgent,f=/iphone/i.test(e),g=/chrome/i.test(e),h=/android/i.test(e);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(b,e){var i,j,k,l,m,n;return!b&&this.length>0?(i=a(this[0]),i.data(a.mask.dataName)()):(e=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null},e),j=a.mask.definitions,k=[],l=n=b.length,m=null,a.each(b.split(""),function(a,b){"?"==b?(n--,l=a):j[b]?(k.push(new RegExp(j[b])),null===m&&(m=k.length-1)):k.push(null)}),this.trigger("unmask").each(function(){function i(a){for(;++a=0&&!k[a];);return a}function p(a,b){var c,d;if(!(0>a)){for(c=a,d=i(b);n>c;c++)if(k[c]){if(!(n>d&&k[c].test(x[d])))break;x[c]=x[d],x[d]=e.placeholder,d=i(d)}u(),w.caret(Math.max(m,a))}}function q(a){var b,c,d,f;for(b=a,c=e.placeholder;n>b;b++)if(k[b]){if(d=i(b),f=x[b],x[b]=c,!(n>d&&k[d].test(f)))break;c=f}}function r(a){var b,c,d,e=a.which;8===e||46===e||f&&127===e?(b=w.caret(),c=b.begin,d=b.end,0===d-c&&(c=46!==e?o(c):d=i(c-1),d=46===e?i(d):d),t(c,d),p(c,d-1),a.preventDefault()):27==e&&(w.val(z),w.caret(0,v()),a.preventDefault())}function s(b){var c,d,f,g=b.which,j=w.caret();if(0==g){if(j.begin>=n)return w.val(w.val().substr(0,n)),b.preventDefault(),!1;j.begin==j.end&&(g=w.val().charCodeAt(j.begin-1),j.begin--,j.end--)}b.ctrlKey||b.altKey||b.metaKey||32>g||g&&(0!==j.end-j.begin&&(t(j.begin,j.end),p(j.begin,j.end-1)),c=i(j.begin-1),n>c&&(d=String.fromCharCode(g),k[c].test(d)&&(q(c),x[c]=d,u(),f=i(c),h?setTimeout(a.proxy(a.fn.caret,w,f),0):w.caret(f),e.completed&&f>=n&&e.completed.call(w))),b.preventDefault())}function t(a,b){var c;for(c=a;b>c&&n>c;c++)k[c]&&(x[c]=e.placeholder)}function u(){w.val(x.join(""))}function v(a){var b,c,d,f=w.val(),g=-1;for(b=0,d=0;n>b;b++)if(k[b]){for(x[b]=e.placeholder;d++f.length)break}else x[b]===f.charAt(d)&&b!==l&&(d++,g=b);return a?u():l>g+1?e.autoclear||x.join("")===y?(w.val(""),t(0,n)):u():(u(),w.val(w.val().substring(0,g+1))),l?b:m}var w=a(this),x=a.map(b.split(""),function(a){return"?"!=a?j[a]?e.placeholder:a:void 0}),y=x.join(""),z=w.val();w.data(a.mask.dataName,function(){return a.map(x,function(a,b){return k[b]&&a!=e.placeholder?a:null}).join("")}),w.attr("readonly")||w.one("unmask",function(){w.unbind(".mask").removeData(a.mask.dataName)}).bind("focus.mask",function(){clearTimeout(c);var a;z=w.val(),a=v(),c=setTimeout(function(){u(),a==b.length?w.caret(0,a):w.caret(a)},10)}).bind("blur.mask",function(){v(),w.val()!=z&&w.change()}).bind("keydown.mask",r).bind("keypress.mask",s).bind(d,function(){setTimeout(function(){var a=v(!0);w.caret(a),e.completed&&a==w.val().length&&e.completed.call(w)},0)}),g&&h&&w.bind("keyup.mask",s),v()}))}})}(jQuery); \ No newline at end of file +!function(a){function b(){var a=document.createElement("input"),b="onpaste";return a.setAttribute(b,""),"function"==typeof a[b]?"paste":"input"}var c,d=b()+".mask",e=navigator.userAgent,f=/iphone/i.test(e),g=/chrome/i.test(e),h=/android/i.test(e);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(b,e){var i,j,k,l,m,n,o;return!b&&this.length>0?(i=a(this[0]),i.data(a.mask.dataName)()):(e=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null},e),j=a.mask.definitions,k=[],l=n=b.length,m=null,a.each(b.split(""),function(a,b){"?"==b?(n--,l=a):j[b]?(k.push(new RegExp(j[b])),null===m&&(m=k.length-1)):k.push(null)}),this.trigger("unmask").each(function(){function i(a){for(;++a=0&&!k[a];);return a}function q(a,b){var c,d;if(!(0>a)){for(c=a,d=i(b);n>c;c++)if(k[c]){if(!(n>d&&k[c].test(A[d])))break;A[c]=A[d],A[d]=e.placeholder,d=i(d)}x(),z.caret(Math.max(m,a))}}function r(a){var b,c,d,f;for(b=a,c=e.placeholder;n>b;b++)if(k[b]){if(d=i(b),f=A[b],A[b]=c,!(n>d&&k[d].test(f)))break;c=f}}function s(){var a=z.val(),b=z.caret();if(a.length0&&!k[b.begin-1];)b.begin--;if(0===b.begin)for(;b.begin=n)return z.val(z.val().substr(0,n)),b.preventDefault(),!1;j.begin==j.end&&(g=z.val().charCodeAt(j.begin-1),j.begin--,j.end--)}if(!(b.ctrlKey||b.altKey||b.metaKey||32>g)&&g&&13!==g){if(j.end-j.begin!==0&&(w(j.begin,j.end),q(j.begin,j.end-1)),c=i(j.begin-1),n>c&&(d=String.fromCharCode(g),k[c].test(d))){if(r(c),A[c]=d,x(),f=i(c),h){var l=function(){a.proxy(a.fn.caret,z,f)()};setTimeout(l,0)}else z.caret(f);e.completed&&f>=n&&e.completed.call(z)}b.preventDefault()}}function w(a,b){var c;for(c=a;b>c&&n>c;c++)k[c]&&(A[c]=e.placeholder)}function x(){z.val(A.join(""))}function y(a){var b,c,d,f=z.val(),g=-1;for(b=0,d=0;n>b;b++)if(k[b]){for(A[b]=e.placeholder;d++f.length)break}else A[b]===f.charAt(d)&&b!==l&&(d++,g=b);return a?x():l>g+1?e.autoclear||A.join("")===B?(z.val()&&z.val(""),w(0,n)):x():(x(),z.val(z.val().substring(0,g+1))),l?b:m}var z=a(this),A=a.map(b.split(""),function(a){return"?"!=a?j[a]?e.placeholder:a:void 0}),B=A.join(""),C=z.val();z.data(a.mask.dataName,function(){return a.map(A,function(a,b){return k[b]&&a!=e.placeholder?a:null}).join("")}),z.attr("readonly")||z.one("unmask",function(){z.off(".mask").removeData(a.mask.dataName)}).on("focus.mask",function(){clearTimeout(c);var a;C=z.val(),a=y(),c=setTimeout(function(){x(),a==b.replace("?","").length?z.caret(0,a):z.caret(a)},10)}).on("blur.mask",t).on("keydown.mask",u).on("keypress.mask",v).on(d,function(){setTimeout(function(){var a=y(!0);z.caret(a),e.completed&&a==z.val().length&&e.completed.call(z)},0)}),g&&h&&z.off("input.mask").on("input.mask",s),y()}))}})}(jQuery); \ No newline at end of file diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 5117780..d2d10d7 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -69,7 +69,8 @@ $.fn.extend({ tests, partialPosition, firstNonMaskPos, - len, oldVal, bindInputEvent; + len, + oldVal; if (!mask && this.length > 0) { input = $(this[0]); @@ -166,36 +167,38 @@ $.fn.extend({ } } } - function inputEvent(e) { - var curVal = input.val(); - var pos = input.caret(); - if (curVal.length < oldVal.length) { - // a deletion or backspace happened - checkVal(true); - while (pos.begin > 0 && !tests[pos.begin-1]) - pos.begin--; - if (pos.begin === 0) - { - while (pos.begin < firstNonMaskPos && !tests[pos.begin]) - pos.begin++; - } - input.caret(pos.begin,pos.begin); - } else { - var pos2 = checkVal(true); - while (pos.begin < len && !tests[pos.begin]) - pos.begin++; - input.caret(pos.begin,pos.begin); + function androidInputEvent(e) { + var curVal = input.val(); + var pos = input.caret(); + if (curVal.length < oldVal.length) { + // a deletion or backspace happened + checkVal(true); + while (pos.begin > 0 && !tests[pos.begin-1]) + pos.begin--; + if (pos.begin === 0) + { + while (pos.begin < firstNonMaskPos && !tests[pos.begin]) + pos.begin++; } - if (settings.completed && pos == input.val().length) - settings.completed.call(input); + input.caret(pos.begin,pos.begin); + } else { + var pos2 = checkVal(true); + while (pos.begin < len && !tests[pos.begin]) + pos.begin++; + + input.caret(pos.begin,pos.begin); } - function blurEvent(e) { - checkVal(); + if (settings.completed && pos == input.val().length) + settings.completed.call(input); + } + + function blurEvent(e) { + checkVal(); - if (input.val() != focusText) - input.change(); - } + if (input.val() != focusText) + input.change(); + } function keydownEvent(e) { var k = e.which, @@ -227,12 +230,6 @@ $.fn.extend({ } function keypressEvent(e) { - if (bindInputEvent) - { - bindInputEvent = false; - input.off('input.mask'); - } - ; var k = e.which, pos = input.caret(), p, @@ -393,13 +390,11 @@ $.fn.extend({ settings.completed.call(input); }, 0); }); - if (chrome && android) // bind with input event - { - bindInputEvent = true; - input.bind('input.mask', inputEvent); - } else + if (chrome && android) { - bindInputEvent = false; + input + .off('input.mask') + .on('input.mask', androidInputEvent); } checkVal(); //Perform initial check for existing values }); From 179d895ede3d366fe3c479ff1b7072d4359e2272 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Sun, 28 Sep 2014 21:33:14 -0500 Subject: [PATCH 078/116] Shouldn't need this android keypress workaround now. --- dist/jquery.maskedinput.js | 6 ------ dist/jquery.maskedinput.min.js | 2 +- src/jquery.maskedinput.js | 17 ----------------- 3 files changed, 1 insertion(+), 24 deletions(-) diff --git a/dist/jquery.maskedinput.js b/dist/jquery.maskedinput.js index e0eeeb4..6b689a1 100644 --- a/dist/jquery.maskedinput.js +++ b/dist/jquery.maskedinput.js @@ -98,12 +98,6 @@ } function keypressEvent(e) { var p, c, next, k = e.which, pos = input.caret(); - if (0 == k) { - if (pos.begin >= len) return input.val(input.val().substr(0, len)), e.preventDefault(), - !1; - pos.begin == pos.end && (k = input.val().charCodeAt(pos.begin - 1), pos.begin--, - pos.end--); - } if (!(e.ctrlKey || e.altKey || e.metaKey || 32 > k) && k && 13 !== k) { if (pos.end - pos.begin !== 0 && (clearBuffer(pos.begin, pos.end), shiftL(pos.begin, pos.end - 1)), p = seekNext(pos.begin - 1), len > p && (c = String.fromCharCode(k), tests[p].test(c))) { diff --git a/dist/jquery.maskedinput.min.js b/dist/jquery.maskedinput.min.js index 67889ad..895a33b 100644 --- a/dist/jquery.maskedinput.min.js +++ b/dist/jquery.maskedinput.min.js @@ -4,4 +4,4 @@ Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) Version: 1.3.1 */ -!function(a){function b(){var a=document.createElement("input"),b="onpaste";return a.setAttribute(b,""),"function"==typeof a[b]?"paste":"input"}var c,d=b()+".mask",e=navigator.userAgent,f=/iphone/i.test(e),g=/chrome/i.test(e),h=/android/i.test(e);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(b,e){var i,j,k,l,m,n,o;return!b&&this.length>0?(i=a(this[0]),i.data(a.mask.dataName)()):(e=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null},e),j=a.mask.definitions,k=[],l=n=b.length,m=null,a.each(b.split(""),function(a,b){"?"==b?(n--,l=a):j[b]?(k.push(new RegExp(j[b])),null===m&&(m=k.length-1)):k.push(null)}),this.trigger("unmask").each(function(){function i(a){for(;++a=0&&!k[a];);return a}function q(a,b){var c,d;if(!(0>a)){for(c=a,d=i(b);n>c;c++)if(k[c]){if(!(n>d&&k[c].test(A[d])))break;A[c]=A[d],A[d]=e.placeholder,d=i(d)}x(),z.caret(Math.max(m,a))}}function r(a){var b,c,d,f;for(b=a,c=e.placeholder;n>b;b++)if(k[b]){if(d=i(b),f=A[b],A[b]=c,!(n>d&&k[d].test(f)))break;c=f}}function s(){var a=z.val(),b=z.caret();if(a.length0&&!k[b.begin-1];)b.begin--;if(0===b.begin)for(;b.begin=n)return z.val(z.val().substr(0,n)),b.preventDefault(),!1;j.begin==j.end&&(g=z.val().charCodeAt(j.begin-1),j.begin--,j.end--)}if(!(b.ctrlKey||b.altKey||b.metaKey||32>g)&&g&&13!==g){if(j.end-j.begin!==0&&(w(j.begin,j.end),q(j.begin,j.end-1)),c=i(j.begin-1),n>c&&(d=String.fromCharCode(g),k[c].test(d))){if(r(c),A[c]=d,x(),f=i(c),h){var l=function(){a.proxy(a.fn.caret,z,f)()};setTimeout(l,0)}else z.caret(f);e.completed&&f>=n&&e.completed.call(z)}b.preventDefault()}}function w(a,b){var c;for(c=a;b>c&&n>c;c++)k[c]&&(A[c]=e.placeholder)}function x(){z.val(A.join(""))}function y(a){var b,c,d,f=z.val(),g=-1;for(b=0,d=0;n>b;b++)if(k[b]){for(A[b]=e.placeholder;d++f.length)break}else A[b]===f.charAt(d)&&b!==l&&(d++,g=b);return a?x():l>g+1?e.autoclear||A.join("")===B?(z.val()&&z.val(""),w(0,n)):x():(x(),z.val(z.val().substring(0,g+1))),l?b:m}var z=a(this),A=a.map(b.split(""),function(a){return"?"!=a?j[a]?e.placeholder:a:void 0}),B=A.join(""),C=z.val();z.data(a.mask.dataName,function(){return a.map(A,function(a,b){return k[b]&&a!=e.placeholder?a:null}).join("")}),z.attr("readonly")||z.one("unmask",function(){z.off(".mask").removeData(a.mask.dataName)}).on("focus.mask",function(){clearTimeout(c);var a;C=z.val(),a=y(),c=setTimeout(function(){x(),a==b.replace("?","").length?z.caret(0,a):z.caret(a)},10)}).on("blur.mask",t).on("keydown.mask",u).on("keypress.mask",v).on(d,function(){setTimeout(function(){var a=y(!0);z.caret(a),e.completed&&a==z.val().length&&e.completed.call(z)},0)}),g&&h&&z.off("input.mask").on("input.mask",s),y()}))}})}(jQuery); \ No newline at end of file +!function(a){function b(){var a=document.createElement("input"),b="onpaste";return a.setAttribute(b,""),"function"==typeof a[b]?"paste":"input"}var c,d=b()+".mask",e=navigator.userAgent,f=/iphone/i.test(e),g=/chrome/i.test(e),h=/android/i.test(e);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(b,e){var i,j,k,l,m,n,o;return!b&&this.length>0?(i=a(this[0]),i.data(a.mask.dataName)()):(e=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null},e),j=a.mask.definitions,k=[],l=n=b.length,m=null,a.each(b.split(""),function(a,b){"?"==b?(n--,l=a):j[b]?(k.push(new RegExp(j[b])),null===m&&(m=k.length-1)):k.push(null)}),this.trigger("unmask").each(function(){function i(a){for(;++a=0&&!k[a];);return a}function q(a,b){var c,d;if(!(0>a)){for(c=a,d=i(b);n>c;c++)if(k[c]){if(!(n>d&&k[c].test(A[d])))break;A[c]=A[d],A[d]=e.placeholder,d=i(d)}x(),z.caret(Math.max(m,a))}}function r(a){var b,c,d,f;for(b=a,c=e.placeholder;n>b;b++)if(k[b]){if(d=i(b),f=A[b],A[b]=c,!(n>d&&k[d].test(f)))break;c=f}}function s(){var a=z.val(),b=z.caret();if(a.length0&&!k[b.begin-1];)b.begin--;if(0===b.begin)for(;b.beging)&&g&&13!==g){if(j.end-j.begin!==0&&(w(j.begin,j.end),q(j.begin,j.end-1)),c=i(j.begin-1),n>c&&(d=String.fromCharCode(g),k[c].test(d))){if(r(c),A[c]=d,x(),f=i(c),h){var l=function(){a.proxy(a.fn.caret,z,f)()};setTimeout(l,0)}else z.caret(f);e.completed&&f>=n&&e.completed.call(z)}b.preventDefault()}}function w(a,b){var c;for(c=a;b>c&&n>c;c++)k[c]&&(A[c]=e.placeholder)}function x(){z.val(A.join(""))}function y(a){var b,c,d,f=z.val(),g=-1;for(b=0,d=0;n>b;b++)if(k[b]){for(A[b]=e.placeholder;d++f.length)break}else A[b]===f.charAt(d)&&b!==l&&(d++,g=b);return a?x():l>g+1?e.autoclear||A.join("")===B?(z.val()&&z.val(""),w(0,n)):x():(x(),z.val(z.val().substring(0,g+1))),l?b:m}var z=a(this),A=a.map(b.split(""),function(a){return"?"!=a?j[a]?e.placeholder:a:void 0}),B=A.join(""),C=z.val();z.data(a.mask.dataName,function(){return a.map(A,function(a,b){return k[b]&&a!=e.placeholder?a:null}).join("")}),z.attr("readonly")||z.one("unmask",function(){z.off(".mask").removeData(a.mask.dataName)}).on("focus.mask",function(){clearTimeout(c);var a;C=z.val(),a=y(),c=setTimeout(function(){x(),a==b.replace("?","").length?z.caret(0,a):z.caret(a)},10)}).on("blur.mask",t).on("keydown.mask",u).on("keypress.mask",v).on(d,function(){setTimeout(function(){var a=y(!0);z.caret(a),e.completed&&a==z.val().length&&e.completed.call(z)},0)}),g&&h&&z.off("input.mask").on("input.mask",s),y()}))}})}(jQuery); \ No newline at end of file diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index d2d10d7..dd388f1 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -236,23 +236,6 @@ $.fn.extend({ c, next; - if (k == 0) { - // unable to detect key pressed. Grab it from pos and adjust - // this is a failsafe for mobile chrome - // which can't detect keypress events - // reliably - if (pos.begin >= len) { - input.val(input.val().substr(0, len)); - e.preventDefault(); - return false; - } - if (pos.begin == pos.end) { - k = input.val().charCodeAt(pos.begin - 1); - pos.begin--; - pos.end--; - } - } - if (e.ctrlKey || e.altKey || e.metaKey || k < 32) {//Ignore return; } else if ( k && k !== 13 ) { From ed5ab64ef32ae47dff5967aba7a092abe1a911b6 Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Mon, 6 Oct 2014 22:06:09 -0400 Subject: [PATCH 079/116] add bower.json --- bower.json | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 bower.json diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..e8d3bcd --- /dev/null +++ b/bower.json @@ -0,0 +1,29 @@ +{ + "name": "jquery.maskedinput", + "version": "1.3.1", + "homepage": "http://digitalbush.com/projects/masked-input-plugin/", + "authors": [ + "Josh Bush (digitalbush.com)" + ], + "description": "jQuery Masked Input Plugin", + "moduleType": [ + "es6" + ], + "keywords": [ + "input", + "form", + "mask", + "jquery" + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "spec", + "lib" + ], + "dependencies": { + "jquery": ">=1.8.3 <2.0" + } +} From fbf77453fe7c42c8d92d99b0833dd4af8cc94562 Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Mon, 6 Oct 2014 22:06:17 -0400 Subject: [PATCH 080/116] ignore bower_components/ --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 3175dd7..ae2f59e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .idea/* node_modules/ +bower_components/ From 0b2f909f38f6762a933039440b4b50d413076b8d Mon Sep 17 00:00:00 2001 From: Jared Barboza Date: Mon, 6 Oct 2014 22:16:00 -0400 Subject: [PATCH 081/116] fix formatting of cmds, add bower instructions --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ac4c6c0..1637dd8 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,21 @@ If your requirements aren't met by the predefined placeholders, you can always a By design, this plugin will reject input which doesn't complete the mask. You can bypass this by using a '?' character at the position where you would like to consider input optional. For example, a mask of "(999) 999-9999? x99999" would require only the first 10 digits of a phone number with extension being optional. + +Getting the bits +---------------- +We generally recommend that you use [bower](http://bower.io) to install jquery.maskedinput plugin, however you can download a copy of the latest release by clicking [this link right here.](https://github.com/digitalBush/jquery.maskedinput/archive/1.3.1.zip) + + $ bower install --save jquery.maskedinput + + Setting up your Developer Environment ------------------------------------- jQuery Masked Input uses [NodeJS](http://www.nodejs.org) and [GruntJS](http://www.gruntjs.com) as it's developer platform and build automation tool. To get your environment setup correctly, you'll need nodejs version 0.8.25 or greater installed. You'll also need to install the grunt command line tool: - $ sudo npm install -g grunt-cli + $ sudo npm install -g grunt-cli Once node is installed on your system all that you need to do is install the developer dependencies and run the grunt build: From a006b22a6a617e95edb187173641ee53210dc4f5 Mon Sep 17 00:00:00 2001 From: Angela Nicholas Date: Tue, 7 Oct 2014 14:10:24 -0700 Subject: [PATCH 082/116] clear buffer when shifting characters to the left --- spec/Focus.Spec.js | 17 +++++++++++++++++ src/jquery.maskedinput.js | 1 + 2 files changed, 18 insertions(+) diff --git a/spec/Focus.Spec.js b/spec/Focus.Spec.js index 9964893..de3976c 100644 --- a/spec/Focus.Spec.js +++ b/spec/Focus.Spec.js @@ -126,6 +126,23 @@ feature("Leaving A Masked Input",function(){ expect(input).toHaveValue("1_"); }); }); + + scenario("Shifts characters left on blur with autoclear false",function(){ + given("a mask with 10 placeholders",function(){ + input.mask("(999) 999-9999", { autoclear: false }); + }); + when("focusing input",function(){ + input.focus(); + }); + waits(20); + when("typing characters at the end of the mask and blurring",function(){ + input.caret(12); + input.mashKeys("44").blur(); + }); + then("characters should shift left to beginning of mask",function(){ + expect(input).toHaveValue("(44_) ___-____"); + }); + }); }); feature("Optional marker",function(){ diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index dd388f1..1350673 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -305,6 +305,7 @@ $.fn.extend({ } } if (pos > test.length) { + clearBuffer(i + 1, len); break; } } else if (buffer[i] === test.charAt(pos) && i !== partialPosition) { From 43739da3f73e5ed3e839110bc9b692be4fa4abc7 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Thu, 9 Oct 2014 21:00:10 -0500 Subject: [PATCH 083/116] Fire completed callback when user types in middle of input. Closes #254 Closes #214 Closes #174 Closes #126 Closes #88 --- dist/jquery.maskedinput.js | 21 +++++-- dist/jquery.maskedinput.min.js | 2 +- spec/Completed.Spec.js | 102 +++++++++++++++++++++++++++++++++ src/jquery.maskedinput.js | 43 +++++++++----- 4 files changed, 147 insertions(+), 21 deletions(-) create mode 100644 spec/Completed.Spec.js diff --git a/dist/jquery.maskedinput.js b/dist/jquery.maskedinput.js index 6b689a1..e4b1ae2 100644 --- a/dist/jquery.maskedinput.js +++ b/dist/jquery.maskedinput.js @@ -38,7 +38,7 @@ return this.trigger("unmask"); }, mask: function(mask, settings) { - var input, defs, tests, partialPosition, firstNonMaskPos, len, oldVal; + var input, defs, tests, partialPosition, firstNonMaskPos, lastRequiredNonMaskPos, len, oldVal; return !mask && this.length > 0 ? (input = $(this[0]), input.data($.mask.dataName)()) : (settings = $.extend({ autoclear: $.mask.autoclear, placeholder: $.mask.placeholder, @@ -46,8 +46,14 @@ }, settings), defs = $.mask.definitions, tests = [], partialPosition = len = mask.length, firstNonMaskPos = null, $.each(mask.split(""), function(i, c) { "?" == c ? (len--, partialPosition = i) : defs[c] ? (tests.push(new RegExp(defs[c])), - null === firstNonMaskPos && (firstNonMaskPos = tests.length - 1)) : tests.push(null); + null === firstNonMaskPos && (firstNonMaskPos = tests.length - 1), partialPosition > i && (lastRequiredNonMaskPos = tests.length - 1)) : tests.push(null); }), this.trigger("unmask").each(function() { + function tryFireCompleted() { + if (settings.completed) { + for (var i = firstNonMaskPos; lastRequiredNonMaskPos >= i; i++) if (tests[i] && buffer[i] === settings.placeholder) return; + settings.completed.call(input); + } + } function seekNext(pos) { for (;++pos < len && !tests[pos]; ) ; return pos; @@ -83,7 +89,7 @@ for (checkVal(!0); pos.begin < len && !tests[pos.begin]; ) pos.begin++; input.caret(pos.begin, pos.begin); } - settings.completed && pos == input.val().length && settings.completed.call(input); + tryFireCompleted(); } function blurEvent() { checkVal(), input.val() != focusText && input.change(); @@ -107,7 +113,7 @@ }; setTimeout(proxy, 0); } else input.caret(next); - settings.completed && next >= len && settings.completed.call(input); + pos.begin <= lastRequiredNonMaskPos && tryFireCompleted(); } e.preventDefault(); } @@ -127,7 +133,10 @@ buffer[i] = c, lastMatch = i; break; } - if (pos > test.length) break; + if (pos > test.length) { + clearBuffer(i + 1, len); + break; + } } else buffer[i] === test.charAt(pos) && i !== partialPosition && (pos++, lastMatch = i); return allow ? writeBuffer() : partialPosition > lastMatch + 1 ? settings.autoclear || buffer.join("") === defaultBuffer ? (input.val() && input.val(""), clearBuffer(0, len)) : writeBuffer() : (writeBuffer(), input.val(input.val().substring(0, lastMatch + 1))), @@ -151,7 +160,7 @@ }).on("blur.mask", blurEvent).on("keydown.mask", keydownEvent).on("keypress.mask", keypressEvent).on(pasteEventName, function() { setTimeout(function() { var pos = checkVal(!0); - input.caret(pos), settings.completed && pos == input.val().length && settings.completed.call(input); + input.caret(pos), tryFireCompleted(); }, 0); }), chrome && android && input.off("input.mask").on("input.mask", androidInputEvent), checkVal(); diff --git a/dist/jquery.maskedinput.min.js b/dist/jquery.maskedinput.min.js index 895a33b..5b54bdb 100644 --- a/dist/jquery.maskedinput.min.js +++ b/dist/jquery.maskedinput.min.js @@ -4,4 +4,4 @@ Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) Version: 1.3.1 */ -!function(a){function b(){var a=document.createElement("input"),b="onpaste";return a.setAttribute(b,""),"function"==typeof a[b]?"paste":"input"}var c,d=b()+".mask",e=navigator.userAgent,f=/iphone/i.test(e),g=/chrome/i.test(e),h=/android/i.test(e);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(b,e){var i,j,k,l,m,n,o;return!b&&this.length>0?(i=a(this[0]),i.data(a.mask.dataName)()):(e=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null},e),j=a.mask.definitions,k=[],l=n=b.length,m=null,a.each(b.split(""),function(a,b){"?"==b?(n--,l=a):j[b]?(k.push(new RegExp(j[b])),null===m&&(m=k.length-1)):k.push(null)}),this.trigger("unmask").each(function(){function i(a){for(;++a=0&&!k[a];);return a}function q(a,b){var c,d;if(!(0>a)){for(c=a,d=i(b);n>c;c++)if(k[c]){if(!(n>d&&k[c].test(A[d])))break;A[c]=A[d],A[d]=e.placeholder,d=i(d)}x(),z.caret(Math.max(m,a))}}function r(a){var b,c,d,f;for(b=a,c=e.placeholder;n>b;b++)if(k[b]){if(d=i(b),f=A[b],A[b]=c,!(n>d&&k[d].test(f)))break;c=f}}function s(){var a=z.val(),b=z.caret();if(a.length0&&!k[b.begin-1];)b.begin--;if(0===b.begin)for(;b.beging)&&g&&13!==g){if(j.end-j.begin!==0&&(w(j.begin,j.end),q(j.begin,j.end-1)),c=i(j.begin-1),n>c&&(d=String.fromCharCode(g),k[c].test(d))){if(r(c),A[c]=d,x(),f=i(c),h){var l=function(){a.proxy(a.fn.caret,z,f)()};setTimeout(l,0)}else z.caret(f);e.completed&&f>=n&&e.completed.call(z)}b.preventDefault()}}function w(a,b){var c;for(c=a;b>c&&n>c;c++)k[c]&&(A[c]=e.placeholder)}function x(){z.val(A.join(""))}function y(a){var b,c,d,f=z.val(),g=-1;for(b=0,d=0;n>b;b++)if(k[b]){for(A[b]=e.placeholder;d++f.length)break}else A[b]===f.charAt(d)&&b!==l&&(d++,g=b);return a?x():l>g+1?e.autoclear||A.join("")===B?(z.val()&&z.val(""),w(0,n)):x():(x(),z.val(z.val().substring(0,g+1))),l?b:m}var z=a(this),A=a.map(b.split(""),function(a){return"?"!=a?j[a]?e.placeholder:a:void 0}),B=A.join(""),C=z.val();z.data(a.mask.dataName,function(){return a.map(A,function(a,b){return k[b]&&a!=e.placeholder?a:null}).join("")}),z.attr("readonly")||z.one("unmask",function(){z.off(".mask").removeData(a.mask.dataName)}).on("focus.mask",function(){clearTimeout(c);var a;C=z.val(),a=y(),c=setTimeout(function(){x(),a==b.replace("?","").length?z.caret(0,a):z.caret(a)},10)}).on("blur.mask",t).on("keydown.mask",u).on("keypress.mask",v).on(d,function(){setTimeout(function(){var a=y(!0);z.caret(a),e.completed&&a==z.val().length&&e.completed.call(z)},0)}),g&&h&&z.off("input.mask").on("input.mask",s),y()}))}})}(jQuery); \ No newline at end of file +!function(a){function b(){var a=document.createElement("input"),b="onpaste";return a.setAttribute(b,""),"function"==typeof a[b]?"paste":"input"}var c,d=b()+".mask",e=navigator.userAgent,f=/iphone/i.test(e),g=/chrome/i.test(e),h=/android/i.test(e);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(b,e){var i,j,k,l,m,n,o,p;return!b&&this.length>0?(i=a(this[0]),i.data(a.mask.dataName)()):(e=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null},e),j=a.mask.definitions,k=[],l=o=b.length,m=null,a.each(b.split(""),function(a,b){"?"==b?(o--,l=a):j[b]?(k.push(new RegExp(j[b])),null===m&&(m=k.length-1),l>a&&(n=k.length-1)):k.push(null)}),this.trigger("unmask").each(function(){function i(){if(e.completed){for(var a=m;n>=a;a++)if(k[a]&&C[a]===e.placeholder)return;e.completed.call(B)}}function q(a){for(;++a=0&&!k[a];);return a}function s(a,b){var c,d;if(!(0>a)){for(c=a,d=q(b);o>c;c++)if(k[c]){if(!(o>d&&k[c].test(C[d])))break;C[c]=C[d],C[d]=e.placeholder,d=q(d)}z(),B.caret(Math.max(m,a))}}function t(a){var b,c,d,f;for(b=a,c=e.placeholder;o>b;b++)if(k[b]){if(d=q(b),f=C[b],C[b]=c,!(o>d&&k[d].test(f)))break;c=f}}function u(){var a=B.val(),b=B.caret();if(a.length0&&!k[b.begin-1];)b.begin--;if(0===b.begin)for(;b.beginf)&&f&&13!==f){if(g.end-g.begin!==0&&(y(g.begin,g.end),s(g.begin,g.end-1)),c=q(g.begin-1),o>c&&(d=String.fromCharCode(f),k[c].test(d))){if(t(c),C[c]=d,z(),e=q(c),h){var j=function(){a.proxy(a.fn.caret,B,e)()};setTimeout(j,0)}else B.caret(e);g.begin<=n&&i()}b.preventDefault()}}function y(a,b){var c;for(c=a;b>c&&o>c;c++)k[c]&&(C[c]=e.placeholder)}function z(){B.val(C.join(""))}function A(a){var b,c,d,f=B.val(),g=-1;for(b=0,d=0;o>b;b++)if(k[b]){for(C[b]=e.placeholder;d++f.length){y(b+1,o);break}}else C[b]===f.charAt(d)&&b!==l&&(d++,g=b);return a?z():l>g+1?e.autoclear||C.join("")===D?(B.val()&&B.val(""),y(0,o)):z():(z(),B.val(B.val().substring(0,g+1))),l?b:m}var B=a(this),C=a.map(b.split(""),function(a){return"?"!=a?j[a]?e.placeholder:a:void 0}),D=C.join(""),E=B.val();B.data(a.mask.dataName,function(){return a.map(C,function(a,b){return k[b]&&a!=e.placeholder?a:null}).join("")}),B.attr("readonly")||B.one("unmask",function(){B.off(".mask").removeData(a.mask.dataName)}).on("focus.mask",function(){clearTimeout(c);var a;E=B.val(),a=A(),c=setTimeout(function(){z(),a==b.replace("?","").length?B.caret(0,a):B.caret(a)},10)}).on("blur.mask",v).on("keydown.mask",w).on("keypress.mask",x).on(d,function(){setTimeout(function(){var a=A(!0);B.caret(a),i()},0)}),g&&h&&B.off("input.mask").on("input.mask",u),A()}))}})}(jQuery); \ No newline at end of file diff --git a/spec/Completed.Spec.js b/spec/Completed.Spec.js new file mode 100644 index 0000000..34a5b8a --- /dev/null +++ b/spec/Completed.Spec.js @@ -0,0 +1,102 @@ +feature("Completed callback", function() { + scenario('Completing mask by typing last character',function(){ + var completed=false; + given("an input with a completed callback", function(){ + input.mask("99",{completed:function(){completed=true;}}); + }); + + when("typing left to right",function(){ + input.mashKeys("12"); + }); + + then("completed callback should be called",function(){ + expect(completed).toBeTruthy(); + }); + then("value should be correct",function(){ + expect(input).toHaveValue('12'); + }); + }); + + scenario('Completing mask by typing first character',function(){ + var completed=false; + given("an input with a completed callback", function(){ + input.val("12").mask("99",{completed:function(){completed=true;}}); + }); + + when("replacing first character value",function(){ + input + .caret(1) + .mashKeys(function(keys){keys.type(keys.backspace)}) + .mashKeys("3"); + }); + + then("completed callback should be called",function(){ + expect(completed).toBeTruthy(); + }); + + then("value should be correct",function(){ + expect(input).toHaveValue('32'); + }); + }); + + scenario('Typing last character of incomplete mask',function(){ + var completed=false; + given("an input with a completed callback", function(){ + input + .mask("99",{completed:function(){completed=true;}}) + .mashKeys("1") + .mashKeys(function(keys){keys.type(keys.backspace)}); + }); + + when("moving cursor to last position and typing",function(){ + input.caret(1).mashKeys("5"); + }); + + then("completed callback should not be called",function(){ + expect(completed).toBeFalsy(); + }); + + then("value should be correct",function(){ + expect(input).toHaveValue('_5'); + }); + + }); + + scenario('Typing last character of required portion of mask containing optional',function(){ + var completed=false; + given("an input with a completed callback", function(){ + input.mask("99?99",{completed:function(){completed=true;}}); + }); + + when("typing left to right",function(){ + input.mashKeys("12"); + }); + + then("completed callback should be called",function(){ + expect(completed).toBeTruthy(); + }); + + then("value should be correct",function(){ + expect(input).toHaveValue('12__'); + }); + }); + + scenario('Typing all characters of required portion of mask containing optional',function(){ + var completedCount=0; + given("an input with a completed callback", function(){ + input.mask("99?99",{completed:function(){completedCount++;}}); + }); + + when("typing left to right",function(){ + input.mashKeys("1234"); + }); + + then("completed callback should be called",function(){ + expect(completedCount).toEqual(1); + }); + + then("value should be correct",function(){ + expect(input).toHaveValue('1234'); + }); + }); +}); diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 1350673..1b7b9f9 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -69,6 +69,7 @@ $.fn.extend({ tests, partialPosition, firstNonMaskPos, + lastRequiredNonMaskPos, len, oldVal; @@ -97,6 +98,9 @@ $.fn.extend({ if (firstNonMaskPos === null) { firstNonMaskPos = tests.length - 1; } + if(i < partialPosition){ + lastRequiredNonMaskPos = tests.length - 1; + } } else { tests.push(null); } @@ -105,15 +109,28 @@ $.fn.extend({ return this.trigger("unmask").each(function() { var input = $(this), buffer = $.map( - mask.split(""), - function(c, i) { - if (c != '?') { - return defs[c] ? settings.placeholder : c; - } - }), + mask.split(""), + function(c, i) { + if (c != '?') { + return defs[c] ? settings.placeholder : c; + } + }), defaultBuffer = buffer.join(''), focusText = input.val(); + function tryFireCompleted(){ + if (!settings.completed) { + return; + } + + for (var i = firstNonMaskPos; i <= lastRequiredNonMaskPos; i++) { + if (tests[i] && buffer[i] === settings.placeholder) { + return; + } + } + settings.completed.call(input); + } + function seekNext(pos) { while (++pos < len && !tests[pos]); return pos; @@ -189,8 +206,8 @@ $.fn.extend({ input.caret(pos.begin,pos.begin); } - if (settings.completed && pos == input.val().length) - settings.completed.call(input); + + tryFireCompleted(); } function blurEvent(e) { @@ -264,10 +281,9 @@ $.fn.extend({ }else{ input.caret(next); } - - if (settings.completed && next >= len) { - settings.completed.call(input); - } + if(pos.begin <= lastRequiredNonMaskPos){ + tryFireCompleted(); + } } } e.preventDefault(); @@ -370,8 +386,7 @@ $.fn.extend({ setTimeout(function() { var pos=checkVal(true); input.caret(pos); - if (settings.completed && pos == input.val().length) - settings.completed.call(input); + tryFireCompleted(); }, 0); }); if (chrome && android) From e91b438f8d4e9a729f519f1c974ff5d8a286a406 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Sun, 12 Oct 2014 22:42:58 -0500 Subject: [PATCH 084/116] Moving readonly checks into handlers. Fixes #253 Fixes #157 --- demo/index.html | 5 +-- dist/jquery.maskedinput.js | 56 +++++++++++++++++++--------------- dist/jquery.maskedinput.min.js | 2 +- spec/Readonly.Spec.js | 16 ++++++++++ spec/lib/jquery.keymasher.js | 38 +++++++++++------------ src/jquery.maskedinput.js | 20 ++++++++++-- 6 files changed, 88 insertions(+), 49 deletions(-) create mode 100644 spec/Readonly.Spec.js diff --git a/demo/index.html b/demo/index.html index d297b1e..055b774 100644 --- a/demo/index.html +++ b/demo/index.html @@ -7,7 +7,7 @@ $(function() { $.mask.definitions['~'] = "[+-]"; $("#date").mask("99/99/9999",{completed:function(){alert("completed!");}}); - $("#phone").mask("(999) 999-9999"); + $(".phone").mask("(999) 999-9999"); $("#phoneExt").mask("(999) 999-9999? x99999"); $("#iphone").mask("+33 999 999 999"); $("#tin").mask("99-9999999"); @@ -31,7 +31,8 @@ - + + diff --git a/dist/jquery.maskedinput.js b/dist/jquery.maskedinput.js index e4b1ae2..5e57b0f 100644 --- a/dist/jquery.maskedinput.js +++ b/dist/jquery.maskedinput.js @@ -95,27 +95,31 @@ checkVal(), input.val() != focusText && input.change(); } function keydownEvent(e) { - var pos, begin, end, k = e.which; - oldVal = input.val(), 8 === k || 46 === k || iPhone && 127 === k ? (pos = input.caret(), - begin = pos.begin, end = pos.end, end - begin === 0 && (begin = 46 !== k ? seekPrev(begin) : end = seekNext(begin - 1), - end = 46 === k ? seekNext(end) : end), clearBuffer(begin, end), shiftL(begin, end - 1), - e.preventDefault()) : 13 === k ? blurEvent.call(this, e) : 27 === k && (input.val(focusText), - input.caret(0, checkVal()), e.preventDefault()); + if (!input.prop("readonly")) { + var pos, begin, end, k = e.which; + oldVal = input.val(), 8 === k || 46 === k || iPhone && 127 === k ? (pos = input.caret(), + begin = pos.begin, end = pos.end, end - begin === 0 && (begin = 46 !== k ? seekPrev(begin) : end = seekNext(begin - 1), + end = 46 === k ? seekNext(end) : end), clearBuffer(begin, end), shiftL(begin, end - 1), + e.preventDefault()) : 13 === k ? blurEvent.call(this, e) : 27 === k && (input.val(focusText), + input.caret(0, checkVal()), e.preventDefault()); + } } function keypressEvent(e) { - var p, c, next, k = e.which, pos = input.caret(); - if (!(e.ctrlKey || e.altKey || e.metaKey || 32 > k) && k && 13 !== k) { - if (pos.end - pos.begin !== 0 && (clearBuffer(pos.begin, pos.end), shiftL(pos.begin, pos.end - 1)), - p = seekNext(pos.begin - 1), len > p && (c = String.fromCharCode(k), tests[p].test(c))) { - if (shiftR(p), buffer[p] = c, writeBuffer(), next = seekNext(p), android) { - var proxy = function() { - $.proxy($.fn.caret, input, next)(); - }; - setTimeout(proxy, 0); - } else input.caret(next); - pos.begin <= lastRequiredNonMaskPos && tryFireCompleted(); + if (!input.prop("readonly")) { + var p, c, next, k = e.which, pos = input.caret(); + if (!(e.ctrlKey || e.altKey || e.metaKey || 32 > k) && k && 13 !== k) { + if (pos.end - pos.begin !== 0 && (clearBuffer(pos.begin, pos.end), shiftL(pos.begin, pos.end - 1)), + p = seekNext(pos.begin - 1), len > p && (c = String.fromCharCode(k), tests[p].test(c))) { + if (shiftR(p), buffer[p] = c, writeBuffer(), next = seekNext(p), android) { + var proxy = function() { + $.proxy($.fn.caret, input, next)(); + }; + setTimeout(proxy, 0); + } else input.caret(next); + pos.begin <= lastRequiredNonMaskPos && tryFireCompleted(); + } + e.preventDefault(); } - e.preventDefault(); } } function clearBuffer(start, end) { @@ -149,16 +153,18 @@ return $.map(buffer, function(c, i) { return tests[i] && c != settings.placeholder ? c : null; }).join(""); - }), input.attr("readonly") || input.one("unmask", function() { + }), input.one("unmask", function() { input.off(".mask").removeData($.mask.dataName); }).on("focus.mask", function() { - clearTimeout(caretTimeoutId); - var pos; - focusText = input.val(), pos = checkVal(), caretTimeoutId = setTimeout(function() { - writeBuffer(), pos == mask.replace("?", "").length ? input.caret(0, pos) : input.caret(pos); - }, 10); + if (!input.prop("readonly")) { + clearTimeout(caretTimeoutId); + var pos; + focusText = input.val(), pos = checkVal(), caretTimeoutId = setTimeout(function() { + writeBuffer(), pos == mask.replace("?", "").length ? input.caret(0, pos) : input.caret(pos); + }, 10); + } }).on("blur.mask", blurEvent).on("keydown.mask", keydownEvent).on("keypress.mask", keypressEvent).on(pasteEventName, function() { - setTimeout(function() { + input.prop("readonly") || setTimeout(function() { var pos = checkVal(!0); input.caret(pos), tryFireCompleted(); }, 0); diff --git a/dist/jquery.maskedinput.min.js b/dist/jquery.maskedinput.min.js index 5b54bdb..4093f44 100644 --- a/dist/jquery.maskedinput.min.js +++ b/dist/jquery.maskedinput.min.js @@ -4,4 +4,4 @@ Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) Version: 1.3.1 */ -!function(a){function b(){var a=document.createElement("input"),b="onpaste";return a.setAttribute(b,""),"function"==typeof a[b]?"paste":"input"}var c,d=b()+".mask",e=navigator.userAgent,f=/iphone/i.test(e),g=/chrome/i.test(e),h=/android/i.test(e);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(b,e){var i,j,k,l,m,n,o,p;return!b&&this.length>0?(i=a(this[0]),i.data(a.mask.dataName)()):(e=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null},e),j=a.mask.definitions,k=[],l=o=b.length,m=null,a.each(b.split(""),function(a,b){"?"==b?(o--,l=a):j[b]?(k.push(new RegExp(j[b])),null===m&&(m=k.length-1),l>a&&(n=k.length-1)):k.push(null)}),this.trigger("unmask").each(function(){function i(){if(e.completed){for(var a=m;n>=a;a++)if(k[a]&&C[a]===e.placeholder)return;e.completed.call(B)}}function q(a){for(;++a=0&&!k[a];);return a}function s(a,b){var c,d;if(!(0>a)){for(c=a,d=q(b);o>c;c++)if(k[c]){if(!(o>d&&k[c].test(C[d])))break;C[c]=C[d],C[d]=e.placeholder,d=q(d)}z(),B.caret(Math.max(m,a))}}function t(a){var b,c,d,f;for(b=a,c=e.placeholder;o>b;b++)if(k[b]){if(d=q(b),f=C[b],C[b]=c,!(o>d&&k[d].test(f)))break;c=f}}function u(){var a=B.val(),b=B.caret();if(a.length0&&!k[b.begin-1];)b.begin--;if(0===b.begin)for(;b.beginf)&&f&&13!==f){if(g.end-g.begin!==0&&(y(g.begin,g.end),s(g.begin,g.end-1)),c=q(g.begin-1),o>c&&(d=String.fromCharCode(f),k[c].test(d))){if(t(c),C[c]=d,z(),e=q(c),h){var j=function(){a.proxy(a.fn.caret,B,e)()};setTimeout(j,0)}else B.caret(e);g.begin<=n&&i()}b.preventDefault()}}function y(a,b){var c;for(c=a;b>c&&o>c;c++)k[c]&&(C[c]=e.placeholder)}function z(){B.val(C.join(""))}function A(a){var b,c,d,f=B.val(),g=-1;for(b=0,d=0;o>b;b++)if(k[b]){for(C[b]=e.placeholder;d++f.length){y(b+1,o);break}}else C[b]===f.charAt(d)&&b!==l&&(d++,g=b);return a?z():l>g+1?e.autoclear||C.join("")===D?(B.val()&&B.val(""),y(0,o)):z():(z(),B.val(B.val().substring(0,g+1))),l?b:m}var B=a(this),C=a.map(b.split(""),function(a){return"?"!=a?j[a]?e.placeholder:a:void 0}),D=C.join(""),E=B.val();B.data(a.mask.dataName,function(){return a.map(C,function(a,b){return k[b]&&a!=e.placeholder?a:null}).join("")}),B.attr("readonly")||B.one("unmask",function(){B.off(".mask").removeData(a.mask.dataName)}).on("focus.mask",function(){clearTimeout(c);var a;E=B.val(),a=A(),c=setTimeout(function(){z(),a==b.replace("?","").length?B.caret(0,a):B.caret(a)},10)}).on("blur.mask",v).on("keydown.mask",w).on("keypress.mask",x).on(d,function(){setTimeout(function(){var a=A(!0);B.caret(a),i()},0)}),g&&h&&B.off("input.mask").on("input.mask",u),A()}))}})}(jQuery); \ No newline at end of file +!function(a){function b(){var a=document.createElement("input"),b="onpaste";return a.setAttribute(b,""),"function"==typeof a[b]?"paste":"input"}var c,d=b()+".mask",e=navigator.userAgent,f=/iphone/i.test(e),g=/chrome/i.test(e),h=/android/i.test(e);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(b,e){var i,j,k,l,m,n,o,p;return!b&&this.length>0?(i=a(this[0]),i.data(a.mask.dataName)()):(e=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null},e),j=a.mask.definitions,k=[],l=o=b.length,m=null,a.each(b.split(""),function(a,b){"?"==b?(o--,l=a):j[b]?(k.push(new RegExp(j[b])),null===m&&(m=k.length-1),l>a&&(n=k.length-1)):k.push(null)}),this.trigger("unmask").each(function(){function i(){if(e.completed){for(var a=m;n>=a;a++)if(k[a]&&C[a]===e.placeholder)return;e.completed.call(B)}}function q(a){for(;++a=0&&!k[a];);return a}function s(a,b){var c,d;if(!(0>a)){for(c=a,d=q(b);o>c;c++)if(k[c]){if(!(o>d&&k[c].test(C[d])))break;C[c]=C[d],C[d]=e.placeholder,d=q(d)}z(),B.caret(Math.max(m,a))}}function t(a){var b,c,d,f;for(b=a,c=e.placeholder;o>b;b++)if(k[b]){if(d=q(b),f=C[b],C[b]=c,!(o>d&&k[d].test(f)))break;c=f}}function u(){var a=B.val(),b=B.caret();if(a.length0&&!k[b.begin-1];)b.begin--;if(0===b.begin)for(;b.beginf)&&f&&13!==f){if(g.end-g.begin!==0&&(y(g.begin,g.end),s(g.begin,g.end-1)),c=q(g.begin-1),o>c&&(d=String.fromCharCode(f),k[c].test(d))){if(t(c),C[c]=d,z(),e=q(c),h){var j=function(){a.proxy(a.fn.caret,B,e)()};setTimeout(j,0)}else B.caret(e);g.begin<=n&&i()}b.preventDefault()}}}function y(a,b){var c;for(c=a;b>c&&o>c;c++)k[c]&&(C[c]=e.placeholder)}function z(){B.val(C.join(""))}function A(a){var b,c,d,f=B.val(),g=-1;for(b=0,d=0;o>b;b++)if(k[b]){for(C[b]=e.placeholder;d++f.length){y(b+1,o);break}}else C[b]===f.charAt(d)&&b!==l&&(d++,g=b);return a?z():l>g+1?e.autoclear||C.join("")===D?(B.val()&&B.val(""),y(0,o)):z():(z(),B.val(B.val().substring(0,g+1))),l?b:m}var B=a(this),C=a.map(b.split(""),function(a){return"?"!=a?j[a]?e.placeholder:a:void 0}),D=C.join(""),E=B.val();B.data(a.mask.dataName,function(){return a.map(C,function(a,b){return k[b]&&a!=e.placeholder?a:null}).join("")}),B.one("unmask",function(){B.off(".mask").removeData(a.mask.dataName)}).on("focus.mask",function(){if(!B.prop("readonly")){clearTimeout(c);var a;E=B.val(),a=A(),c=setTimeout(function(){z(),a==b.replace("?","").length?B.caret(0,a):B.caret(a)},10)}}).on("blur.mask",v).on("keydown.mask",w).on("keypress.mask",x).on(d,function(){B.prop("readonly")||setTimeout(function(){var a=A(!0);B.caret(a),i()},0)}),g&&h&&B.off("input.mask").on("input.mask",u),A()}))}})}(jQuery); \ No newline at end of file diff --git a/spec/Readonly.Spec.js b/spec/Readonly.Spec.js new file mode 100644 index 0000000..6f37d1f --- /dev/null +++ b/spec/Readonly.Spec.js @@ -0,0 +1,16 @@ +feature("Readonly Inputs", function() { + scenario('Typing',function(){ + + given("a input with readonly added after mask", function(){ + input.mask("99").attr("readonly",true); + }); + + when("typing left to right",function(){ + input.mashKeys("12"); + }); + + then("Input should be ignored",function(){ + expect(input).toHaveValue(""); + }); + }); +}); diff --git a/spec/lib/jquery.keymasher.js b/spec/lib/jquery.keymasher.js index d8d9df5..bb430f6 100644 --- a/spec/lib/jquery.keymasher.js +++ b/spec/lib/jquery.keymasher.js @@ -1,23 +1,23 @@ /* Key Masher plugin for jQuery (https://github.com/digitalBush/jquery.keymasher) - Copyright (c) 2010-2013 Josh Bush (digitalbush.com) + Copyright (c) 2010-2014 Josh Bush (digitalbush.com) Licensed under the MIT license - Version: 0.3 + Version: 0.4 */ (function($,undefined){ //numberPad={'0':96,'1':97,'2':98,'3':99,'4':100,'5':101,'6':102,'7':103,'8':104,'9':105,'*':106,'+':107,'-':109,'.':110,'/':111}, - + var keys=(function(){ var defs={}, - keys = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`-=[]\\;',./ \t\n", + keys = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`-=[]\\;',./ \t\n\r", shifted = "abcdefghijklmnopqrstuvwxyz!@#$%^&*()~_+{}|:\"<>?", noprint={shift:16,ctrl:17,meta:91,alt:18,f1:112,f2:113,f3:114,f4:115,f5:116,f6:117,f7:118,f8:119,f9:120,f10:121,f11:122,f12:123, capslock:20,numlock:144,scrolllock:145,pageup:33,pagedown:34,end:35,home:36,backspace:8, insert:45, 'delete':46,pause:19,esc:27,left:37,up:38,right:39,down:40,printscreen:44}; $.each(keys.split(''),function(index,value){ - var keyCode=value.charCodeAt(0),shift=shifted[index]; + var keyCode=value.charCodeAt(0),shift=shifted[index]; defs[value]={keyCode:keyCode,charCode:keyCode,shift:shift}; if(shift) defs[shift]={keyCode:keyCode,charCode:shift.charCodeAt(0),shift:value,requiresShift:index>=26}; @@ -29,39 +29,39 @@ var KeyMasher=function(elm){ var modifierState={alt: false, ctrl: false, meta: false, shift: false}, forced={}; - + var queueModifierEvent=function(direction,modifier,isForced){ forced[modifier]=isForced; - modifierState[modifier]=(direction=='down'); + modifierState[modifier]=(direction=='down'); var event=$.extend($.Event(), modifierState, {type:'key'+direction, keyCode: keys[modifier].keyCode, charCode: 0}); elm.trigger(event); }; - + var queueStroke=function(key){ if($.type(key)==='string') - key=keys[key]; + key=keys[key]; if(key.requiresShift && !modifierState.shift) queueModifierEvent('down','shift',true); else if(modifierState.shift && key.shift) key=keys[key.shift]; - + var ignore = !key.charCode || modifierState.alt || modifierState.ctrl || modifierState.meta, down = $.extend($.Event('keydown'), modifierState, {keyCode: key.keyCode, charCode: 0, which:key.keyCode}), press = $.extend($.Event('keypress'), modifierState, {keyCode: key.charCode, charCode: key.charCode, which: key.charCode}), - up = $.extend($.Event('keyup'), modifierState, {keyCode: key.keyCode, charCode: 0, which:key.keyCode}); - + up = $.extend($.Event('keyup'), modifierState, {keyCode: key.keyCode, charCode: 0, which:key.keyCode}); + elm.trigger(down); if(!down.isDefaultPrevented() && !ignore){ elm.trigger(press); - if(!press.isDefaultPrevented()){ + if(!press.isDefaultPrevented() && !elm.prop("readonly")){ //need to do caret positioning elm.val(elm.val()+String.fromCharCode(key.charCode)); } } elm.trigger(up); - + if(forced.shift) - queueModifierEvent('up','shift'); + queueModifierEvent('up','shift'); }; var public={ @@ -71,13 +71,13 @@ public.type(typing); $.each(toks,function(index,value){queueModifierEvent('up',value);}); return public; - }, + }, type:function(){ $.each(arguments,function(index,typing){ if($.type(typing)==='string') $.each(typing.split(''),function(index,value){queueStroke(value);}); else - queueStroke(typing); + queueStroke(typing); }); return public; } @@ -91,7 +91,7 @@ fn=function(keys){keys.type(typing)}; } return this.each(function(){ - fn(KeyMasher($(this))); + fn(KeyMasher($(this))); }); }; -})(jQuery); \ No newline at end of file +})(jQuery); diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 1b7b9f9..b93820a 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -218,6 +218,10 @@ $.fn.extend({ } function keydownEvent(e) { + if (input.prop("readonly")){ + return; + } + var k = e.which, pos, begin, @@ -247,6 +251,10 @@ $.fn.extend({ } function keypressEvent(e) { + if (input.prop("readonly")){ + return; + } + var k = e.which, pos = input.caret(), p, @@ -355,14 +363,18 @@ $.fn.extend({ }).join(''); }); - if (!input.attr("readonly")) - input + + input .one("unmask", function() { input .off(".mask") .removeData($.mask.dataName); }) .on("focus.mask", function() { + if (input.prop("readonly")){ + return; + } + clearTimeout(caretTimeoutId); var pos; @@ -383,6 +395,10 @@ $.fn.extend({ .on("keydown.mask", keydownEvent) .on("keypress.mask", keypressEvent) .on(pasteEventName, function() { + if (input.prop("readonly")){ + return; + } + setTimeout(function() { var pos=checkVal(true); input.caret(pos); From 08b4ac995ecffe9b10cf0bfb8f732d9dde30e81e Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Wed, 15 Oct 2014 21:48:19 -0500 Subject: [PATCH 085/116] Adding UMD wrapper https://github.com/umdjs/umd/blob/master/jqueryPluginCommonjs.js Thanks for the nudge @AndrewEastwood and @jd-boyd Closes #65 Closes #241 --- dist/jquery.maskedinput.js | 6 ++++-- dist/jquery.maskedinput.min.js | 2 +- src/jquery.maskedinput.js | 15 +++++++++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/dist/jquery.maskedinput.js b/dist/jquery.maskedinput.js index 5e57b0f..5cfad94 100644 --- a/dist/jquery.maskedinput.js +++ b/dist/jquery.maskedinput.js @@ -4,7 +4,9 @@ Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) Version: 1.3.1 */ -!function($) { +!function(factory) { + "function" == typeof define && define.amd ? define([ "jquery" ], factory) : factory("object" == typeof exports ? require("jquery") : jQuery); +}(function($) { function getPasteEvent() { var el = document.createElement("input"), name = "onpaste"; return el.setAttribute(name, ""), "function" == typeof el[name] ? "paste" : "input"; @@ -173,4 +175,4 @@ })); } }); -}(jQuery); \ No newline at end of file +}); \ No newline at end of file diff --git a/dist/jquery.maskedinput.min.js b/dist/jquery.maskedinput.min.js index 4093f44..6d1edc7 100644 --- a/dist/jquery.maskedinput.min.js +++ b/dist/jquery.maskedinput.min.js @@ -4,4 +4,4 @@ Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) Version: 1.3.1 */ -!function(a){function b(){var a=document.createElement("input"),b="onpaste";return a.setAttribute(b,""),"function"==typeof a[b]?"paste":"input"}var c,d=b()+".mask",e=navigator.userAgent,f=/iphone/i.test(e),g=/chrome/i.test(e),h=/android/i.test(e);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(b,e){var i,j,k,l,m,n,o,p;return!b&&this.length>0?(i=a(this[0]),i.data(a.mask.dataName)()):(e=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null},e),j=a.mask.definitions,k=[],l=o=b.length,m=null,a.each(b.split(""),function(a,b){"?"==b?(o--,l=a):j[b]?(k.push(new RegExp(j[b])),null===m&&(m=k.length-1),l>a&&(n=k.length-1)):k.push(null)}),this.trigger("unmask").each(function(){function i(){if(e.completed){for(var a=m;n>=a;a++)if(k[a]&&C[a]===e.placeholder)return;e.completed.call(B)}}function q(a){for(;++a=0&&!k[a];);return a}function s(a,b){var c,d;if(!(0>a)){for(c=a,d=q(b);o>c;c++)if(k[c]){if(!(o>d&&k[c].test(C[d])))break;C[c]=C[d],C[d]=e.placeholder,d=q(d)}z(),B.caret(Math.max(m,a))}}function t(a){var b,c,d,f;for(b=a,c=e.placeholder;o>b;b++)if(k[b]){if(d=q(b),f=C[b],C[b]=c,!(o>d&&k[d].test(f)))break;c=f}}function u(){var a=B.val(),b=B.caret();if(a.length0&&!k[b.begin-1];)b.begin--;if(0===b.begin)for(;b.beginf)&&f&&13!==f){if(g.end-g.begin!==0&&(y(g.begin,g.end),s(g.begin,g.end-1)),c=q(g.begin-1),o>c&&(d=String.fromCharCode(f),k[c].test(d))){if(t(c),C[c]=d,z(),e=q(c),h){var j=function(){a.proxy(a.fn.caret,B,e)()};setTimeout(j,0)}else B.caret(e);g.begin<=n&&i()}b.preventDefault()}}}function y(a,b){var c;for(c=a;b>c&&o>c;c++)k[c]&&(C[c]=e.placeholder)}function z(){B.val(C.join(""))}function A(a){var b,c,d,f=B.val(),g=-1;for(b=0,d=0;o>b;b++)if(k[b]){for(C[b]=e.placeholder;d++f.length){y(b+1,o);break}}else C[b]===f.charAt(d)&&b!==l&&(d++,g=b);return a?z():l>g+1?e.autoclear||C.join("")===D?(B.val()&&B.val(""),y(0,o)):z():(z(),B.val(B.val().substring(0,g+1))),l?b:m}var B=a(this),C=a.map(b.split(""),function(a){return"?"!=a?j[a]?e.placeholder:a:void 0}),D=C.join(""),E=B.val();B.data(a.mask.dataName,function(){return a.map(C,function(a,b){return k[b]&&a!=e.placeholder?a:null}).join("")}),B.one("unmask",function(){B.off(".mask").removeData(a.mask.dataName)}).on("focus.mask",function(){if(!B.prop("readonly")){clearTimeout(c);var a;E=B.val(),a=A(),c=setTimeout(function(){z(),a==b.replace("?","").length?B.caret(0,a):B.caret(a)},10)}}).on("blur.mask",v).on("keydown.mask",w).on("keypress.mask",x).on(d,function(){B.prop("readonly")||setTimeout(function(){var a=A(!0);B.caret(a),i()},0)}),g&&h&&B.off("input.mask").on("input.mask",u),A()}))}})}(jQuery); \ No newline at end of file +!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof exports?require("jquery"):jQuery)}(function(a){function b(){var a=document.createElement("input"),b="onpaste";return a.setAttribute(b,""),"function"==typeof a[b]?"paste":"input"}var c,d=b()+".mask",e=navigator.userAgent,f=/iphone/i.test(e),g=/chrome/i.test(e),h=/android/i.test(e);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(b,e){var i,j,k,l,m,n,o,p;return!b&&this.length>0?(i=a(this[0]),i.data(a.mask.dataName)()):(e=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null},e),j=a.mask.definitions,k=[],l=o=b.length,m=null,a.each(b.split(""),function(a,b){"?"==b?(o--,l=a):j[b]?(k.push(new RegExp(j[b])),null===m&&(m=k.length-1),l>a&&(n=k.length-1)):k.push(null)}),this.trigger("unmask").each(function(){function i(){if(e.completed){for(var a=m;n>=a;a++)if(k[a]&&C[a]===e.placeholder)return;e.completed.call(B)}}function q(a){for(;++a=0&&!k[a];);return a}function s(a,b){var c,d;if(!(0>a)){for(c=a,d=q(b);o>c;c++)if(k[c]){if(!(o>d&&k[c].test(C[d])))break;C[c]=C[d],C[d]=e.placeholder,d=q(d)}z(),B.caret(Math.max(m,a))}}function t(a){var b,c,d,f;for(b=a,c=e.placeholder;o>b;b++)if(k[b]){if(d=q(b),f=C[b],C[b]=c,!(o>d&&k[d].test(f)))break;c=f}}function u(){var a=B.val(),b=B.caret();if(a.length0&&!k[b.begin-1];)b.begin--;if(0===b.begin)for(;b.beginf)&&f&&13!==f){if(g.end-g.begin!==0&&(y(g.begin,g.end),s(g.begin,g.end-1)),c=q(g.begin-1),o>c&&(d=String.fromCharCode(f),k[c].test(d))){if(t(c),C[c]=d,z(),e=q(c),h){var j=function(){a.proxy(a.fn.caret,B,e)()};setTimeout(j,0)}else B.caret(e);g.begin<=n&&i()}b.preventDefault()}}}function y(a,b){var c;for(c=a;b>c&&o>c;c++)k[c]&&(C[c]=e.placeholder)}function z(){B.val(C.join(""))}function A(a){var b,c,d,f=B.val(),g=-1;for(b=0,d=0;o>b;b++)if(k[b]){for(C[b]=e.placeholder;d++f.length){y(b+1,o);break}}else C[b]===f.charAt(d)&&b!==l&&(d++,g=b);return a?z():l>g+1?e.autoclear||C.join("")===D?(B.val()&&B.val(""),y(0,o)):z():(z(),B.val(B.val().substring(0,g+1))),l?b:m}var B=a(this),C=a.map(b.split(""),function(a){return"?"!=a?j[a]?e.placeholder:a:void 0}),D=C.join(""),E=B.val();B.data(a.mask.dataName,function(){return a.map(C,function(a,b){return k[b]&&a!=e.placeholder?a:null}).join("")}),B.one("unmask",function(){B.off(".mask").removeData(a.mask.dataName)}).on("focus.mask",function(){if(!B.prop("readonly")){clearTimeout(c);var a;E=B.val(),a=A(),c=setTimeout(function(){z(),a==b.replace("?","").length?B.caret(0,a):B.caret(a)},10)}}).on("blur.mask",v).on("keydown.mask",w).on("keypress.mask",x).on(d,function(){B.prop("readonly")||setTimeout(function(){var a=A(!0);B.caret(a),i()},0)}),g&&h&&B.off("input.mask").on("input.mask",u),A()}))}})}); \ No newline at end of file diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index b93820a..ccf496d 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -1,4 +1,15 @@ -(function($) { +(function (factory) { + if (typeof define === 'function' && define.amd) { + // AMD. Register as an anonymous module. + define(['jquery'], factory); + } else if (typeof exports === 'object') { + // Node/CommonJS + factory(require('jquery')); + } else { + // Browser globals + factory(jQuery); + } +}(function ($) { function getPasteEvent() { var el = document.createElement('input'), @@ -415,4 +426,4 @@ $.fn.extend({ }); } }); -})(jQuery); +})); From f7c9cba70c79657a74eef1bcfae5c01e3fc22991 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Wed, 15 Oct 2014 22:31:12 -0500 Subject: [PATCH 086/116] Making sure we only show optional portion of input when we've filled a placeholder Fixes #63 --- dist/jquery.maskedinput.js | 2 +- dist/jquery.maskedinput.min.js | 2 +- spec/Focus.Spec.js | 74 ----------------------------- spec/Optional.Spec.js | 85 ++++++++++++++++++++++++++++++++++ src/jquery.maskedinput.js | 4 +- 5 files changed, 90 insertions(+), 77 deletions(-) create mode 100644 spec/Optional.Spec.js diff --git a/dist/jquery.maskedinput.js b/dist/jquery.maskedinput.js index 5cfad94..df5bb31 100644 --- a/dist/jquery.maskedinput.js +++ b/dist/jquery.maskedinput.js @@ -143,7 +143,7 @@ clearBuffer(i + 1, len); break; } - } else buffer[i] === test.charAt(pos) && i !== partialPosition && (pos++, lastMatch = i); + } else buffer[i] === test.charAt(pos) && i !== partialPosition && (pos++, partialPosition > i && (lastMatch = i)); return allow ? writeBuffer() : partialPosition > lastMatch + 1 ? settings.autoclear || buffer.join("") === defaultBuffer ? (input.val() && input.val(""), clearBuffer(0, len)) : writeBuffer() : (writeBuffer(), input.val(input.val().substring(0, lastMatch + 1))), partialPosition ? i : firstNonMaskPos; diff --git a/dist/jquery.maskedinput.min.js b/dist/jquery.maskedinput.min.js index 6d1edc7..0aa3247 100644 --- a/dist/jquery.maskedinput.min.js +++ b/dist/jquery.maskedinput.min.js @@ -4,4 +4,4 @@ Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) Version: 1.3.1 */ -!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof exports?require("jquery"):jQuery)}(function(a){function b(){var a=document.createElement("input"),b="onpaste";return a.setAttribute(b,""),"function"==typeof a[b]?"paste":"input"}var c,d=b()+".mask",e=navigator.userAgent,f=/iphone/i.test(e),g=/chrome/i.test(e),h=/android/i.test(e);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(b,e){var i,j,k,l,m,n,o,p;return!b&&this.length>0?(i=a(this[0]),i.data(a.mask.dataName)()):(e=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null},e),j=a.mask.definitions,k=[],l=o=b.length,m=null,a.each(b.split(""),function(a,b){"?"==b?(o--,l=a):j[b]?(k.push(new RegExp(j[b])),null===m&&(m=k.length-1),l>a&&(n=k.length-1)):k.push(null)}),this.trigger("unmask").each(function(){function i(){if(e.completed){for(var a=m;n>=a;a++)if(k[a]&&C[a]===e.placeholder)return;e.completed.call(B)}}function q(a){for(;++a=0&&!k[a];);return a}function s(a,b){var c,d;if(!(0>a)){for(c=a,d=q(b);o>c;c++)if(k[c]){if(!(o>d&&k[c].test(C[d])))break;C[c]=C[d],C[d]=e.placeholder,d=q(d)}z(),B.caret(Math.max(m,a))}}function t(a){var b,c,d,f;for(b=a,c=e.placeholder;o>b;b++)if(k[b]){if(d=q(b),f=C[b],C[b]=c,!(o>d&&k[d].test(f)))break;c=f}}function u(){var a=B.val(),b=B.caret();if(a.length0&&!k[b.begin-1];)b.begin--;if(0===b.begin)for(;b.beginf)&&f&&13!==f){if(g.end-g.begin!==0&&(y(g.begin,g.end),s(g.begin,g.end-1)),c=q(g.begin-1),o>c&&(d=String.fromCharCode(f),k[c].test(d))){if(t(c),C[c]=d,z(),e=q(c),h){var j=function(){a.proxy(a.fn.caret,B,e)()};setTimeout(j,0)}else B.caret(e);g.begin<=n&&i()}b.preventDefault()}}}function y(a,b){var c;for(c=a;b>c&&o>c;c++)k[c]&&(C[c]=e.placeholder)}function z(){B.val(C.join(""))}function A(a){var b,c,d,f=B.val(),g=-1;for(b=0,d=0;o>b;b++)if(k[b]){for(C[b]=e.placeholder;d++f.length){y(b+1,o);break}}else C[b]===f.charAt(d)&&b!==l&&(d++,g=b);return a?z():l>g+1?e.autoclear||C.join("")===D?(B.val()&&B.val(""),y(0,o)):z():(z(),B.val(B.val().substring(0,g+1))),l?b:m}var B=a(this),C=a.map(b.split(""),function(a){return"?"!=a?j[a]?e.placeholder:a:void 0}),D=C.join(""),E=B.val();B.data(a.mask.dataName,function(){return a.map(C,function(a,b){return k[b]&&a!=e.placeholder?a:null}).join("")}),B.one("unmask",function(){B.off(".mask").removeData(a.mask.dataName)}).on("focus.mask",function(){if(!B.prop("readonly")){clearTimeout(c);var a;E=B.val(),a=A(),c=setTimeout(function(){z(),a==b.replace("?","").length?B.caret(0,a):B.caret(a)},10)}}).on("blur.mask",v).on("keydown.mask",w).on("keypress.mask",x).on(d,function(){B.prop("readonly")||setTimeout(function(){var a=A(!0);B.caret(a),i()},0)}),g&&h&&B.off("input.mask").on("input.mask",u),A()}))}})}); \ No newline at end of file +!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof exports?require("jquery"):jQuery)}(function(a){function b(){var a=document.createElement("input"),b="onpaste";return a.setAttribute(b,""),"function"==typeof a[b]?"paste":"input"}var c,d=b()+".mask",e=navigator.userAgent,f=/iphone/i.test(e),g=/chrome/i.test(e),h=/android/i.test(e);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(b,e){var i,j,k,l,m,n,o,p;return!b&&this.length>0?(i=a(this[0]),i.data(a.mask.dataName)()):(e=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null},e),j=a.mask.definitions,k=[],l=o=b.length,m=null,a.each(b.split(""),function(a,b){"?"==b?(o--,l=a):j[b]?(k.push(new RegExp(j[b])),null===m&&(m=k.length-1),l>a&&(n=k.length-1)):k.push(null)}),this.trigger("unmask").each(function(){function i(){if(e.completed){for(var a=m;n>=a;a++)if(k[a]&&C[a]===e.placeholder)return;e.completed.call(B)}}function q(a){for(;++a=0&&!k[a];);return a}function s(a,b){var c,d;if(!(0>a)){for(c=a,d=q(b);o>c;c++)if(k[c]){if(!(o>d&&k[c].test(C[d])))break;C[c]=C[d],C[d]=e.placeholder,d=q(d)}z(),B.caret(Math.max(m,a))}}function t(a){var b,c,d,f;for(b=a,c=e.placeholder;o>b;b++)if(k[b]){if(d=q(b),f=C[b],C[b]=c,!(o>d&&k[d].test(f)))break;c=f}}function u(){var a=B.val(),b=B.caret();if(a.length0&&!k[b.begin-1];)b.begin--;if(0===b.begin)for(;b.beginf)&&f&&13!==f){if(g.end-g.begin!==0&&(y(g.begin,g.end),s(g.begin,g.end-1)),c=q(g.begin-1),o>c&&(d=String.fromCharCode(f),k[c].test(d))){if(t(c),C[c]=d,z(),e=q(c),h){var j=function(){a.proxy(a.fn.caret,B,e)()};setTimeout(j,0)}else B.caret(e);g.begin<=n&&i()}b.preventDefault()}}}function y(a,b){var c;for(c=a;b>c&&o>c;c++)k[c]&&(C[c]=e.placeholder)}function z(){B.val(C.join(""))}function A(a){var b,c,d,f=B.val(),g=-1;for(b=0,d=0;o>b;b++)if(k[b]){for(C[b]=e.placeholder;d++f.length){y(b+1,o);break}}else C[b]===f.charAt(d)&&b!==l&&(d++,l>b&&(g=b));return a?z():l>g+1?e.autoclear||C.join("")===D?(B.val()&&B.val(""),y(0,o)):z():(z(),B.val(B.val().substring(0,g+1))),l?b:m}var B=a(this),C=a.map(b.split(""),function(a){return"?"!=a?j[a]?e.placeholder:a:void 0}),D=C.join(""),E=B.val();B.data(a.mask.dataName,function(){return a.map(C,function(a,b){return k[b]&&a!=e.placeholder?a:null}).join("")}),B.one("unmask",function(){B.off(".mask").removeData(a.mask.dataName)}).on("focus.mask",function(){if(!B.prop("readonly")){clearTimeout(c);var a;E=B.val(),a=A(),c=setTimeout(function(){z(),a==b.replace("?","").length?B.caret(0,a):B.caret(a)},10)}}).on("blur.mask",v).on("keydown.mask",w).on("keypress.mask",x).on(d,function(){B.prop("readonly")||setTimeout(function(){var a=A(!0);B.caret(a),i()},0)}),g&&h&&B.off("input.mask").on("input.mask",u),A()}))}})}); \ No newline at end of file diff --git a/spec/Focus.Spec.js b/spec/Focus.Spec.js index de3976c..deb9c91 100644 --- a/spec/Focus.Spec.js +++ b/spec/Focus.Spec.js @@ -144,77 +144,3 @@ feature("Leaving A Masked Input",function(){ }); }); }); - -feature("Optional marker",function(){ - scenario("Placeholders not filled to marker",function(){ - given("a mask with an optional marker",function(){ - input.mask("99?99"); - }); - when("typing one character and leaving",function(){ - input.mashKeys("1").blur(); - }); - then("value should be empty",function(){ - expect(input).toHaveValue(""); - }); - }); - - scenario("Placeholders not filled to marker and autoclear = false", function() { - given("a mask with an optional marker",function(){ - input.mask("99?99", { autoclear: false }); - }); - when("typing one character and leaving",function(){ - input.mashKeys("1").blur(); - }); - then("value should be empty",function(){ - expect(input).toHaveValue("1___"); - }); - }); - - scenario("Placeholders filled to marker",function(){ - given("a mask with an optional marker",function(){ - input.mask("99?99"); - }); - when("typing two characters and leaving",function(){ - input.mashKeys("12").blur(); - }); - then("value should remain",function(){ - expect(input).toHaveValue("12"); - }); - }); - - scenario("Placeholders filled to marker and autoclear = false", function() { - given("a mask with an optional marker",function(){ - input.mask("99?99", { autoclear: false }); - }); - when("typing two characters and leaving",function(){ - input.mashKeys("12").blur(); - }); - then("value should remain",function(){ - expect(input).toHaveValue("12"); - }); - }); - - scenario("Placeholders filled, one marker filled, and autoclear = false", function() { - given("a mask with an optional marker",function(){ - input.mask("99?99", { autoclear: false }); - }); - when("typing three characters and leaving",function(){ - input.mashKeys("123").blur(); - }); - then("value should remain",function(){ - expect(input).toHaveValue("123"); - }); - }); - - scenario("Placeholders and markers filled, and autoclear = false", function() { - given("a mask with an optional marker",function(){ - input.mask("99?99", { autoclear: false }); - }); - when("typing four characters and leaving",function(){ - input.mashKeys("1234").blur(); - }); - then("value should remain",function(){ - expect(input).toHaveValue("1234"); - }); - }); -}); diff --git a/spec/Optional.Spec.js b/spec/Optional.Spec.js new file mode 100644 index 0000000..83e14ae --- /dev/null +++ b/spec/Optional.Spec.js @@ -0,0 +1,85 @@ +feature("Optional marker",function(){ + scenario("Placeholders not filled to marker",function(){ + given("a mask with an optional marker",function(){ + input.mask("99?99"); + }); + when("typing one character and leaving",function(){ + input.mashKeys("1").blur(); + }); + then("value should be empty",function(){ + expect(input).toHaveValue(""); + }); + }); + + scenario("Placeholders not filled to marker and autoclear = false", function() { + given("a mask with an optional marker",function(){ + input.mask("99?99", { autoclear: false }); + }); + when("typing one character and leaving",function(){ + input.mashKeys("1").blur(); + }); + then("value should be empty",function(){ + expect(input).toHaveValue("1___"); + }); + }); + + scenario("Placeholders filled to marker",function(){ + given("a mask with an optional marker",function(){ + input.mask("99?99"); + }); + when("typing two characters and leaving",function(){ + input.mashKeys("12").blur(); + }); + then("value should remain",function(){ + expect(input).toHaveValue("12"); + }); + }); + + scenario("Placeholders filled to marker with literals after",function(){ + given("a mask with an optional marker and literals",function(){ + input.mask("99!? x 99"); + }); + when("typing two characters and leaving",function(){ + input.mashKeys("12").blur(); + }); + then("value should remain",function(){ + expect(input).toHaveValue("12!"); + }); + }); + + scenario("Placeholders filled to marker and autoclear = false", function() { + given("a mask with an optional marker",function(){ + input.mask("99?99", { autoclear: false }); + }); + when("typing two characters and leaving",function(){ + input.mashKeys("12").blur(); + }); + then("value should remain",function(){ + expect(input).toHaveValue("12"); + }); + }); + + scenario("Placeholders filled, one marker filled, and autoclear = false", function() { + given("a mask with an optional marker",function(){ + input.mask("99?99", { autoclear: false }); + }); + when("typing three characters and leaving",function(){ + input.mashKeys("123").blur(); + }); + then("value should remain",function(){ + expect(input).toHaveValue("123"); + }); + }); + + scenario("Placeholders and markers filled, and autoclear = false", function() { + given("a mask with an optional marker",function(){ + input.mask("99?99", { autoclear: false }); + }); + when("typing four characters and leaving",function(){ + input.mashKeys("1234").blur(); + }); + then("value should remain",function(){ + expect(input).toHaveValue("1234"); + }); + }); +}); diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index ccf496d..f35803e 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -345,7 +345,9 @@ $.fn.extend({ } } else if (buffer[i] === test.charAt(pos) && i !== partialPosition) { pos++; - lastMatch = i; + if( i < partialPosition){ + lastMatch = i; + } } } if (allow) { From ea2c78e7b77cd41a93916a21effec07d86bad093 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Wed, 15 Oct 2014 22:32:44 -0500 Subject: [PATCH 087/116] Adding a few specs around masks ending in a literal --- spec/Completed.Spec.js | 20 ++++++++++++++++++++ spec/Focus.Spec.js | 12 ++++++++++++ 2 files changed, 32 insertions(+) diff --git a/spec/Completed.Spec.js b/spec/Completed.Spec.js index 34a5b8a..be71e62 100644 --- a/spec/Completed.Spec.js +++ b/spec/Completed.Spec.js @@ -99,4 +99,24 @@ feature("Completed callback", function() { expect(input).toHaveValue('1234'); }); }); + + scenario('Completing mask by typing last character with literal to right',function(){ + var completed=false; + given("an input with a completed callback", function(){ + input.mask("99!",{completed:function(){completed=true;}}); + }); + + when("typing left to right",function(){ + input.mashKeys("12"); + }); + + then("completed callback should be called",function(){ + expect(completed).toBeTruthy(); + }); + then("value should be correct",function(){ + expect(input).toHaveValue('12!'); + }); + }); + + }); diff --git a/spec/Focus.Spec.js b/spec/Focus.Spec.js index deb9c91..e77b19d 100644 --- a/spec/Focus.Spec.js +++ b/spec/Focus.Spec.js @@ -113,6 +113,18 @@ feature("Leaving A Masked Input",function(){ }); }); + scenario("Mask ending in literal",function(){ + given("a mask ending in a literal",function(){ + input.mask("99!"); + }); + when("typing two characters and blurring",function(){ + input.mashKeys("12").blur(); + }); + then("value should remain",function(){ + expect(input).toHaveValue("12!"); + }); + }); + scenario("Empty placeholders remaining with autoclear set to false",function(){ given("a mask with two placeholders",function(){ input.mask("99", { autoclear: false }); From 8abb18bdf5b427cfb39c22ae0ed44a19aef46bfd Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Wed, 15 Oct 2014 23:12:49 -0500 Subject: [PATCH 088/116] Fix issue applying mask with trailing literal to input with initial value Fixes #86 --- dist/jquery.maskedinput.js | 2 +- dist/jquery.maskedinput.min.js | 2 +- spec/Init.Spec.js | 12 ++++++++++++ src/jquery.maskedinput.js | 6 ++++-- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/dist/jquery.maskedinput.js b/dist/jquery.maskedinput.js index df5bb31..9a71c02 100644 --- a/dist/jquery.maskedinput.js +++ b/dist/jquery.maskedinput.js @@ -143,7 +143,7 @@ clearBuffer(i + 1, len); break; } - } else buffer[i] === test.charAt(pos) && i !== partialPosition && (pos++, partialPosition > i && (lastMatch = i)); + } else buffer[i] === test.charAt(pos) && pos++, partialPosition > i && (lastMatch = i); return allow ? writeBuffer() : partialPosition > lastMatch + 1 ? settings.autoclear || buffer.join("") === defaultBuffer ? (input.val() && input.val(""), clearBuffer(0, len)) : writeBuffer() : (writeBuffer(), input.val(input.val().substring(0, lastMatch + 1))), partialPosition ? i : firstNonMaskPos; diff --git a/dist/jquery.maskedinput.min.js b/dist/jquery.maskedinput.min.js index 0aa3247..357e809 100644 --- a/dist/jquery.maskedinput.min.js +++ b/dist/jquery.maskedinput.min.js @@ -4,4 +4,4 @@ Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) Version: 1.3.1 */ -!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof exports?require("jquery"):jQuery)}(function(a){function b(){var a=document.createElement("input"),b="onpaste";return a.setAttribute(b,""),"function"==typeof a[b]?"paste":"input"}var c,d=b()+".mask",e=navigator.userAgent,f=/iphone/i.test(e),g=/chrome/i.test(e),h=/android/i.test(e);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(b,e){var i,j,k,l,m,n,o,p;return!b&&this.length>0?(i=a(this[0]),i.data(a.mask.dataName)()):(e=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null},e),j=a.mask.definitions,k=[],l=o=b.length,m=null,a.each(b.split(""),function(a,b){"?"==b?(o--,l=a):j[b]?(k.push(new RegExp(j[b])),null===m&&(m=k.length-1),l>a&&(n=k.length-1)):k.push(null)}),this.trigger("unmask").each(function(){function i(){if(e.completed){for(var a=m;n>=a;a++)if(k[a]&&C[a]===e.placeholder)return;e.completed.call(B)}}function q(a){for(;++a=0&&!k[a];);return a}function s(a,b){var c,d;if(!(0>a)){for(c=a,d=q(b);o>c;c++)if(k[c]){if(!(o>d&&k[c].test(C[d])))break;C[c]=C[d],C[d]=e.placeholder,d=q(d)}z(),B.caret(Math.max(m,a))}}function t(a){var b,c,d,f;for(b=a,c=e.placeholder;o>b;b++)if(k[b]){if(d=q(b),f=C[b],C[b]=c,!(o>d&&k[d].test(f)))break;c=f}}function u(){var a=B.val(),b=B.caret();if(a.length0&&!k[b.begin-1];)b.begin--;if(0===b.begin)for(;b.beginf)&&f&&13!==f){if(g.end-g.begin!==0&&(y(g.begin,g.end),s(g.begin,g.end-1)),c=q(g.begin-1),o>c&&(d=String.fromCharCode(f),k[c].test(d))){if(t(c),C[c]=d,z(),e=q(c),h){var j=function(){a.proxy(a.fn.caret,B,e)()};setTimeout(j,0)}else B.caret(e);g.begin<=n&&i()}b.preventDefault()}}}function y(a,b){var c;for(c=a;b>c&&o>c;c++)k[c]&&(C[c]=e.placeholder)}function z(){B.val(C.join(""))}function A(a){var b,c,d,f=B.val(),g=-1;for(b=0,d=0;o>b;b++)if(k[b]){for(C[b]=e.placeholder;d++f.length){y(b+1,o);break}}else C[b]===f.charAt(d)&&b!==l&&(d++,l>b&&(g=b));return a?z():l>g+1?e.autoclear||C.join("")===D?(B.val()&&B.val(""),y(0,o)):z():(z(),B.val(B.val().substring(0,g+1))),l?b:m}var B=a(this),C=a.map(b.split(""),function(a){return"?"!=a?j[a]?e.placeholder:a:void 0}),D=C.join(""),E=B.val();B.data(a.mask.dataName,function(){return a.map(C,function(a,b){return k[b]&&a!=e.placeholder?a:null}).join("")}),B.one("unmask",function(){B.off(".mask").removeData(a.mask.dataName)}).on("focus.mask",function(){if(!B.prop("readonly")){clearTimeout(c);var a;E=B.val(),a=A(),c=setTimeout(function(){z(),a==b.replace("?","").length?B.caret(0,a):B.caret(a)},10)}}).on("blur.mask",v).on("keydown.mask",w).on("keypress.mask",x).on(d,function(){B.prop("readonly")||setTimeout(function(){var a=A(!0);B.caret(a),i()},0)}),g&&h&&B.off("input.mask").on("input.mask",u),A()}))}})}); \ No newline at end of file +!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof exports?require("jquery"):jQuery)}(function(a){function b(){var a=document.createElement("input"),b="onpaste";return a.setAttribute(b,""),"function"==typeof a[b]?"paste":"input"}var c,d=b()+".mask",e=navigator.userAgent,f=/iphone/i.test(e),g=/chrome/i.test(e),h=/android/i.test(e);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(b,e){var i,j,k,l,m,n,o,p;return!b&&this.length>0?(i=a(this[0]),i.data(a.mask.dataName)()):(e=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null},e),j=a.mask.definitions,k=[],l=o=b.length,m=null,a.each(b.split(""),function(a,b){"?"==b?(o--,l=a):j[b]?(k.push(new RegExp(j[b])),null===m&&(m=k.length-1),l>a&&(n=k.length-1)):k.push(null)}),this.trigger("unmask").each(function(){function i(){if(e.completed){for(var a=m;n>=a;a++)if(k[a]&&C[a]===e.placeholder)return;e.completed.call(B)}}function q(a){for(;++a=0&&!k[a];);return a}function s(a,b){var c,d;if(!(0>a)){for(c=a,d=q(b);o>c;c++)if(k[c]){if(!(o>d&&k[c].test(C[d])))break;C[c]=C[d],C[d]=e.placeholder,d=q(d)}z(),B.caret(Math.max(m,a))}}function t(a){var b,c,d,f;for(b=a,c=e.placeholder;o>b;b++)if(k[b]){if(d=q(b),f=C[b],C[b]=c,!(o>d&&k[d].test(f)))break;c=f}}function u(){var a=B.val(),b=B.caret();if(a.length0&&!k[b.begin-1];)b.begin--;if(0===b.begin)for(;b.beginf)&&f&&13!==f){if(g.end-g.begin!==0&&(y(g.begin,g.end),s(g.begin,g.end-1)),c=q(g.begin-1),o>c&&(d=String.fromCharCode(f),k[c].test(d))){if(t(c),C[c]=d,z(),e=q(c),h){var j=function(){a.proxy(a.fn.caret,B,e)()};setTimeout(j,0)}else B.caret(e);g.begin<=n&&i()}b.preventDefault()}}}function y(a,b){var c;for(c=a;b>c&&o>c;c++)k[c]&&(C[c]=e.placeholder)}function z(){B.val(C.join(""))}function A(a){var b,c,d,f=B.val(),g=-1;for(b=0,d=0;o>b;b++)if(k[b]){for(C[b]=e.placeholder;d++f.length){y(b+1,o);break}}else C[b]===f.charAt(d)&&d++,l>b&&(g=b);return a?z():l>g+1?e.autoclear||C.join("")===D?(B.val()&&B.val(""),y(0,o)):z():(z(),B.val(B.val().substring(0,g+1))),l?b:m}var B=a(this),C=a.map(b.split(""),function(a){return"?"!=a?j[a]?e.placeholder:a:void 0}),D=C.join(""),E=B.val();B.data(a.mask.dataName,function(){return a.map(C,function(a,b){return k[b]&&a!=e.placeholder?a:null}).join("")}),B.one("unmask",function(){B.off(".mask").removeData(a.mask.dataName)}).on("focus.mask",function(){if(!B.prop("readonly")){clearTimeout(c);var a;E=B.val(),a=A(),c=setTimeout(function(){z(),a==b.replace("?","").length?B.caret(0,a):B.caret(a)},10)}}).on("blur.mask",v).on("keydown.mask",w).on("keypress.mask",x).on(d,function(){B.prop("readonly")||setTimeout(function(){var a=A(!0);B.caret(a),i()},0)}),g&&h&&B.off("input.mask").on("input.mask",u),A()}))}})}); \ No newline at end of file diff --git a/spec/Init.Spec.js b/spec/Init.Spec.js index 0bd7dea..e53cfe8 100644 --- a/spec/Init.Spec.js +++ b/spec/Init.Spec.js @@ -23,6 +23,18 @@ feature("Initializing a Mask",function(){ }); }); + scenario("An input with a valid value ending in a literal",function(){ + given("an input with a valid value",function(){ + input.val("12"); + }); + when("setting a mask",function(){ + input.mask("(99)"); + }); + then("the value should be intact",function(){ + expect(input).toHaveValue("(12)"); + }); + }); + scenario("An input with an invalid value and placeholders remaining",function(){ given("an invalid input value",function(){ input.val("55555555"); diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index f35803e..452ef94 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -343,8 +343,10 @@ $.fn.extend({ clearBuffer(i + 1, len); break; } - } else if (buffer[i] === test.charAt(pos) && i !== partialPosition) { - pos++; + } else { + if (buffer[i] === test.charAt(pos)) { + pos++; + } if( i < partialPosition){ lastMatch = i; } From 73bd7ec1d863d811f15fe78a9cf33e78c385a573 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Thu, 16 Oct 2014 22:00:21 -0500 Subject: [PATCH 089/116] Adding specs for bug that repeated mask literals when they match placeholders Closes #68 Closes #123 --- spec/Focus.Spec.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec/Focus.Spec.js b/spec/Focus.Spec.js index e77b19d..4e5743e 100644 --- a/spec/Focus.Spec.js +++ b/spec/Focus.Spec.js @@ -35,6 +35,34 @@ feature("Focusing A Masked Input",function(){ }); }); + scenario("Mask starts with a literal that fits first placeholder",function(){ + given("a mask beginning with a literal",function(){ + input.mask("19").focus(); + }); + waits(20); + when("blurring",function(){ + input.blur(); + }); + waits(20); + then("input value should be correct",function(){ + expect(input).toHaveValue(''); + }); + }); + + scenario("Mask starts with a literal that fits first placeholder and autoclear set to false",function(){ + given("a mask beginning with a literal",function(){ + input.mask("?19",{autoclear: false}).focus(); + }); + waits(20); + when("blurring",function(){ + input.blur(); + }); + waits(20); + then("input value should be correct",function(){ + expect(input).toHaveValue(''); + }); + }); + scenario("Masking a hidden input",function(){ var error; $(window).on("error.test",function(err){error=err;}) From 90faa7b9675c30e078998782676de99374605eeb Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Thu, 16 Oct 2014 22:55:31 -0500 Subject: [PATCH 090/116] Calling mask() to get raw value shouldn't error when mask not applied #107 Thank for the spec @FagnerMartinsBrack! --- dist/jquery.maskedinput.js | 9 +++++++-- dist/jquery.maskedinput.min.js | 2 +- spec/Raw.Spec.js | 13 ++++++++++++- src/jquery.maskedinput.js | 4 +++- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/dist/jquery.maskedinput.js b/dist/jquery.maskedinput.js index 9a71c02..b8bc88a 100644 --- a/dist/jquery.maskedinput.js +++ b/dist/jquery.maskedinput.js @@ -41,7 +41,12 @@ }, mask: function(mask, settings) { var input, defs, tests, partialPosition, firstNonMaskPos, lastRequiredNonMaskPos, len, oldVal; - return !mask && this.length > 0 ? (input = $(this[0]), input.data($.mask.dataName)()) : (settings = $.extend({ + if (!mask && this.length > 0) { + input = $(this[0]); + var fn = input.data($.mask.dataName); + return fn ? fn() : void 0; + } + return settings = $.extend({ autoclear: $.mask.autoclear, placeholder: $.mask.placeholder, completed: null @@ -172,7 +177,7 @@ }, 0); }), chrome && android && input.off("input.mask").on("input.mask", androidInputEvent), checkVal(); - })); + }); } }); }); \ No newline at end of file diff --git a/dist/jquery.maskedinput.min.js b/dist/jquery.maskedinput.min.js index 357e809..99a704b 100644 --- a/dist/jquery.maskedinput.min.js +++ b/dist/jquery.maskedinput.min.js @@ -4,4 +4,4 @@ Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) Version: 1.3.1 */ -!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof exports?require("jquery"):jQuery)}(function(a){function b(){var a=document.createElement("input"),b="onpaste";return a.setAttribute(b,""),"function"==typeof a[b]?"paste":"input"}var c,d=b()+".mask",e=navigator.userAgent,f=/iphone/i.test(e),g=/chrome/i.test(e),h=/android/i.test(e);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(b,e){var i,j,k,l,m,n,o,p;return!b&&this.length>0?(i=a(this[0]),i.data(a.mask.dataName)()):(e=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null},e),j=a.mask.definitions,k=[],l=o=b.length,m=null,a.each(b.split(""),function(a,b){"?"==b?(o--,l=a):j[b]?(k.push(new RegExp(j[b])),null===m&&(m=k.length-1),l>a&&(n=k.length-1)):k.push(null)}),this.trigger("unmask").each(function(){function i(){if(e.completed){for(var a=m;n>=a;a++)if(k[a]&&C[a]===e.placeholder)return;e.completed.call(B)}}function q(a){for(;++a=0&&!k[a];);return a}function s(a,b){var c,d;if(!(0>a)){for(c=a,d=q(b);o>c;c++)if(k[c]){if(!(o>d&&k[c].test(C[d])))break;C[c]=C[d],C[d]=e.placeholder,d=q(d)}z(),B.caret(Math.max(m,a))}}function t(a){var b,c,d,f;for(b=a,c=e.placeholder;o>b;b++)if(k[b]){if(d=q(b),f=C[b],C[b]=c,!(o>d&&k[d].test(f)))break;c=f}}function u(){var a=B.val(),b=B.caret();if(a.length0&&!k[b.begin-1];)b.begin--;if(0===b.begin)for(;b.beginf)&&f&&13!==f){if(g.end-g.begin!==0&&(y(g.begin,g.end),s(g.begin,g.end-1)),c=q(g.begin-1),o>c&&(d=String.fromCharCode(f),k[c].test(d))){if(t(c),C[c]=d,z(),e=q(c),h){var j=function(){a.proxy(a.fn.caret,B,e)()};setTimeout(j,0)}else B.caret(e);g.begin<=n&&i()}b.preventDefault()}}}function y(a,b){var c;for(c=a;b>c&&o>c;c++)k[c]&&(C[c]=e.placeholder)}function z(){B.val(C.join(""))}function A(a){var b,c,d,f=B.val(),g=-1;for(b=0,d=0;o>b;b++)if(k[b]){for(C[b]=e.placeholder;d++f.length){y(b+1,o);break}}else C[b]===f.charAt(d)&&d++,l>b&&(g=b);return a?z():l>g+1?e.autoclear||C.join("")===D?(B.val()&&B.val(""),y(0,o)):z():(z(),B.val(B.val().substring(0,g+1))),l?b:m}var B=a(this),C=a.map(b.split(""),function(a){return"?"!=a?j[a]?e.placeholder:a:void 0}),D=C.join(""),E=B.val();B.data(a.mask.dataName,function(){return a.map(C,function(a,b){return k[b]&&a!=e.placeholder?a:null}).join("")}),B.one("unmask",function(){B.off(".mask").removeData(a.mask.dataName)}).on("focus.mask",function(){if(!B.prop("readonly")){clearTimeout(c);var a;E=B.val(),a=A(),c=setTimeout(function(){z(),a==b.replace("?","").length?B.caret(0,a):B.caret(a)},10)}}).on("blur.mask",v).on("keydown.mask",w).on("keypress.mask",x).on(d,function(){B.prop("readonly")||setTimeout(function(){var a=A(!0);B.caret(a),i()},0)}),g&&h&&B.off("input.mask").on("input.mask",u),A()}))}})}); \ No newline at end of file +!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof exports?require("jquery"):jQuery)}(function(a){function b(){var a=document.createElement("input"),b="onpaste";return a.setAttribute(b,""),"function"==typeof a[b]?"paste":"input"}var c,d=b()+".mask",e=navigator.userAgent,f=/iphone/i.test(e),g=/chrome/i.test(e),h=/android/i.test(e);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(b,e){var i,j,k,l,m,n,o,p;if(!b&&this.length>0){i=a(this[0]);var q=i.data(a.mask.dataName);return q?q():void 0}return e=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null},e),j=a.mask.definitions,k=[],l=o=b.length,m=null,a.each(b.split(""),function(a,b){"?"==b?(o--,l=a):j[b]?(k.push(new RegExp(j[b])),null===m&&(m=k.length-1),l>a&&(n=k.length-1)):k.push(null)}),this.trigger("unmask").each(function(){function i(){if(e.completed){for(var a=m;n>=a;a++)if(k[a]&&C[a]===e.placeholder)return;e.completed.call(B)}}function q(a){for(;++a=0&&!k[a];);return a}function s(a,b){var c,d;if(!(0>a)){for(c=a,d=q(b);o>c;c++)if(k[c]){if(!(o>d&&k[c].test(C[d])))break;C[c]=C[d],C[d]=e.placeholder,d=q(d)}z(),B.caret(Math.max(m,a))}}function t(a){var b,c,d,f;for(b=a,c=e.placeholder;o>b;b++)if(k[b]){if(d=q(b),f=C[b],C[b]=c,!(o>d&&k[d].test(f)))break;c=f}}function u(){var a=B.val(),b=B.caret();if(a.length0&&!k[b.begin-1];)b.begin--;if(0===b.begin)for(;b.beginf)&&f&&13!==f){if(g.end-g.begin!==0&&(y(g.begin,g.end),s(g.begin,g.end-1)),c=q(g.begin-1),o>c&&(d=String.fromCharCode(f),k[c].test(d))){if(t(c),C[c]=d,z(),e=q(c),h){var j=function(){a.proxy(a.fn.caret,B,e)()};setTimeout(j,0)}else B.caret(e);g.begin<=n&&i()}b.preventDefault()}}}function y(a,b){var c;for(c=a;b>c&&o>c;c++)k[c]&&(C[c]=e.placeholder)}function z(){B.val(C.join(""))}function A(a){var b,c,d,f=B.val(),g=-1;for(b=0,d=0;o>b;b++)if(k[b]){for(C[b]=e.placeholder;d++f.length){y(b+1,o);break}}else C[b]===f.charAt(d)&&d++,l>b&&(g=b);return a?z():l>g+1?e.autoclear||C.join("")===D?(B.val()&&B.val(""),y(0,o)):z():(z(),B.val(B.val().substring(0,g+1))),l?b:m}var B=a(this),C=a.map(b.split(""),function(a){return"?"!=a?j[a]?e.placeholder:a:void 0}),D=C.join(""),E=B.val();B.data(a.mask.dataName,function(){return a.map(C,function(a,b){return k[b]&&a!=e.placeholder?a:null}).join("")}),B.one("unmask",function(){B.off(".mask").removeData(a.mask.dataName)}).on("focus.mask",function(){if(!B.prop("readonly")){clearTimeout(c);var a;E=B.val(),a=A(),c=setTimeout(function(){z(),a==b.replace("?","").length?B.caret(0,a):B.caret(a)},10)}}).on("blur.mask",v).on("keydown.mask",w).on("keypress.mask",x).on(d,function(){B.prop("readonly")||setTimeout(function(){var a=A(!0);B.caret(a),i()},0)}),g&&h&&B.off("input.mask").on("input.mask",u),A()})}})}); \ No newline at end of file diff --git a/spec/Raw.Spec.js b/spec/Raw.Spec.js index 97be1f8..52a3ed7 100644 --- a/spec/Raw.Spec.js +++ b/spec/Raw.Spec.js @@ -54,6 +54,17 @@ feature("Getting raw value",function(){ expect(input.mask()).toEqual("12"); }); }); + + scenario("Verify if the input hasn't the mask bound through the raw value", function() { + given("an input without a mask", function() { + input + .mask("9/9-9_9").unmask(); + }); + + then("The raw value should be undefined and no error must occur", function() { + expect(input.mask()).toBe(undefined); + }); + }); }); feature("Getting raw value with autoclear set to false", function() { @@ -136,4 +147,4 @@ feature("Getting raw value with autoclear set to false", function() { expect(input.mask()).toEqual("1"); }); }); -}); \ No newline at end of file +}); diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 452ef94..3c449ad 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -86,8 +86,10 @@ $.fn.extend({ if (!mask && this.length > 0) { input = $(this[0]); - return input.data($.mask.dataName)(); + var fn = input.data($.mask.dataName) + return fn?fn():undefined; } + settings = $.extend({ autoclear: $.mask.autoclear, placeholder: $.mask.placeholder, // Load default placeholder From 79b032919bb284d2b3f557ff437afa099334a4f9 Mon Sep 17 00:00:00 2001 From: Simon Timms Date: Thu, 16 Oct 2014 21:56:02 -0600 Subject: [PATCH 091/116] Updating and renaming nuspec file --- dist/{jquery.makedinput.nuspec => jquery.maskedinput.nuspec} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename dist/{jquery.makedinput.nuspec => jquery.maskedinput.nuspec} (94%) diff --git a/dist/jquery.makedinput.nuspec b/dist/jquery.maskedinput.nuspec similarity index 94% rename from dist/jquery.makedinput.nuspec rename to dist/jquery.maskedinput.nuspec index 632c0c4..eb59bb1 100644 --- a/dist/jquery.makedinput.nuspec +++ b/dist/jquery.maskedinput.nuspec @@ -11,7 +11,7 @@ A jQuery plugin which applies a mask to input boxes to provide both a UI hint for users as well as some rudimentary input checking. jQuery,plugins - + From c1f485dbb56eb243e6a9ec1b68bae047563dd9c7 Mon Sep 17 00:00:00 2001 From: Patrick Kettner Date: Fri, 17 Oct 2014 01:48:41 -0400 Subject: [PATCH 092/116] Fallback to event.keyCode for firefox fixes #199 --- src/jquery.maskedinput.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 3c449ad..32dbd35 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -235,7 +235,7 @@ $.fn.extend({ return; } - var k = e.which, + var k = e.which || e.keyCode, pos, begin, end; @@ -268,7 +268,7 @@ $.fn.extend({ return; } - var k = e.which, + var k = e.which || e.keyCode, pos = input.caret(), p, c, From 085c49ca9b3525882b3e9e4fb89b5b715c7dc849 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Sun, 19 Oct 2014 22:16:05 -0500 Subject: [PATCH 093/116] Adding support for multi character placeholders Fixes #120 Fixes #237 Closes #56 Closes #149 Closes #252 --- demo/index.html | 2 +- dist/jquery.maskedinput.js | 23 +++++++++++++---------- dist/jquery.maskedinput.min.js | 2 +- spec/Placeholder.spec.js | 27 +++++++++++++++++++++++++++ src/jquery.maskedinput.js | 20 +++++++++++++------- 5 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 spec/Placeholder.spec.js diff --git a/demo/index.html b/demo/index.html index 055b774..eafcc82 100644 --- a/demo/index.html +++ b/demo/index.html @@ -6,7 +6,7 @@ - + $(function() { $.mask.definitions['~'] = "[+-]"; From 9333fdce00ec2422b1a0a5aa8872f5a99b7b6033 Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Sat, 31 Jan 2015 20:32:04 -0600 Subject: [PATCH 107/116] Removing upper jQuery limit. Closes #291 --- bower.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bower.json b/bower.json index 6f0399b..346209a 100644 --- a/bower.json +++ b/bower.json @@ -25,6 +25,6 @@ "lib" ], "dependencies": { - "jquery": ">=1.8.3 <2.0" + "jquery": ">=1.8.3" } } From 24c3bfaec10c043b408f4f2bc94e5926c0059927 Mon Sep 17 00:00:00 2001 From: Dimitrios Kanellopoulos Date: Mon, 9 Feb 2015 23:50:02 +0100 Subject: [PATCH 108/116] Fix oldVal not beeing set --- src/jquery.maskedinput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 9b38bd9..8adcc4f 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -199,7 +199,7 @@ $.fn.extend({ function androidInputEvent(e) { var curVal = input.val(); var pos = input.caret(); - if (curVal.length < oldVal.length) { + if (oldVal && oldVal.length && oldVal.length > curVal.length ) { // a deletion or backspace happened checkVal(true); while (pos.begin > 0 && !tests[pos.begin-1]) From f19728180c604c1b0c62d6aa97af262c0c04d71c Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Tue, 10 Feb 2015 21:17:34 -0600 Subject: [PATCH 109/116] 1.4.1 --- bower.json | 2 +- dist/jquery.maskedinput.js | 182 +++++++++++++++++++++++++++++++++ dist/jquery.maskedinput.min.js | 7 ++ jquery.maskedinput.nuspec | 2 +- package.json | 2 +- 5 files changed, 192 insertions(+), 3 deletions(-) create mode 100644 dist/jquery.maskedinput.js create mode 100644 dist/jquery.maskedinput.min.js diff --git a/bower.json b/bower.json index 346209a..63e7ab7 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "jquery.maskedinput", - "version": "1.4.0", + "version": "1.4.1", "homepage": "http://digitalbush.com/projects/masked-input-plugin/", "authors": [ "Josh Bush (digitalbush.com)" diff --git a/dist/jquery.maskedinput.js b/dist/jquery.maskedinput.js new file mode 100644 index 0000000..4ac3cfc --- /dev/null +++ b/dist/jquery.maskedinput.js @@ -0,0 +1,182 @@ +/* + jQuery Masked Input Plugin + Copyright (c) 2007 - 2015 Josh Bush (digitalbush.com) + Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) + Version: 1.4.1 +*/ +!function(factory) { + "function" == typeof define && define.amd ? define([ "jquery" ], factory) : factory("object" == typeof exports ? require("jquery") : jQuery); +}(function($) { + var caretTimeoutId, ua = navigator.userAgent, iPhone = /iphone/i.test(ua), chrome = /chrome/i.test(ua), android = /android/i.test(ua); + $.mask = { + definitions: { + "9": "[0-9]", + a: "[A-Za-z]", + "*": "[A-Za-z0-9]" + }, + autoclear: !0, + dataName: "rawMaskFn", + placeholder: "_" + }, $.fn.extend({ + caret: function(begin, end) { + var range; + if (0 !== this.length && !this.is(":hidden")) return "number" == typeof begin ? (end = "number" == typeof end ? end : begin, + this.each(function() { + this.setSelectionRange ? this.setSelectionRange(begin, end) : this.createTextRange && (range = this.createTextRange(), + range.collapse(!0), range.moveEnd("character", end), range.moveStart("character", begin), + range.select()); + })) : (this[0].setSelectionRange ? (begin = this[0].selectionStart, end = this[0].selectionEnd) : document.selection && document.selection.createRange && (range = document.selection.createRange(), + begin = 0 - range.duplicate().moveStart("character", -1e5), end = begin + range.text.length), + { + begin: begin, + end: end + }); + }, + unmask: function() { + return this.trigger("unmask"); + }, + mask: function(mask, settings) { + var input, defs, tests, partialPosition, firstNonMaskPos, lastRequiredNonMaskPos, len, oldVal; + if (!mask && this.length > 0) { + input = $(this[0]); + var fn = input.data($.mask.dataName); + return fn ? fn() : void 0; + } + return settings = $.extend({ + autoclear: $.mask.autoclear, + placeholder: $.mask.placeholder, + completed: null + }, settings), defs = $.mask.definitions, tests = [], partialPosition = len = mask.length, + firstNonMaskPos = null, $.each(mask.split(""), function(i, c) { + "?" == c ? (len--, partialPosition = i) : defs[c] ? (tests.push(new RegExp(defs[c])), + null === firstNonMaskPos && (firstNonMaskPos = tests.length - 1), partialPosition > i && (lastRequiredNonMaskPos = tests.length - 1)) : tests.push(null); + }), this.trigger("unmask").each(function() { + function tryFireCompleted() { + if (settings.completed) { + for (var i = firstNonMaskPos; lastRequiredNonMaskPos >= i; i++) if (tests[i] && buffer[i] === getPlaceholder(i)) return; + settings.completed.call(input); + } + } + function getPlaceholder(i) { + return settings.placeholder.charAt(i < settings.placeholder.length ? i : 0); + } + function seekNext(pos) { + for (;++pos < len && !tests[pos]; ) ; + return pos; + } + function seekPrev(pos) { + for (;--pos >= 0 && !tests[pos]; ) ; + return pos; + } + function shiftL(begin, end) { + var i, j; + if (!(0 > begin)) { + for (i = begin, j = seekNext(end); len > i; i++) if (tests[i]) { + if (!(len > j && tests[i].test(buffer[j]))) break; + buffer[i] = buffer[j], buffer[j] = getPlaceholder(j), j = seekNext(j); + } + writeBuffer(), input.caret(Math.max(firstNonMaskPos, begin)); + } + } + function shiftR(pos) { + var i, c, j, t; + for (i = pos, c = getPlaceholder(pos); len > i; i++) if (tests[i]) { + if (j = seekNext(i), t = buffer[i], buffer[i] = c, !(len > j && tests[j].test(t))) break; + c = t; + } + } + function androidInputEvent() { + var curVal = input.val(), pos = input.caret(); + if (oldVal && oldVal.length && oldVal.length > curVal.length) { + for (checkVal(!0); pos.begin > 0 && !tests[pos.begin - 1]; ) pos.begin--; + if (0 === pos.begin) for (;pos.begin < firstNonMaskPos && !tests[pos.begin]; ) pos.begin++; + input.caret(pos.begin, pos.begin); + } else { + for (checkVal(!0); pos.begin < len && !tests[pos.begin]; ) pos.begin++; + input.caret(pos.begin, pos.begin); + } + tryFireCompleted(); + } + function blurEvent() { + checkVal(), input.val() != focusText && input.change(); + } + function keydownEvent(e) { + if (!input.prop("readonly")) { + var pos, begin, end, k = e.which || e.keyCode; + oldVal = input.val(), 8 === k || 46 === k || iPhone && 127 === k ? (pos = input.caret(), + begin = pos.begin, end = pos.end, end - begin === 0 && (begin = 46 !== k ? seekPrev(begin) : end = seekNext(begin - 1), + end = 46 === k ? seekNext(end) : end), clearBuffer(begin, end), shiftL(begin, end - 1), + e.preventDefault()) : 13 === k ? blurEvent.call(this, e) : 27 === k && (input.val(focusText), + input.caret(0, checkVal()), e.preventDefault()); + } + } + function keypressEvent(e) { + if (!input.prop("readonly")) { + var p, c, next, k = e.which || e.keyCode, pos = input.caret(); + if (!(e.ctrlKey || e.altKey || e.metaKey || 32 > k) && k && 13 !== k) { + if (pos.end - pos.begin !== 0 && (clearBuffer(pos.begin, pos.end), shiftL(pos.begin, pos.end - 1)), + p = seekNext(pos.begin - 1), len > p && (c = String.fromCharCode(k), tests[p].test(c))) { + if (shiftR(p), buffer[p] = c, writeBuffer(), next = seekNext(p), android) { + var proxy = function() { + $.proxy($.fn.caret, input, next)(); + }; + setTimeout(proxy, 0); + } else input.caret(next); + pos.begin <= lastRequiredNonMaskPos && tryFireCompleted(); + } + e.preventDefault(); + } + } + } + function clearBuffer(start, end) { + var i; + for (i = start; end > i && len > i; i++) tests[i] && (buffer[i] = getPlaceholder(i)); + } + function writeBuffer() { + input.val(buffer.join("")); + } + function checkVal(allow) { + var i, c, pos, test = input.val(), lastMatch = -1; + for (i = 0, pos = 0; len > i; i++) if (tests[i]) { + for (buffer[i] = getPlaceholder(i); pos++ < test.length; ) if (c = test.charAt(pos - 1), + tests[i].test(c)) { + buffer[i] = c, lastMatch = i; + break; + } + if (pos > test.length) { + clearBuffer(i + 1, len); + break; + } + } else buffer[i] === test.charAt(pos) && pos++, partialPosition > i && (lastMatch = i); + return allow ? writeBuffer() : partialPosition > lastMatch + 1 ? settings.autoclear || buffer.join("") === defaultBuffer ? (input.val() && input.val(""), + clearBuffer(0, len)) : writeBuffer() : (writeBuffer(), input.val(input.val().substring(0, lastMatch + 1))), + partialPosition ? i : firstNonMaskPos; + } + var input = $(this), buffer = $.map(mask.split(""), function(c, i) { + return "?" != c ? defs[c] ? getPlaceholder(i) : c : void 0; + }), defaultBuffer = buffer.join(""), focusText = input.val(); + input.data($.mask.dataName, function() { + return $.map(buffer, function(c, i) { + return tests[i] && c != getPlaceholder(i) ? c : null; + }).join(""); + }), input.one("unmask", function() { + input.off(".mask").removeData($.mask.dataName); + }).on("focus.mask", function() { + if (!input.prop("readonly")) { + clearTimeout(caretTimeoutId); + var pos; + focusText = input.val(), pos = checkVal(), caretTimeoutId = setTimeout(function() { + input.get(0) === document.activeElement && (writeBuffer(), pos == mask.replace("?", "").length ? input.caret(0, pos) : input.caret(pos)); + }, 10); + } + }).on("blur.mask", blurEvent).on("keydown.mask", keydownEvent).on("keypress.mask", keypressEvent).on("input.mask paste.mask", function() { + input.prop("readonly") || setTimeout(function() { + var pos = checkVal(!0); + input.caret(pos), tryFireCompleted(); + }, 0); + }), chrome && android && input.off("input.mask").on("input.mask", androidInputEvent), + checkVal(); + }); + } + }); +}); \ No newline at end of file diff --git a/dist/jquery.maskedinput.min.js b/dist/jquery.maskedinput.min.js new file mode 100644 index 0000000..d4dfd01 --- /dev/null +++ b/dist/jquery.maskedinput.min.js @@ -0,0 +1,7 @@ +/* + jQuery Masked Input Plugin + Copyright (c) 2007 - 2015 Josh Bush (digitalbush.com) + Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) + Version: 1.4.1 +*/ +!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof exports?require("jquery"):jQuery)}(function(a){var b,c=navigator.userAgent,d=/iphone/i.test(c),e=/chrome/i.test(c),f=/android/i.test(c);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(c,g){var h,i,j,k,l,m,n,o;if(!c&&this.length>0){h=a(this[0]);var p=h.data(a.mask.dataName);return p?p():void 0}return g=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null},g),i=a.mask.definitions,j=[],k=n=c.length,l=null,a.each(c.split(""),function(a,b){"?"==b?(n--,k=a):i[b]?(j.push(new RegExp(i[b])),null===l&&(l=j.length-1),k>a&&(m=j.length-1)):j.push(null)}),this.trigger("unmask").each(function(){function h(){if(g.completed){for(var a=l;m>=a;a++)if(j[a]&&C[a]===p(a))return;g.completed.call(B)}}function p(a){return g.placeholder.charAt(a=0&&!j[a];);return a}function s(a,b){var c,d;if(!(0>a)){for(c=a,d=q(b);n>c;c++)if(j[c]){if(!(n>d&&j[c].test(C[d])))break;C[c]=C[d],C[d]=p(d),d=q(d)}z(),B.caret(Math.max(l,a))}}function t(a){var b,c,d,e;for(b=a,c=p(a);n>b;b++)if(j[b]){if(d=q(b),e=C[b],C[b]=c,!(n>d&&j[d].test(e)))break;c=e}}function u(){var a=B.val(),b=B.caret();if(o&&o.length&&o.length>a.length){for(A(!0);b.begin>0&&!j[b.begin-1];)b.begin--;if(0===b.begin)for(;b.beging)&&g&&13!==g){if(i.end-i.begin!==0&&(y(i.begin,i.end),s(i.begin,i.end-1)),c=q(i.begin-1),n>c&&(d=String.fromCharCode(g),j[c].test(d))){if(t(c),C[c]=d,z(),e=q(c),f){var k=function(){a.proxy(a.fn.caret,B,e)()};setTimeout(k,0)}else B.caret(e);i.begin<=m&&h()}b.preventDefault()}}}function y(a,b){var c;for(c=a;b>c&&n>c;c++)j[c]&&(C[c]=p(c))}function z(){B.val(C.join(""))}function A(a){var b,c,d,e=B.val(),f=-1;for(b=0,d=0;n>b;b++)if(j[b]){for(C[b]=p(b);d++e.length){y(b+1,n);break}}else C[b]===e.charAt(d)&&d++,k>b&&(f=b);return a?z():k>f+1?g.autoclear||C.join("")===D?(B.val()&&B.val(""),y(0,n)):z():(z(),B.val(B.val().substring(0,f+1))),k?b:l}var B=a(this),C=a.map(c.split(""),function(a,b){return"?"!=a?i[a]?p(b):a:void 0}),D=C.join(""),E=B.val();B.data(a.mask.dataName,function(){return a.map(C,function(a,b){return j[b]&&a!=p(b)?a:null}).join("")}),B.one("unmask",function(){B.off(".mask").removeData(a.mask.dataName)}).on("focus.mask",function(){if(!B.prop("readonly")){clearTimeout(b);var a;E=B.val(),a=A(),b=setTimeout(function(){B.get(0)===document.activeElement&&(z(),a==c.replace("?","").length?B.caret(0,a):B.caret(a))},10)}}).on("blur.mask",v).on("keydown.mask",w).on("keypress.mask",x).on("input.mask paste.mask",function(){B.prop("readonly")||setTimeout(function(){var a=A(!0);B.caret(a),h()},0)}),e&&f&&B.off("input.mask").on("input.mask",u),A()})}})}); \ No newline at end of file diff --git a/jquery.maskedinput.nuspec b/jquery.maskedinput.nuspec index 0271931..9e88140 100644 --- a/jquery.maskedinput.nuspec +++ b/jquery.maskedinput.nuspec @@ -2,7 +2,7 @@ jQuery.MaskedInput - 1.4.0.0 + 1.4.1.0 digitalBush stimms https://github.com/digitalBush/jquery.maskedinput/blob/master/LICENSE diff --git a/package.json b/package.json index 8bd604c..8c65c9b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jquery.maskedinput", - "version": "1.4.0", + "version": "1.4.1", "author": "Josh Bush (digitalbush.com)", "description": "jQuery Masked Input Plugin", "devDependencies": { From 0d017f402fefac2dae427654f9ba74b23d2b93a3 Mon Sep 17 00:00:00 2001 From: Ben Cooley Date: Mon, 11 May 2015 10:39:55 -0400 Subject: [PATCH 110/116] Ensuring caret does not execute if the element does not have focus. This fixes an issue in IE11 where the user may not be able to move to another input element --- src/jquery.maskedinput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 8adcc4f..ee6d1ab 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -34,7 +34,7 @@ $.fn.extend({ caret: function(begin, end) { var range; - if (this.length === 0 || this.is(":hidden")) { + if (this.length === 0 || this.is(":hidden") || this.get(0) !== document.activeElement) { return; } From 7ae3485fccf9b408f81b4f7f0645a6b74dbe66b8 Mon Sep 17 00:00:00 2001 From: Mike Bobrov Date: Tue, 12 May 2015 11:10:55 +0300 Subject: [PATCH 111/116] A little update to the documentation Of course it can be easily found from the source code, but it would be very convenient to see it in the documentation --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 97c7745..825533a 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,13 @@ jQuery(function($){ }); ``` +Optionally, if you would like to disable the automatic discarding of the uncomplete input, you may pass an optional argument to the maskedinput method +```html +jQuery(function($){ + $("#product").mask("99/99/9999",{autoclear: false}); +}); +``` + You can now supply your own mask definitions. ```html jQuery(function($){ From b21979c52c3146cc3e9e33f8d93ee8e49e1f8617 Mon Sep 17 00:00:00 2001 From: Allan Maia Fernandes Date: Mon, 18 May 2015 16:07:48 -0300 Subject: [PATCH 112/116] Forcing mask to string This code: $('[data-masked]').each(function() { $(this).mask($(this).data('masked')); }); Will give an error, when data-masked="99999" ... the $(this).data returns a number object, not string. --- src/jquery.maskedinput.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 8adcc4f..9106d5e 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -94,6 +94,8 @@ $.fn.extend({ partialPosition = len = mask.length; firstNonMaskPos = null; + mask = String(mask); + $.each(mask.split(""), function(i, c) { if (c == '?') { len--; From 89aa9d33d41f2cbcc27ab6afe3c8c9fa4d5df99a Mon Sep 17 00:00:00 2001 From: Matt Sich Date: Thu, 11 Jun 2015 11:48:47 -0400 Subject: [PATCH 113/116] fixed android issue where cursor would not move and input would be typed backwards --- src/jquery.maskedinput.js | 59 +++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 8adcc4f..64f961d 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -196,32 +196,43 @@ $.fn.extend({ } } - function androidInputEvent(e) { - var curVal = input.val(); - var pos = input.caret(); - if (oldVal && oldVal.length && oldVal.length > curVal.length ) { - // a deletion or backspace happened - checkVal(true); - while (pos.begin > 0 && !tests[pos.begin-1]) - pos.begin--; - if (pos.begin === 0) - { - while (pos.begin < firstNonMaskPos && !tests[pos.begin]) - pos.begin++; - } - input.caret(pos.begin,pos.begin); - } else { - var pos2 = checkVal(true); - while (pos.begin < len && !tests[pos.begin]) - pos.begin++; - - input.caret(pos.begin,pos.begin); - } + function androidInputEvent(e) { + console.log(input); + var curVal = input.val(); + var pos = input.caret(); + if (oldVal && oldVal.length && oldVal.length > curVal.length ) { + // a deletion or backspace happened + checkVal(true); + while (pos.begin > 0 && !tests[pos.begin-1]) + pos.begin--; + if (pos.begin === 0) + { + while (pos.begin < firstNonMaskPos && !tests[pos.begin]) + pos.begin++; + } + input.caret(pos.begin,pos.begin); + } else { + var pos2 = checkVal(true); + var lastEnteredValue = curVal.charAt(pos.begin); + if (pos.begin < len){ + if(!tests[pos.begin]){ + pos.begin++; + if(tests[pos.begin].test(lastEnteredValue)){ + pos.begin++; + } + }else{ + if(tests[pos.begin].test(lastEnteredValue)){ + pos.begin++; + } + } + } + input.caret(pos.begin,pos.begin); + } + tryFireCompleted(); + } - tryFireCompleted(); - } - function blurEvent(e) { + function blurEvent(e) { checkVal(); if (input.val() != focusText) From e0708d76794a26b2516dc615cc0b7307001172f2 Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Thu, 11 Jun 2015 22:53:23 -0400 Subject: [PATCH 114/116] Remove moot `version` property from bower.json Per bower/bower.json-spec@a325da3 Also their maintainer says they probably won't ever use it: http://stackoverflow.com/questions/24844901/bowers-bower-json-file-version-property --- bower.json | 1 - 1 file changed, 1 deletion(-) diff --git a/bower.json b/bower.json index 63e7ab7..e6368cb 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,5 @@ { "name": "jquery.maskedinput", - "version": "1.4.1", "homepage": "http://digitalbush.com/projects/masked-input-plugin/", "authors": [ "Josh Bush (digitalbush.com)" From 2d5e1e2827aeed48c4fd6a680191e8b89e94c4b1 Mon Sep 17 00:00:00 2001 From: MattSich Date: Thu, 18 Jun 2015 20:58:57 -0400 Subject: [PATCH 115/116] Update jquery.maskedinput.js Removed log --- src/jquery.maskedinput.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 64f961d..df85c09 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -197,7 +197,6 @@ $.fn.extend({ } function androidInputEvent(e) { - console.log(input); var curVal = input.val(); var pos = input.caret(); if (oldVal && oldVal.length && oldVal.length > curVal.length ) { From 4a146e61b83e756446ce17f67f934abd644dafbb Mon Sep 17 00:00:00 2001 From: Josh Bush Date: Sun, 10 Dec 2017 13:57:16 -0600 Subject: [PATCH 116/116] =?UTF-8?q?=F0=9F=91=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 825533a..87889b9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,12 @@ Masked Input Plugin for jQuery ============================== -[![Build Status](https://travis-ci.org/digitalBush/jquery.maskedinput.png)](https://travis-ci.org/digitalBush/jquery.maskedinput) +**Notice: This project is no longer being maintained.** + +I started this project [over 10 years ago](https://forum.jquery.com/topic/jquery-introduction-and-masked-input-plugin) to fill a need for a side project I was working on at the time. Nothing ever became of that side project, but this little plugin lived on. Over the years it brought me joy to stumble on sites using this thing. It was super encouraging to hear from people using it in their own products. I tried for a while to maintain it, even after I had moved away from front end web development. + +The time has come to officially call it quits. The web has changed(**A LOT**) and there are better things out there like [Cleave.js](https://nosir.github.io/cleave.js/). I'll leave this repo up for posterity in an archived state. Thank you to everyone who contributed to or used this plugin over the years. + + Overview -------- This is a masked input plugin for the jQuery javascript library. It allows a user to more easily enter fixed width input where you would like them to enter the data in a certain format (dates,phone numbers, etc). It has been tested on Internet Explorer, Firefox, Safari, Opera, and Chrome. A mask is defined by a format made up of mask literals and mask definitions. Any character not in the definitions list below is considered a mask literal. Mask literals will be automatically entered for the user as they type and will not be able to be removed by the user.The following mask definitions are predefined:
Date99/99/9999
Phone(999) 999-9999
Phone(999) 999-9999
Phone(Readonly)(999) 999-9999
Phone + Ext(999) 999-9999? x99999
Int'l Phone+33 999 999 999
Tax ID99-9999999