inputmask.js 145 KB

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