Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / pybind / mgr / dashboard / static / AdminLTE-2.3.7 / 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                         function validateRegexToken(token, fromGroup) {
94                             var isvalid = false;
95                             if (fromGroup) {
96                                 regexPart += "(";
97                                 openGroupCount++;
98                             }
99                             for (var mndx = 0; mndx < token["matches"].length; mndx++) {
100                                 var matchToken = token["matches"][mndx];
101                                 if (matchToken["isGroup"] == true) {
102                                     isvalid = validateRegexToken(matchToken, true);
103                                 } else if (matchToken["isQuantifier"] == true) {
104                                     matchToken = matchToken["matches"][0];
105                                     var quantifierMax = opts.quantifierFilter.exec(matchToken)[0].replace("}", "");
106                                     var testExp = regexPart + "{1," + quantifierMax + "}"; //relax quantifier validation
107                                     for (var j = 0; j < openGroupCount; j++) {
108                                         testExp += ")";
109                                     }
110                                     var exp = new RegExp("^(" + testExp + ")$");
111                                     isvalid = exp.test(bufferStr);
112                                     regexPart += matchToken;
113                                 } else if (matchToken["isLiteral"] == true) {
114                                     matchToken = matchToken["matches"][0];
115                                     var testExp = regexPart, openGroupCloser = "";
116                                     for (var j = 0; j < openGroupCount; j++) {
117                                         openGroupCloser += ")";
118                                     }
119                                     for (var k = 0; k < matchToken.length; k++) { //relax literal validation
120                                         testExp = (testExp + matchToken[k]).replace(/\|$/, "");
121                                         var exp = new RegExp("^(" + testExp + openGroupCloser + ")$");
122                                         isvalid = exp.test(bufferStr);
123                                         if (isvalid) break;
124                                     }
125                                     regexPart += matchToken;
126                                     //console.log(bufferStr + " " + exp + " " + isvalid);
127                                 } else {
128                                     regexPart += matchToken;
129                                     var testExp = regexPart.replace(/\|$/, "");
130                                     for (var j = 0; j < openGroupCount; j++) {
131                                         testExp += ")";
132                                     }
133                                     var exp = new RegExp("^(" + testExp + ")$");
134                                     isvalid = exp.test(bufferStr);
135                                     //console.log(bufferStr + " " + exp + " " + isvalid);
136                                 }
137                                 if (isvalid) break;
138                             }
139
140                             if (fromGroup) {
141                                 regexPart += ")";
142                                 openGroupCount--;
143                             }
144
145                             return isvalid;
146                         }
147
148
149                         if (opts.regexTokens == null) {
150                             analyseRegex();
151                         }
152
153                         var cbuffer = buffer.slice(), regexPart = "", isValid = false, openGroupCount = 0;
154                         cbuffer.splice(pos, 0, chrs);
155                         var bufferStr = cbuffer.join('');
156                         for (var i = 0; i < opts.regexTokens.length; i++) {
157                             var regexToken = opts.regexTokens[i];
158                             isValid = validateRegexToken(regexToken, regexToken["isGroup"]);
159                             if (isValid) break;
160                         }
161
162                         return isValid;
163                     },
164                     cardinality: 1
165                 }
166             }
167         }
168     });
169 })(jQuery);