Browse Source

#307 - relax literal checking in regex extension

Robin Herbots 12 years ago
parent
commit
281a9cb1d0

+ 1 - 1
build.properties

@@ -7,7 +7,7 @@ distdir = dist
 
 build.major = 2
 build.minor = 3
-build.revision = 20
+build.revision = 22
 
 target = jquery.inputmask.bundle.js
 target.min = jquery.inputmask.bundle.min.js

+ 1 - 1
component.json

@@ -1,6 +1,6 @@
 {
     "name": "jquery.inputmask",
-    "version": "2.3.20",
+    "version": "2.3.22",
     "main": "./dist/jquery.inputmask.bundle.js",
     "dependencies": {
         "jquery": ">=1.5"

BIN
dist/jQuery.InputMask.2.3.20.nupkg


BIN
dist/jQuery.InputMask.2.3.22.nupkg


+ 36 - 20
dist/jquery.inputmask.bundle.js

@@ -3,7 +3,7 @@
 * http://github.com/RobinHerbots/jquery.inputmask
 * Copyright (c) 2010 - 2013 Robin Herbots
 * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-* Version: 2.3.20
+* Version: 2.3.22
 */
 
 (function ($) {
@@ -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);
 
@@ -1560,7 +1559,7 @@ Input Mask plugin extensions
 http://github.com/RobinHerbots/jquery.inputmask
 Copyright (c) 2010 - 2013 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 2.3.20
+Version: 2.3.22
 
 Optional extensions on the jquery.inputmask base
 */
@@ -1662,7 +1661,7 @@ Input Mask plugin extensions
 http://github.com/RobinHerbots/jquery.inputmask
 Copyright (c) 2010 - 2012 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 2.3.20
+Version: 2.3.22
 
 Optional extensions on the jquery.inputmask base
 */
@@ -2139,7 +2138,7 @@ Input Mask plugin extensions
 http://github.com/RobinHerbots/jquery.inputmask
 Copyright (c) 2010 - 2013 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 2.3.20
+Version: 2.3.22
 
 Optional extensions on the jquery.inputmask base
 */
@@ -2308,7 +2307,7 @@ Input Mask plugin extensions
 http://github.com/RobinHerbots/jquery.inputmask
 Copyright (c) 2010 - 2013 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 2.3.20
+Version: 2.3.22
 
 Regex extensions on the jquery.inputmask base
 Allows for using regular expressions as a mask
@@ -2331,6 +2330,7 @@ Allows for using regular expressions as a mask
                             this.matches = [];
                             this.isGroup = false;
                             this.isQuantifier = false;
+                            this.isLiteral = false;
                         }
                         function analyseRegex() {
                             var currentToken = new regexToken(), match, m, opengroups = [];
@@ -2343,10 +2343,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 {
@@ -2354,6 +2350,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);
@@ -2365,7 +2363,6 @@ Allows for using regular expressions as a mask
                                         } else {
                                             opts.regexTokens.push(groupToken);
                                             currentToken = new regexToken();
-                                            opts.regexTokens.push(currentToken);
                                         }
                                         break;
                                     case "{": //Quantifier
@@ -2383,13 +2380,19 @@ Allows for using regular expressions as a mask
                                         // ^ or $ anchor
                                         // Dot (.)
                                         // Literal character sequence
+                                        var literal = new regexToken();
+                                        literal.isLiteral = true;
+                                        literal.matches.push(m);
                                         if (opengroups.length > 0) {
-                                            opengroups[opengroups.length - 1]["matches"].push(m);
+                                            opengroups[opengroups.length - 1]["matches"].push(literal);
                                         } else {
-                                            currentToken.matches.push(m);
+                                            currentToken.matches.push(literal);
                                         }
                                 }
                             }
+
+                            if (currentToken.matches.length > 0)
+                                opts.regexTokens.push(currentToken);
                         };
 
                         function validateRegexToken(token, fromGroup) {
@@ -2412,8 +2415,21 @@ Allows for using regular expressions as a mask
                                     var exp = new RegExp("^(" + testExp + ")$");
                                     isvalid = exp.test(bufferStr);
                                     regexPart += matchToken;
-                                }
-                                else {
+                                } else if (matchToken["isLiteral"] == true) {
+                                    matchToken = matchToken["matches"][0];
+                                    var testExp = regexPart, openGroupCloser = "";
+                                    for (var j = 0; j < openGroupCount; j++) {
+                                        openGroupCloser += ")";
+                                    }
+                                    for (var k = 0; k < matchToken.length; k++) { //relax literal validation
+                                        testExp = (testExp + matchToken[k]).replace(/\|$/, "");
+                                        var exp = new RegExp("^(" + testExp + openGroupCloser + ")$");
+                                        isvalid = exp.test(bufferStr);
+                                        if (isvalid) break;
+                                    }
+                                    regexPart += matchToken;
+                                    console.log(bufferStr + " " + exp + " " + isvalid);
+                                } else {
                                     regexPart += matchToken;
                                     var testExp = regexPart.replace(/\|$/, "");
                                     for (var j = 0; j < openGroupCount; j++) {
@@ -2421,7 +2437,7 @@ Allows for using regular expressions as a mask
                                     }
                                     var exp = new RegExp("^(" + testExp + ")$");
                                     isvalid = exp.test(bufferStr);
-                                    //console.log(bufferStr + " " + exp + " " + isvalid);
+                                    console.log(bufferStr + " " + exp + " " + isvalid);
                                 }
                                 if (isvalid) break;
                             }

File diff suppressed because it is too large
+ 69 - 69
dist/jquery.inputmask.bundle.min.js


File diff suppressed because it is too large
+ 1 - 1
dist/min/jquery.inputmask.js


File diff suppressed because it is too large
+ 4 - 4
dist/min/jquery.inputmask.regex.extensions.js


+ 1 - 1
jquery.inputmask.jquery.json

@@ -8,7 +8,7 @@
 		"inputmask",
 		"mask"
     ],
-    "version": "2.3.20",
+    "version": "2.3.22",
     "author": {
         "name": "Robin Herbots",
         "url": "http://github.com/RobinHerbots/jquery.inputmask"

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

@@ -26,6 +26,7 @@ Allows for using regular expressions as a mask
                             this.matches = [];
                             this.isGroup = false;
                             this.isQuantifier = false;
+                            this.isLiteral = false;
                         }
                         function analyseRegex() {
                             var currentToken = new regexToken(), match, m, opengroups = [];
@@ -75,10 +76,13 @@ Allows for using regular expressions as a mask
                                         // ^ or $ anchor
                                         // Dot (.)
                                         // Literal character sequence
+                                        var literal = new regexToken();
+                                        literal.isLiteral = true;
+                                        literal.matches.push(m);
                                         if (opengroups.length > 0) {
-                                            opengroups[opengroups.length - 1]["matches"].push(m);
+                                            opengroups[opengroups.length - 1]["matches"].push(literal);
                                         } else {
-                                            currentToken.matches.push(m);
+                                            currentToken.matches.push(literal);
                                         }
                                 }
                             }
@@ -107,8 +111,21 @@ Allows for using regular expressions as a mask
                                     var exp = new RegExp("^(" + testExp + ")$");
                                     isvalid = exp.test(bufferStr);
                                     regexPart += matchToken;
-                                }
-                                else {
+                                } else if (matchToken["isLiteral"] == true) {
+                                    matchToken = matchToken["matches"][0];
+                                    var testExp = regexPart, openGroupCloser = "";
+                                    for (var j = 0; j < openGroupCount; j++) {
+                                        openGroupCloser += ")";
+                                    }
+                                    for (var k = 0; k < matchToken.length; k++) { //relax literal validation
+                                        testExp = (testExp + matchToken[k]).replace(/\|$/, "");
+                                        var exp = new RegExp("^(" + testExp + openGroupCloser + ")$");
+                                        isvalid = exp.test(bufferStr);
+                                        if (isvalid) break;
+                                    }
+                                    regexPart += matchToken;
+                                    console.log(bufferStr + " " + exp + " " + isvalid);
+                                } else {
                                     regexPart += matchToken;
                                     var testExp = regexPart.replace(/\|$/, "");
                                     for (var j = 0; j < openGroupCount; j++) {
@@ -116,7 +133,7 @@ Allows for using regular expressions as a mask
                                     }
                                     var exp = new RegExp("^(" + testExp + ")$");
                                     isvalid = exp.test(bufferStr);
-                                    //console.log(bufferStr + " " + exp + " " + isvalid);
+                                    console.log(bufferStr + " " + exp + " " + isvalid);
                                 }
                                 if (isvalid) break;
                             }