Browse Source

optimize analyseMask

Robin Herbots 12 years ago
parent
commit
d7438a906d
1 changed files with 39 additions and 34 deletions
  1. 39 34
      js/jquery.inputmask.js

+ 39 - 34
js/jquery.inputmask.js

@@ -12,10 +12,9 @@
             //options default
             //options default
             defaults: {
             defaults: {
                 placeholder: "_",
                 placeholder: "_",
-                optionalmarker: {
-                    start: "[",
-                    end: "]"
-                },
+                optionalmarker: { start: "[", end: "]" },
+                quantifiermarker: { start: "{", end: "}" },
+                groupmarker: { start: "(", end: ")" },
                 escapeChar: "\\",
                 escapeChar: "\\",
                 mask: null,
                 mask: null,
                 oncomplete: $.noop, //executes when the mask is complete
                 oncomplete: $.noop, //executes when the mask is complete
@@ -311,7 +310,8 @@
                 var genmasks = []; //used to keep track of the masks that where processed, to avoid duplicates
                 var genmasks = []; //used to keep track of the masks that where processed, to avoid duplicates
                 var maskTokens = [];
                 var maskTokens = [];
                 function analyseMask(mask) { //just an idea - not in use for the moment
                 function analyseMask(mask) { //just an idea - not in use for the moment
-                    var tokenizer = /\[\^?]?(?:[^\\\]]+|\\[\S\s]?)*]?|\\(?:0(?:[0-3][0-7]{0,2}|[4-7][0-7]?)?|[1-9][0-9]*|x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|c[A-Za-z]|[\S\s]?)|\((?:\?[:=!]?)?|(?:[?*+]|\{[0-9]+(?:,[0-9]*)?\})\??|[^.?*+^${[()|\\]+|./g;
+                    var tokenizer = /(?:[?*+]|\{[0-9]+(?:,[0-9]*)?\})\??|[^.?*+^${[]()|\\]+|./g;
+                    //var tokenizer = /\[\^?]?(?:[^\\\]]+ | \\[\S\s]?)*]? | \\(?:0(?:[0-3][0-7]{0,2} | [4-7][0-7]?)? | [1-9][0-9]* | x[0-9A-Fa-f]{2} | u[0-9A-Fa-f]{4} | c[A-Za-z] | [\S\s]?) | \((?:\?[:=!]?)? | (?:[?*+] | \{[0-9]+(?:,[0-9]*)?\})\?? | [^.?*+^${[() | \\]+ | ./g;
                     function maskToken() {
                     function maskToken() {
                         this.matches = [];
                         this.matches = [];
                         this.isGroup = false;
                         this.isGroup = false;
@@ -319,61 +319,66 @@
                         this.isQuantifier = false;
                         this.isQuantifier = false;
                     };
                     };
                     var currentToken = new maskToken(),
                     var currentToken = new maskToken(),
-                        match, m, opengroups = [], openoptionals = [];
+                        match, m, openenings = [];
 
 
                     maskTokens = [];
                     maskTokens = [];
 
 
-                    // The tokenizer regex does most of the tokenization grunt work
                     while (match = tokenizer.exec(mask)) {
                     while (match = tokenizer.exec(mask)) {
                         m = match[0];
                         m = match[0];
                         switch (m.charAt(0)) {
                         switch (m.charAt(0)) {
-                            case "[": // optional opening
-                                if (currentToken["isGroup"] !== true) {
-                                    currentToken = new maskToken();
-                                    maskTokens.push(currentToken);
-                                }
-                                if (opengroups.length > 0) {
-                                    opengroups[opengroups.length - 1]["matches"].push(m);
+                            case opts.optionalmarker.end:
+                                // optional closing
+                            case opts.groupmarker.end:
+                                // Group closing
+                                var openingToken = openenings.pop();
+                                if (openenings.length > 0) {
+                                    openenings[openenings.length - 1]["matches"].push(openingToken);
                                 } else {
                                 } else {
-                                    currentToken.matches.push(m);
+                                    currentToken = new maskToken();
+                                    maskTokens.push(openingToken);
                                 }
                                 }
                                 break;
                                 break;
-                            case "(": // Group opening
+                            case opts.optionalmarker.start:
+                                // optional opening
+                                if (currentToken.matches.length > 0)
+                                    maskTokens.push(currentToken);
                                 currentToken = new maskToken();
                                 currentToken = new maskToken();
-                                currentToken.isGroup = true;
-                                opengroups.push(currentToken);
+                                currentToken.isOptional = true;
+                                openenings.push(currentToken);
                                 break;
                                 break;
-                            case ")": // Group closing
-                                var groupToken = opengroups.pop();
-                                if (opengroups.length > 0) {
-                                    opengroups[opengroups.length - 1]["matches"].push(groupToken);
-                                } else {
-                                    currentToken = groupToken;
+                            case opts.groupmarker.start:
+                                // Group opening
+                                if (currentToken.matches.length > 0)
                                     maskTokens.push(currentToken);
                                     maskTokens.push(currentToken);
-                                }
+                                currentToken = new maskToken();
+                                currentToken.isGroup = true;
+                                openenings.push(currentToken);
                                 break;
                                 break;
-                            case "{": //Quantifier
+                            case opts.quantifiermarker.start:
+                                //Quantifier
                                 var quantifier = new maskToken();
                                 var quantifier = new maskToken();
                                 quantifier.isQuantifier = true;
                                 quantifier.isQuantifier = true;
                                 quantifier.matches.push(m);
                                 quantifier.matches.push(m);
-                                if (opengroups.length > 0) {
-                                    opengroups[opengroups.length - 1]["matches"].push(quantifier);
+                                if (openenings.length > 0) {
+                                    openenings[openenings.length - 1]["matches"].push(quantifier);
                                 } else {
                                 } else {
                                     currentToken.matches.push(quantifier);
                                     currentToken.matches.push(quantifier);
+                                    maskTokens.push(currentToken);
+                                    currentToken = new maskToken();
                                 }
                                 }
                                 break;
                                 break;
                             default:
                             default:
-                                // Vertical bar (alternator) 
-                                // ^ or $ anchor
-                                // Dot (.)
-                                // Literal character sequence
-                                if (opengroups.length > 0) {
-                                    opengroups[opengroups.length - 1]["matches"].push(m);
+                                if (openenings.length > 0) {
+                                    openenings[openenings.length - 1]["matches"].push(m);
                                 } else {
                                 } else {
                                     currentToken.matches.push(m);
                                     currentToken.matches.push(m);
                                 }
                                 }
                         }
                         }
                     }
                     }
+                    if (currentToken.matches.length > 0)
+                        maskTokens.push(currentToken);
+
+                    return maskTokens;
                 }
                 }
 
 
                 function markOptional(maskPart) { //needed for the clearOptionalTail functionality
                 function markOptional(maskPart) { //needed for the clearOptionalTail functionality