inputmask.js 179 KB

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