inputmask.js 145 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742
  1. /*!
  2. * inputmask.js
  3. * https://github.com/RobinHerbots/Inputmask
  4. * Copyright (c) 2010 - 2018 Robin Herbots
  5. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. * Version: 4.0.3-beta.3
  7. */
  8. (function(factory) {
  9. if (typeof define === "function" && define.amd) {
  10. define([ "./dependencyLibs/inputmask.dependencyLib", "./global/window" ], factory);
  11. } else if (typeof exports === "object") {
  12. module.exports = factory(require("./dependencyLibs/inputmask.dependencyLib"), require("./global/window"));
  13. } else {
  14. window.Inputmask = factory(window.dependencyLib || jQuery, window);
  15. }
  16. })(function($, window, undefined) {
  17. var document = window.document, ua = navigator.userAgent, ie = ua.indexOf("MSIE ") > 0 || ua.indexOf("Trident/") > 0, mobile = isInputEventSupported("touchstart"), iemobile = /iemobile/i.test(ua), iphone = /iphone/i.test(ua) && !iemobile;
  18. function Inputmask(alias, options, internal) {
  19. if (!(this instanceof Inputmask)) {
  20. return new Inputmask(alias, options, internal);
  21. }
  22. this.el = undefined;
  23. this.events = {};
  24. this.maskset = undefined;
  25. this.refreshValue = false;
  26. if (internal !== true) {
  27. if ($.isPlainObject(alias)) {
  28. options = alias;
  29. } else {
  30. options = options || {};
  31. if (alias) options.alias = alias;
  32. }
  33. this.opts = $.extend(true, {}, this.defaults, options);
  34. this.noMasksCache = options && options.definitions !== undefined;
  35. this.userOptions = options || {};
  36. this.isRTL = this.opts.numericInput;
  37. resolveAlias(this.opts.alias, options, this.opts);
  38. }
  39. }
  40. Inputmask.prototype = {
  41. dataAttribute: "data-inputmask",
  42. defaults: {
  43. placeholder: "_",
  44. optionalmarker: [ "[", "]" ],
  45. quantifiermarker: [ "{", "}" ],
  46. groupmarker: [ "(", ")" ],
  47. alternatormarker: "|",
  48. escapeChar: "\\",
  49. mask: null,
  50. regex: null,
  51. oncomplete: $.noop,
  52. onincomplete: $.noop,
  53. oncleared: $.noop,
  54. repeat: 0,
  55. greedy: false,
  56. autoUnmask: false,
  57. removeMaskOnSubmit: false,
  58. clearMaskOnLostFocus: true,
  59. insertMode: true,
  60. clearIncomplete: false,
  61. alias: null,
  62. onKeyDown: $.noop,
  63. onBeforeMask: null,
  64. onBeforePaste: function(pastedValue, opts) {
  65. return $.isFunction(opts.onBeforeMask) ? opts.onBeforeMask.call(this, pastedValue, opts) : pastedValue;
  66. },
  67. onBeforeWrite: null,
  68. onUnMask: null,
  69. showMaskOnFocus: true,
  70. showMaskOnHover: true,
  71. onKeyValidation: $.noop,
  72. skipOptionalPartCharacter: " ",
  73. numericInput: false,
  74. rightAlign: false,
  75. undoOnEscape: true,
  76. radixPoint: "",
  77. _radixDance: false,
  78. groupSeparator: "",
  79. keepStatic: null,
  80. positionCaretOnTab: true,
  81. tabThrough: false,
  82. supportsInputType: [ "text", "tel", "password", "search" ],
  83. ignorables: [ 8, 9, 13, 19, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 93, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 0, 229 ],
  84. isComplete: null,
  85. preValidation: null,
  86. postValidation: null,
  87. staticDefinitionSymbol: undefined,
  88. jitMasking: false,
  89. nullable: true,
  90. inputEventOnly: false,
  91. noValuePatching: false,
  92. positionCaretOnClick: "lvp",
  93. casing: null,
  94. inputmode: "verbatim",
  95. colorMask: false,
  96. disablePredictiveText: false,
  97. importDataAttributes: true,
  98. shiftPositions: true
  99. },
  100. definitions: {
  101. 9: {
  102. validator: "[0-9\uff11-\uff19]",
  103. definitionSymbol: "*"
  104. },
  105. a: {
  106. validator: "[A-Za-z\u0410-\u044f\u0401\u0451\xc0-\xff\xb5]",
  107. definitionSymbol: "*"
  108. },
  109. "*": {
  110. validator: "[0-9\uff11-\uff19A-Za-z\u0410-\u044f\u0401\u0451\xc0-\xff\xb5]"
  111. }
  112. },
  113. aliases: {},
  114. masksCache: {},
  115. mask: function(elems) {
  116. var that = this;
  117. function importAttributeOptions(npt, opts, userOptions, dataAttribute) {
  118. if (opts.importDataAttributes === true) {
  119. var attrOptions = npt.getAttribute(dataAttribute), option, dataoptions, optionData, p;
  120. function importOption(option, optionData) {
  121. optionData = optionData !== undefined ? optionData : npt.getAttribute(dataAttribute + "-" + option);
  122. if (optionData !== null) {
  123. if (typeof optionData === "string") {
  124. if (option.indexOf("on") === 0) optionData = window[optionData]; else if (optionData === "false") optionData = false; else if (optionData === "true") optionData = true;
  125. }
  126. userOptions[option] = optionData;
  127. }
  128. }
  129. if (attrOptions && attrOptions !== "") {
  130. attrOptions = attrOptions.replace(/'/g, '"');
  131. dataoptions = JSON.parse("{" + attrOptions + "}");
  132. }
  133. if (dataoptions) {
  134. optionData = undefined;
  135. for (p in dataoptions) {
  136. if (p.toLowerCase() === "alias") {
  137. optionData = dataoptions[p];
  138. break;
  139. }
  140. }
  141. }
  142. importOption("alias", optionData);
  143. if (userOptions.alias) {
  144. resolveAlias(userOptions.alias, userOptions, opts);
  145. }
  146. for (option in opts) {
  147. if (dataoptions) {
  148. optionData = undefined;
  149. for (p in dataoptions) {
  150. if (p.toLowerCase() === option.toLowerCase()) {
  151. optionData = dataoptions[p];
  152. break;
  153. }
  154. }
  155. }
  156. importOption(option, optionData);
  157. }
  158. }
  159. $.extend(true, opts, userOptions);
  160. if (npt.dir === "rtl" || opts.rightAlign) {
  161. npt.style.textAlign = "right";
  162. }
  163. if (npt.dir === "rtl" || opts.numericInput) {
  164. npt.dir = "ltr";
  165. npt.removeAttribute("dir");
  166. opts.isRTL = true;
  167. }
  168. return Object.keys(userOptions).length;
  169. }
  170. if (typeof elems === "string") {
  171. elems = document.getElementById(elems) || document.querySelectorAll(elems);
  172. }
  173. elems = elems.nodeName ? [ elems ] : elems;
  174. $.each(elems, function(ndx, el) {
  175. var scopedOpts = $.extend(true, {}, that.opts);
  176. if (importAttributeOptions(el, scopedOpts, $.extend(true, {}, that.userOptions), that.dataAttribute)) {
  177. var maskset = generateMaskSet(scopedOpts, that.noMasksCache);
  178. if (maskset !== undefined) {
  179. if (el.inputmask !== undefined) {
  180. el.inputmask.opts.autoUnmask = true;
  181. el.inputmask.remove();
  182. }
  183. el.inputmask = new Inputmask(undefined, undefined, true);
  184. el.inputmask.opts = scopedOpts;
  185. el.inputmask.noMasksCache = that.noMasksCache;
  186. el.inputmask.userOptions = $.extend(true, {}, that.userOptions);
  187. el.inputmask.isRTL = scopedOpts.isRTL || scopedOpts.numericInput;
  188. el.inputmask.el = el;
  189. el.inputmask.maskset = maskset;
  190. $.data(el, "_inputmask_opts", scopedOpts);
  191. maskScope.call(el.inputmask, {
  192. action: "mask"
  193. });
  194. }
  195. }
  196. });
  197. return elems && elems[0] ? elems[0].inputmask || this : this;
  198. },
  199. option: function(options, noremask) {
  200. if (typeof options === "string") {
  201. return this.opts[options];
  202. } else if (typeof options === "object") {
  203. $.extend(this.userOptions, options);
  204. if (this.el && noremask !== true) {
  205. this.mask(this.el);
  206. }
  207. return this;
  208. }
  209. },
  210. unmaskedvalue: function(value) {
  211. this.maskset = this.maskset || generateMaskSet(this.opts, this.noMasksCache);
  212. return maskScope.call(this, {
  213. action: "unmaskedvalue",
  214. value: value
  215. });
  216. },
  217. remove: function() {
  218. return maskScope.call(this, {
  219. action: "remove"
  220. });
  221. },
  222. getemptymask: function() {
  223. this.maskset = this.maskset || generateMaskSet(this.opts, this.noMasksCache);
  224. return maskScope.call(this, {
  225. action: "getemptymask"
  226. });
  227. },
  228. hasMaskedValue: function() {
  229. return !this.opts.autoUnmask;
  230. },
  231. isComplete: function() {
  232. this.maskset = this.maskset || generateMaskSet(this.opts, this.noMasksCache);
  233. return maskScope.call(this, {
  234. action: "isComplete"
  235. });
  236. },
  237. getmetadata: function() {
  238. this.maskset = this.maskset || generateMaskSet(this.opts, this.noMasksCache);
  239. return maskScope.call(this, {
  240. action: "getmetadata"
  241. });
  242. },
  243. isValid: function(value) {
  244. this.maskset = this.maskset || generateMaskSet(this.opts, this.noMasksCache);
  245. return maskScope.call(this, {
  246. action: "isValid",
  247. value: value
  248. });
  249. },
  250. format: function(value, metadata) {
  251. this.maskset = this.maskset || generateMaskSet(this.opts, this.noMasksCache);
  252. return maskScope.call(this, {
  253. action: "format",
  254. value: value,
  255. metadata: metadata
  256. });
  257. },
  258. setValue: function(value) {
  259. if (this.el) {
  260. $(this.el).trigger("setvalue", [ value ]);
  261. }
  262. },
  263. analyseMask: function(mask, regexMask, opts) {
  264. var tokenizer = /(?:[?*+]|\{[0-9\+\*]+(?:,[0-9\+\*]*)?(?:\|[0-9\+\*]*)?\})|[^.?*+^${[]()|\\]+|./g, regexTokenizer = /\[\^?]?(?:[^\\\]]+|\\[\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, escaped = false, currentToken = new MaskToken(), match, m, openenings = [], maskTokens = [], openingToken, currentOpeningToken, alternator, lastMatch, groupToken;
  265. function MaskToken(isGroup, isOptional, isQuantifier, isAlternator) {
  266. this.matches = [];
  267. this.openGroup = isGroup || false;
  268. this.alternatorGroup = false;
  269. this.isGroup = isGroup || false;
  270. this.isOptional = isOptional || false;
  271. this.isQuantifier = isQuantifier || false;
  272. this.isAlternator = isAlternator || false;
  273. this.quantifier = {
  274. min: 1,
  275. max: 1
  276. };
  277. }
  278. function insertTestDefinition(mtoken, element, position) {
  279. position = position !== undefined ? position : mtoken.matches.length;
  280. var prevMatch = mtoken.matches[position - 1];
  281. if (regexMask) {
  282. if (element.indexOf("[") === 0 || escaped && /\\d|\\s|\\w]/i.test(element) || element === ".") {
  283. mtoken.matches.splice(position++, 0, {
  284. fn: new RegExp(element, opts.casing ? "i" : ""),
  285. optionality: false,
  286. newBlockMarker: prevMatch === undefined ? "master" : prevMatch.def !== element,
  287. casing: null,
  288. def: element,
  289. placeholder: undefined,
  290. nativeDef: element
  291. });
  292. } else {
  293. if (escaped) element = element[element.length - 1];
  294. $.each(element.split(""), function(ndx, lmnt) {
  295. prevMatch = mtoken.matches[position - 1];
  296. mtoken.matches.splice(position++, 0, {
  297. fn: null,
  298. optionality: false,
  299. newBlockMarker: prevMatch === undefined ? "master" : prevMatch.def !== lmnt && prevMatch.fn !== null,
  300. casing: null,
  301. def: opts.staticDefinitionSymbol || lmnt,
  302. placeholder: opts.staticDefinitionSymbol !== undefined ? lmnt : undefined,
  303. nativeDef: (escaped ? "'" : "") + lmnt
  304. });
  305. });
  306. }
  307. escaped = false;
  308. } else {
  309. var maskdef = (opts.definitions ? opts.definitions[element] : undefined) || Inputmask.prototype.definitions[element];
  310. if (maskdef && !escaped) {
  311. mtoken.matches.splice(position++, 0, {
  312. fn: maskdef.validator ? typeof maskdef.validator == "string" ? new RegExp(maskdef.validator, opts.casing ? "i" : "") : new function() {
  313. this.test = maskdef.validator;
  314. }() : new RegExp("."),
  315. optionality: false,
  316. newBlockMarker: prevMatch === undefined ? "master" : prevMatch.def !== (maskdef.definitionSymbol || element),
  317. casing: maskdef.casing,
  318. def: maskdef.definitionSymbol || element,
  319. placeholder: maskdef.placeholder,
  320. nativeDef: element
  321. });
  322. } else {
  323. mtoken.matches.splice(position++, 0, {
  324. fn: null,
  325. optionality: false,
  326. newBlockMarker: prevMatch === undefined ? "master" : prevMatch.def !== element && prevMatch.fn !== null,
  327. casing: null,
  328. def: opts.staticDefinitionSymbol || element,
  329. placeholder: opts.staticDefinitionSymbol !== undefined ? element : undefined,
  330. nativeDef: (escaped ? "'" : "") + element
  331. });
  332. escaped = false;
  333. }
  334. }
  335. }
  336. function verifyGroupMarker(maskToken) {
  337. if (maskToken && maskToken.matches) {
  338. $.each(maskToken.matches, function(ndx, token) {
  339. var nextToken = maskToken.matches[ndx + 1];
  340. if ((nextToken === undefined || (nextToken.matches === undefined || nextToken.isQuantifier === false)) && token && token.isGroup) {
  341. token.isGroup = false;
  342. if (!regexMask) {
  343. insertTestDefinition(token, opts.groupmarker[0], 0);
  344. if (token.openGroup !== true) {
  345. insertTestDefinition(token, opts.groupmarker[1]);
  346. }
  347. }
  348. }
  349. verifyGroupMarker(token);
  350. });
  351. }
  352. }
  353. function defaultCase() {
  354. if (openenings.length > 0) {
  355. currentOpeningToken = openenings[openenings.length - 1];
  356. insertTestDefinition(currentOpeningToken, m);
  357. if (currentOpeningToken.isAlternator) {
  358. alternator = openenings.pop();
  359. for (var mndx = 0; mndx < alternator.matches.length; mndx++) {
  360. if (alternator.matches[mndx].isGroup) alternator.matches[mndx].isGroup = false;
  361. }
  362. if (openenings.length > 0) {
  363. currentOpeningToken = openenings[openenings.length - 1];
  364. currentOpeningToken.matches.push(alternator);
  365. } else {
  366. currentToken.matches.push(alternator);
  367. }
  368. }
  369. } else {
  370. insertTestDefinition(currentToken, m);
  371. }
  372. }
  373. function reverseTokens(maskToken) {
  374. function reverseStatic(st) {
  375. if (st === opts.optionalmarker[0]) st = opts.optionalmarker[1]; else if (st === opts.optionalmarker[1]) st = opts.optionalmarker[0]; else if (st === opts.groupmarker[0]) st = opts.groupmarker[1]; else if (st === opts.groupmarker[1]) st = opts.groupmarker[0];
  376. return st;
  377. }
  378. maskToken.matches = maskToken.matches.reverse();
  379. for (var match in maskToken.matches) {
  380. if (maskToken.matches.hasOwnProperty(match)) {
  381. var intMatch = parseInt(match);
  382. if (maskToken.matches[match].isQuantifier && maskToken.matches[intMatch + 1] && maskToken.matches[intMatch + 1].isGroup) {
  383. var qt = maskToken.matches[match];
  384. maskToken.matches.splice(match, 1);
  385. maskToken.matches.splice(intMatch + 1, 0, qt);
  386. }
  387. if (maskToken.matches[match].matches !== undefined) {
  388. maskToken.matches[match] = reverseTokens(maskToken.matches[match]);
  389. } else {
  390. maskToken.matches[match] = reverseStatic(maskToken.matches[match]);
  391. }
  392. }
  393. }
  394. return maskToken;
  395. }
  396. function groupify(matches) {
  397. var groupToken = new MaskToken(true);
  398. groupToken.openGroup = false;
  399. groupToken.matches = matches;
  400. return groupToken;
  401. }
  402. if (regexMask) {
  403. opts.optionalmarker[0] = undefined;
  404. opts.optionalmarker[1] = undefined;
  405. }
  406. while (match = regexMask ? regexTokenizer.exec(mask) : tokenizer.exec(mask)) {
  407. m = match[0];
  408. if (regexMask) {
  409. switch (m.charAt(0)) {
  410. case "?":
  411. m = "{0,1}";
  412. break;
  413. case "+":
  414. case "*":
  415. m = "{" + m + "}";
  416. break;
  417. }
  418. }
  419. if (escaped) {
  420. defaultCase();
  421. continue;
  422. }
  423. switch (m.charAt(0)) {
  424. case "(?=":
  425. break;
  426. case "(?!":
  427. break;
  428. case "(?<=":
  429. break;
  430. case "(?<!":
  431. break;
  432. case opts.escapeChar:
  433. escaped = true;
  434. if (regexMask) {
  435. defaultCase();
  436. }
  437. break;
  438. case opts.optionalmarker[1]:
  439. case opts.groupmarker[1]:
  440. openingToken = openenings.pop();
  441. openingToken.openGroup = false;
  442. if (openingToken !== undefined) {
  443. if (openenings.length > 0) {
  444. currentOpeningToken = openenings[openenings.length - 1];
  445. currentOpeningToken.matches.push(openingToken);
  446. if (currentOpeningToken.isAlternator) {
  447. alternator = openenings.pop();
  448. for (var mndx = 0; mndx < alternator.matches.length; mndx++) {
  449. alternator.matches[mndx].isGroup = false;
  450. alternator.matches[mndx].alternatorGroup = false;
  451. }
  452. if (openenings.length > 0) {
  453. currentOpeningToken = openenings[openenings.length - 1];
  454. currentOpeningToken.matches.push(alternator);
  455. } else {
  456. currentToken.matches.push(alternator);
  457. }
  458. }
  459. } else {
  460. currentToken.matches.push(openingToken);
  461. }
  462. } else defaultCase();
  463. break;
  464. case opts.optionalmarker[0]:
  465. openenings.push(new MaskToken(false, true));
  466. break;
  467. case opts.groupmarker[0]:
  468. openenings.push(new MaskToken(true));
  469. break;
  470. case opts.quantifiermarker[0]:
  471. var quantifier = new MaskToken(false, false, true);
  472. m = m.replace(/[{}]/g, "");
  473. var mqj = m.split("|"), mq = mqj[0].split(","), mq0 = isNaN(mq[0]) ? mq[0] : parseInt(mq[0]), mq1 = mq.length === 1 ? mq0 : isNaN(mq[1]) ? mq[1] : parseInt(mq[1]);
  474. if (mq0 === "*" || mq0 === "+") {
  475. mq0 = mq1 === "*" ? 0 : 1;
  476. }
  477. quantifier.quantifier = {
  478. min: mq0,
  479. max: mq1,
  480. jit: mqj[1]
  481. };
  482. var matches = openenings.length > 0 ? openenings[openenings.length - 1].matches : currentToken.matches;
  483. match = matches.pop();
  484. if (match.isAlternator) {
  485. matches.push(match);
  486. matches = match.matches;
  487. var groupToken = new MaskToken(true);
  488. var tmpMatch = matches.pop();
  489. matches.push(groupToken);
  490. matches = groupToken.matches;
  491. match = tmpMatch;
  492. }
  493. if (!match.isGroup) {
  494. match = groupify([ match ]);
  495. }
  496. matches.push(match);
  497. matches.push(quantifier);
  498. break;
  499. case opts.alternatormarker:
  500. function groupQuantifier(matches) {
  501. var lastMatch = matches.pop();
  502. if (lastMatch.isQuantifier) {
  503. lastMatch = groupify([ matches.pop(), lastMatch ]);
  504. }
  505. return lastMatch;
  506. }
  507. if (openenings.length > 0) {
  508. currentOpeningToken = openenings[openenings.length - 1];
  509. var subToken = currentOpeningToken.matches[currentOpeningToken.matches.length - 1];
  510. if (currentOpeningToken.openGroup && (subToken.matches === undefined || subToken.isGroup === false && subToken.isAlternator === false)) {
  511. lastMatch = openenings.pop();
  512. } else {
  513. lastMatch = groupQuantifier(currentOpeningToken.matches);
  514. }
  515. } else {
  516. lastMatch = groupQuantifier(currentToken.matches);
  517. }
  518. if (lastMatch.isAlternator) {
  519. openenings.push(lastMatch);
  520. } else {
  521. if (lastMatch.alternatorGroup) {
  522. alternator = openenings.pop();
  523. lastMatch.alternatorGroup = false;
  524. } else {
  525. alternator = new MaskToken(false, false, false, true);
  526. }
  527. alternator.matches.push(lastMatch);
  528. openenings.push(alternator);
  529. if (lastMatch.openGroup) {
  530. lastMatch.openGroup = false;
  531. var alternatorGroup = new MaskToken(true);
  532. alternatorGroup.alternatorGroup = true;
  533. openenings.push(alternatorGroup);
  534. }
  535. }
  536. break;
  537. default:
  538. defaultCase();
  539. }
  540. }
  541. while (openenings.length > 0) {
  542. openingToken = openenings.pop();
  543. currentToken.matches.push(openingToken);
  544. }
  545. if (currentToken.matches.length > 0) {
  546. verifyGroupMarker(currentToken);
  547. maskTokens.push(currentToken);
  548. }
  549. if (opts.numericInput || opts.isRTL) {
  550. reverseTokens(maskTokens[0]);
  551. }
  552. return maskTokens;
  553. }
  554. };
  555. Inputmask.extendDefaults = function(options) {
  556. $.extend(true, Inputmask.prototype.defaults, options);
  557. };
  558. Inputmask.extendDefinitions = function(definition) {
  559. $.extend(true, Inputmask.prototype.definitions, definition);
  560. };
  561. Inputmask.extendAliases = function(alias) {
  562. $.extend(true, Inputmask.prototype.aliases, alias);
  563. };
  564. Inputmask.format = function(value, options, metadata) {
  565. return Inputmask(options).format(value, metadata);
  566. };
  567. Inputmask.unmask = function(value, options) {
  568. return Inputmask(options).unmaskedvalue(value);
  569. };
  570. Inputmask.isValid = function(value, options) {
  571. return Inputmask(options).isValid(value);
  572. };
  573. Inputmask.remove = function(elems) {
  574. if (typeof elems === "string") {
  575. elems = document.getElementById(elems) || document.querySelectorAll(elems);
  576. }
  577. elems = elems.nodeName ? [ elems ] : elems;
  578. $.each(elems, function(ndx, el) {
  579. if (el.inputmask) el.inputmask.remove();
  580. });
  581. };
  582. Inputmask.setValue = function(elems, value) {
  583. if (typeof elems === "string") {
  584. elems = document.getElementById(elems) || document.querySelectorAll(elems);
  585. }
  586. elems = elems.nodeName ? [ elems ] : elems;
  587. $.each(elems, function(ndx, el) {
  588. if (el.inputmask) el.inputmask.setValue(value); else $(el).trigger("setvalue", [ value ]);
  589. });
  590. };
  591. Inputmask.escapeRegex = function(str) {
  592. var specials = [ "/", ".", "*", "+", "?", "|", "(", ")", "[", "]", "{", "}", "\\", "$", "^" ];
  593. return str.replace(new RegExp("(\\" + specials.join("|\\") + ")", "gim"), "\\$1");
  594. };
  595. Inputmask.keyCode = {
  596. BACKSPACE: 8,
  597. BACKSPACE_SAFARI: 127,
  598. DELETE: 46,
  599. DOWN: 40,
  600. END: 35,
  601. ENTER: 13,
  602. ESCAPE: 27,
  603. HOME: 36,
  604. INSERT: 45,
  605. LEFT: 37,
  606. PAGE_DOWN: 34,
  607. PAGE_UP: 33,
  608. RIGHT: 39,
  609. SPACE: 32,
  610. TAB: 9,
  611. UP: 38,
  612. X: 88,
  613. CONTROL: 17
  614. };
  615. Inputmask.dependencyLib = $;
  616. function resolveAlias(aliasStr, options, opts) {
  617. var aliasDefinition = Inputmask.prototype.aliases[aliasStr];
  618. if (aliasDefinition) {
  619. if (aliasDefinition.alias) resolveAlias(aliasDefinition.alias, undefined, opts);
  620. $.extend(true, opts, aliasDefinition);
  621. $.extend(true, opts, options);
  622. return true;
  623. } else if (opts.mask === null) {
  624. opts.mask = aliasStr;
  625. }
  626. return false;
  627. }
  628. function generateMaskSet(opts, nocache) {
  629. function generateMask(mask, metadata, opts) {
  630. var regexMask = false;
  631. if (mask === null || mask === "") {
  632. regexMask = opts.regex !== null;
  633. if (regexMask) {
  634. mask = opts.regex;
  635. mask = mask.replace(/^(\^)(.*)(\$)$/, "$2");
  636. } else {
  637. regexMask = true;
  638. mask = ".*";
  639. }
  640. }
  641. if (mask.length === 1 && opts.greedy === false && opts.repeat !== 0) {
  642. opts.placeholder = "";
  643. }
  644. if (opts.repeat > 0 || opts.repeat === "*" || opts.repeat === "+") {
  645. var repeatStart = opts.repeat === "*" ? 0 : opts.repeat === "+" ? 1 : opts.repeat;
  646. mask = opts.groupmarker[0] + mask + opts.groupmarker[1] + opts.quantifiermarker[0] + repeatStart + "," + opts.repeat + opts.quantifiermarker[1];
  647. }
  648. var masksetDefinition, maskdefKey = regexMask ? "regex_" + opts.regex : opts.numericInput ? mask.split("").reverse().join("") : mask;
  649. if (Inputmask.prototype.masksCache[maskdefKey] === undefined || nocache === true) {
  650. masksetDefinition = {
  651. mask: mask,
  652. maskToken: Inputmask.prototype.analyseMask(mask, regexMask, opts),
  653. validPositions: {},
  654. _buffer: undefined,
  655. buffer: undefined,
  656. tests: {},
  657. excludes: {},
  658. metadata: metadata,
  659. maskLength: undefined
  660. };
  661. if (nocache !== true) {
  662. Inputmask.prototype.masksCache[maskdefKey] = masksetDefinition;
  663. masksetDefinition = $.extend(true, {}, Inputmask.prototype.masksCache[maskdefKey]);
  664. }
  665. } else masksetDefinition = $.extend(true, {}, Inputmask.prototype.masksCache[maskdefKey]);
  666. return masksetDefinition;
  667. }
  668. var ms;
  669. if ($.isFunction(opts.mask)) {
  670. opts.mask = opts.mask(opts);
  671. }
  672. if ($.isArray(opts.mask)) {
  673. if (opts.mask.length > 1) {
  674. if (opts.keepStatic === null) {
  675. opts.keepStatic = "auto";
  676. for (var i = 0; i < opts.mask.length; i++) {
  677. if (opts.mask[i].charAt(0) !== opts.mask[0].charAt(0)) {
  678. opts.keepStatic = true;
  679. break;
  680. }
  681. }
  682. }
  683. var altMask = opts.groupmarker[0];
  684. $.each(opts.isRTL ? opts.mask.reverse() : opts.mask, function(ndx, msk) {
  685. if (altMask.length > 1) {
  686. altMask += opts.groupmarker[1] + opts.alternatormarker + opts.groupmarker[0];
  687. }
  688. if (msk.mask !== undefined && !$.isFunction(msk.mask)) {
  689. altMask += msk.mask;
  690. } else {
  691. altMask += msk;
  692. }
  693. });
  694. altMask += opts.groupmarker[1];
  695. return generateMask(altMask, opts.mask, opts);
  696. } else opts.mask = opts.mask.pop();
  697. }
  698. if (opts.mask && opts.mask.mask !== undefined && !$.isFunction(opts.mask.mask)) {
  699. ms = generateMask(opts.mask.mask, opts.mask, opts);
  700. } else {
  701. ms = generateMask(opts.mask, opts.mask, opts);
  702. }
  703. return ms;
  704. }
  705. function isInputEventSupported(eventName) {
  706. var el = document.createElement("input"), evName = "on" + eventName, isSupported = evName in el;
  707. if (!isSupported) {
  708. el.setAttribute(evName, "return;");
  709. isSupported = typeof el[evName] === "function";
  710. }
  711. el = null;
  712. return isSupported;
  713. }
  714. function maskScope(actionObj, maskset, opts) {
  715. maskset = maskset || this.maskset;
  716. opts = opts || this.opts;
  717. var inputmask = this, el = this.el, isRTL = this.isRTL, undoValue, $el, skipKeyPressEvent = false, skipInputEvent = false, ignorable = false, maxLength, mouseEnter = false, colorMask, originalPlaceholder;
  718. function getMaskTemplate(baseOnInput, minimalPos, includeMode, noJit, clearOptionalTail) {
  719. var greedy = opts.greedy;
  720. if (clearOptionalTail) opts.greedy = false;
  721. minimalPos = minimalPos || 0;
  722. var maskTemplate = [], ndxIntlzr, pos = 0, test, testPos, lvp = getLastValidPosition();
  723. do {
  724. if (baseOnInput === true && getMaskSet().validPositions[pos]) {
  725. testPos = clearOptionalTail && getMaskSet().validPositions[pos].match.optionality === true && getMaskSet().validPositions[pos + 1] === undefined && (getMaskSet().validPositions[pos].generatedInput === true || getMaskSet().validPositions[pos].input == opts.skipOptionalPartCharacter && pos > 0) ? determineTestTemplate(pos, getTests(pos, ndxIntlzr, pos - 1)) : getMaskSet().validPositions[pos];
  726. test = testPos.match;
  727. ndxIntlzr = testPos.locator.slice();
  728. maskTemplate.push(includeMode === true ? testPos.input : includeMode === false ? test.nativeDef : getPlaceholder(pos, test));
  729. } else {
  730. testPos = getTestTemplate(pos, ndxIntlzr, pos - 1);
  731. test = testPos.match;
  732. ndxIntlzr = testPos.locator.slice();
  733. var jitMasking = noJit === true ? false : opts.jitMasking !== false ? opts.jitMasking : test.jit;
  734. if (jitMasking === false || jitMasking === undefined || typeof jitMasking === "number" && isFinite(jitMasking) && jitMasking > pos) {
  735. maskTemplate.push(includeMode === false ? test.nativeDef : getPlaceholder(pos, test));
  736. }
  737. }
  738. if (opts.keepStatic === "auto") {
  739. if (test.newBlockMarker && test.fn !== null) {
  740. opts.keepStatic = pos - 1;
  741. }
  742. }
  743. pos++;
  744. } while ((maxLength === undefined || pos < maxLength) && (test.fn !== null || test.def !== "") || minimalPos > pos);
  745. if (maskTemplate[maskTemplate.length - 1] === "") {
  746. maskTemplate.pop();
  747. }
  748. if (includeMode !== false || getMaskSet().maskLength === undefined) getMaskSet().maskLength = pos - 1;
  749. opts.greedy = greedy;
  750. return maskTemplate;
  751. }
  752. function getMaskSet() {
  753. return maskset;
  754. }
  755. function resetMaskSet(soft) {
  756. var maskset = getMaskSet();
  757. maskset.buffer = undefined;
  758. if (soft !== true) {
  759. maskset.validPositions = {};
  760. maskset.p = 0;
  761. }
  762. }
  763. function getLastValidPosition(closestTo, strict, validPositions) {
  764. var before = -1, after = -1, valids = validPositions || getMaskSet().validPositions;
  765. if (closestTo === undefined) closestTo = -1;
  766. for (var posNdx in valids) {
  767. var psNdx = parseInt(posNdx);
  768. if (valids[psNdx] && (strict || valids[psNdx].generatedInput !== true)) {
  769. if (psNdx <= closestTo) before = psNdx;
  770. if (psNdx >= closestTo) after = psNdx;
  771. }
  772. }
  773. return before === -1 || before == closestTo ? after : after == -1 ? before : closestTo - before < after - closestTo ? before : after;
  774. }
  775. function getDecisionTaker(tst) {
  776. var decisionTaker = tst.locator[tst.alternation];
  777. if (typeof decisionTaker == "string" && decisionTaker.length > 0) {
  778. decisionTaker = decisionTaker.split(",")[0];
  779. }
  780. return decisionTaker !== undefined ? decisionTaker.toString() : "";
  781. }
  782. function getLocator(tst, align) {
  783. var locator = (tst.alternation != undefined ? tst.mloc[getDecisionTaker(tst)] : tst.locator).join("");
  784. if (locator !== "") while (locator.length < align) locator += "0";
  785. return locator;
  786. }
  787. function determineTestTemplate(pos, tests) {
  788. pos = pos > 0 ? pos - 1 : 0;
  789. var altTest = getTest(pos), targetLocator = getLocator(altTest), tstLocator, closest, bestMatch;
  790. for (var ndx = 0; ndx < tests.length; ndx++) {
  791. var tst = tests[ndx];
  792. tstLocator = getLocator(tst, targetLocator.length);
  793. var distance = Math.abs(tstLocator - targetLocator);
  794. if (closest === undefined || tstLocator !== "" && distance < closest || bestMatch && bestMatch.match.optionality && bestMatch.match.newBlockMarker === "master" && (!tst.match.optionality || !tst.match.newBlockMarker) || bestMatch && bestMatch.match.optionalQuantifier && !tst.match.optionalQuantifier) {
  795. closest = distance;
  796. bestMatch = tst;
  797. }
  798. }
  799. return bestMatch;
  800. }
  801. function getTestTemplate(pos, ndxIntlzr, tstPs) {
  802. return getMaskSet().validPositions[pos] || determineTestTemplate(pos, getTests(pos, ndxIntlzr ? ndxIntlzr.slice() : ndxIntlzr, tstPs));
  803. }
  804. function getTest(pos, tests) {
  805. if (getMaskSet().validPositions[pos]) {
  806. return getMaskSet().validPositions[pos];
  807. }
  808. return (tests || getTests(pos))[0];
  809. }
  810. function positionCanMatchDefinition(pos, def) {
  811. var valid = false, tests = getTests(pos);
  812. for (var tndx = 0; tndx < tests.length; tndx++) {
  813. if (tests[tndx].match && tests[tndx].match.def === def) {
  814. valid = true;
  815. break;
  816. }
  817. }
  818. return valid;
  819. }
  820. function getTests(pos, ndxIntlzr, tstPs) {
  821. var maskTokens = getMaskSet().maskToken, testPos = ndxIntlzr ? tstPs : 0, ndxInitializer = ndxIntlzr ? ndxIntlzr.slice() : [ 0 ], matches = [], insertStop = false, latestMatch, cacheDependency = ndxIntlzr ? ndxIntlzr.join("") : "", offset = 0;
  822. function resolveTestFromToken(maskToken, ndxInitializer, loopNdx, quantifierRecurse) {
  823. function handleMatch(match, loopNdx, quantifierRecurse) {
  824. function isFirstMatch(latestMatch, tokenGroup) {
  825. var firstMatch = $.inArray(latestMatch, tokenGroup.matches) === 0;
  826. if (!firstMatch) {
  827. $.each(tokenGroup.matches, function(ndx, match) {
  828. if (match.isQuantifier === true) firstMatch = isFirstMatch(latestMatch, tokenGroup.matches[ndx - 1]); else if (match.hasOwnProperty("matches")) firstMatch = isFirstMatch(latestMatch, match);
  829. if (firstMatch) return false;
  830. });
  831. }
  832. return firstMatch;
  833. }
  834. function resolveNdxInitializer(pos, alternateNdx, targetAlternation) {
  835. var bestMatch, indexPos;
  836. if (getMaskSet().tests[pos] || getMaskSet().validPositions[pos]) {
  837. $.each(getMaskSet().tests[pos] || [ getMaskSet().validPositions[pos] ], function(ndx, lmnt) {
  838. if (lmnt.mloc[alternateNdx]) {
  839. bestMatch = lmnt;
  840. return false;
  841. }
  842. var alternation = targetAlternation !== undefined ? targetAlternation : lmnt.alternation, ndxPos = lmnt.locator[alternation] !== undefined ? lmnt.locator[alternation].toString().indexOf(alternateNdx) : -1;
  843. if ((indexPos === undefined || ndxPos < indexPos) && ndxPos !== -1) {
  844. bestMatch = lmnt;
  845. indexPos = ndxPos;
  846. }
  847. });
  848. }
  849. if (bestMatch) {
  850. var bestMatchAltIndex = bestMatch.locator[bestMatch.alternation];
  851. var locator = bestMatch.mloc[alternateNdx] || bestMatch.mloc[bestMatchAltIndex] || bestMatch.locator;
  852. return locator.slice((targetAlternation !== undefined ? targetAlternation : bestMatch.alternation) + 1);
  853. } else {
  854. return targetAlternation !== undefined ? resolveNdxInitializer(pos, alternateNdx) : undefined;
  855. }
  856. }
  857. function isSubsetOf(source, target) {
  858. function expand(pattern) {
  859. var expanded = [], start, end;
  860. for (var i = 0, l = pattern.length; i < l; i++) {
  861. if (pattern.charAt(i) === "-") {
  862. end = pattern.charCodeAt(i + 1);
  863. while (++start < end) expanded.push(String.fromCharCode(start));
  864. } else {
  865. start = pattern.charCodeAt(i);
  866. expanded.push(pattern.charAt(i));
  867. }
  868. }
  869. return expanded.join("");
  870. }
  871. if (opts.regex && source.match.fn !== null && target.match.fn !== null) {
  872. return expand(target.match.def.replace(/[\[\]]/g, "")).indexOf(expand(source.match.def.replace(/[\[\]]/g, ""))) !== -1;
  873. }
  874. return source.match.def === target.match.nativeDef;
  875. }
  876. function staticCanMatchDefinition(source, target) {
  877. var sloc = source.locator.slice(source.alternation).join(""), tloc = target.locator.slice(target.alternation).join(""), canMatch = sloc == tloc, canMatch = canMatch && source.match.fn === null && target.match.fn !== null ? target.match.fn.test(source.match.def, getMaskSet(), pos, false, opts, false) : false;
  878. return canMatch;
  879. }
  880. function setMergeLocators(targetMatch, altMatch) {
  881. if (altMatch === undefined || targetMatch.alternation === altMatch.alternation && targetMatch.locator[targetMatch.alternation].toString().indexOf(altMatch.locator[altMatch.alternation]) === -1) {
  882. targetMatch.mloc = targetMatch.mloc || {};
  883. var locNdx = targetMatch.locator[targetMatch.alternation];
  884. if (locNdx === undefined) targetMatch.alternation = undefined; else {
  885. if (typeof locNdx === "string") locNdx = locNdx.split(",")[0];
  886. if (targetMatch.mloc[locNdx] === undefined) targetMatch.mloc[locNdx] = targetMatch.locator.slice();
  887. if (altMatch !== undefined) {
  888. for (var ndx in altMatch.mloc) {
  889. if (typeof ndx === "string") ndx = ndx.split(",")[0];
  890. if (targetMatch.mloc[ndx] === undefined) targetMatch.mloc[ndx] = altMatch.mloc[ndx];
  891. }
  892. targetMatch.locator[targetMatch.alternation] = Object.keys(targetMatch.mloc).join(",");
  893. }
  894. return true;
  895. }
  896. }
  897. return false;
  898. }
  899. if (testPos > 500 && quantifierRecurse !== undefined) {
  900. throw "Inputmask: There is probably an error in your mask definition or in the code. Create an issue on github with an example of the mask you are using. " + getMaskSet().mask;
  901. }
  902. if (testPos === pos && match.matches === undefined) {
  903. matches.push({
  904. match: match,
  905. locator: loopNdx.reverse(),
  906. cd: cacheDependency,
  907. mloc: {}
  908. });
  909. return true;
  910. } else if (match.matches !== undefined) {
  911. if (match.isGroup && quantifierRecurse !== match) {
  912. match = handleMatch(maskToken.matches[$.inArray(match, maskToken.matches) + 1], loopNdx, quantifierRecurse);
  913. if (match) return true;
  914. } else if (match.isOptional) {
  915. var optionalToken = match;
  916. match = resolveTestFromToken(match, ndxInitializer, loopNdx, quantifierRecurse);
  917. if (match) {
  918. $.each(matches, function(ndx, mtch) {
  919. mtch.match.optionality = true;
  920. });
  921. latestMatch = matches[matches.length - 1].match;
  922. if (quantifierRecurse === undefined && isFirstMatch(latestMatch, optionalToken)) {
  923. insertStop = true;
  924. testPos = pos;
  925. } else return true;
  926. }
  927. } else if (match.isAlternator) {
  928. var alternateToken = match, malternateMatches = [], maltMatches, currentMatches = matches.slice(), loopNdxCnt = loopNdx.length;
  929. var altIndex = ndxInitializer.length > 0 ? ndxInitializer.shift() : -1;
  930. if (altIndex === -1 || typeof altIndex === "string") {
  931. var currentPos = testPos, ndxInitializerClone = ndxInitializer.slice(), altIndexArr = [], amndx;
  932. if (typeof altIndex == "string") {
  933. altIndexArr = altIndex.split(",");
  934. } else {
  935. for (amndx = 0; amndx < alternateToken.matches.length; amndx++) {
  936. altIndexArr.push(amndx.toString());
  937. }
  938. }
  939. if (getMaskSet().excludes[pos]) {
  940. var altIndexArrClone = altIndexArr.slice();
  941. for (var i = 0, el = getMaskSet().excludes[pos].length; i < el; i++) {
  942. altIndexArr.splice(altIndexArr.indexOf(getMaskSet().excludes[pos][i].toString()), 1);
  943. }
  944. if (altIndexArr.length === 0) {
  945. getMaskSet().excludes[pos] = undefined;
  946. altIndexArr = altIndexArrClone;
  947. }
  948. }
  949. if (opts.keepStatic === true || isFinite(parseInt(opts.keepStatic)) && currentPos >= opts.keepStatic) altIndexArr = altIndexArr.slice(0, 1);
  950. var unMatchedAlternation = false;
  951. for (var ndx = 0; ndx < altIndexArr.length; ndx++) {
  952. amndx = parseInt(altIndexArr[ndx]);
  953. matches = [];
  954. ndxInitializer = typeof altIndex === "string" ? resolveNdxInitializer(testPos, amndx, loopNdxCnt) || ndxInitializerClone.slice() : ndxInitializerClone.slice();
  955. if (alternateToken.matches[amndx] && handleMatch(alternateToken.matches[amndx], [ amndx ].concat(loopNdx), quantifierRecurse)) match = true; else if (ndx === 0) {
  956. unMatchedAlternation = true;
  957. }
  958. maltMatches = matches.slice();
  959. testPos = currentPos;
  960. matches = [];
  961. for (var ndx1 = 0; ndx1 < maltMatches.length; ndx1++) {
  962. var altMatch = maltMatches[ndx1], dropMatch = false;
  963. altMatch.match.jit = altMatch.match.jit || unMatchedAlternation;
  964. altMatch.alternation = altMatch.alternation || loopNdxCnt;
  965. setMergeLocators(altMatch);
  966. for (var ndx2 = 0; ndx2 < malternateMatches.length; ndx2++) {
  967. var altMatch2 = malternateMatches[ndx2];
  968. if (typeof altIndex !== "string" || altMatch.alternation !== undefined && $.inArray(altMatch.locator[altMatch.alternation].toString(), altIndexArr) !== -1) {
  969. if (altMatch.match.nativeDef === altMatch2.match.nativeDef) {
  970. dropMatch = true;
  971. setMergeLocators(altMatch2, altMatch);
  972. break;
  973. } else if (isSubsetOf(altMatch, altMatch2)) {
  974. if (setMergeLocators(altMatch, altMatch2)) {
  975. dropMatch = true;
  976. malternateMatches.splice(malternateMatches.indexOf(altMatch2), 0, altMatch);
  977. }
  978. break;
  979. } else if (isSubsetOf(altMatch2, altMatch)) {
  980. setMergeLocators(altMatch2, altMatch);
  981. break;
  982. } else if (staticCanMatchDefinition(altMatch, altMatch2)) {
  983. if (setMergeLocators(altMatch, altMatch2)) {
  984. dropMatch = true;
  985. malternateMatches.splice(malternateMatches.indexOf(altMatch2), 0, altMatch);
  986. }
  987. break;
  988. }
  989. }
  990. }
  991. if (!dropMatch) {
  992. malternateMatches.push(altMatch);
  993. }
  994. }
  995. }
  996. matches = currentMatches.concat(malternateMatches);
  997. testPos = pos;
  998. insertStop = matches.length > 0;
  999. match = malternateMatches.length > 0;
  1000. ndxInitializer = ndxInitializerClone.slice();
  1001. } else match = handleMatch(alternateToken.matches[altIndex] || maskToken.matches[altIndex], [ altIndex ].concat(loopNdx), quantifierRecurse);
  1002. if (match) return true;
  1003. } else if (match.isQuantifier && quantifierRecurse !== maskToken.matches[$.inArray(match, maskToken.matches) - 1]) {
  1004. var qt = match;
  1005. for (var qndx = ndxInitializer.length > 0 ? ndxInitializer.shift() : 0; qndx < (isNaN(qt.quantifier.max) ? qndx + 1 : qt.quantifier.max) && testPos <= pos; qndx++) {
  1006. var tokenGroup = maskToken.matches[$.inArray(qt, maskToken.matches) - 1];
  1007. match = handleMatch(tokenGroup, [ qndx ].concat(loopNdx), tokenGroup);
  1008. if (match) {
  1009. latestMatch = matches[matches.length - 1].match;
  1010. latestMatch.optionalQuantifier = qndx > qt.quantifier.min - 1;
  1011. latestMatch.jit = (qndx || 1) * tokenGroup.matches.indexOf(latestMatch) >= qt.quantifier.jit;
  1012. if (latestMatch.optionalQuantifier && isFirstMatch(latestMatch, tokenGroup)) {
  1013. insertStop = true;
  1014. testPos = pos;
  1015. break;
  1016. }
  1017. if (latestMatch.jit && !latestMatch.optionalQuantifier) {
  1018. offset = tokenGroup.matches.indexOf(latestMatch);
  1019. testPos = pos;
  1020. insertStop = true;
  1021. break;
  1022. }
  1023. return true;
  1024. }
  1025. }
  1026. } else {
  1027. match = resolveTestFromToken(match, ndxInitializer, loopNdx, quantifierRecurse);
  1028. if (match) return true;
  1029. }
  1030. } else {
  1031. testPos++;
  1032. }
  1033. }
  1034. for (var tndx = ndxInitializer.length > 0 ? ndxInitializer.shift() : 0; tndx < maskToken.matches.length; tndx = tndx + 1 + offset) {
  1035. offset = 0;
  1036. if (maskToken.matches[tndx].isQuantifier !== true) {
  1037. var match = handleMatch(maskToken.matches[tndx], [ tndx ].concat(loopNdx), quantifierRecurse);
  1038. if (match && testPos === pos) {
  1039. return match;
  1040. } else if (testPos > pos) {
  1041. break;
  1042. }
  1043. }
  1044. }
  1045. }
  1046. function mergeLocators(pos, tests) {
  1047. var locator = [];
  1048. if (!$.isArray(tests)) tests = [ tests ];
  1049. if (tests.length > 0) {
  1050. if (tests[0].alternation === undefined) {
  1051. locator = determineTestTemplate(pos, tests.slice()).locator.slice();
  1052. if (locator.length === 0) locator = tests[0].locator.slice();
  1053. } else {
  1054. $.each(tests, function(ndx, tst) {
  1055. if (tst.def !== "") {
  1056. if (locator.length === 0) locator = tst.locator.slice(); else {
  1057. for (var i = 0; i < locator.length; i++) {
  1058. if (tst.locator[i] && locator[i].toString().indexOf(tst.locator[i]) === -1) {
  1059. locator[i] += "," + tst.locator[i];
  1060. }
  1061. }
  1062. }
  1063. }
  1064. });
  1065. }
  1066. }
  1067. return locator;
  1068. }
  1069. if (pos > -1) {
  1070. if (ndxIntlzr === undefined) {
  1071. var previousPos = pos - 1, test;
  1072. while ((test = getMaskSet().validPositions[previousPos] || getMaskSet().tests[previousPos]) === undefined && previousPos > -1) {
  1073. previousPos--;
  1074. }
  1075. if (test !== undefined && previousPos > -1) {
  1076. ndxInitializer = mergeLocators(previousPos, test);
  1077. cacheDependency = ndxInitializer.join("");
  1078. testPos = previousPos;
  1079. }
  1080. }
  1081. if (getMaskSet().tests[pos] && getMaskSet().tests[pos][0].cd === cacheDependency) {
  1082. return getMaskSet().tests[pos];
  1083. }
  1084. for (var mtndx = ndxInitializer.shift(); mtndx < maskTokens.length; mtndx++) {
  1085. var match = resolveTestFromToken(maskTokens[mtndx], ndxInitializer, [ mtndx ]);
  1086. if (match && testPos === pos || testPos > pos) {
  1087. break;
  1088. }
  1089. }
  1090. }
  1091. if (matches.length === 0 || insertStop) {
  1092. matches.push({
  1093. match: {
  1094. fn: null,
  1095. optionality: false,
  1096. casing: null,
  1097. def: "",
  1098. placeholder: ""
  1099. },
  1100. locator: [],
  1101. mloc: {},
  1102. cd: cacheDependency
  1103. });
  1104. }
  1105. if (ndxIntlzr !== undefined && getMaskSet().tests[pos]) {
  1106. return $.extend(true, [], matches);
  1107. }
  1108. getMaskSet().tests[pos] = $.extend(true, [], matches);
  1109. return getMaskSet().tests[pos];
  1110. }
  1111. function getBufferTemplate() {
  1112. if (getMaskSet()._buffer === undefined) {
  1113. getMaskSet()._buffer = getMaskTemplate(false, 1);
  1114. if (getMaskSet().buffer === undefined) getMaskSet().buffer = getMaskSet()._buffer.slice();
  1115. }
  1116. return getMaskSet()._buffer;
  1117. }
  1118. function getBuffer(noCache) {
  1119. if (getMaskSet().buffer === undefined || noCache === true) {
  1120. getMaskSet().buffer = getMaskTemplate(true, getLastValidPosition(), true);
  1121. if (getMaskSet()._buffer === undefined) getMaskSet()._buffer = getMaskSet().buffer.slice();
  1122. }
  1123. return getMaskSet().buffer;
  1124. }
  1125. function refreshFromBuffer(start, end, buffer) {
  1126. var i, p;
  1127. if (start === true) {
  1128. resetMaskSet();
  1129. start = 0;
  1130. end = buffer.length;
  1131. } else {
  1132. for (i = start; i < end; i++) {
  1133. delete getMaskSet().validPositions[i];
  1134. }
  1135. }
  1136. p = start;
  1137. for (i = start; i < end; i++) {
  1138. resetMaskSet(true);
  1139. if (buffer[i] !== opts.skipOptionalPartCharacter) {
  1140. var valResult = isValid(p, buffer[i], true, true);
  1141. if (valResult !== false) {
  1142. resetMaskSet(true);
  1143. p = valResult.caret !== undefined ? valResult.caret : valResult.pos + 1;
  1144. }
  1145. }
  1146. }
  1147. }
  1148. function casing(elem, test, pos) {
  1149. switch (opts.casing || test.casing) {
  1150. case "upper":
  1151. elem = elem.toUpperCase();
  1152. break;
  1153. case "lower":
  1154. elem = elem.toLowerCase();
  1155. break;
  1156. case "title":
  1157. var posBefore = getMaskSet().validPositions[pos - 1];
  1158. if (pos === 0 || posBefore && posBefore.input === String.fromCharCode(Inputmask.keyCode.SPACE)) {
  1159. elem = elem.toUpperCase();
  1160. } else {
  1161. elem = elem.toLowerCase();
  1162. }
  1163. break;
  1164. default:
  1165. if ($.isFunction(opts.casing)) {
  1166. var args = Array.prototype.slice.call(arguments);
  1167. args.push(getMaskSet().validPositions);
  1168. elem = opts.casing.apply(this, args);
  1169. }
  1170. }
  1171. return elem;
  1172. }
  1173. function checkAlternationMatch(altArr1, altArr2, na) {
  1174. var altArrC = opts.greedy ? altArr2 : altArr2.slice(0, 1), isMatch = false, naArr = na !== undefined ? na.split(",") : [], naNdx;
  1175. for (var i = 0; i < naArr.length; i++) {
  1176. if ((naNdx = altArr1.indexOf(naArr[i])) !== -1) {
  1177. altArr1.splice(naNdx, 1);
  1178. }
  1179. }
  1180. for (var alndx = 0; alndx < altArr1.length; alndx++) {
  1181. if ($.inArray(altArr1[alndx], altArrC) !== -1) {
  1182. isMatch = true;
  1183. break;
  1184. }
  1185. }
  1186. return isMatch;
  1187. }
  1188. function alternate(pos, c, strict, fromSetValid, rAltPos) {
  1189. var validPsClone = $.extend(true, {}, getMaskSet().validPositions), lastAlt, alternation, isValidRslt = false, altPos, prevAltPos, i, validPos, decisionPos, lAltPos = rAltPos !== undefined ? rAltPos : getLastValidPosition();
  1190. if (lAltPos === -1 && rAltPos === undefined) {
  1191. lastAlt = 0;
  1192. prevAltPos = getTest(lastAlt);
  1193. alternation = prevAltPos.alternation;
  1194. } else {
  1195. for (;lAltPos >= 0; lAltPos--) {
  1196. altPos = getMaskSet().validPositions[lAltPos];
  1197. if (altPos && altPos.alternation !== undefined) {
  1198. if (prevAltPos && prevAltPos.locator[altPos.alternation] !== altPos.locator[altPos.alternation]) {
  1199. break;
  1200. }
  1201. lastAlt = lAltPos;
  1202. alternation = getMaskSet().validPositions[lastAlt].alternation;
  1203. prevAltPos = altPos;
  1204. }
  1205. }
  1206. }
  1207. if (alternation !== undefined) {
  1208. decisionPos = parseInt(lastAlt);
  1209. getMaskSet().excludes[decisionPos] = getMaskSet().excludes[decisionPos] || [];
  1210. if (pos !== true) {
  1211. getMaskSet().excludes[decisionPos].push(getDecisionTaker(prevAltPos));
  1212. }
  1213. var validInputsClone = [], staticInputsBeforePos = 0;
  1214. for (i = decisionPos; i < getLastValidPosition(undefined, true) + 1; i++) {
  1215. validPos = getMaskSet().validPositions[i];
  1216. if (validPos && validPos.generatedInput !== true) {
  1217. validInputsClone.push(validPos.input);
  1218. } else if (i < pos) staticInputsBeforePos++;
  1219. delete getMaskSet().validPositions[i];
  1220. }
  1221. while (getMaskSet().excludes[decisionPos] && getMaskSet().excludes[decisionPos].length < 10) {
  1222. var posOffset = staticInputsBeforePos * -1, validInputs = validInputsClone.slice();
  1223. getMaskSet().tests[decisionPos] = undefined;
  1224. resetMaskSet(true);
  1225. isValidRslt = true;
  1226. while (validInputs.length > 0) {
  1227. var input = validInputs.shift();
  1228. if (!(isValidRslt = isValid(getLastValidPosition(undefined, true) + 1, input, false, fromSetValid, true))) {
  1229. break;
  1230. }
  1231. }
  1232. if (isValidRslt && c !== undefined) {
  1233. var targetLvp = getLastValidPosition(pos) + 1;
  1234. for (i = decisionPos; i < getLastValidPosition() + 1; i++) {
  1235. validPos = getMaskSet().validPositions[i];
  1236. if ((validPos === undefined || validPos.match.fn == null) && i < pos + posOffset) {
  1237. posOffset++;
  1238. }
  1239. }
  1240. pos = pos + posOffset;
  1241. isValidRslt = isValid(pos > targetLvp ? targetLvp : pos, c, strict, fromSetValid, true);
  1242. }
  1243. if (!isValidRslt) {
  1244. resetMaskSet();
  1245. prevAltPos = getTest(decisionPos);
  1246. getMaskSet().validPositions = $.extend(true, {}, validPsClone);
  1247. if (getMaskSet().excludes[decisionPos]) {
  1248. var decisionTaker = getDecisionTaker(prevAltPos);
  1249. if (getMaskSet().excludes[decisionPos].indexOf(decisionTaker) !== -1) {
  1250. isValidRslt = alternate(pos, c, strict, fromSetValid, decisionPos - 1);
  1251. break;
  1252. }
  1253. getMaskSet().excludes[decisionPos].push(decisionTaker);
  1254. for (i = decisionPos; i < getLastValidPosition(undefined, true) + 1; i++) delete getMaskSet().validPositions[i];
  1255. } else {
  1256. isValidRslt = alternate(pos, c, strict, fromSetValid, decisionPos - 1);
  1257. break;
  1258. }
  1259. } else break;
  1260. }
  1261. }
  1262. getMaskSet().excludes[decisionPos] = undefined;
  1263. return isValidRslt;
  1264. }
  1265. function isValid(pos, c, strict, fromSetValid, fromAlternate, validateOnly) {
  1266. function isSelection(posObj) {
  1267. return isRTL ? posObj.begin - posObj.end > 1 || posObj.begin - posObj.end === 1 : posObj.end - posObj.begin > 1 || posObj.end - posObj.begin === 1;
  1268. }
  1269. strict = strict === true;
  1270. var maskPos = pos;
  1271. if (pos.begin !== undefined) {
  1272. maskPos = isRTL ? pos.end : pos.begin;
  1273. }
  1274. function _isValid(position, c, strict) {
  1275. var rslt = false;
  1276. $.each(getTests(position), function(ndx, tst) {
  1277. var test = tst.match;
  1278. getBuffer(true);
  1279. rslt = test.fn != null ? test.fn.test(c, getMaskSet(), position, strict, opts, isSelection(pos)) : (c === test.def || c === opts.skipOptionalPartCharacter) && test.def !== "" ? {
  1280. c: getPlaceholder(position, test, true) || test.def,
  1281. pos: position
  1282. } : false;
  1283. if (rslt !== false) {
  1284. var elem = rslt.c !== undefined ? rslt.c : c, validatedPos = position;
  1285. elem = elem === opts.skipOptionalPartCharacter && test.fn === null ? getPlaceholder(position, test, true) || test.def : elem;
  1286. if (rslt.remove !== undefined) {
  1287. if (!$.isArray(rslt.remove)) rslt.remove = [ rslt.remove ];
  1288. $.each(rslt.remove.sort(function(a, b) {
  1289. return b - a;
  1290. }), function(ndx, lmnt) {
  1291. revalidateMask({
  1292. begin: lmnt,
  1293. end: lmnt + 1
  1294. });
  1295. });
  1296. }
  1297. if (rslt.insert !== undefined) {
  1298. if (!$.isArray(rslt.insert)) rslt.insert = [ rslt.insert ];
  1299. $.each(rslt.insert.sort(function(a, b) {
  1300. return a - b;
  1301. }), function(ndx, lmnt) {
  1302. isValid(lmnt.pos, lmnt.c, true, fromSetValid);
  1303. });
  1304. }
  1305. if (rslt !== true && rslt.pos !== undefined && rslt.pos !== position) {
  1306. validatedPos = rslt.pos;
  1307. }
  1308. if (rslt !== true && rslt.pos === undefined && rslt.c === undefined) {
  1309. return false;
  1310. }
  1311. if (!revalidateMask(pos, $.extend({}, tst, {
  1312. input: casing(elem, test, validatedPos)
  1313. }), fromSetValid, validatedPos)) {
  1314. rslt = false;
  1315. }
  1316. return false;
  1317. }
  1318. });
  1319. return rslt;
  1320. }
  1321. var result = true, positionsClone = $.extend(true, {}, getMaskSet().validPositions);
  1322. if ($.isFunction(opts.preValidation) && !strict && fromSetValid !== true && validateOnly !== true) {
  1323. result = opts.preValidation(getBuffer(), maskPos, c, isSelection(pos), opts, getMaskSet());
  1324. }
  1325. if (result === true) {
  1326. trackbackPositions(undefined, maskPos, true);
  1327. if (maxLength === undefined || maskPos < maxLength) {
  1328. result = _isValid(maskPos, c, strict);
  1329. if ((!strict || fromSetValid === true) && result === false && validateOnly !== true) {
  1330. var currentPosValid = getMaskSet().validPositions[maskPos];
  1331. if (currentPosValid && currentPosValid.match.fn === null && (currentPosValid.match.def === c || c === opts.skipOptionalPartCharacter)) {
  1332. result = {
  1333. caret: seekNext(maskPos)
  1334. };
  1335. } else if ((opts.insertMode || getMaskSet().validPositions[seekNext(maskPos)] === undefined) && !isMask(maskPos, true)) {
  1336. for (var nPos = maskPos + 1, snPos = seekNext(maskPos); nPos <= snPos; nPos++) {
  1337. result = _isValid(nPos, c, strict);
  1338. if (result !== false) {
  1339. result = trackbackPositions(maskPos, result.pos !== undefined ? result.pos : nPos) || result;
  1340. maskPos = nPos;
  1341. break;
  1342. }
  1343. }
  1344. }
  1345. }
  1346. }
  1347. if (result === false && opts.keepStatic !== false && (opts.regex == null || isComplete(getBuffer())) && !strict && fromAlternate !== true) {
  1348. result = alternate(maskPos, c, strict, fromSetValid);
  1349. }
  1350. if (result === true) {
  1351. result = {
  1352. pos: maskPos
  1353. };
  1354. }
  1355. }
  1356. if ($.isFunction(opts.postValidation) && result !== false && !strict && fromSetValid !== true && validateOnly !== true) {
  1357. var postResult = opts.postValidation(getBuffer(true), pos.begin !== undefined ? isRTL ? pos.end : pos.begin : pos, result, opts);
  1358. if (postResult !== undefined) {
  1359. if (postResult.refreshFromBuffer && postResult.buffer) {
  1360. var refresh = postResult.refreshFromBuffer;
  1361. refreshFromBuffer(refresh === true ? refresh : refresh.start, refresh.end, postResult.buffer);
  1362. }
  1363. result = postResult === true ? result : postResult;
  1364. }
  1365. }
  1366. if (result && result.pos === undefined) {
  1367. result.pos = maskPos;
  1368. }
  1369. if (result === false || validateOnly === true) {
  1370. resetMaskSet(true);
  1371. getMaskSet().validPositions = $.extend(true, {}, positionsClone);
  1372. }
  1373. return result;
  1374. }
  1375. function trackbackPositions(originalPos, newPos, fillOnly) {
  1376. var result;
  1377. if (originalPos === undefined) {
  1378. for (originalPos = newPos - 1; originalPos > 0; originalPos--) {
  1379. if (getMaskSet().validPositions[originalPos]) break;
  1380. }
  1381. }
  1382. for (var ps = originalPos; ps < newPos; ps++) {
  1383. if (getMaskSet().validPositions[ps] === undefined && !isMask(ps, true)) {
  1384. var vp = ps == 0 ? getTest(ps) : getMaskSet().validPositions[ps - 1];
  1385. if (vp) {
  1386. var tests = getTests(ps).slice();
  1387. if (tests[tests.length - 1].match.def === "") tests.pop();
  1388. var bestMatch = determineTestTemplate(ps, tests);
  1389. bestMatch = $.extend({}, bestMatch, {
  1390. input: getPlaceholder(ps, bestMatch.match, true) || bestMatch.match.def
  1391. });
  1392. bestMatch.generatedInput = true;
  1393. revalidateMask(ps, bestMatch, true);
  1394. if (fillOnly !== true) {
  1395. var cvpInput = getMaskSet().validPositions[newPos].input;
  1396. getMaskSet().validPositions[newPos] = undefined;
  1397. result = isValid(newPos, cvpInput, true, true);
  1398. }
  1399. }
  1400. }
  1401. }
  1402. return result;
  1403. }
  1404. function revalidateMask(pos, validTest, fromSetValid, validatedPos) {
  1405. function IsEnclosedStatic(pos, valids, selection) {
  1406. var posMatch = valids[pos];
  1407. if (posMatch !== undefined && (posMatch.match.fn === null && posMatch.match.optionality !== true || posMatch.input === opts.radixPoint)) {
  1408. var prevMatch = selection.begin <= pos - 1 ? valids[pos - 1] && valids[pos - 1].match.fn === null && valids[pos - 1] : valids[pos - 1], nextMatch = selection.end > pos + 1 ? valids[pos + 1] && valids[pos + 1].match.fn === null && valids[pos + 1] : valids[pos + 1];
  1409. return prevMatch && nextMatch;
  1410. }
  1411. return false;
  1412. }
  1413. var begin = pos.begin !== undefined ? pos.begin : pos, end = pos.end !== undefined ? pos.end : pos;
  1414. if (pos.begin > pos.end) {
  1415. begin = pos.end;
  1416. end = pos.begin;
  1417. }
  1418. validatedPos = validatedPos !== undefined ? validatedPos : begin;
  1419. if (begin !== end || opts.insertMode && getMaskSet().validPositions[validatedPos] !== undefined && fromSetValid === undefined) {
  1420. var positionsClone = $.extend(true, {}, getMaskSet().validPositions), lvp = getLastValidPosition(undefined, true), i;
  1421. getMaskSet().p = begin;
  1422. for (i = lvp; i >= begin; i--) {
  1423. if (getMaskSet().validPositions[i] && getMaskSet().validPositions[i].match.nativeDef === "+") {
  1424. opts.isNegative = false;
  1425. }
  1426. delete getMaskSet().validPositions[i];
  1427. }
  1428. var valid = true, j = validatedPos, vps = getMaskSet().validPositions, needsValidation = false, posMatch = j, i = j;
  1429. if (validTest) {
  1430. getMaskSet().validPositions[validatedPos] = $.extend(true, {}, validTest);
  1431. posMatch++;
  1432. j++;
  1433. if (begin < end) i++;
  1434. }
  1435. for (;i <= lvp; i++) {
  1436. var t = positionsClone[i];
  1437. if (t !== undefined && (i >= end || i >= begin && t.generatedInput !== true && IsEnclosedStatic(i, positionsClone, {
  1438. begin: begin,
  1439. end: end
  1440. }))) {
  1441. while (getTest(posMatch).match.def !== "") {
  1442. if (needsValidation === false && positionsClone[posMatch] && positionsClone[posMatch].match.nativeDef === t.match.nativeDef) {
  1443. getMaskSet().validPositions[posMatch] = $.extend(true, {}, positionsClone[posMatch]);
  1444. getMaskSet().validPositions[posMatch].input = t.input;
  1445. trackbackPositions(undefined, posMatch, true);
  1446. j = posMatch + 1;
  1447. valid = true;
  1448. } else if (opts.shiftPositions && positionCanMatchDefinition(posMatch, t.match.def)) {
  1449. var result = isValid(posMatch, t.input, true, true);
  1450. valid = result !== false;
  1451. j = result.caret || result.insert ? getLastValidPosition() : posMatch + 1;
  1452. needsValidation = true;
  1453. } else {
  1454. valid = t.generatedInput === true || t.input === opts.radixPoint && opts.numericInput === true;
  1455. }
  1456. if (valid) break;
  1457. if (!valid && posMatch > end && isMask(posMatch, true) && (t.match.fn !== null || posMatch > getMaskSet().maskLength)) {
  1458. break;
  1459. }
  1460. posMatch++;
  1461. }
  1462. if (getTest(posMatch).match.def == "") valid = false;
  1463. posMatch = j;
  1464. }
  1465. if (!valid) break;
  1466. }
  1467. if (!valid) {
  1468. getMaskSet().validPositions = $.extend(true, {}, positionsClone);
  1469. resetMaskSet(true);
  1470. return false;
  1471. }
  1472. } else if (validTest) {
  1473. getMaskSet().validPositions[validatedPos] = $.extend(true, {}, validTest);
  1474. }
  1475. resetMaskSet(true);
  1476. return true;
  1477. }
  1478. function isMask(pos, strict) {
  1479. var test = getTestTemplate(pos).match;
  1480. if (test.def === "") test = getTest(pos).match;
  1481. if (test.fn != null) {
  1482. return test.fn;
  1483. }
  1484. if (strict !== true && pos > -1) {
  1485. var tests = getTests(pos);
  1486. return tests.length > 1 + (tests[tests.length - 1].match.def === "" ? 1 : 0);
  1487. }
  1488. return false;
  1489. }
  1490. function seekNext(pos, newBlock) {
  1491. var position = pos + 1;
  1492. while (getTest(position).match.def !== "" && (newBlock === true && (getTest(position).match.newBlockMarker !== true || !isMask(position)) || newBlock !== true && !isMask(position))) {
  1493. position++;
  1494. }
  1495. return position;
  1496. }
  1497. function seekPrevious(pos, newBlock) {
  1498. var position = pos, tests;
  1499. if (position <= 0) return 0;
  1500. while (--position > 0 && (newBlock === true && getTest(position).match.newBlockMarker !== true || newBlock !== true && !isMask(position) && (tests = getTests(position),
  1501. tests.length < 2 || tests.length === 2 && tests[1].match.def === ""))) {}
  1502. return position;
  1503. }
  1504. function getBufferElement(position) {
  1505. return getMaskSet().validPositions[position] === undefined ? getPlaceholder(position) : getMaskSet().validPositions[position].input;
  1506. }
  1507. function writeBuffer(input, buffer, caretPos, event, triggerEvents) {
  1508. if (event && $.isFunction(opts.onBeforeWrite)) {
  1509. var result = opts.onBeforeWrite.call(inputmask, event, buffer, caretPos, opts);
  1510. if (result) {
  1511. if (result.refreshFromBuffer) {
  1512. var refresh = result.refreshFromBuffer;
  1513. refreshFromBuffer(refresh === true ? refresh : refresh.start, refresh.end, result.buffer || buffer);
  1514. buffer = getBuffer(true);
  1515. }
  1516. if (caretPos !== undefined) caretPos = result.caret !== undefined ? result.caret : caretPos;
  1517. }
  1518. }
  1519. if (input !== undefined) {
  1520. input.inputmask._valueSet(buffer.join(""));
  1521. if (caretPos !== undefined && (event === undefined || event.type !== "blur")) {
  1522. caret(input, caretPos);
  1523. } else renderColorMask(input, caretPos, buffer.length === 0);
  1524. if (triggerEvents === true) {
  1525. var $input = $(input), nptVal = input.inputmask._valueGet();
  1526. skipInputEvent = true;
  1527. $input.trigger("input");
  1528. setTimeout(function() {
  1529. if (nptVal === getBufferTemplate().join("")) {
  1530. $input.trigger("cleared");
  1531. } else if (isComplete(buffer) === true) {
  1532. $input.trigger("complete");
  1533. }
  1534. }, 0);
  1535. }
  1536. }
  1537. }
  1538. function getPlaceholder(pos, test, returnPL) {
  1539. test = test || getTest(pos).match;
  1540. if (test.placeholder !== undefined || returnPL === true) {
  1541. return $.isFunction(test.placeholder) ? test.placeholder(opts) : test.placeholder;
  1542. } else if (test.fn === null) {
  1543. if (pos > -1 && getMaskSet().validPositions[pos] === undefined) {
  1544. var tests = getTests(pos), staticAlternations = [], prevTest;
  1545. if (tests.length > 1 + (tests[tests.length - 1].match.def === "" ? 1 : 0)) {
  1546. for (var i = 0; i < tests.length; i++) {
  1547. if (tests[i].match.optionality !== true && tests[i].match.optionalQuantifier !== true && (tests[i].match.fn === null || (prevTest === undefined || tests[i].match.fn.test(prevTest.match.def, getMaskSet(), pos, true, opts) !== false))) {
  1548. staticAlternations.push(tests[i]);
  1549. if (tests[i].match.fn === null) prevTest = tests[i];
  1550. if (staticAlternations.length > 1) {
  1551. if (/[0-9a-bA-Z]/.test(staticAlternations[0].match.def)) {
  1552. return opts.placeholder.charAt(pos % opts.placeholder.length);
  1553. }
  1554. }
  1555. }
  1556. }
  1557. }
  1558. }
  1559. return test.def;
  1560. }
  1561. return opts.placeholder.charAt(pos % opts.placeholder.length);
  1562. }
  1563. function HandleNativePlaceholder(npt, value) {
  1564. if (ie && npt.inputmask._valueGet() !== value) {
  1565. var buffer = getBuffer().slice(), nptValue = npt.inputmask._valueGet();
  1566. if (nptValue !== value) {
  1567. if (getLastValidPosition() === -1 && nptValue === getBufferTemplate().join("")) {
  1568. buffer = [];
  1569. } else {
  1570. clearOptionalTail(buffer);
  1571. }
  1572. writeBuffer(npt, buffer);
  1573. }
  1574. } else if (npt.placeholder !== value) {
  1575. npt.placeholder = value;
  1576. if (npt.placeholder === "") npt.removeAttribute("placeholder");
  1577. }
  1578. }
  1579. var EventRuler = {
  1580. on: function(input, eventName, eventHandler) {
  1581. var ev = function(e) {
  1582. var that = this;
  1583. if (that.inputmask === undefined && this.nodeName !== "FORM") {
  1584. var imOpts = $.data(that, "_inputmask_opts");
  1585. if (imOpts) new Inputmask(imOpts).mask(that); else EventRuler.off(that);
  1586. } else if (e.type !== "setvalue" && this.nodeName !== "FORM" && (that.disabled || that.readOnly && !(e.type === "keydown" && (e.ctrlKey && e.keyCode === 67) || opts.tabThrough === false && e.keyCode === Inputmask.keyCode.TAB))) {
  1587. e.preventDefault();
  1588. } else {
  1589. switch (e.type) {
  1590. case "input":
  1591. if (skipInputEvent === true) {
  1592. skipInputEvent = false;
  1593. return e.preventDefault();
  1594. }
  1595. if (mobile) {
  1596. var args = arguments;
  1597. setTimeout(function() {
  1598. eventHandler.apply(that, args);
  1599. caret(that, that.inputmask.caretPos, undefined, true);
  1600. }, 0);
  1601. return false;
  1602. }
  1603. break;
  1604. case "keydown":
  1605. skipKeyPressEvent = false;
  1606. skipInputEvent = false;
  1607. break;
  1608. case "keypress":
  1609. if (skipKeyPressEvent === true) {
  1610. return e.preventDefault();
  1611. }
  1612. skipKeyPressEvent = true;
  1613. break;
  1614. case "click":
  1615. if (iemobile || iphone) {
  1616. var args = arguments;
  1617. setTimeout(function() {
  1618. eventHandler.apply(that, args);
  1619. }, 0);
  1620. return false;
  1621. }
  1622. break;
  1623. }
  1624. var returnVal = eventHandler.apply(that, arguments);
  1625. if (returnVal === false) {
  1626. e.preventDefault();
  1627. e.stopPropagation();
  1628. }
  1629. return returnVal;
  1630. }
  1631. };
  1632. input.inputmask.events[eventName] = input.inputmask.events[eventName] || [];
  1633. input.inputmask.events[eventName].push(ev);
  1634. if ($.inArray(eventName, [ "submit", "reset" ]) !== -1) {
  1635. if (input.form !== null) $(input.form).on(eventName, ev);
  1636. } else {
  1637. $(input).on(eventName, ev);
  1638. }
  1639. },
  1640. off: function(input, event) {
  1641. if (input.inputmask && input.inputmask.events) {
  1642. var events;
  1643. if (event) {
  1644. events = [];
  1645. events[event] = input.inputmask.events[event];
  1646. } else {
  1647. events = input.inputmask.events;
  1648. }
  1649. $.each(events, function(eventName, evArr) {
  1650. while (evArr.length > 0) {
  1651. var ev = evArr.pop();
  1652. if ($.inArray(eventName, [ "submit", "reset" ]) !== -1) {
  1653. if (input.form !== null) $(input.form).off(eventName, ev);
  1654. } else {
  1655. $(input).off(eventName, ev);
  1656. }
  1657. }
  1658. delete input.inputmask.events[eventName];
  1659. });
  1660. }
  1661. }
  1662. };
  1663. var EventHandlers = {
  1664. keydownEvent: function(e) {
  1665. var input = this, $input = $(input), k = e.keyCode, pos = caret(input);
  1666. if (k === Inputmask.keyCode.BACKSPACE || k === Inputmask.keyCode.DELETE || iphone && k === Inputmask.keyCode.BACKSPACE_SAFARI || e.ctrlKey && k === Inputmask.keyCode.X && !isInputEventSupported("cut")) {
  1667. e.preventDefault();
  1668. handleRemove(input, k, pos);
  1669. writeBuffer(input, getBuffer(true), getMaskSet().p, e, input.inputmask._valueGet() !== getBuffer().join(""));
  1670. } else if (k === Inputmask.keyCode.END || k === Inputmask.keyCode.PAGE_DOWN) {
  1671. e.preventDefault();
  1672. var caretPos = seekNext(getLastValidPosition());
  1673. caret(input, e.shiftKey ? pos.begin : caretPos, caretPos, true);
  1674. } else if (k === Inputmask.keyCode.HOME && !e.shiftKey || k === Inputmask.keyCode.PAGE_UP) {
  1675. e.preventDefault();
  1676. caret(input, 0, e.shiftKey ? pos.begin : 0, true);
  1677. } else if ((opts.undoOnEscape && k === Inputmask.keyCode.ESCAPE || k === 90 && e.ctrlKey) && e.altKey !== true) {
  1678. checkVal(input, true, false, undoValue.split(""));
  1679. $input.trigger("click");
  1680. } else if (k === Inputmask.keyCode.INSERT && !(e.shiftKey || e.ctrlKey)) {
  1681. opts.insertMode = !opts.insertMode;
  1682. input.setAttribute("im-insert", opts.insertMode);
  1683. } else if (opts.tabThrough === true && k === Inputmask.keyCode.TAB) {
  1684. if (e.shiftKey === true) {
  1685. if (getTest(pos.begin).match.fn === null) {
  1686. pos.begin = seekNext(pos.begin);
  1687. }
  1688. pos.end = seekPrevious(pos.begin, true);
  1689. pos.begin = seekPrevious(pos.end, true);
  1690. } else {
  1691. pos.begin = seekNext(pos.begin, true);
  1692. pos.end = seekNext(pos.begin, true);
  1693. if (pos.end < getMaskSet().maskLength) pos.end--;
  1694. }
  1695. if (pos.begin < getMaskSet().maskLength) {
  1696. e.preventDefault();
  1697. caret(input, pos.begin, pos.end);
  1698. }
  1699. }
  1700. opts.onKeyDown.call(this, e, getBuffer(), caret(input).begin, opts);
  1701. ignorable = $.inArray(k, opts.ignorables) !== -1;
  1702. },
  1703. keypressEvent: function(e, checkval, writeOut, strict, ndx) {
  1704. var input = this, $input = $(input), k = e.which || e.charCode || e.keyCode;
  1705. if (checkval !== true && (!(e.ctrlKey && e.altKey) && (e.ctrlKey || e.metaKey || ignorable))) {
  1706. if (k === Inputmask.keyCode.ENTER && undoValue !== getBuffer().join("")) {
  1707. undoValue = getBuffer().join("");
  1708. setTimeout(function() {
  1709. $input.trigger("change");
  1710. }, 0);
  1711. }
  1712. return true;
  1713. } else {
  1714. if (k) {
  1715. if (k === 46 && e.shiftKey === false && opts.radixPoint !== "") k = opts.radixPoint.charCodeAt(0);
  1716. var pos = checkval ? {
  1717. begin: ndx,
  1718. end: ndx
  1719. } : caret(input), forwardPosition, c = String.fromCharCode(k), offset = 0;
  1720. if (opts._radixDance && opts.numericInput) {
  1721. var caretPos = getBuffer().indexOf(opts.radixPoint.charAt(0)) + 1;
  1722. if (pos.begin <= caretPos) {
  1723. if (k === opts.radixPoint.charCodeAt(0)) offset = 1;
  1724. pos.begin -= 1;
  1725. pos.end -= 1;
  1726. }
  1727. }
  1728. getMaskSet().writeOutBuffer = true;
  1729. var valResult = isValid(pos, c, strict);
  1730. if (valResult !== false) {
  1731. resetMaskSet(true);
  1732. forwardPosition = valResult.caret !== undefined ? valResult.caret : seekNext(valResult.pos.begin ? valResult.pos.begin : valResult.pos);
  1733. getMaskSet().p = forwardPosition;
  1734. }
  1735. forwardPosition = (opts.numericInput && valResult.caret === undefined ? seekPrevious(forwardPosition) : forwardPosition) + offset;
  1736. if (writeOut !== false) {
  1737. setTimeout(function() {
  1738. opts.onKeyValidation.call(input, k, valResult, opts);
  1739. }, 0);
  1740. if (getMaskSet().writeOutBuffer && valResult !== false) {
  1741. var buffer = getBuffer();
  1742. writeBuffer(input, buffer, forwardPosition, e, checkval !== true);
  1743. }
  1744. }
  1745. e.preventDefault();
  1746. if (checkval) {
  1747. if (valResult !== false) valResult.forwardPosition = forwardPosition;
  1748. return valResult;
  1749. }
  1750. }
  1751. }
  1752. },
  1753. pasteEvent: function(e) {
  1754. var input = this, ev = e.originalEvent || e, $input = $(input), inputValue = input.inputmask._valueGet(true), caretPos = caret(input), tempValue;
  1755. if (isRTL) {
  1756. tempValue = caretPos.end;
  1757. caretPos.end = caretPos.begin;
  1758. caretPos.begin = tempValue;
  1759. }
  1760. var valueBeforeCaret = inputValue.substr(0, caretPos.begin), valueAfterCaret = inputValue.substr(caretPos.end, inputValue.length);
  1761. if (valueBeforeCaret === (isRTL ? getBufferTemplate().reverse() : getBufferTemplate()).slice(0, caretPos.begin).join("")) valueBeforeCaret = "";
  1762. if (valueAfterCaret === (isRTL ? getBufferTemplate().reverse() : getBufferTemplate()).slice(caretPos.end).join("")) valueAfterCaret = "";
  1763. if (window.clipboardData && window.clipboardData.getData) {
  1764. inputValue = valueBeforeCaret + window.clipboardData.getData("Text") + valueAfterCaret;
  1765. } else if (ev.clipboardData && ev.clipboardData.getData) {
  1766. inputValue = valueBeforeCaret + ev.clipboardData.getData("text/plain") + valueAfterCaret;
  1767. } else return true;
  1768. var pasteValue = inputValue;
  1769. if ($.isFunction(opts.onBeforePaste)) {
  1770. pasteValue = opts.onBeforePaste.call(inputmask, inputValue, opts);
  1771. if (pasteValue === false) {
  1772. return e.preventDefault();
  1773. }
  1774. if (!pasteValue) {
  1775. pasteValue = inputValue;
  1776. }
  1777. }
  1778. checkVal(input, false, false, pasteValue.toString().split(""));
  1779. writeBuffer(input, getBuffer(), seekNext(getLastValidPosition()), e, undoValue !== getBuffer().join(""));
  1780. return e.preventDefault();
  1781. },
  1782. inputFallBackEvent: function(e) {
  1783. function radixPointHandler(input, inputValue, caretPos) {
  1784. if (inputValue.charAt(caretPos.begin - 1) === "." && opts.radixPoint !== "") {
  1785. inputValue = inputValue.split("");
  1786. inputValue[caretPos.begin - 1] = opts.radixPoint.charAt(0);
  1787. inputValue = inputValue.join("");
  1788. }
  1789. return inputValue;
  1790. }
  1791. function ieMobileHandler(input, inputValue, caretPos) {
  1792. if (iemobile) {
  1793. var inputChar = inputValue.replace(getBuffer().join(""), "");
  1794. if (inputChar.length === 1) {
  1795. var iv = inputValue.split("");
  1796. iv.splice(caretPos.begin, 0, inputChar);
  1797. inputValue = iv.join("");
  1798. }
  1799. }
  1800. return inputValue;
  1801. }
  1802. var input = this, inputValue = input.inputmask._valueGet();
  1803. if (getBuffer().join("") !== inputValue) {
  1804. var caretPos = caret(input);
  1805. inputValue = radixPointHandler(input, inputValue, caretPos);
  1806. inputValue = ieMobileHandler(input, inputValue, caretPos);
  1807. if (getBuffer().join("") !== inputValue) {
  1808. var buffer = getBuffer().join(""), offset = !opts.numericInput && inputValue.length > buffer.length ? -1 : 0, frontPart = inputValue.substr(0, caretPos.begin), backPart = inputValue.substr(caretPos.begin), frontBufferPart = buffer.substr(0, caretPos.begin + offset), backBufferPart = buffer.substr(caretPos.begin + offset);
  1809. var selection = caretPos, entries = "", isEntry = false;
  1810. if (frontPart !== frontBufferPart) {
  1811. var fpl = (isEntry = frontPart.length >= frontBufferPart.length) ? frontPart.length : frontBufferPart.length, i;
  1812. for (i = 0; frontPart.charAt(i) === frontBufferPart.charAt(i) && i < fpl; i++) ;
  1813. if (isEntry) {
  1814. selection.begin = i - offset;
  1815. entries += frontPart.slice(i, selection.end);
  1816. }
  1817. }
  1818. if (backPart !== backBufferPart) {
  1819. if (backPart.length > backBufferPart.length) {
  1820. entries += backPart.slice(0, 1);
  1821. } else {
  1822. if (backPart.length < backBufferPart.length) {
  1823. selection.end += backBufferPart.length - backPart.length;
  1824. if (!isEntry && opts.radixPoint !== "" && backPart === "" && frontPart.charAt(selection.begin + offset - 1) === opts.radixPoint) {
  1825. selection.begin--;
  1826. entries = opts.radixPoint;
  1827. }
  1828. }
  1829. }
  1830. }
  1831. writeBuffer(input, getBuffer(), {
  1832. begin: selection.begin + offset,
  1833. end: selection.end + offset
  1834. });
  1835. if (entries.length > 0) {
  1836. $.each(entries.split(""), function(ndx, entry) {
  1837. var keypress = new $.Event("keypress");
  1838. keypress.which = entry.charCodeAt(0);
  1839. ignorable = false;
  1840. EventHandlers.keypressEvent.call(input, keypress);
  1841. });
  1842. } else {
  1843. if (selection.begin === selection.end - 1) {
  1844. selection.begin = seekPrevious(selection.begin + 1);
  1845. if (selection.begin === selection.end - 1) {
  1846. caret(input, selection.begin);
  1847. } else {
  1848. caret(input, selection.begin, selection.end);
  1849. }
  1850. }
  1851. var keydown = new $.Event("keydown");
  1852. keydown.keyCode = opts.numericInput ? Inputmask.keyCode.BACKSPACE : Inputmask.keyCode.DELETE;
  1853. EventHandlers.keydownEvent.call(input, keydown);
  1854. }
  1855. e.preventDefault();
  1856. }
  1857. }
  1858. },
  1859. beforeInputEvent: function(e) {
  1860. if (e.cancelable) {
  1861. var input = this;
  1862. switch (e.inputType) {
  1863. case "insertText":
  1864. $.each(e.data.split(""), function(ndx, entry) {
  1865. var keypress = new $.Event("keypress");
  1866. keypress.which = entry.charCodeAt(0);
  1867. ignorable = false;
  1868. EventHandlers.keypressEvent.call(input, keypress);
  1869. });
  1870. return e.preventDefault();
  1871. case "deleteContentBackward":
  1872. var keydown = new $.Event("keydown");
  1873. keydown.keyCode = Inputmask.keyCode.BACKSPACE;
  1874. EventHandlers.keydownEvent.call(input, keydown);
  1875. return e.preventDefault();
  1876. case "deleteContentForward":
  1877. var keydown = new $.Event("keydown");
  1878. keydown.keyCode = Inputmask.keyCode.DELETE;
  1879. EventHandlers.keydownEvent.call(input, keydown);
  1880. return e.preventDefault();
  1881. }
  1882. }
  1883. },
  1884. setValueEvent: function(e) {
  1885. this.inputmask.refreshValue = false;
  1886. var input = this, value = e && e.detail ? e.detail[0] : arguments[1], value = value || input.inputmask._valueGet(true);
  1887. if ($.isFunction(opts.onBeforeMask)) value = opts.onBeforeMask.call(inputmask, value, opts) || value;
  1888. value = value.split("");
  1889. checkVal(input, true, false, value);
  1890. undoValue = getBuffer().join("");
  1891. if ((opts.clearMaskOnLostFocus || opts.clearIncomplete) && input.inputmask._valueGet() === getBufferTemplate().join("")) {
  1892. input.inputmask._valueSet("");
  1893. }
  1894. },
  1895. focusEvent: function(e) {
  1896. var input = this, nptValue = input.inputmask._valueGet();
  1897. if (opts.showMaskOnFocus && (!opts.showMaskOnHover || opts.showMaskOnHover && nptValue === "")) {
  1898. if (input.inputmask._valueGet() !== getBuffer().join("")) {
  1899. writeBuffer(input, getBuffer(), seekNext(getLastValidPosition()));
  1900. } else if (mouseEnter === false) {
  1901. caret(input, seekNext(getLastValidPosition()));
  1902. }
  1903. }
  1904. if (opts.positionCaretOnTab === true && mouseEnter === false) {
  1905. EventHandlers.clickEvent.apply(input, [ e, true ]);
  1906. }
  1907. undoValue = getBuffer().join("");
  1908. },
  1909. mouseleaveEvent: function(e) {
  1910. var input = this;
  1911. mouseEnter = false;
  1912. if (opts.clearMaskOnLostFocus && document.activeElement !== input) {
  1913. HandleNativePlaceholder(input, originalPlaceholder);
  1914. }
  1915. },
  1916. clickEvent: function(e, tabbed) {
  1917. function doRadixFocus(clickPos) {
  1918. if (opts.radixPoint !== "") {
  1919. var vps = getMaskSet().validPositions;
  1920. if (vps[clickPos] === undefined || vps[clickPos].input === getPlaceholder(clickPos)) {
  1921. if (clickPos < seekNext(-1)) return true;
  1922. var radixPos = $.inArray(opts.radixPoint, getBuffer());
  1923. if (radixPos !== -1) {
  1924. for (var vp in vps) {
  1925. if (radixPos < vp && vps[vp].input !== getPlaceholder(vp)) {
  1926. return false;
  1927. }
  1928. }
  1929. return true;
  1930. }
  1931. }
  1932. }
  1933. return false;
  1934. }
  1935. var input = this;
  1936. setTimeout(function() {
  1937. if (document.activeElement === input) {
  1938. var selectedCaret = caret(input);
  1939. if (tabbed) {
  1940. if (isRTL) {
  1941. selectedCaret.end = selectedCaret.begin;
  1942. } else {
  1943. selectedCaret.begin = selectedCaret.end;
  1944. }
  1945. }
  1946. if (selectedCaret.begin === selectedCaret.end) {
  1947. switch (opts.positionCaretOnClick) {
  1948. case "none":
  1949. break;
  1950. case "select":
  1951. caret(input, 0, getBuffer().length);
  1952. break;
  1953. case "ignore":
  1954. caret(input, seekNext(getLastValidPosition()));
  1955. break;
  1956. case "radixFocus":
  1957. if (doRadixFocus(selectedCaret.begin)) {
  1958. var radixPos = getBuffer().join("").indexOf(opts.radixPoint);
  1959. caret(input, opts.numericInput ? seekNext(radixPos) : radixPos);
  1960. break;
  1961. }
  1962. default:
  1963. var clickPosition = selectedCaret.begin, lvclickPosition = getLastValidPosition(clickPosition, true), lastPosition = seekNext(lvclickPosition);
  1964. if (clickPosition < lastPosition) {
  1965. caret(input, !isMask(clickPosition, true) && !isMask(clickPosition - 1, true) ? seekNext(clickPosition) : clickPosition);
  1966. } else {
  1967. var lvp = getMaskSet().validPositions[lvclickPosition], tt = getTestTemplate(lastPosition, lvp ? lvp.match.locator : undefined, lvp), placeholder = getPlaceholder(lastPosition, tt.match);
  1968. if (placeholder !== "" && getBuffer()[lastPosition] !== placeholder && tt.match.optionalQuantifier !== true && tt.match.newBlockMarker !== true || !isMask(lastPosition, opts.keepStatic) && tt.match.def === placeholder) {
  1969. var newPos = seekNext(lastPosition);
  1970. if (clickPosition >= newPos || clickPosition === lastPosition) {
  1971. lastPosition = newPos;
  1972. }
  1973. }
  1974. caret(input, lastPosition);
  1975. }
  1976. break;
  1977. }
  1978. }
  1979. }
  1980. }, 0);
  1981. },
  1982. cutEvent: function(e) {
  1983. var input = this, $input = $(input), pos = caret(input), ev = e.originalEvent || e;
  1984. var clipboardData = window.clipboardData || ev.clipboardData, clipData = isRTL ? getBuffer().slice(pos.end, pos.begin) : getBuffer().slice(pos.begin, pos.end);
  1985. clipboardData.setData("text", isRTL ? clipData.reverse().join("") : clipData.join(""));
  1986. if (document.execCommand) document.execCommand("copy");
  1987. handleRemove(input, Inputmask.keyCode.DELETE, pos);
  1988. writeBuffer(input, getBuffer(), getMaskSet().p, e, undoValue !== getBuffer().join(""));
  1989. },
  1990. blurEvent: function(e) {
  1991. var $input = $(this), input = this;
  1992. if (input.inputmask) {
  1993. HandleNativePlaceholder(input, originalPlaceholder);
  1994. var nptValue = input.inputmask._valueGet(), buffer = getBuffer().slice();
  1995. if (nptValue !== "" || colorMask !== undefined) {
  1996. if (opts.clearMaskOnLostFocus) {
  1997. if (getLastValidPosition() === -1 && nptValue === getBufferTemplate().join("")) {
  1998. buffer = [];
  1999. } else {
  2000. clearOptionalTail(buffer);
  2001. }
  2002. }
  2003. if (isComplete(buffer) === false) {
  2004. setTimeout(function() {
  2005. $input.trigger("incomplete");
  2006. }, 0);
  2007. if (opts.clearIncomplete) {
  2008. resetMaskSet();
  2009. if (opts.clearMaskOnLostFocus) {
  2010. buffer = [];
  2011. } else {
  2012. buffer = getBufferTemplate().slice();
  2013. }
  2014. }
  2015. }
  2016. writeBuffer(input, buffer, undefined, e);
  2017. }
  2018. if (undoValue !== getBuffer().join("")) {
  2019. undoValue = buffer.join("");
  2020. $input.trigger("change");
  2021. }
  2022. }
  2023. },
  2024. mouseenterEvent: function(e) {
  2025. var input = this;
  2026. mouseEnter = true;
  2027. if (document.activeElement !== input && opts.showMaskOnHover) {
  2028. HandleNativePlaceholder(input, (isRTL ? getBuffer().slice().reverse() : getBuffer()).join(""));
  2029. }
  2030. },
  2031. submitEvent: function(e) {
  2032. if (undoValue !== getBuffer().join("")) {
  2033. $el.trigger("change");
  2034. }
  2035. if (opts.clearMaskOnLostFocus && getLastValidPosition() === -1 && el.inputmask._valueGet && el.inputmask._valueGet() === getBufferTemplate().join("")) {
  2036. el.inputmask._valueSet("");
  2037. }
  2038. if (opts.clearIncomplete && isComplete(getBuffer()) === false) {
  2039. el.inputmask._valueSet("");
  2040. }
  2041. if (opts.removeMaskOnSubmit) {
  2042. el.inputmask._valueSet(el.inputmask.unmaskedvalue(), true);
  2043. setTimeout(function() {
  2044. writeBuffer(el, getBuffer());
  2045. }, 0);
  2046. }
  2047. },
  2048. resetEvent: function(e) {
  2049. el.inputmask.refreshValue = true;
  2050. setTimeout(function() {
  2051. $el.trigger("setvalue");
  2052. }, 0);
  2053. }
  2054. };
  2055. function checkVal(input, writeOut, strict, nptvl, initiatingEvent) {
  2056. var inputmask = this || input.inputmask, inputValue = nptvl.slice(), charCodes = "", initialNdx = -1, result = undefined;
  2057. function isTemplateMatch(ndx, charCodes) {
  2058. var charCodeNdx = getMaskTemplate(true, 0, false).slice(ndx, seekNext(ndx)).join("").replace(/'/g, "").indexOf(charCodes);
  2059. return charCodeNdx !== -1 && !isMask(ndx) && (getTest(ndx).match.nativeDef === charCodes.charAt(0) || getTest(ndx).match.fn === null && getTest(ndx).match.nativeDef === "'" + charCodes.charAt(0) || getTest(ndx).match.nativeDef === " " && (getTest(ndx + 1).match.nativeDef === charCodes.charAt(0) || getTest(ndx + 1).match.fn === null && getTest(ndx + 1).match.nativeDef === "'" + charCodes.charAt(0)));
  2060. }
  2061. resetMaskSet();
  2062. if (!strict && opts.autoUnmask !== true) {
  2063. var staticInput = getBufferTemplate().slice(0, seekNext(-1)).join(""), matches = inputValue.join("").match(new RegExp("^" + Inputmask.escapeRegex(staticInput), "g"));
  2064. if (matches && matches.length > 0) {
  2065. inputValue.splice(0, matches.length * staticInput.length);
  2066. initialNdx = seekNext(initialNdx);
  2067. }
  2068. } else {
  2069. initialNdx = seekNext(initialNdx);
  2070. }
  2071. if (initialNdx === -1) {
  2072. getMaskSet().p = seekNext(initialNdx);
  2073. initialNdx = 0;
  2074. } else getMaskSet().p = initialNdx;
  2075. inputmask.caretPos = {
  2076. begin: initialNdx
  2077. };
  2078. $.each(inputValue, function(ndx, charCode) {
  2079. if (charCode !== undefined) {
  2080. if (getMaskSet().validPositions[ndx] === undefined && inputValue[ndx] === getPlaceholder(ndx) && isMask(ndx, true) && isValid(ndx, inputValue[ndx], true, undefined, undefined, true) === false) {
  2081. getMaskSet().p++;
  2082. } else {
  2083. var keypress = new $.Event("_checkval");
  2084. keypress.which = charCode.charCodeAt(0);
  2085. charCodes += charCode;
  2086. var lvp = getLastValidPosition(undefined, true);
  2087. if (!isTemplateMatch(initialNdx, charCodes)) {
  2088. result = EventHandlers.keypressEvent.call(input, keypress, true, false, strict, inputmask.caretPos.begin);
  2089. if (result) {
  2090. initialNdx = inputmask.caretPos.begin + 1;
  2091. charCodes = "";
  2092. }
  2093. } else {
  2094. result = EventHandlers.keypressEvent.call(input, keypress, true, false, strict, lvp + 1);
  2095. }
  2096. if (result) {
  2097. writeBuffer(undefined, getBuffer(), result.forwardPosition, keypress, false);
  2098. inputmask.caretPos = {
  2099. begin: result.forwardPosition,
  2100. end: result.forwardPosition
  2101. };
  2102. }
  2103. }
  2104. }
  2105. });
  2106. if (writeOut) writeBuffer(input, getBuffer(), result ? result.forwardPosition : undefined, initiatingEvent || new $.Event("checkval"), initiatingEvent && initiatingEvent.type === "input");
  2107. }
  2108. function unmaskedvalue(input) {
  2109. if (input) {
  2110. if (input.inputmask === undefined) {
  2111. return input.value;
  2112. }
  2113. if (input.inputmask && input.inputmask.refreshValue) {
  2114. EventHandlers.setValueEvent.call(input);
  2115. }
  2116. }
  2117. var umValue = [], vps = getMaskSet().validPositions;
  2118. for (var pndx in vps) {
  2119. if (vps[pndx].match && vps[pndx].match.fn != null) {
  2120. umValue.push(vps[pndx].input);
  2121. }
  2122. }
  2123. var unmaskedValue = umValue.length === 0 ? "" : (isRTL ? umValue.reverse() : umValue).join("");
  2124. if ($.isFunction(opts.onUnMask)) {
  2125. var bufferValue = (isRTL ? getBuffer().slice().reverse() : getBuffer()).join("");
  2126. unmaskedValue = opts.onUnMask.call(inputmask, bufferValue, unmaskedValue, opts);
  2127. }
  2128. return unmaskedValue;
  2129. }
  2130. function caret(input, begin, end, notranslate) {
  2131. function translatePosition(pos) {
  2132. if (isRTL && typeof pos === "number" && (!opts.greedy || opts.placeholder !== "") && el) {
  2133. pos = el.inputmask._valueGet().length - pos;
  2134. }
  2135. return pos;
  2136. }
  2137. var range;
  2138. if (begin !== undefined) {
  2139. if ($.isArray(begin)) {
  2140. end = isRTL ? begin[0] : begin[1];
  2141. begin = isRTL ? begin[1] : begin[0];
  2142. }
  2143. if (begin.begin !== undefined) {
  2144. end = isRTL ? begin.begin : begin.end;
  2145. begin = isRTL ? begin.end : begin.begin;
  2146. }
  2147. if (typeof begin === "number") {
  2148. begin = notranslate ? begin : translatePosition(begin);
  2149. end = notranslate ? end : translatePosition(end);
  2150. end = typeof end == "number" ? end : begin;
  2151. var scrollCalc = parseInt(((input.ownerDocument.defaultView || window).getComputedStyle ? (input.ownerDocument.defaultView || window).getComputedStyle(input, null) : input.currentStyle).fontSize) * end;
  2152. input.scrollLeft = scrollCalc > input.scrollWidth ? scrollCalc : 0;
  2153. input.inputmask.caretPos = {
  2154. begin: begin,
  2155. end: end
  2156. };
  2157. if (input === document.activeElement) {
  2158. if ("selectionStart" in input) {
  2159. input.selectionStart = begin;
  2160. input.selectionEnd = end;
  2161. } else if (window.getSelection) {
  2162. range = document.createRange();
  2163. if (input.firstChild === undefined || input.firstChild === null) {
  2164. var textNode = document.createTextNode("");
  2165. input.appendChild(textNode);
  2166. }
  2167. range.setStart(input.firstChild, begin < input.inputmask._valueGet().length ? begin : input.inputmask._valueGet().length);
  2168. range.setEnd(input.firstChild, end < input.inputmask._valueGet().length ? end : input.inputmask._valueGet().length);
  2169. range.collapse(true);
  2170. var sel = window.getSelection();
  2171. sel.removeAllRanges();
  2172. sel.addRange(range);
  2173. } else if (input.createTextRange) {
  2174. range = input.createTextRange();
  2175. range.collapse(true);
  2176. range.moveEnd("character", end);
  2177. range.moveStart("character", begin);
  2178. range.select();
  2179. }
  2180. renderColorMask(input, {
  2181. begin: begin,
  2182. end: end
  2183. });
  2184. }
  2185. }
  2186. } else {
  2187. if ("selectionStart" in input) {
  2188. begin = input.selectionStart;
  2189. end = input.selectionEnd;
  2190. } else if (window.getSelection) {
  2191. range = window.getSelection().getRangeAt(0);
  2192. if (range.commonAncestorContainer.parentNode === input || range.commonAncestorContainer === input) {
  2193. begin = range.startOffset;
  2194. end = range.endOffset;
  2195. }
  2196. } else if (document.selection && document.selection.createRange) {
  2197. range = document.selection.createRange();
  2198. begin = 0 - range.duplicate().moveStart("character", -input.inputmask._valueGet().length);
  2199. end = begin + range.text.length;
  2200. }
  2201. return {
  2202. begin: notranslate ? begin : translatePosition(begin),
  2203. end: notranslate ? end : translatePosition(end)
  2204. };
  2205. }
  2206. }
  2207. function determineLastRequiredPosition(returnDefinition) {
  2208. var buffer = getMaskTemplate(true, getLastValidPosition(), true, true), bl = buffer.length, pos, lvp = getLastValidPosition(), positions = {}, lvTest = getMaskSet().validPositions[lvp], ndxIntlzr = lvTest !== undefined ? lvTest.locator.slice() : undefined, testPos;
  2209. for (pos = lvp + 1; pos < buffer.length; pos++) {
  2210. testPos = getTestTemplate(pos, ndxIntlzr, pos - 1);
  2211. ndxIntlzr = testPos.locator.slice();
  2212. positions[pos] = $.extend(true, {}, testPos);
  2213. }
  2214. var lvTestAlt = lvTest && lvTest.alternation !== undefined ? lvTest.locator[lvTest.alternation] : undefined;
  2215. for (pos = bl - 1; pos > lvp; pos--) {
  2216. testPos = positions[pos];
  2217. if ((testPos.match.optionality || testPos.match.optionalQuantifier && testPos.match.newBlockMarker || lvTestAlt && (lvTestAlt !== positions[pos].locator[lvTest.alternation] && testPos.match.fn != null || testPos.match.fn === null && testPos.locator[lvTest.alternation] && checkAlternationMatch(testPos.locator[lvTest.alternation].toString().split(","), lvTestAlt.toString().split(",")) && getTests(pos)[0].def !== "")) && buffer[pos] === getPlaceholder(pos, testPos.match)) {
  2218. bl--;
  2219. } else break;
  2220. }
  2221. return returnDefinition ? {
  2222. l: bl,
  2223. def: positions[bl] ? positions[bl].match : undefined
  2224. } : bl;
  2225. }
  2226. function clearOptionalTail(buffer) {
  2227. buffer.length = 0;
  2228. var template = getMaskTemplate(true, 0, true, undefined, true), lmnt, validPos;
  2229. while (lmnt = template.shift(), lmnt !== undefined) buffer.push(lmnt);
  2230. return buffer;
  2231. }
  2232. function isComplete(buffer) {
  2233. if ($.isFunction(opts.isComplete)) return opts.isComplete(buffer, opts);
  2234. if (opts.repeat === "*") return undefined;
  2235. var complete = false, lrp = determineLastRequiredPosition(true), aml = seekPrevious(lrp.l);
  2236. if (lrp.def === undefined || lrp.def.newBlockMarker || lrp.def.optionality || lrp.def.optionalQuantifier) {
  2237. complete = true;
  2238. for (var i = 0; i <= aml; i++) {
  2239. var test = getTestTemplate(i).match;
  2240. if (test.fn !== null && getMaskSet().validPositions[i] === undefined && test.optionality !== true && test.optionalQuantifier !== true || test.fn === null && buffer[i] !== getPlaceholder(i, test)) {
  2241. complete = false;
  2242. break;
  2243. }
  2244. }
  2245. }
  2246. return complete;
  2247. }
  2248. function handleRemove(input, k, pos, strict, fromIsValid) {
  2249. if (opts.numericInput || isRTL) {
  2250. if (k === Inputmask.keyCode.BACKSPACE) {
  2251. k = Inputmask.keyCode.DELETE;
  2252. } else if (k === Inputmask.keyCode.DELETE) {
  2253. k = Inputmask.keyCode.BACKSPACE;
  2254. }
  2255. if (isRTL) {
  2256. var pend = pos.end;
  2257. pos.end = pos.begin;
  2258. pos.begin = pend;
  2259. }
  2260. }
  2261. if (k === Inputmask.keyCode.BACKSPACE && pos.end - pos.begin < 1) {
  2262. pos.begin = seekPrevious(pos.begin);
  2263. if (getMaskSet().validPositions[pos.begin] !== undefined && getMaskSet().validPositions[pos.begin].input === opts.groupSeparator) {
  2264. pos.begin--;
  2265. }
  2266. } else if (k === Inputmask.keyCode.DELETE && pos.begin === pos.end) {
  2267. pos.end = isMask(pos.end, true) && (getMaskSet().validPositions[pos.end] && getMaskSet().validPositions[pos.end].input !== opts.radixPoint) ? pos.end + 1 : seekNext(pos.end) + 1;
  2268. if (getMaskSet().validPositions[pos.begin] !== undefined && getMaskSet().validPositions[pos.begin].input === opts.groupSeparator) {
  2269. pos.end++;
  2270. }
  2271. }
  2272. revalidateMask(pos);
  2273. if (strict !== true && opts.keepStatic !== false || opts.regex !== null) {
  2274. var result = alternate(true);
  2275. if (result) {
  2276. var newPos = result.caret !== undefined ? result.caret : result.pos ? seekNext(result.pos.begin ? result.pos.begin : result.pos) : getLastValidPosition(-1, true);
  2277. if (k !== Inputmask.keyCode.DELETE || pos.begin > newPos) {
  2278. pos.begin == newPos;
  2279. }
  2280. }
  2281. }
  2282. var lvp = getLastValidPosition(pos.begin, true);
  2283. if (lvp < pos.begin || pos.begin === -1) {
  2284. getMaskSet().p = seekNext(lvp);
  2285. } else if (strict !== true) {
  2286. getMaskSet().p = pos.begin;
  2287. if (fromIsValid !== true) {
  2288. while (getMaskSet().p < lvp && getMaskSet().validPositions[getMaskSet().p] === undefined) {
  2289. getMaskSet().p++;
  2290. }
  2291. }
  2292. }
  2293. }
  2294. function initializeColorMask(input) {
  2295. var computedStyle = (input.ownerDocument.defaultView || window).getComputedStyle(input, null);
  2296. function findCaretPos(clientx) {
  2297. var e = document.createElement("span"), caretPos;
  2298. for (var style in computedStyle) {
  2299. if (isNaN(style) && style.indexOf("font") !== -1) {
  2300. e.style[style] = computedStyle[style];
  2301. }
  2302. }
  2303. e.style.textTransform = computedStyle.textTransform;
  2304. e.style.letterSpacing = computedStyle.letterSpacing;
  2305. e.style.position = "absolute";
  2306. e.style.height = "auto";
  2307. e.style.width = "auto";
  2308. e.style.visibility = "hidden";
  2309. e.style.whiteSpace = "nowrap";
  2310. document.body.appendChild(e);
  2311. var inputText = input.inputmask._valueGet(), previousWidth = 0, itl;
  2312. for (caretPos = 0, itl = inputText.length; caretPos <= itl; caretPos++) {
  2313. e.innerHTML += inputText.charAt(caretPos) || "_";
  2314. if (e.offsetWidth >= clientx) {
  2315. var offset1 = clientx - previousWidth;
  2316. var offset2 = e.offsetWidth - clientx;
  2317. e.innerHTML = inputText.charAt(caretPos);
  2318. offset1 -= e.offsetWidth / 3;
  2319. caretPos = offset1 < offset2 ? caretPos - 1 : caretPos;
  2320. break;
  2321. }
  2322. previousWidth = e.offsetWidth;
  2323. }
  2324. document.body.removeChild(e);
  2325. return caretPos;
  2326. }
  2327. var template = document.createElement("div");
  2328. template.style.width = computedStyle.width;
  2329. template.style.textAlign = computedStyle.textAlign;
  2330. colorMask = document.createElement("div");
  2331. input.inputmask.colorMask = colorMask;
  2332. colorMask.className = "im-colormask";
  2333. input.parentNode.insertBefore(colorMask, input);
  2334. input.parentNode.removeChild(input);
  2335. colorMask.appendChild(input);
  2336. colorMask.appendChild(template);
  2337. input.style.left = template.offsetLeft + "px";
  2338. $(colorMask).on("mouseleave", function(e) {
  2339. return EventHandlers.mouseleaveEvent.call(input, [ e ]);
  2340. });
  2341. $(colorMask).on("mouseenter", function(e) {
  2342. return EventHandlers.mouseenterEvent.call(input, [ e ]);
  2343. });
  2344. $(colorMask).on("click", function(e) {
  2345. caret(input, findCaretPos(e.clientX));
  2346. return EventHandlers.clickEvent.call(input, [ e ]);
  2347. });
  2348. }
  2349. Inputmask.prototype.positionColorMask = function(input, template) {
  2350. input.style.left = template.offsetLeft + "px";
  2351. };
  2352. function renderColorMask(input, caretPos, clear) {
  2353. var maskTemplate = [], isStatic = false, test, testPos, ndxIntlzr, pos = 0;
  2354. function setEntry(entry) {
  2355. if (entry === undefined) entry = "";
  2356. if (!isStatic && (test.fn === null || testPos.input === undefined)) {
  2357. isStatic = true;
  2358. maskTemplate.push("<span class='im-static'>" + entry);
  2359. } else if (isStatic && (test.fn !== null && testPos.input !== undefined || test.def === "")) {
  2360. isStatic = false;
  2361. var mtl = maskTemplate.length;
  2362. maskTemplate[mtl - 1] = maskTemplate[mtl - 1] + "</span>";
  2363. maskTemplate.push(entry);
  2364. } else maskTemplate.push(entry);
  2365. }
  2366. function setCaret() {
  2367. if (document.activeElement === input) {
  2368. maskTemplate.splice(caretPos.begin, 0, caretPos.begin === caretPos.end || caretPos.end > getMaskSet().maskLength ? '<mark class="im-caret" style="border-right-width: 1px;border-right-style: solid;">' : '<mark class="im-caret-select">');
  2369. maskTemplate.splice(caretPos.end + 1, 0, "</mark>");
  2370. }
  2371. }
  2372. if (colorMask !== undefined) {
  2373. var buffer = getBuffer();
  2374. if (caretPos === undefined) {
  2375. caretPos = caret(input);
  2376. } else if (caretPos.begin === undefined) {
  2377. caretPos = {
  2378. begin: caretPos,
  2379. end: caretPos
  2380. };
  2381. }
  2382. if (clear !== true) {
  2383. var lvp = getLastValidPosition();
  2384. do {
  2385. if (getMaskSet().validPositions[pos]) {
  2386. testPos = getMaskSet().validPositions[pos];
  2387. test = testPos.match;
  2388. ndxIntlzr = testPos.locator.slice();
  2389. setEntry(buffer[pos]);
  2390. } else {
  2391. testPos = getTestTemplate(pos, ndxIntlzr, pos - 1);
  2392. test = testPos.match;
  2393. ndxIntlzr = testPos.locator.slice();
  2394. if (opts.jitMasking === false || pos < lvp || typeof opts.jitMasking === "number" && isFinite(opts.jitMasking) && opts.jitMasking > pos) {
  2395. setEntry(getPlaceholder(pos, test));
  2396. } else isStatic = false;
  2397. }
  2398. pos++;
  2399. } while ((maxLength === undefined || pos < maxLength) && (test.fn !== null || test.def !== "") || lvp > pos || isStatic);
  2400. if (isStatic) setEntry();
  2401. setCaret();
  2402. }
  2403. var template = colorMask.getElementsByTagName("div")[0];
  2404. template.innerHTML = maskTemplate.join("");
  2405. input.inputmask.positionColorMask(input, template);
  2406. }
  2407. }
  2408. function mask(elem) {
  2409. function isElementTypeSupported(input, opts) {
  2410. function patchValueProperty(npt) {
  2411. var valueGet;
  2412. var valueSet;
  2413. function patchValhook(type) {
  2414. if ($.valHooks && ($.valHooks[type] === undefined || $.valHooks[type].inputmaskpatch !== true)) {
  2415. var valhookGet = $.valHooks[type] && $.valHooks[type].get ? $.valHooks[type].get : function(elem) {
  2416. return elem.value;
  2417. };
  2418. var valhookSet = $.valHooks[type] && $.valHooks[type].set ? $.valHooks[type].set : function(elem, value) {
  2419. elem.value = value;
  2420. return elem;
  2421. };
  2422. $.valHooks[type] = {
  2423. get: function(elem) {
  2424. if (elem.inputmask) {
  2425. if (elem.inputmask.opts.autoUnmask) {
  2426. return elem.inputmask.unmaskedvalue();
  2427. } else {
  2428. var result = valhookGet(elem);
  2429. return getLastValidPosition(undefined, undefined, elem.inputmask.maskset.validPositions) !== -1 || opts.nullable !== true ? result : "";
  2430. }
  2431. } else return valhookGet(elem);
  2432. },
  2433. set: function(elem, value) {
  2434. var $elem = $(elem), result;
  2435. result = valhookSet(elem, value);
  2436. if (elem.inputmask) {
  2437. $elem.trigger("setvalue", [ value ]);
  2438. }
  2439. return result;
  2440. },
  2441. inputmaskpatch: true
  2442. };
  2443. }
  2444. }
  2445. function getter() {
  2446. if (this.inputmask) {
  2447. return this.inputmask.opts.autoUnmask ? this.inputmask.unmaskedvalue() : getLastValidPosition() !== -1 || opts.nullable !== true ? document.activeElement === this && opts.clearMaskOnLostFocus ? (isRTL ? clearOptionalTail(getBuffer().slice()).reverse() : clearOptionalTail(getBuffer().slice())).join("") : valueGet.call(this) : "";
  2448. } else return valueGet.call(this);
  2449. }
  2450. function setter(value) {
  2451. valueSet.call(this, value);
  2452. if (this.inputmask) {
  2453. $(this).trigger("setvalue", [ value ]);
  2454. }
  2455. }
  2456. function installNativeValueSetFallback(npt) {
  2457. EventRuler.on(npt, "mouseenter", function(event) {
  2458. var $input = $(this), input = this, value = input.inputmask._valueGet();
  2459. if (value !== getBuffer().join("")) {
  2460. $input.trigger("setvalue");
  2461. }
  2462. });
  2463. }
  2464. if (!npt.inputmask.__valueGet) {
  2465. if (opts.noValuePatching !== true) {
  2466. if (Object.getOwnPropertyDescriptor) {
  2467. if (typeof Object.getPrototypeOf !== "function") {
  2468. Object.getPrototypeOf = typeof "test".__proto__ === "object" ? function(object) {
  2469. return object.__proto__;
  2470. } : function(object) {
  2471. return object.constructor.prototype;
  2472. };
  2473. }
  2474. var valueProperty = Object.getPrototypeOf ? Object.getOwnPropertyDescriptor(Object.getPrototypeOf(npt), "value") : undefined;
  2475. if (valueProperty && valueProperty.get && valueProperty.set) {
  2476. valueGet = valueProperty.get;
  2477. valueSet = valueProperty.set;
  2478. Object.defineProperty(npt, "value", {
  2479. get: getter,
  2480. set: setter,
  2481. configurable: true
  2482. });
  2483. } else if (npt.tagName !== "INPUT") {
  2484. valueGet = function() {
  2485. return this.textContent;
  2486. };
  2487. valueSet = function(value) {
  2488. this.textContent = value;
  2489. };
  2490. Object.defineProperty(npt, "value", {
  2491. get: getter,
  2492. set: setter,
  2493. configurable: true
  2494. });
  2495. }
  2496. } else if (document.__lookupGetter__ && npt.__lookupGetter__("value")) {
  2497. valueGet = npt.__lookupGetter__("value");
  2498. valueSet = npt.__lookupSetter__("value");
  2499. npt.__defineGetter__("value", getter);
  2500. npt.__defineSetter__("value", setter);
  2501. }
  2502. npt.inputmask.__valueGet = valueGet;
  2503. npt.inputmask.__valueSet = valueSet;
  2504. }
  2505. npt.inputmask._valueGet = function(overruleRTL) {
  2506. return isRTL && overruleRTL !== true ? valueGet.call(this.el).split("").reverse().join("") : valueGet.call(this.el);
  2507. };
  2508. npt.inputmask._valueSet = function(value, overruleRTL) {
  2509. valueSet.call(this.el, value === null || value === undefined ? "" : overruleRTL !== true && isRTL ? value.split("").reverse().join("") : value);
  2510. };
  2511. if (valueGet === undefined) {
  2512. valueGet = function() {
  2513. return this.value;
  2514. };
  2515. valueSet = function(value) {
  2516. this.value = value;
  2517. };
  2518. patchValhook(npt.type);
  2519. installNativeValueSetFallback(npt);
  2520. }
  2521. }
  2522. }
  2523. var elementType = input.getAttribute("type");
  2524. var isSupported = input.tagName === "INPUT" && $.inArray(elementType, opts.supportsInputType) !== -1 || input.isContentEditable || input.tagName === "TEXTAREA";
  2525. if (!isSupported) {
  2526. if (input.tagName === "INPUT") {
  2527. var el = document.createElement("input");
  2528. el.setAttribute("type", elementType);
  2529. isSupported = el.type === "text";
  2530. el = null;
  2531. } else isSupported = "partial";
  2532. }
  2533. if (isSupported !== false) {
  2534. patchValueProperty(input);
  2535. } else input.inputmask = undefined;
  2536. return isSupported;
  2537. }
  2538. EventRuler.off(elem);
  2539. var isSupported = isElementTypeSupported(elem, opts);
  2540. if (isSupported !== false) {
  2541. el = elem;
  2542. $el = $(el);
  2543. originalPlaceholder = el.placeholder;
  2544. maxLength = el !== undefined ? el.maxLength : undefined;
  2545. if (maxLength === -1) maxLength = undefined;
  2546. if (opts.colorMask === true) {
  2547. initializeColorMask(el);
  2548. }
  2549. if (mobile) {
  2550. if ("inputmode" in el) {
  2551. el.inputmode = opts.inputmode;
  2552. el.setAttribute("inputmode", opts.inputmode);
  2553. }
  2554. if (opts.disablePredictiveText === true) {
  2555. if ("autocorrect" in el) {
  2556. el.autocorrect = false;
  2557. } else {
  2558. if (opts.colorMask !== true) {
  2559. initializeColorMask(el);
  2560. }
  2561. el.type = "password";
  2562. }
  2563. }
  2564. }
  2565. if (isSupported === true) {
  2566. el.setAttribute("im-insert", opts.insertMode);
  2567. EventRuler.on(el, "submit", EventHandlers.submitEvent);
  2568. EventRuler.on(el, "reset", EventHandlers.resetEvent);
  2569. EventRuler.on(el, "blur", EventHandlers.blurEvent);
  2570. EventRuler.on(el, "focus", EventHandlers.focusEvent);
  2571. if (opts.colorMask !== true) {
  2572. EventRuler.on(el, "click", EventHandlers.clickEvent);
  2573. EventRuler.on(el, "mouseleave", EventHandlers.mouseleaveEvent);
  2574. EventRuler.on(el, "mouseenter", EventHandlers.mouseenterEvent);
  2575. }
  2576. EventRuler.on(el, "paste", EventHandlers.pasteEvent);
  2577. EventRuler.on(el, "cut", EventHandlers.cutEvent);
  2578. EventRuler.on(el, "complete", opts.oncomplete);
  2579. EventRuler.on(el, "incomplete", opts.onincomplete);
  2580. EventRuler.on(el, "cleared", opts.oncleared);
  2581. if (!mobile && opts.inputEventOnly !== true) {
  2582. EventRuler.on(el, "keydown", EventHandlers.keydownEvent);
  2583. EventRuler.on(el, "keypress", EventHandlers.keypressEvent);
  2584. } else {
  2585. el.removeAttribute("maxLength");
  2586. }
  2587. EventRuler.on(el, "input", EventHandlers.inputFallBackEvent);
  2588. EventRuler.on(el, "beforeinput", EventHandlers.beforeInputEvent);
  2589. }
  2590. EventRuler.on(el, "setvalue", EventHandlers.setValueEvent);
  2591. undoValue = getBufferTemplate().join("");
  2592. if (el.inputmask._valueGet(true) !== "" || opts.clearMaskOnLostFocus === false || document.activeElement === el) {
  2593. var initialValue = $.isFunction(opts.onBeforeMask) ? opts.onBeforeMask.call(inputmask, el.inputmask._valueGet(true), opts) || el.inputmask._valueGet(true) : el.inputmask._valueGet(true);
  2594. if (initialValue !== "") checkVal(el, true, false, initialValue.split(""));
  2595. var buffer = getBuffer().slice();
  2596. undoValue = buffer.join("");
  2597. if (isComplete(buffer) === false) {
  2598. if (opts.clearIncomplete) {
  2599. resetMaskSet();
  2600. }
  2601. }
  2602. if (opts.clearMaskOnLostFocus && document.activeElement !== el) {
  2603. if (getLastValidPosition() === -1) {
  2604. buffer = [];
  2605. } else {
  2606. clearOptionalTail(buffer);
  2607. }
  2608. }
  2609. if (opts.clearMaskOnLostFocus === false || opts.showMaskOnFocus && document.activeElement === el || el.inputmask._valueGet(true) !== "") writeBuffer(el, buffer);
  2610. if (document.activeElement === el) {
  2611. caret(el, seekNext(getLastValidPosition()));
  2612. }
  2613. }
  2614. }
  2615. }
  2616. var valueBuffer;
  2617. if (actionObj !== undefined) {
  2618. switch (actionObj.action) {
  2619. case "isComplete":
  2620. el = actionObj.el;
  2621. return isComplete(getBuffer());
  2622. case "unmaskedvalue":
  2623. if (el === undefined || actionObj.value !== undefined) {
  2624. valueBuffer = actionObj.value;
  2625. valueBuffer = ($.isFunction(opts.onBeforeMask) ? opts.onBeforeMask.call(inputmask, valueBuffer, opts) || valueBuffer : valueBuffer).split("");
  2626. checkVal.call(this, undefined, false, false, valueBuffer);
  2627. if ($.isFunction(opts.onBeforeWrite)) opts.onBeforeWrite.call(inputmask, undefined, getBuffer(), 0, opts);
  2628. }
  2629. return unmaskedvalue(el);
  2630. case "mask":
  2631. mask(el);
  2632. break;
  2633. case "format":
  2634. valueBuffer = ($.isFunction(opts.onBeforeMask) ? opts.onBeforeMask.call(inputmask, actionObj.value, opts) || actionObj.value : actionObj.value).split("");
  2635. checkVal.call(this, undefined, true, false, valueBuffer);
  2636. if (actionObj.metadata) {
  2637. return {
  2638. value: isRTL ? getBuffer().slice().reverse().join("") : getBuffer().join(""),
  2639. metadata: maskScope.call(this, {
  2640. action: "getmetadata"
  2641. }, maskset, opts)
  2642. };
  2643. }
  2644. return isRTL ? getBuffer().slice().reverse().join("") : getBuffer().join("");
  2645. case "isValid":
  2646. if (actionObj.value) {
  2647. valueBuffer = actionObj.value.split("");
  2648. checkVal.call(this, undefined, true, true, valueBuffer);
  2649. } else {
  2650. actionObj.value = getBuffer().join("");
  2651. }
  2652. var buffer = getBuffer();
  2653. var rl = determineLastRequiredPosition(), lmib = buffer.length - 1;
  2654. for (;lmib > rl; lmib--) {
  2655. if (isMask(lmib)) break;
  2656. }
  2657. buffer.splice(rl, lmib + 1 - rl);
  2658. return isComplete(buffer) && actionObj.value === getBuffer().join("");
  2659. case "getemptymask":
  2660. return getBufferTemplate().join("");
  2661. case "remove":
  2662. if (el && el.inputmask) {
  2663. $.data(el, "_inputmask_opts", null);
  2664. $el = $(el);
  2665. el.inputmask._valueSet(opts.autoUnmask ? unmaskedvalue(el) : el.inputmask._valueGet(true));
  2666. EventRuler.off(el);
  2667. if (el.inputmask.colorMask) {
  2668. colorMask = el.inputmask.colorMask;
  2669. colorMask.removeChild(el);
  2670. colorMask.parentNode.insertBefore(el, colorMask);
  2671. colorMask.parentNode.removeChild(colorMask);
  2672. }
  2673. var valueProperty;
  2674. if (Object.getOwnPropertyDescriptor && Object.getPrototypeOf) {
  2675. valueProperty = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(el), "value");
  2676. if (valueProperty) {
  2677. if (el.inputmask.__valueGet) {
  2678. Object.defineProperty(el, "value", {
  2679. get: el.inputmask.__valueGet,
  2680. set: el.inputmask.__valueSet,
  2681. configurable: true
  2682. });
  2683. }
  2684. }
  2685. } else if (document.__lookupGetter__ && el.__lookupGetter__("value")) {
  2686. if (el.inputmask.__valueGet) {
  2687. el.__defineGetter__("value", el.inputmask.__valueGet);
  2688. el.__defineSetter__("value", el.inputmask.__valueSet);
  2689. }
  2690. }
  2691. el.inputmask = undefined;
  2692. }
  2693. return el;
  2694. break;
  2695. case "getmetadata":
  2696. if ($.isArray(maskset.metadata)) {
  2697. var maskTarget = getMaskTemplate(true, 0, false).join("");
  2698. $.each(maskset.metadata, function(ndx, mtdt) {
  2699. if (mtdt.mask === maskTarget) {
  2700. maskTarget = mtdt;
  2701. return false;
  2702. }
  2703. });
  2704. return maskTarget;
  2705. }
  2706. return maskset.metadata;
  2707. }
  2708. }
  2709. }
  2710. return Inputmask;
  2711. });