sync
[bootswatch] / AdminLTE-master / js / plugins / input-mask / jquery.inputmask.regex.extensions.js
1 /*
2 Input Mask plugin extensions
3 http://github.com/RobinHerbots/jquery.inputmask
4 Copyright (c) 2010 - 2014 Robin Herbots
5 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
6 Version: 0.0.0
7
8 Regex extensions on the jquery.inputmask base
9 Allows for using regular expressions as a mask
10 */
11 (function ($) {
12     $.extend($.inputmask.defaults.aliases, { // $(selector).inputmask("Regex", { regex: "[0-9]*"}
13         'Regex': {
14             mask: "r",
15             greedy: false,
16             repeat: "*",
17             regex: null,
18             regexTokens: null,
19             //Thx to https://github.com/slevithan/regex-colorizer for the tokenizer regex
20             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,
21             quantifierFilter: /[0-9]+[^,]/,
22             definitions: {
23                 'r': {
24                     validator: function (chrs, buffer, pos, strict, opts) {
25                         function regexToken() {
26                             this.matches = [];
27                             this.isGroup = false;
28                             this.isQuantifier = false;
29                             this.isLiteral = false;
30                         }
31                         function analyseRegex() {
32                             var currentToken = new regexToken(), match, m, opengroups = [];
33
34                             opts.regexTokens = [];
35
36                             // The tokenizer regex does most of the tokenization grunt work
37                             while (match = opts.tokenizer.exec(opts.regex)) {
38                                 m = match[0];
39                                 switch (m.charAt(0)) {
40                                     case "[": // Character class
41                                     case "\\":  // Escape or backreference
42                                         if (opengroups.length > 0) {
43                                             opengroups[opengroups.length - 1]["matches"].push(m);
44                                         } else {
45                                             currentToken.matches.push(m);
46                                         }
47                                         break;
48                                     case "(": // Group opening
49                                         if (!currentToken.isGroup && currentToken.matches.length > 0)
50                                             opts.regexTokens.push(currentToken);
51                                         currentToken = new regexToken();
52                                         currentToken.isGroup = true;
53                                         opengroups.push(currentToken);
54                                         break;
55                                     case ")": // Group closing
56                                         var groupToken = opengroups.pop();
57                                         if (opengroups.length > 0) {
58                                             opengroups[opengroups.length - 1]["matches"].push(groupToken);
59                                         } else {
60                                             opts.regexTokens.push(groupToken);
61                                             currentToken = new regexToken();
62                                         }
63                                         break;
64                                     case "{": //Quantifier
65                                         var quantifier = new regexToken();
66                                         quantifier.isQuantifier = true;
67                                         quantifier.matches.push(m);
68                                         if (opengroups.length > 0) {
69                                             opengroups[opengroups.length - 1]["matches"].push(quantifier);
70                                         } else {
71                                             currentToken.matches.push(quantifier);
72                                         }
73                                         break;
74                                     default:
75                                         // Vertical bar (alternator) 
76                                         // ^ or $ anchor
77                                         // Dot (.)
78                                         // Literal character sequence
79                                         var literal = new regexToken();
80                                         literal.isLiteral = true;
81                                         literal.matches.push(m);
82                                         if (opengroups.length > 0) {
83                                             opengroups[opengroups.length - 1]["matches"].push(literal);
84                                         } else {
85                                             currentToken.matches.push(literal);
86                                         }
87                                 }
88                             }
89
90                             if (currentToken.matches.length > 0)
91                                 opts.regexTokens.push(currentToken);
92                         };
93
94                         function validateRegexToken(token, fromGroup) {
95                             var isvalid = false;
96                             if (fromGroup) {
97                                 regexPart += "(";
98                                 openGroupCount++;
99                             }
100                             for (var mndx = 0; mndx < token["matches"].length; mndx++) {
101                                 var matchToken = token["matches"][mndx];
102                                 if (matchToken["isGroup"] == true) {
103                                     isvalid = validateRegexToken(matchToken, true);
104                                 } else if (matchToken["isQuantifier"] == true) {
105                                     matchToken = matchToken["matches"][0];
106                                     var quantifierMax = opts.quantifierFilter.exec(matchToken)[0].replace("}", "");
107                                     var testExp = regexPart + "{1," + quantifierMax + "}"; //relax quantifier validation
108                                     for (var j = 0; j < openGroupCount; j++) {
109                                         testExp += ")";
110                                     }
111                                     var exp = new RegExp("^(" + testExp + ")$");
112                                     isvalid = exp.test(bufferStr);
113                                     regexPart += matchToken;
114                                 } else if (matchToken["isLiteral"] == true) {
115                                     matchToken = matchToken["matches"][0];
116                                     var testExp = regexPart, openGroupCloser = "";
117                                     for (var j = 0; j < openGroupCount; j++) {
118                                         openGroupCloser += ")";
119                                     }
120                                     for (var k = 0; k < matchToken.length; k++) { //relax literal validation
121                                         testExp = (testExp + matchToken[k]).replace(/\|$/, "");
122                                         var exp = new RegExp("^(" + testExp + openGroupCloser + ")$");
123                                         isvalid = exp.test(bufferStr);
124                                         if (isvalid) break;
125                                     }
126                                     regexPart += matchToken;
127                                     //console.log(bufferStr + " " + exp + " " + isvalid);
128                                 } else {
129                                     regexPart += matchToken;
130                                     var testExp = regexPart.replace(/\|$/, "");
131                                     for (var j = 0; j < openGroupCount; j++) {
132                                         testExp += ")";
133                                     }
134                                     var exp = new RegExp("^(" + testExp + ")$");
135                                     isvalid = exp.test(bufferStr);
136                                     //console.log(bufferStr + " " + exp + " " + isvalid);
137                                 }
138                                 if (isvalid) break;
139                             }
140
141                             if (fromGroup) {
142                                 regexPart += ")";
143                                 openGroupCount--;
144                             }
145
146                             return isvalid;
147                         }
148
149
150                         if (opts.regexTokens == null) {
151                             analyseRegex();
152                         }
153
154                         var cbuffer = buffer.slice(), regexPart = "", isValid = false, openGroupCount = 0;
155                         cbuffer.splice(pos, 0, chrs);
156                         var bufferStr = cbuffer.join('');
157                         for (var i = 0; i < opts.regexTokens.length; i++) {
158                             var regexToken = opts.regexTokens[i];
159                             isValid = validateRegexToken(regexToken, regexToken["isGroup"]);
160                             if (isValid) break;
161                         }
162
163                         return isValid;
164                     },
165                     cardinality: 1
166                 }
167             }
168         }
169     });
170 })(jQuery);