Robin Herbots 12 years ago
parent
commit
e495bbde71
2 changed files with 51 additions and 17 deletions
  1. 38 14
      js/jquery.inputmask.js
  2. 13 3
      qunit/tests.js

+ 38 - 14
js/jquery.inputmask.js

@@ -296,14 +296,21 @@
             iphone = navigator.userAgent.match(new RegExp("iphone", "i")) !== null,
             android = navigator.userAgent.match(new RegExp("android.*safari.*", "i")) !== null,
             androidchrome = navigator.userAgent.match(new RegExp("android.*chrome.*", "i")) !== null,
-            pasteEvent = isInputEventSupported('paste') ? 'paste' : isInputEventSupported('input') ? 'input' : "propertychange";
+            pasteEvent = isInputEventSupported('paste') ? 'paste' : isInputEventSupported('input') ? 'input' : "propertychange",
+            androidchrome32 = false;
+
+        if (androidchrome) {
+            var browser = navigator.userAgent.match(new RegExp("chrome.*", "i")),
+                version = parseInt(new RegExp(/[0-9]+/).exec(browser));
+            androidchrome32 = (version == 32);
+        }
 
         //masking scope
         //actionObj definition see below
         function maskScope(masksets, activeMasksetIndex, opts, actionObj) {
             var isRTL = false,
                 valueOnFocus = getActiveBuffer().join(''),
-                $el, chromeValueOnInput,
+                $el,
                 skipKeyPressEvent = false, //Safari 5.1.x - modal dialog fires keypress twice workaround
                 skipInputEvent = false, //skip when triggered from within inputmask
                 ignorable = false;
@@ -1331,11 +1338,6 @@
             function keyupEvent(e) {
                 var $input = $(this), input = this, k = e.keyCode, buffer = getActiveBuffer();
 
-                if (androidchrome && k == opts.keyCode.BACKSPACE) {
-                    if (chromeValueOnInput == input._valueGet())
-                        keydownEvent.call(this, e);
-                }
-
                 opts.onKeyUp.call(this, e, buffer, opts); //extra stuff to execute on keyup
                 if (k == opts.keyCode.TAB && opts.showMaskOnFocus) {
                     if ($input.hasClass('focus.inputmask') && input._valueGet().length == 0) {
@@ -1361,7 +1363,6 @@
                 }
                 var input = this, $input = $(input);
 
-                chromeValueOnInput = getActiveBuffer().join('');
                 checkVal(input, false, false);
                 writeBuffer(input, getActiveBuffer());
                 if (isComplete(getActiveBuffer()) === true)
@@ -1369,6 +1370,30 @@
                 $input.click();
             }
 
+            function chromeInputEvent(e) {
+                if (skipInputEvent === true) {
+                    skipInputEvent = false;
+                    return true;
+                }
+                var input = this, $input = $(input);
+
+                //backspace in chrome32 only fires input event - detect & treat
+                var caretPos = caret(input),
+                    currentValue = input._valueGet();
+                if (currentValue.charAt(caretPos.begin) != getActiveBuffer()[caretPos.begin] && !isMask(caretPos.begin)) {
+                    e.keyCode = opts.keyCode.BACKSPACE;
+                    keydownEvent.call(input, e);
+                } else {
+                    checkVal(input, false, false);
+                    writeBuffer(input, getActiveBuffer());
+                    if (isComplete(getActiveBuffer()) === true)
+                        $input.trigger("complete");
+                    $input.click();
+                }
+                
+                e.preventDefault()
+            }
+
             function mask(el) {
                 $el = $(el);
                 if ($el.is(":input")) {
@@ -1553,13 +1578,12 @@
                     ).bind('cleared.inputmask', opts.oncleared
                     ).bind("keyup.inputmask", keyupEvent);
 
-                    if (androidchrome) {
-                        $el.bind("input.inputmask", inputEvent);
-                    } else {
-                        $el.bind("keydown.inputmask", keydownEvent
-                        ).bind("keypress.inputmask", keypressEvent);
-                    }
+                    $el.bind("keydown.inputmask", keydownEvent
+                         ).bind("keypress.inputmask", keypressEvent
+                         ).bind("keyup.inputmask", keyupEvent);
 
+                    if (androidchrome32)
+                        $el.bind("input.inputmask", chromeInputEvent);
                     if (msie10)
                         $el.bind("input.inputmask", inputEvent);
 

+ 13 - 3
qunit/tests.js

@@ -418,7 +418,7 @@ test("inputmask(\"999:99\", { placeholder: \"0\"}) value=\"007:20\"", function (
     $("#testmask").remove();
 });
 
-test("inputmask(\"99 999 999 999 9999 \\D\\E*** 9999\") ~ value=\"01 650 103 002 0001 DE101 5170\"", function () {
+test("inputmask(\"99 999 999 999 9999 \\D\\E*** 9999\") ~ value=\"01 650 103 002 0001 DE101 5170\" - wuSam", function () {
     $('body').append('<input type="text" id="testmask" value="01 650 103 002 0001 DE101 5170" />');
     $("#testmask").inputmask("99 999 999 999 9999 \\D\\E*** 9999");
     equal($("#testmask").val(), "01 650 103 002 0001 DE101 5170", "Result " + $("#testmask").val());
@@ -426,7 +426,7 @@ test("inputmask(\"99 999 999 999 9999 \\D\\E*** 9999\") ~ value=\"01 650 103 002
     $("#testmask").remove();
 });
 
-test("inputmask(\"99 999 999 999 9999 \\D\\E*** 9999\") ~ value=\"016501030020001DE1015170\"", function () {
+test("inputmask(\"99 999 999 999 9999 \\D\\E*** 9999\") ~ value=\"016501030020001DE1015170\" - wuSam", function () {
     $('body').append('<input type="text" id="testmask" value="016501030020001DE1015170" />');
     $("#testmask").inputmask("99 999 999 999 9999 \\D\\E*** 9999");
     equal($("#testmask").val(), "01 650 103 002 0001 DE101 5170", "Result " + $("#testmask").val());
@@ -434,7 +434,17 @@ test("inputmask(\"99 999 999 999 9999 \\D\\E*** 9999\") ~ value=\"01650103002000
     $("#testmask").remove();
 });
 
-test("inputmask(\"\\D\\E***\") ~ value=\"DE001\"", function () {
+test("inputmask(\"99 999 999 999 9999 \\D\\E*** 9999\") ~ value=\"016501030020001DE1015170\" replace 2 with 3 - wuSam", function () {
+    $('body').append('<input type="text" id="testmask" value="016501030020001DE1015170" />');
+    $("#testmask").inputmask("99 999 999 999 9999 \\D\\E*** 9999");
+    caret($("#testmask"), 13, 14);
+	$("#testmask").Type("3");
+	equal($("#testmask").val(), "01 650 103 003 0001 DE101 5170", "Result " + $("#testmask").val());
+	
+    $("#testmask").remove();
+});
+
+test("inputmask(\"\\D\\E***\") ~ value=\"DE001\" - wuSam", function () {
     $('body').append('<input type="text" id="testmask" value="DE001" />');
     $("#testmask").inputmask("\\D\\E***");
     equal($("#testmask").val(), "DE001", "Result " + $("#testmask").val());