inputmask.js 145 KB

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