Browse Source

optimize regex a bit

Robin Herbots 12 years ago
parent
commit
fb92b055ac
3 changed files with 45 additions and 10 deletions
  1. 4 5
      js/jquery.inputmask.js
  2. 5 5
      js/jquery.inputmask.regex.extensions.js
  3. 36 0
      qunit/tests.js

+ 4 - 5
js/jquery.inputmask.js

@@ -335,13 +335,13 @@
                                 if (openenings.length > 0) {
                                     openenings[openenings.length - 1]["matches"].push(openingToken);
                                 } else {
-                                    currentToken = new maskToken();
                                     maskTokens.push(openingToken);
+                                    currentToken = new maskToken();
                                 }
                                 break;
                             case opts.optionalmarker.start:
                                 // optional opening
-                                if (currentToken.matches.length > 0)
+                                if (!currentToken.isGroup && currentToken.matches.length > 0)
                                     maskTokens.push(currentToken);
                                 currentToken = new maskToken();
                                 currentToken.isOptional = true;
@@ -349,7 +349,7 @@
                                 break;
                             case opts.groupmarker.start:
                                 // Group opening
-                                if (currentToken.matches.length > 0)
+                                if (!currentToken.isGroup && currentToken.matches.length > 0)
                                     maskTokens.push(currentToken);
                                 currentToken = new maskToken();
                                 currentToken.isGroup = true;
@@ -364,8 +364,6 @@
                                     openenings[openenings.length - 1]["matches"].push(quantifier);
                                 } else {
                                     currentToken.matches.push(quantifier);
-                                    maskTokens.push(currentToken);
-                                    currentToken = new maskToken();
                                 }
                                 break;
                             default:
@@ -376,6 +374,7 @@
                                 }
                         }
                     }
+
                     if (currentToken.matches.length > 0)
                         maskTokens.push(currentToken);
 

+ 5 - 5
js/jquery.inputmask.regex.extensions.js

@@ -38,10 +38,6 @@ Allows for using regular expressions as a mask
                                 switch (m.charAt(0)) {
                                     case "[": // Character class
                                     case "\\":  // Escape or backreference
-                                        if (currentToken["isGroup"] !== true) {
-                                            currentToken = new regexToken();
-                                            opts.regexTokens.push(currentToken);
-                                        }
                                         if (opengroups.length > 0) {
                                             opengroups[opengroups.length - 1]["matches"].push(m);
                                         } else {
@@ -49,6 +45,8 @@ Allows for using regular expressions as a mask
                                         }
                                         break;
                                     case "(": // Group opening
+                                        if (!currentToken.isGroup && currentToken.matches.length > 0)
+                                            opts.regexTokens.push(currentToken);
                                         currentToken = new regexToken();
                                         currentToken.isGroup = true;
                                         opengroups.push(currentToken);
@@ -60,7 +58,6 @@ Allows for using regular expressions as a mask
                                         } else {
                                             opts.regexTokens.push(groupToken);
                                             currentToken = new regexToken();
-                                            opts.regexTokens.push(currentToken);
                                         }
                                         break;
                                     case "{": //Quantifier
@@ -85,6 +82,9 @@ Allows for using regular expressions as a mask
                                         }
                                 }
                             }
+
+                            if (currentToken.matches.length > 0)
+                                opts.regexTokens.push(currentToken);
                         };
 
                         function validateRegexToken(token, fromGroup) {

+ 36 - 0
qunit/tests.js

@@ -1267,6 +1267,42 @@ test("inputmask(\"Regex\", { regex: \"(([2-9][0-9])-([0-9]{3}-[0-9]{3}))|((1|30|
     $("#testmask").remove();
 });
 
+test("inputmask(\"Regex\", { regex: \"([0-9]|[1][0-9]|[2][0-3]?)(\\.(5|25|75))?\" - arame regex 12", function () {
+    $('body').append('<input type="text" id="testmask" />');
+    $("#testmask").inputmask("Regex", { regex: "([0-9]|[1][0-9]|[2][0-3]?)(\\.(5|25|75))?" });
+
+    $("#testmask")[0].focus();
+    $("#testmask").Type("12");
+
+    equal($("#testmask").val(), "12", "Result " + $("#testmask").val());
+
+    $("#testmask").remove();
+});
+
+test("inputmask(\"Regex\", { regex: \"([0-9]|[1][0-9]|[2][0-3]?)(\\.(5|25|75))?\" - arame regex 12.5", function () {
+    $('body').append('<input type="text" id="testmask" />');
+    $("#testmask").inputmask("Regex", { regex: "([0-9]|[1][0-9]|[2][0-3]?)(\\.(5|25|75))?" });
+
+    $("#testmask")[0].focus();
+    $("#testmask").Type("12.5");
+
+    equal($("#testmask").val(), "12.5", "Result " + $("#testmask").val());
+
+    $("#testmask").remove();
+});
+
+test("inputmask(\"Regex\", { regex: \"([0-9]|[1][0-9]|[2][0-3]?)(\\.(5|25|75))?\" - arame regex 12.75", function () {
+    $('body').append('<input type="text" id="testmask" />');
+    $("#testmask").inputmask("Regex", { regex: "([0-9]|[1][0-9]|[2][0-3]?)(\\.(5|25|75))?" });
+
+    $("#testmask")[0].focus();
+    $("#testmask").Type("12.75");
+
+    equal($("#testmask").val(), "12.75", "Result " + $("#testmask").val());
+
+    $("#testmask").remove();
+});
+
 module("Dynamic Masks");
 test("inputmask(\"*{1,20}@*{1,20}.*{2,6}[.*{2}]\" - email mask", function () {
     $('body').append('<input type="text" id="testmask" />');