You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
7.7KB

  1. /*!
  2. * inputmask.regex.extensions.js
  3. * http://github.com/RobinHerbots/jquery.inputmask
  4. * Copyright (c) 2010 - 2016 Robin Herbots
  5. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. * Version: 3.2.7
  7. */
  8. !function(factory) {
  9. "function" == typeof define && define.amd ? define([ "inputmask.dependencyLib", "inputmask" ], factory) : "object" == typeof exports ? module.exports = factory(require("./inputmask.dependencyLib.jquery"), require("./inputmask")) : factory(window.dependencyLib || jQuery, window.Inputmask);
  10. }(function($, Inputmask) {
  11. return Inputmask.extendAliases({
  12. Regex: {
  13. mask: "r",
  14. greedy: !1,
  15. repeat: "*",
  16. regex: null,
  17. regexTokens: null,
  18. 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,
  19. quantifierFilter: /[0-9]+[^,]/,
  20. isComplete: function(buffer, opts) {
  21. return new RegExp(opts.regex).test(buffer.join(""));
  22. },
  23. definitions: {
  24. r: {
  25. validator: function(chrs, maskset, pos, strict, opts) {
  26. function RegexToken(isGroup, isQuantifier) {
  27. this.matches = [], this.isGroup = isGroup || !1, this.isQuantifier = isQuantifier || !1,
  28. this.quantifier = {
  29. min: 1,
  30. max: 1
  31. }, this.repeaterPart = void 0;
  32. }
  33. function analyseRegex() {
  34. var match, m, currentToken = new RegexToken(), opengroups = [];
  35. for (opts.regexTokens = []; match = opts.tokenizer.exec(opts.regex); ) switch (m = match[0],
  36. m.charAt(0)) {
  37. case "(":
  38. opengroups.push(new RegexToken(!0));
  39. break;
  40. case ")":
  41. groupToken = opengroups.pop(), opengroups.length > 0 ? opengroups[opengroups.length - 1].matches.push(groupToken) : currentToken.matches.push(groupToken);
  42. break;
  43. case "{":
  44. case "+":
  45. case "*":
  46. var quantifierToken = new RegexToken(!1, !0);
  47. m = m.replace(/[{}]/g, "");
  48. var mq = m.split(","), mq0 = isNaN(mq[0]) ? mq[0] : parseInt(mq[0]), mq1 = 1 === mq.length ? mq0 : isNaN(mq[1]) ? mq[1] : parseInt(mq[1]);
  49. if (quantifierToken.quantifier = {
  50. min: mq0,
  51. max: mq1
  52. }, opengroups.length > 0) {
  53. var matches = opengroups[opengroups.length - 1].matches;
  54. match = matches.pop(), match.isGroup || (groupToken = new RegexToken(!0), groupToken.matches.push(match),
  55. match = groupToken), matches.push(match), matches.push(quantifierToken);
  56. } else match = currentToken.matches.pop(), match.isGroup || (groupToken = new RegexToken(!0),
  57. groupToken.matches.push(match), match = groupToken), currentToken.matches.push(match),
  58. currentToken.matches.push(quantifierToken);
  59. break;
  60. default:
  61. opengroups.length > 0 ? opengroups[opengroups.length - 1].matches.push(m) : currentToken.matches.push(m);
  62. }
  63. currentToken.matches.length > 0 && opts.regexTokens.push(currentToken);
  64. }
  65. function validateRegexToken(token, fromGroup) {
  66. var isvalid = !1;
  67. fromGroup && (regexPart += "(", openGroupCount++);
  68. for (var mndx = 0; mndx < token.matches.length; mndx++) {
  69. var matchToken = token.matches[mndx];
  70. if (matchToken.isGroup === !0) isvalid = validateRegexToken(matchToken, !0); else if (matchToken.isQuantifier === !0) {
  71. var crrntndx = $.inArray(matchToken, token.matches), matchGroup = token.matches[crrntndx - 1], regexPartBak = regexPart;
  72. if (isNaN(matchToken.quantifier.max)) {
  73. for (;matchToken.repeaterPart && matchToken.repeaterPart !== regexPart && matchToken.repeaterPart.length > regexPart.length && !(isvalid = validateRegexToken(matchGroup, !0)); ) ;
  74. isvalid = isvalid || validateRegexToken(matchGroup, !0), isvalid && (matchToken.repeaterPart = regexPart),
  75. regexPart = regexPartBak + matchToken.quantifier.max;
  76. } else {
  77. for (var i = 0, qm = matchToken.quantifier.max - 1; qm > i && !(isvalid = validateRegexToken(matchGroup, !0)); i++) ;
  78. regexPart = regexPartBak + "{" + matchToken.quantifier.min + "," + matchToken.quantifier.max + "}";
  79. }
  80. } else if (void 0 !== matchToken.matches) for (var k = 0; k < matchToken.length && !(isvalid = validateRegexToken(matchToken[k], fromGroup)); k++) ; else {
  81. var testExp;
  82. if ("[" == matchToken.charAt(0)) {
  83. testExp = regexPart, testExp += matchToken;
  84. for (var j = 0; openGroupCount > j; j++) testExp += ")";
  85. var exp = new RegExp("^(" + testExp + ")$");
  86. isvalid = exp.test(bufferStr);
  87. } else for (var l = 0, tl = matchToken.length; tl > l; l++) if ("\\" !== matchToken.charAt(l)) {
  88. testExp = regexPart, testExp += matchToken.substr(0, l + 1), testExp = testExp.replace(/\|$/, "");
  89. for (var j = 0; openGroupCount > j; j++) testExp += ")";
  90. var exp = new RegExp("^(" + testExp + ")$");
  91. if (isvalid = exp.test(bufferStr)) break;
  92. }
  93. regexPart += matchToken;
  94. }
  95. if (isvalid) break;
  96. }
  97. return fromGroup && (regexPart += ")", openGroupCount--), isvalid;
  98. }
  99. var bufferStr, groupToken, cbuffer = maskset.buffer.slice(), regexPart = "", isValid = !1, openGroupCount = 0;
  100. null === opts.regexTokens && analyseRegex(), cbuffer.splice(pos, 0, chrs), bufferStr = cbuffer.join("");
  101. for (var i = 0; i < opts.regexTokens.length; i++) {
  102. var regexToken = opts.regexTokens[i];
  103. if (isValid = validateRegexToken(regexToken, regexToken.isGroup)) break;
  104. }
  105. return isValid;
  106. },
  107. cardinality: 1
  108. }
  109. }
  110. }
  111. }), Inputmask;
  112. });