use-mask-input 1.0.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/example/App.example.d.ts +3 -0
  2. package/dist/{useMaskInput.test.d.ts → example/index.d.ts} +0 -0
  3. package/dist/index.js +1 -2
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.modern.js +1 -2
  6. package/dist/index.modern.js.map +1 -1
  7. package/dist/useMaskInput.d.ts +3 -3
  8. package/node_modules/inputmask/README.md +109 -79
  9. package/node_modules/inputmask/bundle.js +6 -5
  10. package/node_modules/inputmask/dist/inputmask.es6.js +5 -0
  11. package/node_modules/inputmask/dist/inputmask.js +2892 -2608
  12. package/node_modules/inputmask/dist/inputmask.min.js +3 -3
  13. package/node_modules/inputmask/dist/jquery.inputmask.js +2829 -2534
  14. package/node_modules/inputmask/dist/jquery.inputmask.min.js +3 -3
  15. package/node_modules/inputmask/lib/bindings/inputmask.es6.js +5 -0
  16. package/node_modules/inputmask/lib/canUseDOM.js +7 -0
  17. package/node_modules/inputmask/lib/defaults.js +101 -0
  18. package/node_modules/inputmask/lib/definitions.js +13 -0
  19. package/node_modules/inputmask/lib/dependencyLibs/data.js +8 -0
  20. package/node_modules/inputmask/lib/dependencyLibs/events.js +199 -0
  21. package/node_modules/inputmask/lib/dependencyLibs/extend.js +58 -0
  22. package/node_modules/inputmask/lib/dependencyLibs/inputmask.dependencyLib.jquery.js +4 -3
  23. package/node_modules/inputmask/lib/dependencyLibs/inputmask.dependencyLib.js +12 -343
  24. package/node_modules/inputmask/lib/environment.js +9 -0
  25. package/node_modules/inputmask/lib/escapeRegex.js +4 -0
  26. package/node_modules/inputmask/lib/eventhandlers.js +513 -0
  27. package/node_modules/inputmask/lib/eventruler.js +124 -0
  28. package/node_modules/inputmask/lib/extensions/inputmask.date.extensions.js +552 -385
  29. package/node_modules/inputmask/lib/extensions/inputmask.extensions.js +116 -97
  30. package/node_modules/inputmask/lib/extensions/inputmask.numeric.extensions.js +594 -565
  31. package/node_modules/inputmask/lib/global/window.js +2 -6
  32. package/node_modules/inputmask/lib/inputHandling.js +252 -0
  33. package/node_modules/inputmask/lib/inputmask.js +129 -126
  34. package/node_modules/inputmask/lib/inputmaskElement.js +26 -20
  35. package/node_modules/inputmask/lib/jquery.inputmask.js +3 -1
  36. package/node_modules/inputmask/lib/keycode.json +6 -1
  37. package/node_modules/inputmask/lib/mask-lexer.js +467 -0
  38. package/node_modules/inputmask/lib/mask.js +244 -0
  39. package/node_modules/inputmask/lib/masktoken.js +13 -0
  40. package/node_modules/inputmask/lib/polyfills/Array.includes.js +48 -0
  41. package/node_modules/inputmask/lib/polyfills/Object.getPrototypeOf.js +7 -0
  42. package/node_modules/inputmask/lib/positioning.js +348 -0
  43. package/node_modules/inputmask/lib/validation-tests.js +597 -0
  44. package/node_modules/inputmask/lib/validation.js +664 -0
  45. package/node_modules/inputmask/package.json +41 -71
  46. package/package.json +40 -43
  47. package/node_modules/inputmask/CHANGELOG.md +0 -714
  48. package/node_modules/inputmask/index.js +0 -1
  49. package/node_modules/inputmask/lib/dependencyLibs/inputmask.dependencyLib.jqlite.js +0 -170
  50. package/node_modules/inputmask/lib/maskScope.js +0 -2498
  51. package/node_modules/inputmask/lib/maskset.js +0 -466
@@ -1,2498 +0,0 @@
1
- var $ = require("./dependencyLibs/inputmask.dependencyLib"), window = require("./global/window"),
2
- document = window.document,
3
- ua = (window.navigator && window.navigator.userAgent) || "",
4
- ie = (ua.indexOf("MSIE ") > 0) || (ua.indexOf("Trident/") > 0),
5
- mobile = "ontouchstart" in window, //not entirely correct but will currently do
6
- iemobile = /iemobile/i.test(ua),
7
- iphone = /iphone/i.test(ua) && !iemobile,
8
- keyCode = require("./keycode");
9
-
10
- //masking scope
11
- //actionObj definition see below
12
- module.exports = function maskScope(actionObj, maskset, opts) {
13
- maskset = maskset || this.maskset;
14
- opts = opts || this.opts;
15
-
16
- var inputmask = this,
17
- el = this.el,
18
- isRTL = this.isRTL || (this.isRTL = opts.numericInput),
19
- undoValue,
20
- $el,
21
- skipKeyPressEvent = false, //Safari 5.1.x - modal dialog fires keypress twice workaround
22
- skipInputEvent = false, //skip when triggered from within inputmask
23
- validationEvent = false,
24
- ignorable = false,
25
- maxLength,
26
- mouseEnter = false,
27
- originalPlaceholder = undefined; //needed for FF
28
-
29
- //maskset helperfunctions
30
- function getMaskTemplate(baseOnInput, minimalPos, includeMode, noJit, clearOptionalTail) {
31
- //includeMode true => input, undefined => placeholder, false => mask
32
-
33
- var greedy = opts.greedy;
34
- if (clearOptionalTail) opts.greedy = false;
35
- minimalPos = minimalPos || 0;
36
- var maskTemplate = [],
37
- ndxIntlzr, pos = 0,
38
- test, testPos, jitRenderStatic;
39
- do {
40
- if (baseOnInput === true && maskset.validPositions[pos]) {
41
- testPos = (clearOptionalTail && maskset.validPositions[pos].match.optionality === true
42
- && maskset.validPositions[pos + 1] === undefined
43
- && (maskset.validPositions[pos].generatedInput === true || (maskset.validPositions[pos].input == opts.skipOptionalPartCharacter && pos > 0)))
44
- ? determineTestTemplate(pos, getTests(pos, ndxIntlzr, pos - 1))
45
- : maskset.validPositions[pos];
46
- test = testPos.match;
47
- ndxIntlzr = testPos.locator.slice();
48
- maskTemplate.push(includeMode === true ? testPos.input : includeMode === false ? test.nativeDef : getPlaceholder(pos, test));
49
- } else {
50
- testPos = getTestTemplate(pos, ndxIntlzr, pos - 1);
51
- test = testPos.match;
52
- ndxIntlzr = testPos.locator.slice();
53
- var jitMasking = noJit === true ? false : (opts.jitMasking !== false ? opts.jitMasking : test.jit);
54
- //check for groupSeparator is a hack for the numerics as we don't want the render of the groupSeparator beforehand
55
- jitRenderStatic = (jitRenderStatic && test.static && test.def !== opts.groupSeparator && test.fn === null) || (maskset.validPositions[pos - 1] && test.static && test.def !== opts.groupSeparator && test.fn === null);
56
- if (jitRenderStatic || jitMasking === false || jitMasking === undefined /*|| pos < lvp*/ || (typeof jitMasking === "number" && isFinite(jitMasking) && jitMasking > pos)) {
57
- maskTemplate.push(includeMode === false ? test.nativeDef : getPlaceholder(pos, test));
58
- } else {
59
- jitRenderStatic = false;
60
- }
61
- }
62
-
63
- pos++;
64
- } while ((maxLength === undefined || pos < maxLength) && (test.static !== true || test.def !== "") || minimalPos > pos);
65
- if (maskTemplate[maskTemplate.length - 1] === "") {
66
- maskTemplate.pop(); //drop the last one which is empty
67
- }
68
- if (includeMode !== false || //do not alter the masklength when just retrieving the maskdefinition
69
- maskset.maskLength === undefined) //just make sure the maskLength gets initialized in all cases (needed for isValid)
70
- {
71
- maskset.maskLength = pos - 1;
72
- }
73
-
74
- opts.greedy = greedy;
75
- return maskTemplate;
76
- }
77
-
78
- function resetMaskSet(soft) {
79
- maskset.buffer = undefined;
80
- if (soft !== true) {
81
- maskset.validPositions = {};
82
- maskset.p = 0;
83
- }
84
- }
85
-
86
- function getLastValidPosition(closestTo, strict, validPositions) {
87
- var before = -1,
88
- after = -1,
89
- valids = validPositions || maskset.validPositions; //for use in valhook ~ context switch
90
- if (closestTo === undefined) closestTo = -1;
91
- for (var posNdx in valids) {
92
- var psNdx = parseInt(posNdx);
93
- if (valids[psNdx] && (strict || valids[psNdx].generatedInput !== true)) {
94
- if (psNdx <= closestTo) before = psNdx;
95
- if (psNdx >= closestTo) after = psNdx;
96
- }
97
- }
98
- return (before === -1 || before == closestTo) ? after : after == -1 ? before : (closestTo - before) < (after - closestTo) ? before : after;
99
- }
100
-
101
- function getDecisionTaker(tst) {
102
- var decisionTaker = tst.locator[tst.alternation];
103
- if (typeof decisionTaker == "string" && decisionTaker.length > 0) { //no decision taken ~ take first one as decider
104
- decisionTaker = decisionTaker.split(",")[0];
105
- }
106
- return decisionTaker !== undefined ? decisionTaker.toString() : "";
107
- }
108
-
109
- function getLocator(tst, align) { //need to align the locators to be correct
110
- var locator = (tst.alternation != undefined ? tst.mloc[getDecisionTaker(tst)] : tst.locator).join("");
111
- if (locator !== "") while (locator.length < align) locator += "0";
112
- return locator;
113
- }
114
-
115
- function determineTestTemplate(pos, tests) {
116
- pos = pos > 0 ? pos - 1 : 0;
117
- var altTest = getTest(pos), targetLocator = getLocator(altTest), tstLocator, closest, bestMatch;
118
- for (var ndx = 0; ndx < tests.length; ndx++) { //find best matching
119
- var tst = tests[ndx];
120
- tstLocator = getLocator(tst, targetLocator.length);
121
- var distance = Math.abs(tstLocator - targetLocator);
122
- if (closest === undefined
123
- || (tstLocator !== "" && distance < closest)
124
- || (bestMatch && !opts.greedy && bestMatch.match.optionality && bestMatch.match.newBlockMarker === "master" && (!tst.match.optionality || !tst.match.newBlockMarker))
125
- || (bestMatch && bestMatch.match.optionalQuantifier && !tst.match.optionalQuantifier)) {
126
- closest = distance;
127
- bestMatch = tst;
128
- }
129
- }
130
-
131
- return bestMatch;
132
- }
133
-
134
-
135
- function getTestTemplate(pos, ndxIntlzr, tstPs) {
136
- return maskset.validPositions[pos] || determineTestTemplate(pos, getTests(pos, ndxIntlzr ? ndxIntlzr.slice() : ndxIntlzr, tstPs));
137
- }
138
-
139
- function getTest(pos, tests) {
140
- if (maskset.validPositions[pos]) {
141
- return maskset.validPositions[pos];
142
- }
143
- return (tests || getTests(pos))[0];
144
- }
145
-
146
- function positionCanMatchDefinition(pos, testDefinition, opts) {
147
- var valid = false,
148
- tests = getTests(pos);
149
- for (var tndx = 0; tndx < tests.length; tndx++) {
150
- if (tests[tndx].match &&
151
- ((tests[tndx].match["nativeDef"] === testDefinition.match[opts.shiftPositions ? "def" : "nativeDef"] && (!opts.shiftPositions || !testDefinition.match.static)) ||
152
- tests[tndx].match["nativeDef"] === testDefinition.match["nativeDef"])) {
153
- valid = true;
154
- break;
155
- } else if (tests[tndx].match && tests[tndx].match["def"] === testDefinition.match["nativeDef"]) {
156
- valid = undefined;
157
- break;
158
- }
159
- }
160
- if (valid === false) {
161
- if (maskset.jitOffset[pos] !== undefined) {
162
- valid = positionCanMatchDefinition(pos + maskset.jitOffset[pos], testDefinition, opts);
163
- }
164
- }
165
- return valid;
166
- }
167
-
168
-
169
- function getTests(pos, ndxIntlzr, tstPs) {
170
- var maskTokens = maskset.maskToken,
171
- testPos = ndxIntlzr ? tstPs : 0,
172
- ndxInitializer = ndxIntlzr ? ndxIntlzr.slice() : [0],
173
- matches = [],
174
- insertStop = false,
175
- latestMatch,
176
- cacheDependency = ndxIntlzr ? ndxIntlzr.join("") : "";
177
-
178
- function resolveTestFromToken(maskToken, ndxInitializer, loopNdx, quantifierRecurse) { //ndxInitializer contains a set of indexes to speedup searches in the mtokens
179
- function handleMatch(match, loopNdx, quantifierRecurse) {
180
- function isFirstMatch(latestMatch, tokenGroup) {
181
- var firstMatch = $.inArray(latestMatch, tokenGroup.matches) === 0;
182
- if (!firstMatch) {
183
- $.each(tokenGroup.matches, function (ndx, match) {
184
- if (match.isQuantifier === true) {
185
- firstMatch = isFirstMatch(latestMatch, tokenGroup.matches[ndx - 1]);
186
- } else if (Object.prototype.hasOwnProperty.call(match, "matches")) firstMatch = isFirstMatch(latestMatch, match);
187
- if (firstMatch) return false;
188
- });
189
- }
190
- return firstMatch;
191
- }
192
-
193
- function resolveNdxInitializer(pos, alternateNdx, targetAlternation) {
194
- var bestMatch, indexPos;
195
-
196
- if (maskset.tests[pos] || maskset.validPositions[pos]) {
197
- $.each(maskset.tests[pos] || [maskset.validPositions[pos]], function (ndx, lmnt) {
198
- if (lmnt.mloc[alternateNdx]) {
199
- bestMatch = lmnt;
200
- return false; //break
201
- }
202
- var alternation = targetAlternation !== undefined ? targetAlternation : lmnt.alternation,
203
- ndxPos = lmnt.locator[alternation] !== undefined ? lmnt.locator[alternation].toString().indexOf(alternateNdx) : -1;
204
- if ((indexPos === undefined || ndxPos < indexPos) && ndxPos !== -1) {
205
- bestMatch = lmnt;
206
- indexPos = ndxPos;
207
- }
208
- });
209
- }
210
- if (bestMatch) {
211
- var bestMatchAltIndex = bestMatch.locator[bestMatch.alternation];
212
- var locator = bestMatch.mloc[alternateNdx] || bestMatch.mloc[bestMatchAltIndex] || bestMatch.locator;
213
- return locator.slice((targetAlternation !== undefined ? targetAlternation : bestMatch.alternation) + 1);
214
- } else {
215
- return targetAlternation !== undefined ? resolveNdxInitializer(pos, alternateNdx) : undefined;
216
- }
217
- }
218
-
219
- function isSubsetOf(source, target) {
220
- function expand(pattern) {
221
- var expanded = [], start = -1, end;
222
- for (var i = 0, l = pattern.length; i < l; i++) {
223
- if (pattern.charAt(i) === "-") {
224
- end = pattern.charCodeAt(i + 1);
225
- while (++start < end) expanded.push(String.fromCharCode(start));
226
- } else {
227
- start = pattern.charCodeAt(i);
228
- expanded.push(pattern.charAt(i));
229
- }
230
- }
231
- return expanded.join("");
232
- }
233
-
234
- if (source.match.def === target.match.nativeDef) return true;
235
- if ((opts.regex || (source.match.fn instanceof RegExp && target.match.fn instanceof RegExp)) && source.match.static !== true && target.match.static !== true) { //is regex a subset
236
- return expand(target.match.fn.toString().replace(/[[\]/]/g, "")).indexOf(expand(source.match.fn.toString().replace(/[[\]/]/g, ""))) !== -1;
237
- }
238
- return false;
239
- }
240
-
241
- function staticCanMatchDefinition(source, target) {
242
- return source.match.static === true && target.match.static !== true ? target.match.fn.test(source.match.def, maskset, pos, false, opts, false) : false;
243
- }
244
-
245
- //mergelocators for retrieving the correct locator match when merging
246
- function setMergeLocators(targetMatch, altMatch) {
247
- var alternationNdx = targetMatch.alternation,
248
- shouldMerge = altMatch === undefined || (alternationNdx === altMatch.alternation &&
249
- targetMatch.locator[alternationNdx].toString().indexOf(altMatch.locator[alternationNdx]) === -1);
250
- if (!shouldMerge && alternationNdx > altMatch.alternation) {
251
- for (var i = altMatch.alternation; i < alternationNdx; i++) {
252
- if (targetMatch.locator[i] !== altMatch.locator[i]) {
253
- alternationNdx = i;
254
- shouldMerge = true;
255
- break;
256
- }
257
- }
258
- }
259
-
260
- if (shouldMerge) {
261
- targetMatch.mloc = targetMatch.mloc || {};
262
- var locNdx = targetMatch.locator[alternationNdx];
263
- if (locNdx === undefined) {
264
- targetMatch.alternation = undefined;
265
- } else {
266
- if (typeof locNdx === "string") locNdx = locNdx.split(",")[0];
267
- if (targetMatch.mloc[locNdx] === undefined) targetMatch.mloc[locNdx] = targetMatch.locator.slice();
268
- if (altMatch !== undefined) {
269
- for (var ndx in altMatch.mloc) {
270
- if (typeof ndx === "string") ndx = ndx.split(",")[0];
271
- if (targetMatch.mloc[ndx] === undefined) targetMatch.mloc[ndx] = altMatch.mloc[ndx];
272
- }
273
- targetMatch.locator[alternationNdx] = Object.keys(targetMatch.mloc).join(",");
274
- }
275
- return true;
276
- }
277
- }
278
- return false;
279
- }
280
-
281
- function isSameLevel(targetMatch, altMatch) {
282
- if (targetMatch.locator.length !== altMatch.locator.length) {
283
- return false;
284
- }
285
- for (let locNdx = targetMatch.alternation + 1; locNdx < targetMatch.locator.length; locNdx++) {
286
- if (targetMatch.locator[locNdx] !== altMatch.locator[locNdx]) {
287
- return false;
288
- }
289
- }
290
- return true;
291
- }
292
-
293
- if (testPos > opts._maxTestPos && quantifierRecurse !== undefined) {
294
- 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. " + maskset.mask;
295
- }
296
- if (testPos === pos && match.matches === undefined) {
297
- matches.push({
298
- "match": match,
299
- "locator": loopNdx.reverse(),
300
- "cd": cacheDependency,
301
- "mloc": {}
302
- });
303
- return true;
304
- } else if (match.matches !== undefined) {
305
- if (match.isGroup && quantifierRecurse !== match) { //when a group pass along to the quantifier
306
- match = handleMatch(maskToken.matches[$.inArray(match, maskToken.matches) + 1], loopNdx, quantifierRecurse);
307
- if (match) return true;
308
- } else if (match.isOptional) {
309
- var optionalToken = match, mtchsNdx = matches.length;
310
- match = resolveTestFromToken(match, ndxInitializer, loopNdx, quantifierRecurse);
311
- if (match) {
312
- //mark optionality in matches
313
- $.each(matches, function (ndx, mtch) {
314
- if (ndx >= mtchsNdx) {
315
- mtch.match.optionality = true;
316
- }
317
- });
318
- latestMatch = matches[matches.length - 1].match;
319
- if (quantifierRecurse === undefined && isFirstMatch(latestMatch, optionalToken)) { //prevent loop see #698
320
- insertStop = true; //insert a stop
321
- testPos = pos; //match the position after the group
322
- } else {
323
- return true;
324
- }
325
- }
326
- } else if (match.isAlternator) {
327
- var alternateToken = match,
328
- malternateMatches = [],
329
- maltMatches,
330
- currentMatches = matches.slice(),
331
- loopNdxCnt = loopNdx.length;
332
- var altIndex = ndxInitializer.length > 0 ? ndxInitializer.shift() : -1;
333
- if (altIndex === -1 || typeof altIndex === "string") {
334
- var currentPos = testPos,
335
- ndxInitializerClone = ndxInitializer.slice(),
336
- altIndexArr = [],
337
- amndx;
338
- if (typeof altIndex == "string") {
339
- altIndexArr = altIndex.split(",");
340
- } else {
341
- for (amndx = 0; amndx < alternateToken.matches.length; amndx++) {
342
- altIndexArr.push(amndx.toString());
343
- }
344
- }
345
-
346
- if (maskset.excludes[pos] !== undefined) {
347
- var altIndexArrClone = altIndexArr.slice();
348
- for (var i = 0, exl = maskset.excludes[pos].length; i < exl; i++) {
349
- var excludeSet = maskset.excludes[pos][i].toString().split(":");
350
- if (loopNdx.length == excludeSet[1]) {
351
- altIndexArr.splice(altIndexArr.indexOf(excludeSet[0]), 1);
352
- }
353
- }
354
- if (altIndexArr.length === 0) { //fully alternated => reset
355
- delete maskset.excludes[pos];
356
- altIndexArr = altIndexArrClone;
357
- }
358
- }
359
- if (opts.keepStatic === true || (isFinite(parseInt(opts.keepStatic)) && currentPos >= opts.keepStatic)) altIndexArr = altIndexArr.slice(0, 1);
360
- var unMatchedAlternation = false;
361
- for (var ndx = 0; ndx < altIndexArr.length; ndx++) {
362
- amndx = parseInt(altIndexArr[ndx]);
363
- matches = [];
364
- //set the correct ndxInitializer
365
- ndxInitializer = typeof altIndex === "string" ? resolveNdxInitializer(testPos, amndx, loopNdxCnt) || ndxInitializerClone.slice() : ndxInitializerClone.slice();
366
- if (alternateToken.matches[amndx] && handleMatch(alternateToken.matches[amndx], [amndx].concat(loopNdx), quantifierRecurse)) {
367
- match = true;
368
- } else if (ndx === 0) {
369
- unMatchedAlternation = true;
370
- }
371
-
372
- maltMatches = matches.slice();
373
- testPos = currentPos;
374
- matches = [];
375
-
376
- //fuzzy merge matches
377
- for (var ndx1 = 0; ndx1 < maltMatches.length; ndx1++) {
378
- var altMatch = maltMatches[ndx1],
379
- dropMatch = false;
380
- altMatch.match.jit = altMatch.match.jit || unMatchedAlternation; //mark jit when there are unmatched alternations ex: mask: "(a|aa)"
381
- altMatch.alternation = altMatch.alternation || loopNdxCnt;
382
- setMergeLocators(altMatch);
383
- for (var ndx2 = 0; ndx2 < malternateMatches.length; ndx2++) {
384
- var altMatch2 = malternateMatches[ndx2];
385
- if (typeof altIndex !== "string" || (altMatch.alternation !== undefined && $.inArray(altMatch.locator[altMatch.alternation].toString(), altIndexArr) !== -1)) {
386
- if (altMatch.match.nativeDef === altMatch2.match.nativeDef) {
387
- dropMatch = true;
388
- setMergeLocators(altMatch2, altMatch);
389
- break;
390
- } else if (isSubsetOf(altMatch, altMatch2)) {
391
- if (setMergeLocators(altMatch, altMatch2)) {
392
- dropMatch = true;
393
- malternateMatches.splice(malternateMatches.indexOf(altMatch2), 0, altMatch);
394
- }
395
- break;
396
- } else if (isSubsetOf(altMatch2, altMatch)) {
397
- setMergeLocators(altMatch2, altMatch);
398
- break;
399
- } else if (staticCanMatchDefinition(altMatch, altMatch2)) {
400
- if (!isSameLevel(altMatch, altMatch2) && el.inputmask.userOptions.keepStatic === undefined) {
401
- opts.keepStatic = true;
402
- } else if (setMergeLocators(altMatch, altMatch2)) {
403
- //insert match above general match
404
- dropMatch = true;
405
- malternateMatches.splice(malternateMatches.indexOf(altMatch2), 0, altMatch);
406
- }
407
- break;
408
- }
409
- }
410
- }
411
- if (!dropMatch) {
412
- malternateMatches.push(altMatch);
413
- }
414
- }
415
- }
416
-
417
- matches = currentMatches.concat(malternateMatches);
418
- testPos = pos;
419
- insertStop = matches.length > 0; //insert a stopelemnt when there is an alternate - needed for non-greedy option
420
- match = malternateMatches.length > 0; //set correct match state
421
-
422
- //cloneback
423
- ndxInitializer = ndxInitializerClone.slice();
424
- } else {
425
- match = handleMatch(alternateToken.matches[altIndex] || maskToken.matches[altIndex], [altIndex].concat(loopNdx), quantifierRecurse);
426
- }
427
- if (match) return true;
428
- } else if (match.isQuantifier && quantifierRecurse !== maskToken.matches[$.inArray(match, maskToken.matches) - 1]) {
429
- var qt = match;
430
- for (var qndx = (ndxInitializer.length > 0) ? ndxInitializer.shift() : 0; (qndx < (isNaN(qt.quantifier.max) ? qndx + 1 : qt.quantifier.max)) && testPos <= pos; qndx++) {
431
- var tokenGroup = maskToken.matches[$.inArray(qt, maskToken.matches) - 1];
432
- match = handleMatch(tokenGroup, [qndx].concat(loopNdx), tokenGroup); //set the tokenGroup as quantifierRecurse marker
433
- if (match) {
434
- //get latest match
435
- latestMatch = matches[matches.length - 1].match;
436
- //mark optionality
437
- //TODO FIX RECURSIVE QUANTIFIERS
438
- latestMatch.optionalQuantifier = qndx >= qt.quantifier.min;
439
- // console.log(pos + " " + qt.quantifier.min + " " + latestMatch.optionalQuantifier);
440
- latestMatch.jit = (qndx || 1) * tokenGroup.matches.indexOf(latestMatch) >= qt.quantifier.jit;
441
- if (latestMatch.optionalQuantifier && isFirstMatch(latestMatch, tokenGroup)) {
442
- insertStop = true;
443
- testPos = pos; //match the position after the group
444
- break; //stop quantifierloop && search for next possible match
445
- }
446
- if (latestMatch.jit /*&& !latestMatch.optionalQuantifier*/) {
447
- //always set jitOffset, isvalid checks when to apply
448
- maskset.jitOffset[pos] = tokenGroup.matches.length - tokenGroup.matches.indexOf(latestMatch);
449
- }
450
- return true;
451
- }
452
- }
453
- } else {
454
- match = resolveTestFromToken(match, ndxInitializer, loopNdx, quantifierRecurse);
455
- if (match) return true;
456
- }
457
- } else {
458
- testPos++;
459
- }
460
- }
461
-
462
- //the offset is set in the quantifierloop when git masking is used
463
- for (var tndx = (ndxInitializer.length > 0 ? ndxInitializer.shift() : 0); tndx < maskToken.matches.length; tndx++) {
464
- if (maskToken.matches[tndx].isQuantifier !== true) {
465
- var match = handleMatch(maskToken.matches[tndx], [tndx].concat(loopNdx), quantifierRecurse);
466
- if (match && testPos === pos) {
467
- return match;
468
- } else if (testPos > pos) {
469
- break;
470
- }
471
- }
472
- }
473
- }
474
-
475
- function mergeLocators(pos, tests) {
476
- var locator = [];
477
- if (!$.isArray(tests)) tests = [tests];
478
- if (tests.length > 0) {
479
- if (tests[0].alternation === undefined || opts.keepStatic === true) {
480
- locator = determineTestTemplate(pos, tests.slice()).locator.slice();
481
- if (locator.length === 0) locator = tests[0].locator.slice();
482
- } else {
483
- $.each(tests, function (ndx, tst) {
484
- if (tst.def !== "") {
485
- if (locator.length === 0) {
486
- locator = tst.locator.slice();
487
- } else {
488
- for (var i = 0; i < locator.length; i++) {
489
- if (tst.locator[i] && locator[i].toString().indexOf(tst.locator[i]) === -1) {
490
- locator[i] += "," + tst.locator[i];
491
- }
492
- }
493
- }
494
- }
495
- });
496
- }
497
- }
498
- return locator;
499
- }
500
-
501
- if (pos > -1 && (maxLength === undefined || pos < maxLength)) {
502
- if (ndxIntlzr === undefined) { //determine index initializer
503
- var previousPos = pos - 1,
504
- test;
505
- while ((test = maskset.validPositions[previousPos] || maskset.tests[previousPos]) === undefined && previousPos > -1) {
506
- previousPos--;
507
- }
508
- if (test !== undefined && previousPos > -1) {
509
- ndxInitializer = mergeLocators(previousPos, test);
510
- cacheDependency = ndxInitializer.join("");
511
- testPos = previousPos;
512
- }
513
- }
514
- if (maskset.tests[pos] && maskset.tests[pos][0].cd === cacheDependency) { //cacheDependency is set on all tests, just check on the first
515
- return maskset.tests[pos];
516
- }
517
- for (var mtndx = ndxInitializer.shift(); mtndx < maskTokens.length; mtndx++) {
518
- var match = resolveTestFromToken(maskTokens[mtndx], ndxInitializer, [mtndx]);
519
- if ((match && testPos === pos) || testPos > pos) {
520
- break;
521
- }
522
- }
523
- }
524
- if (matches.length === 0 || insertStop) {
525
- matches.push({
526
- match: {
527
- fn: null,
528
- static: true,
529
- optionality: false,
530
- casing: null,
531
- def: "",
532
- placeholder: ""
533
- },
534
- locator: [],
535
- mloc: {},
536
- cd: cacheDependency
537
- });
538
- }
539
-
540
- if (ndxIntlzr !== undefined && maskset.tests[pos]) { //prioritize full tests for caching
541
- return $.extend(true, [], matches);
542
- }
543
- maskset.tests[pos] = $.extend(true, [], matches); //set a clone to prevent overwriting some props
544
- // console.log(pos + " - " + JSON.stringify(matches));
545
- return maskset.tests[pos];
546
- }
547
-
548
- function getBufferTemplate() {
549
- if (maskset._buffer === undefined) {
550
- //generate template
551
- maskset._buffer = getMaskTemplate(false, 1);
552
- if (maskset.buffer === undefined) maskset.buffer = maskset._buffer.slice();
553
- }
554
- return maskset._buffer;
555
- }
556
-
557
- function getBuffer(noCache) {
558
- if (maskset.buffer === undefined || noCache === true) {
559
- maskset.buffer = getMaskTemplate(true, getLastValidPosition(), true);
560
- if (maskset._buffer === undefined) maskset._buffer = maskset.buffer.slice();
561
- }
562
- return maskset.buffer;
563
- }
564
-
565
- function refreshFromBuffer(start, end, buffer) {
566
- // checkVal.call(inputmask, el, false, true, isRTL ? buffer.reverse() : buffer);
567
- var i, p, skipOptionalPartCharacter = opts.skipOptionalPartCharacter,
568
- bffr = isRTL ? buffer.slice().reverse() : buffer;
569
- opts.skipOptionalPartCharacter = "";
570
- if (start === true) {
571
- resetMaskSet();
572
- maskset.tests = {}; //refresh tests after possible alternating
573
- start = 0;
574
- end = buffer.length;
575
- p = determineNewCaretPosition({begin: 0, end: 0}, false).begin;
576
- } else {
577
- for (i = start; i < end; i++) {
578
- delete maskset.validPositions[i];
579
- }
580
- p = start;
581
- }
582
-
583
- var keypress = new $.Event("keypress");
584
- for (i = start; i < end; i++) {
585
- keypress.which = bffr[i].toString().charCodeAt(0);
586
- ignorable = false; //make sure ignorable is ignored ;-)
587
- var valResult = EventHandlers.keypressEvent.call(el, keypress, true, false, false, p);
588
- if (valResult !== false) {
589
- p = valResult.forwardPosition;
590
- }
591
- }
592
-
593
- opts.skipOptionalPartCharacter = skipOptionalPartCharacter;
594
- }
595
-
596
- function casing(elem, test, pos) {
597
- switch (opts.casing || test.casing) {
598
- case "upper":
599
- elem = elem.toUpperCase();
600
- break;
601
- case "lower":
602
- elem = elem.toLowerCase();
603
- break;
604
- case "title":
605
- var posBefore = maskset.validPositions[pos - 1];
606
- if (pos === 0 || posBefore && posBefore.input === String.fromCharCode(keyCode.SPACE)) {
607
- elem = elem.toUpperCase();
608
- } else {
609
- elem = elem.toLowerCase();
610
- }
611
- break;
612
- default:
613
- if ($.isFunction(opts.casing)) {
614
- var args = Array.prototype.slice.call(arguments);
615
- args.push(maskset.validPositions);
616
- elem = opts.casing.apply(this, args);
617
- }
618
- }
619
-
620
- return elem;
621
- }
622
-
623
- function checkAlternationMatch(altArr1, altArr2, na) {
624
- var altArrC = opts.greedy ? altArr2 : altArr2.slice(0, 1),
625
- isMatch = false,
626
- naArr = na !== undefined ? na.split(",") : [],
627
- naNdx;
628
-
629
- //remove no alternate indexes from alternation array
630
- for (var i = 0; i < naArr.length; i++) {
631
- if ((naNdx = altArr1.indexOf(naArr[i])) !== -1) {
632
- altArr1.splice(naNdx, 1);
633
- }
634
- }
635
-
636
- for (var alndx = 0; alndx < altArr1.length; alndx++) {
637
- if ($.inArray(altArr1[alndx], altArrC) !== -1) {
638
- isMatch = true;
639
- break;
640
- }
641
- }
642
- return isMatch;
643
- }
644
-
645
- function alternate(maskPos, c, strict, fromIsValid, rAltPos, selection) { //pos == true => generalize
646
- var validPsClone = $.extend(true, {}, maskset.validPositions),
647
- tstClone = $.extend(true, {}, maskset.tests),
648
- lastAlt,
649
- alternation,
650
- isValidRslt = false, returnRslt = false,
651
- altPos, prevAltPos, i, validPos,
652
- decisionPos,
653
- lAltPos = rAltPos !== undefined ? rAltPos : getLastValidPosition(), nextPos, input, begin, end;
654
-
655
- if (selection) {
656
- begin = selection.begin;
657
- end = selection.end;
658
- if (selection.begin > selection.end) {
659
- begin = selection.end;
660
- end = selection.begin;
661
- }
662
- }
663
- if (lAltPos === -1 && rAltPos === undefined) { //do not recurse when already paste the beginning
664
- lastAlt = 0;
665
- prevAltPos = getTest(lastAlt);
666
- alternation = prevAltPos.alternation;
667
- } else {
668
- //find last modified alternation
669
- for (; lAltPos >= 0; lAltPos--) {
670
- altPos = maskset.validPositions[lAltPos];
671
- if (altPos && altPos.alternation !== undefined) {
672
- if (prevAltPos && prevAltPos.locator[altPos.alternation] !== altPos.locator[altPos.alternation]) {
673
- break;
674
- }
675
- lastAlt = lAltPos;
676
- alternation = maskset.validPositions[lastAlt].alternation;
677
- prevAltPos = altPos;
678
- }
679
- }
680
- }
681
-
682
- if (alternation !== undefined) {
683
- decisionPos = parseInt(lastAlt);
684
- maskset.excludes[decisionPos] = maskset.excludes[decisionPos] || [];
685
- if (maskPos !== true) { //generalize
686
- maskset.excludes[decisionPos].push(getDecisionTaker(prevAltPos) + ":" + prevAltPos.alternation);
687
- }
688
-
689
- var validInputs = [], resultPos = -1;
690
- for (i = decisionPos; i < getLastValidPosition(undefined, true) + 1; i++) {
691
- if (resultPos === -1 && maskPos <= i && c !== undefined) {
692
- validInputs.push(c);
693
- resultPos = validInputs.length - 1;
694
- }
695
- validPos = maskset.validPositions[i];
696
- if (validPos && validPos.generatedInput !== true && (selection === undefined || (i < begin || i >= end))) {
697
- validInputs.push(validPos.input);
698
- }
699
- delete maskset.validPositions[i];
700
- }
701
- if (resultPos === -1 && c !== undefined) {
702
- validInputs.push(c);
703
- resultPos = validInputs.length - 1;
704
- }
705
-
706
- while (maskset.excludes[decisionPos] !== undefined && maskset.excludes[decisionPos].length < 10) {
707
- // maskset.tests[decisionPos] = undefined; //clear decisionPos
708
- maskset.tests = {}; //clear all
709
- resetMaskSet(true); //clear getbuffer
710
- isValidRslt = true;
711
- for (i = 0; i < validInputs.length; i++) {
712
- nextPos = isValidRslt.caret || (getLastValidPosition(undefined, true) + 1);
713
- input = validInputs[i];
714
- if (!(isValidRslt = isValid(nextPos, input, false, fromIsValid, true))) {
715
- break;
716
- }
717
- if (i === resultPos) {
718
- returnRslt = isValidRslt;
719
- }
720
- if (maskPos == true && isValidRslt) { //return validposition on generalise
721
- returnRslt = {caretPos: i};
722
- }
723
- }
724
- if (!isValidRslt) {
725
- resetMaskSet();
726
- prevAltPos = getTest(decisionPos); //get the current decisionPos to exclude ~ needs to be before restoring the initial validation
727
- //reset & revert
728
- maskset.validPositions = $.extend(true, {}, validPsClone);
729
- maskset.tests = $.extend(true, {}, tstClone); //refresh tests after possible alternating
730
- if (maskset.excludes[decisionPos]) {
731
- var decisionTaker = getDecisionTaker(prevAltPos);
732
- if (maskset.excludes[decisionPos].indexOf(decisionTaker + ":" + prevAltPos.alternation) !== -1) {
733
- returnRslt = alternate(maskPos, c, strict, fromIsValid, decisionPos - 1, selection);
734
- break;
735
- }
736
- maskset.excludes[decisionPos].push(decisionTaker + ":" + prevAltPos.alternation);
737
- for (i = decisionPos; i < getLastValidPosition(undefined, true) + 1; i++) delete maskset.validPositions[i];
738
- } else { //latest alternation
739
- returnRslt = alternate(maskPos, c, strict, fromIsValid, decisionPos - 1, selection);
740
- break;
741
- }
742
- } else {
743
- break;
744
- }
745
- }
746
- }
747
- //reset alternation excludes
748
- if (!returnRslt || opts.keepStatic !== false) {
749
- delete maskset.excludes[decisionPos];
750
- }
751
- return returnRslt;
752
- }
753
-
754
- function isValid(pos, c, strict, fromIsValid, fromAlternate, validateOnly) { //strict true ~ no correction or autofill
755
- function isSelection(posObj) {
756
- return isRTL ? (posObj.begin - posObj.end) > 1 || ((posObj.begin - posObj.end) === 1) :
757
- (posObj.end - posObj.begin) > 1 || ((posObj.end - posObj.begin) === 1);
758
- }
759
-
760
- strict = strict === true; //always set a value to strict to prevent possible strange behavior in the extensions
761
-
762
- var maskPos = pos;
763
- if (pos.begin !== undefined) { //position was a position object - used to handle a delete by typing over a selection
764
- maskPos = isRTL ? pos.end : pos.begin;
765
- }
766
-
767
- function processCommandObject(commandObj) {
768
- if (commandObj !== undefined) {
769
- if (commandObj.remove !== undefined) { //remove position(s)
770
- if (!$.isArray(commandObj.remove)) commandObj.remove = [commandObj.remove];
771
- $.each(commandObj.remove.sort(function (a, b) {
772
- return b.pos - a.pos;
773
- }), function (ndx, lmnt) {
774
- revalidateMask({begin: lmnt, end: lmnt + 1});
775
- });
776
- commandObj.remove = undefined;
777
- }
778
- if (commandObj.insert !== undefined) { //insert position(s)
779
- if (!$.isArray(commandObj.insert)) commandObj.insert = [commandObj.insert];
780
- $.each(commandObj.insert.sort(function (a, b) {
781
- return a.pos - b.pos;
782
- }), function (ndx, lmnt) {
783
- if (lmnt.c !== "") {
784
- isValid(lmnt.pos, lmnt.c, lmnt.strict !== undefined ? lmnt.strict : true, lmnt.fromIsValid !== undefined ? lmnt.fromIsValid : fromIsValid);
785
- }
786
- });
787
- commandObj.insert = undefined;
788
- }
789
-
790
- if (commandObj.refreshFromBuffer && commandObj.buffer) {
791
- var refresh = commandObj.refreshFromBuffer;
792
- refreshFromBuffer(refresh === true ? refresh : refresh.start, refresh.end, commandObj.buffer);
793
- commandObj.refreshFromBuffer = undefined;
794
- }
795
-
796
- if (commandObj.rewritePosition !== undefined) {
797
- maskPos = commandObj.rewritePosition;
798
- // commandObj.rewritePosition = undefined;
799
- commandObj = true;
800
- }
801
- }
802
- return commandObj;
803
- }
804
-
805
- function _isValid(position, c, strict) {
806
- var rslt = false;
807
-
808
- $.each(getTests(position), function (ndx, tst) {
809
- var test = tst.match;
810
- //make sure the buffer is set and correct
811
- getBuffer(true);
812
- //return is false or a json object => { pos: ??, c: ??} or true
813
- rslt = test.fn != null ?
814
- test.fn.test(c, maskset, position, strict, opts, isSelection(pos)) : (c === test.def || c === opts.skipOptionalPartCharacter) && test.def !== "" ? //non mask
815
- {
816
- c: getPlaceholder(position, test, true) || test.def,
817
- pos: position
818
- } : false;
819
-
820
- if (rslt !== false) {
821
- var elem = rslt.c !== undefined ? rslt.c : c, validatedPos = position;
822
- elem = (elem === opts.skipOptionalPartCharacter && test.static === true) ?
823
- (getPlaceholder(position, test, true) || test.def) : elem;
824
-
825
- rslt = processCommandObject(rslt);
826
-
827
- if (rslt !== true && rslt.pos !== undefined && rslt.pos !== position) { //their is a position offset
828
- validatedPos = rslt.pos;
829
- }
830
-
831
- if (rslt !== true && rslt.pos === undefined && rslt.c === undefined) {
832
- return false; //breakout if nothing to insert
833
- }
834
-
835
- if (revalidateMask(pos, $.extend({}, tst, {
836
- "input": casing(elem, test, validatedPos)
837
- }), fromIsValid, validatedPos) === false) {
838
- rslt = false;
839
- }
840
- return false; //break from $.each
841
- }
842
- });
843
- return rslt;
844
- }
845
-
846
- var result = true,
847
- positionsClone = $.extend(true, {}, maskset.validPositions); //clone the currentPositions
848
-
849
- if (opts.keepStatic === false && maskset.excludes[maskPos] !== undefined && fromAlternate !== true && fromIsValid !== true) {
850
- for (var i = maskPos; i < (isRTL ? pos.begin : pos.end); i++) {
851
- if (maskset.excludes[i] !== undefined) {
852
- maskset.excludes[i] = undefined;
853
- delete maskset.tests[i];
854
- }
855
- }
856
- }
857
-
858
- if ($.isFunction(opts.preValidation) && fromIsValid !== true && validateOnly !== true) {
859
- result = opts.preValidation.call(el, getBuffer(), maskPos, c, isSelection(pos), opts, maskset, pos, strict || fromAlternate);
860
- result = processCommandObject(result);
861
- }
862
- if (result === true) { //preValidation result
863
- if (maxLength === undefined || maskPos < maxLength) {
864
- result = _isValid(maskPos, c, strict);
865
- if ((!strict || fromIsValid === true) && result === false && validateOnly !== true) {
866
- var currentPosValid = maskset.validPositions[maskPos];
867
- if (currentPosValid && currentPosValid.match.static === true && (currentPosValid.match.def === c || c === opts.skipOptionalPartCharacter)) {
868
- result = {
869
- "caret": seekNext(maskPos)
870
- };
871
- } else {
872
- if (opts.insertMode || maskset.validPositions[seekNext(maskPos)] === undefined || pos.end > maskPos) { //does the input match on a further position?
873
- var skip = false;
874
- if (maskset.jitOffset[maskPos] && maskset.validPositions[seekNext(maskPos)] === undefined) {
875
- result = isValid(maskPos + maskset.jitOffset[maskPos], c, true);
876
- if (result !== false) {
877
- if (fromAlternate !== true) result.caret = maskPos;
878
- skip = true;
879
- }
880
- }
881
- if (pos.end > maskPos) {
882
- maskset.validPositions[maskPos] = undefined;
883
- }
884
- if (!skip && !isMask(maskPos, opts.keepStatic)) {
885
- for (var nPos = maskPos + 1, snPos = seekNext(maskPos); nPos <= snPos; nPos++) {
886
- // if (!isMask(nPos, true)) {
887
- // continue;
888
- // }
889
- result = _isValid(nPos, c, strict);
890
- if (result !== false) {
891
- result = trackbackPositions(maskPos, result.pos !== undefined ? result.pos : nPos) || result;
892
- maskPos = nPos;
893
- break;
894
- }
895
- }
896
- }
897
- }
898
- }
899
- }
900
- } else {
901
- result = false;
902
- }
903
-
904
-
905
- if (result === false && opts.keepStatic && (isComplete(getBuffer()) || maskPos === 0) && !strict && fromAlternate !== true) { //try fuzzy alternator logic
906
- result = alternate(maskPos, c, strict, fromIsValid, undefined, pos);
907
- } else if (isSelection(pos) && maskset.tests[maskPos] && maskset.tests[maskPos].length > 1 && opts.keepStatic && !strict && fromAlternate !== true) { //selection clears an alternated keepstatic mask ~ #2189
908
- result = alternate(true);
909
- }
910
-
911
- if (result === true) {
912
- result = {
913
- "pos": maskPos
914
- };
915
- }
916
- }
917
- if ($.isFunction(opts.postValidation) && fromIsValid !== true && validateOnly !== true) {
918
- var postResult = opts.postValidation.call(el, getBuffer(true), pos.begin !== undefined ? (isRTL ? pos.end : pos.begin) : pos, c, result, opts, maskset, strict);
919
- if (postResult !== undefined) {
920
- result = postResult === true ? result : postResult;
921
- }
922
- }
923
-
924
- if (result && result.pos === undefined) {
925
- result.pos = maskPos;
926
- }
927
-
928
- if (result === false || validateOnly === true) {
929
- resetMaskSet(true);
930
- maskset.validPositions = $.extend(true, {}, positionsClone); //revert validation changes
931
- } else {
932
- trackbackPositions(undefined, maskPos, true);
933
- }
934
-
935
- var endResult = processCommandObject(result);
936
- // console.log("returned result " + JSON.stringify(endResult));
937
- return endResult;
938
- }
939
-
940
- //fill in best positions according the current input
941
- function trackbackPositions(originalPos, newPos, fillOnly) {
942
- // console.log("trackbackPositions " + originalPos + " " + newPos);
943
- if (originalPos === undefined) {
944
- //find previous valid
945
- for (originalPos = newPos - 1; originalPos > 0; originalPos--) {
946
- if (maskset.validPositions[originalPos]) break;
947
- }
948
- }
949
- for (var ps = originalPos; ps < newPos; ps++) {
950
- if (maskset.validPositions[ps] === undefined && !isMask(ps, true)) {
951
- var vp = ps == 0 ? getTest(ps) : maskset.validPositions[ps - 1];
952
- if (vp) {
953
- var tests = getTests(ps).slice();
954
- if (tests[tests.length - 1].match.def === "") tests.pop();
955
- var bestMatch = determineTestTemplate(ps, tests), np;
956
- if (bestMatch && (bestMatch.match.jit !== true || (bestMatch.match.newBlockMarker === "master" && (np = maskset.validPositions[ps + 1]) && np.match.optionalQuantifier === true))) {
957
- bestMatch = $.extend({}, bestMatch, {
958
- "input": getPlaceholder(ps, bestMatch.match, true) || bestMatch.match.def
959
- });
960
- bestMatch.generatedInput = true;
961
- revalidateMask(ps, bestMatch, true);
962
-
963
- if (fillOnly !== true) {
964
- //revalidate the new position to update the locator value
965
- var cvpInput = maskset.validPositions[newPos].input;
966
- maskset.validPositions[newPos] = undefined;
967
- return isValid(newPos, cvpInput, true, true);
968
- }
969
- }
970
- }
971
- }
972
- }
973
- }
974
-
975
- function revalidateMask(pos, validTest, fromIsValid, validatedPos) {
976
- function IsEnclosedStatic(pos, valids, selection) {
977
- var posMatch = valids[pos];
978
- if (posMatch !== undefined && posMatch.match.static === true && posMatch.match.optionality !== true && (valids[0] === undefined || valids[0].alternation === undefined)) {
979
- var prevMatch = selection.begin <= pos - 1 ? valids[pos - 1] && valids[pos - 1].match.static === true && valids[pos - 1] : valids[pos - 1],
980
- nextMatch = selection.end > pos + 1 ? valids[pos + 1] && valids[pos + 1].match.static === true && valids[pos + 1] : valids[pos + 1];
981
- return prevMatch && nextMatch;
982
- }
983
- return false;
984
- }
985
-
986
- var offset = 0, begin = pos.begin !== undefined ? pos.begin : pos, end = pos.end !== undefined ? pos.end : pos;
987
- if (pos.begin > pos.end) {
988
- begin = pos.end;
989
- end = pos.begin;
990
- }
991
-
992
- validatedPos = validatedPos !== undefined ? validatedPos : begin;
993
- if (begin !== end || (opts.insertMode && maskset.validPositions[validatedPos] !== undefined && fromIsValid === undefined) || validTest === undefined) {
994
- //reposition & revalidate others
995
- var positionsClone = $.extend(true, {}, maskset.validPositions),
996
- lvp = getLastValidPosition(undefined, true),
997
- i;
998
- maskset.p = begin; //needed for alternated position after overtype selection
999
-
1000
- for (i = lvp; i >= begin; i--) {
1001
- delete maskset.validPositions[i];
1002
- if (validTest === undefined) delete maskset.tests[i + 1];
1003
- }
1004
-
1005
- var valid = true, j = validatedPos,
1006
- posMatch = j, t, canMatch;
1007
-
1008
- if (validTest) {
1009
- maskset.validPositions[validatedPos] = $.extend(true, {}, validTest);
1010
- posMatch++;
1011
- j++;
1012
- }
1013
-
1014
- for (i = validTest ? end : end - 1; i <= lvp; i++) {
1015
- if ((t = positionsClone[i]) !== undefined && t.generatedInput !== true &&
1016
- (i >= end || (i >= begin && IsEnclosedStatic(i, positionsClone, {
1017
- begin: begin,
1018
- end: end
1019
- })))) {
1020
- while (getTest(posMatch).match.def !== "") { //loop needed to match further positions
1021
- if ((canMatch = positionCanMatchDefinition(posMatch, t, opts)) !== false || t.match.def === "+") { //validated match //we still need some hackery for the + validator (numeric alias)
1022
- if (t.match.def === "+") getBuffer(true);
1023
- var result = isValid(posMatch, t.input, t.match.def !== "+", t.match.def !== "+");
1024
- valid = result !== false;
1025
- j = (result.pos || posMatch) + 1;
1026
- if (!valid && canMatch) break;
1027
- } else {
1028
- valid = false;
1029
- }
1030
- if (valid) {
1031
- if (validTest === undefined && t.match.static && i === pos.begin) offset++;
1032
- break;
1033
- }
1034
- if (!valid && posMatch > maskset.maskLength) {
1035
- break;
1036
- }
1037
- posMatch++;
1038
- }
1039
- if (getTest(posMatch).match.def == "") {
1040
- valid = false;
1041
- }
1042
- //restore position
1043
- posMatch = j;
1044
- }
1045
- if (!valid) break;
1046
- }
1047
-
1048
- if (!valid) {
1049
- maskset.validPositions = $.extend(true, {}, positionsClone);
1050
- resetMaskSet(true);
1051
- return false;
1052
- }
1053
- } else if (validTest && getTest(validatedPos).match.cd === validTest.match.cd) {
1054
- maskset.validPositions[validatedPos] = $.extend(true, {}, validTest);
1055
- }
1056
-
1057
- resetMaskSet(true);
1058
- return offset;
1059
- }
1060
-
1061
- function isMask(pos, strict, fuzzy) {
1062
- var test = getTestTemplate(pos).match;
1063
- if (test.def === "") test = getTest(pos).match;
1064
-
1065
- if (test.static !== true) {
1066
- return test.fn;
1067
- }
1068
- if (fuzzy === true && (maskset.validPositions[pos] !== undefined && maskset.validPositions[pos].generatedInput !== true)) {
1069
- return true;
1070
- }
1071
-
1072
- if (strict !== true && pos > -1) {
1073
- if (fuzzy) { //check on the number of tests
1074
- var tests = getTests(pos);
1075
- return tests.length > (1 + (tests[tests.length - 1].match.def === "" ? 1 : 0));
1076
- }
1077
- //else based on the template
1078
- var testTemplate = determineTestTemplate(pos, getTests(pos));
1079
- var testPlaceHolder = getPlaceholder(pos, testTemplate.match);
1080
- return testTemplate.match.def !== testPlaceHolder;
1081
-
1082
- }
1083
- return false;
1084
- }
1085
-
1086
- function seekNext(pos, newBlock, fuzzy) {
1087
- if (fuzzy === undefined) fuzzy = true;
1088
- var position = pos + 1;
1089
- while (getTest(position).match.def !== "" &&
1090
- ((newBlock === true && (getTest(position).match.newBlockMarker !== true || !isMask(position, undefined, true))) ||
1091
- (newBlock !== true && !isMask(position, undefined, fuzzy)))) {
1092
- position++;
1093
- }
1094
- return position;
1095
- }
1096
-
1097
- function seekPrevious(pos, newBlock) {
1098
- var position = pos,
1099
- tests;
1100
- if (position <= 0) return 0;
1101
-
1102
- while (--position > 0 &&
1103
- ((newBlock === true && getTest(position).match.newBlockMarker !== true) ||
1104
- (newBlock !== true && !isMask(position, undefined, true) &&
1105
- // eslint-disable-next-line no-empty
1106
- (tests = getTests(position), tests.length < 2 || (tests.length === 2 && tests[1].match.def === ""))))) {
1107
- }
1108
- return position;
1109
- }
1110
-
1111
- function writeBuffer(input, buffer, caretPos, event, triggerEvents) {
1112
- if (event && $.isFunction(opts.onBeforeWrite)) {
1113
- // buffer = buffer.slice(); //prevent uncontrolled manipulation of the internal buffer
1114
- var result = opts.onBeforeWrite.call(inputmask, event, buffer, caretPos, opts);
1115
- if (result) {
1116
- if (result.refreshFromBuffer) {
1117
- var refresh = result.refreshFromBuffer;
1118
- refreshFromBuffer(refresh === true ? refresh : refresh.start, refresh.end, result.buffer || buffer);
1119
- buffer = getBuffer(true);
1120
- }
1121
- if (caretPos !== undefined) caretPos = result.caret !== undefined ? result.caret : caretPos;
1122
- }
1123
- }
1124
- if (input !== undefined) {
1125
- input.inputmask._valueSet(buffer.join(""));
1126
- if (caretPos !== undefined && (event === undefined || event.type !== "blur")) {
1127
- caret(input, caretPos, undefined, undefined, (event !== undefined && event.type === "keydown" && (event.keyCode === keyCode.DELETE || event.keyCode === keyCode.BACKSPACE)));
1128
- }
1129
- if (triggerEvents === true) {
1130
- var $input = $(input), nptVal = input.inputmask._valueGet();
1131
- skipInputEvent = true;
1132
- $input.trigger("input");
1133
- setTimeout(function () { //timeout needed for IE
1134
- if (nptVal === getBufferTemplate().join("")) {
1135
- $input.trigger("cleared");
1136
- } else if (isComplete(buffer) === true) {
1137
- $input.trigger("complete");
1138
- }
1139
- }, 0);
1140
- }
1141
- }
1142
- }
1143
-
1144
- function getPlaceholder(pos, test, returnPL) {
1145
- test = test || getTest(pos).match;
1146
- if (test.placeholder !== undefined || returnPL === true) {
1147
- return $.isFunction(test.placeholder) ? test.placeholder(opts) : test.placeholder;
1148
- } else if (test.static === true) {
1149
- if (pos > -1 && maskset.validPositions[pos] === undefined) {
1150
- var tests = getTests(pos),
1151
- staticAlternations = [],
1152
- prevTest;
1153
- if (tests.length > 1 + (tests[tests.length - 1].match.def === "" ? 1 : 0)) {
1154
- for (var i = 0; i < tests.length; i++) {
1155
- if (tests[i].match.def !== "" && tests[i].match.optionality !== true && tests[i].match.optionalQuantifier !== true &&
1156
- (tests[i].match.static === true || (prevTest === undefined || tests[i].match.fn.test(prevTest.match.def, maskset, pos, true, opts) !== false))) {
1157
- staticAlternations.push(tests[i]);
1158
- if (tests[i].match.static === true) prevTest = tests[i];
1159
- if (staticAlternations.length > 1) {
1160
- if (/[0-9a-bA-Z]/.test(staticAlternations[0].match.def)) {
1161
- return opts.placeholder.charAt(pos % opts.placeholder.length);
1162
- }
1163
- }
1164
- }
1165
- }
1166
- }
1167
- }
1168
- return test.def;
1169
- }
1170
-
1171
- return opts.placeholder.charAt(pos % opts.placeholder.length);
1172
- }
1173
-
1174
- function HandleNativePlaceholder(npt, value) {
1175
- if (ie) {
1176
- if (npt.inputmask._valueGet() !== value && (npt.placeholder !== value || npt.placeholder === "")) {
1177
- var buffer = getBuffer().slice(),
1178
- nptValue = npt.inputmask._valueGet();
1179
- if (nptValue !== value) {
1180
- var lvp = getLastValidPosition();
1181
- if (lvp === -1 && nptValue === getBufferTemplate().join("")) {
1182
- buffer = [];
1183
- } else if (lvp !== -1) { //clearout optional tail of the mask
1184
- clearOptionalTail(buffer);
1185
- }
1186
- writeBuffer(npt, buffer);
1187
- }
1188
- }
1189
- } else if (npt.placeholder !== value) {
1190
- npt.placeholder = value;
1191
- if (npt.placeholder === "") npt.removeAttribute("placeholder");
1192
- }
1193
- }
1194
-
1195
- function determineNewCaretPosition(selectedCaret, tabbed) {
1196
- function doRadixFocus(clickPos) {
1197
- if (opts.radixPoint !== "" && opts.digits !== 0) {
1198
- var vps = maskset.validPositions;
1199
- if (vps[clickPos] === undefined || (vps[clickPos].input === getPlaceholder(clickPos))) {
1200
- if (clickPos < seekNext(-1)) return true;
1201
- var radixPos = $.inArray(opts.radixPoint, getBuffer());
1202
- if (radixPos !== -1) {
1203
- for (var vp in vps) {
1204
- if (vps[vp] && radixPos < vp && vps[vp].input !== getPlaceholder(vp)) {
1205
- return false;
1206
- }
1207
- }
1208
- return true;
1209
- }
1210
- }
1211
- }
1212
- return false;
1213
- }
1214
-
1215
- if (tabbed) {
1216
- if (isRTL) {
1217
- selectedCaret.end = selectedCaret.begin;
1218
- } else {
1219
- selectedCaret.begin = selectedCaret.end;
1220
- }
1221
- }
1222
- if (selectedCaret.begin === selectedCaret.end) {
1223
- switch (opts.positionCaretOnClick) {
1224
- case "none":
1225
- break;
1226
- case "select":
1227
- selectedCaret = {begin: 0, end: getBuffer().length};
1228
- break;
1229
- case "ignore":
1230
- selectedCaret.end = selectedCaret.begin = seekNext(getLastValidPosition());
1231
- break;
1232
- case "radixFocus":
1233
- if (doRadixFocus(selectedCaret.begin)) {
1234
- var radixPos = getBuffer().join("").indexOf(opts.radixPoint);
1235
- selectedCaret.end = selectedCaret.begin = opts.numericInput ? seekNext(radixPos) : radixPos;
1236
- break;
1237
- } //fallback to lvp
1238
- // eslint-disable-next-line no-fallthrough
1239
- default: //lvp:
1240
- var clickPosition = selectedCaret.begin,
1241
- lvclickPosition = getLastValidPosition(clickPosition, true),
1242
- lastPosition = seekNext((lvclickPosition === -1 && !isMask(0)) ? 0 : lvclickPosition);
1243
- if (clickPosition < lastPosition) {
1244
- selectedCaret.end = selectedCaret.begin = !isMask(clickPosition, true) && !isMask(clickPosition - 1, true) ? seekNext(clickPosition) : clickPosition;
1245
- } else {
1246
- var lvp = maskset.validPositions[lvclickPosition],
1247
- tt = getTestTemplate(lastPosition, lvp ? lvp.match.locator : undefined, lvp),
1248
- placeholder = getPlaceholder(lastPosition, tt.match);
1249
- if ((placeholder !== "" && getBuffer()[lastPosition] !== placeholder && tt.match.optionalQuantifier !== true && tt.match.newBlockMarker !== true) || (!isMask(lastPosition, opts.keepStatic) && tt.match.def === placeholder)) {
1250
- var newPos = seekNext(lastPosition);
1251
- if (clickPosition >= newPos || clickPosition === lastPosition) {
1252
- lastPosition = newPos;
1253
- }
1254
- }
1255
- selectedCaret.end = selectedCaret.begin = lastPosition;
1256
- }
1257
- }
1258
- return selectedCaret;
1259
- }
1260
- }
1261
-
1262
- var EventRuler = {
1263
- on: function (input, eventName, eventHandler) {
1264
- var ev = function (e) {
1265
- if (e.originalEvent) {
1266
- e = e.originalEvent || e; //get original event from jquery evenbt
1267
- arguments[0] = e;
1268
- }
1269
-
1270
-
1271
- var that = this, args;
1272
- if (that.inputmask === undefined && this.nodeName !== "FORM") { //happens when cloning an object with jquery.clone
1273
- var imOpts = $.data(that, "_inputmask_opts");
1274
- if (imOpts) {
1275
- (new Inputmask(imOpts)).mask(that);
1276
- } else {
1277
- EventRuler.off(that);
1278
- }
1279
- } 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 === keyCode.TAB))))) {
1280
- e.preventDefault();
1281
- } else {
1282
- switch (e.type) {
1283
- case "input":
1284
- if (skipInputEvent === true || (e.inputType && e.inputType === "insertCompositionText")) {
1285
- skipInputEvent = false;
1286
- return e.preventDefault();
1287
- }
1288
-
1289
- // if (mobile) { //this caudes problem see #2220
1290
- // args = arguments;
1291
- // setTimeout(function () { //needed for caret selection when entering a char on Android 8 - #1818
1292
- // eventHandler.apply(that, args);
1293
- // caret(that, that.inputmask.caretPos, undefined, true);
1294
- // }, 0);
1295
- // return false;
1296
- // }
1297
- break;
1298
- case "keydown":
1299
- //Safari 5.1.x - modal dialog fires keypress twice workaround
1300
- skipKeyPressEvent = false;
1301
- skipInputEvent = false;
1302
- break;
1303
- case "keypress":
1304
- if (skipKeyPressEvent === true) {
1305
- return e.preventDefault();
1306
- }
1307
- skipKeyPressEvent = true;
1308
- break;
1309
- case "click":
1310
- case "focus":
1311
- if (validationEvent) { // #841
1312
- validationEvent = false;
1313
- input.blur();
1314
- HandleNativePlaceholder(input, (isRTL ? getBufferTemplate().slice().reverse() : getBufferTemplate()).join(""));
1315
- setTimeout(function () {
1316
- input.focus();
1317
- }, 3000);
1318
- return false;
1319
- }
1320
- args = arguments;
1321
- setTimeout(function () { //needed for Chrome ~ initial selection clears after the clickevent
1322
- if (!input.inputmask) {
1323
- // `inputmask.remove()` was called before this callback
1324
- return;
1325
- }
1326
- eventHandler.apply(that, args);
1327
- }, 0);
1328
- return false;
1329
- }
1330
- var returnVal = eventHandler.apply(that, arguments);
1331
- if (returnVal === false) {
1332
- e.preventDefault();
1333
- e.stopPropagation();
1334
- }
1335
- return returnVal;
1336
- }
1337
- };
1338
- //keep instance of the event
1339
- input.inputmask.events[eventName] = input.inputmask.events[eventName] || [];
1340
- input.inputmask.events[eventName].push(ev);
1341
-
1342
- if ($.inArray(eventName, ["submit", "reset"]) !== -1) {
1343
- if (input.form !== null) $(input.form).on(eventName, ev);
1344
- } else {
1345
- $(input).on(eventName, ev);
1346
- }
1347
- },
1348
- off: function (input, event) {
1349
- if (input.inputmask && input.inputmask.events) {
1350
- var events;
1351
- if (event) {
1352
- events = [];
1353
- events[event] = input.inputmask.events[event];
1354
- } else {
1355
- events = input.inputmask.events;
1356
- }
1357
- $.each(events, function (eventName, evArr) {
1358
- while (evArr.length > 0) {
1359
- var ev = evArr.pop();
1360
- if ($.inArray(eventName, ["submit", "reset",]) !== -1) {
1361
- if (input.form !== null) $(input.form).off(eventName, ev);
1362
- } else {
1363
- $(input).off(eventName, ev);
1364
- }
1365
- }
1366
- delete input.inputmask.events[eventName];
1367
- });
1368
- }
1369
- }
1370
- };
1371
- var EventHandlers = {
1372
- keydownEvent: function (e) {
1373
- var input = this,
1374
- $input = $(input),
1375
- k = e.keyCode,
1376
- pos = caret(input);
1377
-
1378
- var kdResult = opts.onKeyDown.call(this, e, getBuffer(), pos, opts);
1379
- if (kdResult !== undefined) return kdResult;
1380
-
1381
- //backspace, delete, and escape get special treatment
1382
- if (k === keyCode.BACKSPACE || k === keyCode.DELETE || (iphone && k === keyCode.BACKSPACE_SAFARI) || (e.ctrlKey && k === keyCode.X && !("oncut" in input))) { //backspace/delete
1383
- e.preventDefault(); //stop default action but allow propagation
1384
- handleRemove(input, k, pos);
1385
- writeBuffer(input, getBuffer(true), maskset.p, e, input.inputmask._valueGet() !== getBuffer().join(""));
1386
- } else if (k === keyCode.END || k === keyCode.PAGE_DOWN) { //when END or PAGE_DOWN pressed set position at lastmatch
1387
- e.preventDefault();
1388
- var caretPos = seekNext(getLastValidPosition());
1389
- caret(input, e.shiftKey ? pos.begin : caretPos, caretPos, true);
1390
- } else if ((k === keyCode.HOME && !e.shiftKey) || k === keyCode.PAGE_UP) { //Home or page_up
1391
- e.preventDefault();
1392
- caret(input, 0, e.shiftKey ? pos.begin : 0, true);
1393
- } else if (((opts.undoOnEscape && k === keyCode.ESCAPE) || (k === 90 && e.ctrlKey)) && e.altKey !== true) { //escape && undo && #762
1394
- checkVal(input, true, false, undoValue.split(""));
1395
- $input.trigger("click");
1396
- // } else if (k === keyCode.INSERT && !(e.shiftKey || e.ctrlKey) && inputmask.userOptions.insertMode === undefined) { //insert
1397
- // opts.insertMode = !opts.insertMode;
1398
- // caret(input, pos.begin, pos.end);
1399
- } else if (opts.tabThrough === true && k === keyCode.TAB) {
1400
- if (e.shiftKey === true) {
1401
- if (getTest(pos.begin).match.static === true) {
1402
- pos.begin = seekNext(pos.begin);
1403
- }
1404
- pos.end = seekPrevious(pos.begin, true);
1405
- pos.begin = seekPrevious(pos.end, true);
1406
- } else {
1407
- pos.begin = seekNext(pos.begin, true);
1408
- pos.end = seekNext(pos.begin, true);
1409
- if (pos.end < maskset.maskLength) pos.end--;
1410
- }
1411
- if (pos.begin < maskset.maskLength) {
1412
- e.preventDefault();
1413
- caret(input, pos.begin, pos.end);
1414
- }
1415
- } else if (!e.shiftKey) {
1416
- if (opts.insertModeVisual && opts.insertMode === false) {
1417
- if (k === keyCode.RIGHT) {
1418
- setTimeout(function () {
1419
- var caretPos = caret(input);
1420
- caret(input, caretPos.begin);
1421
- }, 0);
1422
- } else if (k === keyCode.LEFT) {
1423
- setTimeout(function () {
1424
- var caretPos = {
1425
- begin: translatePosition(input.inputmask.caretPos.begin),
1426
- end: translatePosition(input.inputmask.caretPos.end)
1427
- };
1428
- if (isRTL) {
1429
- caret(input, caretPos.begin + (caretPos.begin === maskset.maskLength ? 0 : 1));
1430
- } else {
1431
- caret(input, caretPos.begin - (caretPos.begin === 0 ? 0 : 1));
1432
- }
1433
- }, 0);
1434
- }
1435
- }
1436
- }
1437
-
1438
- ignorable = $.inArray(k, opts.ignorables) !== -1;
1439
- },
1440
- keypressEvent: function (e, checkval, writeOut, strict, ndx) {
1441
- var input = this,
1442
- $input = $(input),
1443
- k = e.which || e.charCode || e.keyCode;
1444
-
1445
- if (checkval !== true && (!(e.ctrlKey && e.altKey) && (e.ctrlKey || e.metaKey || ignorable))) {
1446
- if (k === keyCode.ENTER && undoValue !== getBuffer().join("")) {
1447
- undoValue = getBuffer().join("");
1448
- // e.preventDefault();
1449
- setTimeout(function () {
1450
- $input.trigger("change");
1451
- }, 0);
1452
- }
1453
- skipInputEvent = true; //skip the input as otherwise the skipped char could be picked up for validation by the inputfallback
1454
- return true;
1455
- } else if (k) {
1456
- //special treat the decimal separator
1457
- if ((k === 44 || k === 46) && e.location === 3 && opts.radixPoint !== "") k = opts.radixPoint.charCodeAt(0);
1458
- var pos = checkval ? {
1459
- begin: ndx,
1460
- end: ndx
1461
- } : caret(input),
1462
- forwardPosition, c = String.fromCharCode(k);
1463
-
1464
- maskset.writeOutBuffer = true;
1465
- var valResult = isValid(pos, c, strict);
1466
- if (valResult !== false) {
1467
- resetMaskSet(true);
1468
- forwardPosition = valResult.caret !== undefined ? valResult.caret : seekNext(valResult.pos.begin ? valResult.pos.begin : valResult.pos);
1469
- maskset.p = forwardPosition; //needed for checkval
1470
- }
1471
-
1472
- forwardPosition = ((opts.numericInput && valResult.caret === undefined) ? seekPrevious(forwardPosition) : forwardPosition);
1473
- if (writeOut !== false) {
1474
-
1475
- setTimeout(function () {
1476
- opts.onKeyValidation.call(input, k, valResult);
1477
- }, 0);
1478
- if (maskset.writeOutBuffer && valResult !== false) {
1479
- var buffer = getBuffer();
1480
- writeBuffer(input, buffer, forwardPosition, e, checkval !== true);
1481
- }
1482
- }
1483
-
1484
- e.preventDefault();
1485
-
1486
- if (checkval) {
1487
- if (valResult !== false) valResult.forwardPosition = forwardPosition;
1488
- return valResult;
1489
- }
1490
- }
1491
- },
1492
- pasteEvent: function (e) {
1493
- var input = this,
1494
- inputValue = input.inputmask._valueGet(true),
1495
- caretPos = caret(input),
1496
- tempValue;
1497
-
1498
- if (isRTL) {
1499
- tempValue = caretPos.end;
1500
- caretPos.end = caretPos.begin;
1501
- caretPos.begin = tempValue;
1502
- }
1503
-
1504
- var valueBeforeCaret = inputValue.substr(0, caretPos.begin),
1505
- valueAfterCaret = inputValue.substr(caretPos.end, inputValue.length);
1506
-
1507
- if (valueBeforeCaret == (isRTL ? getBufferTemplate().slice().reverse() : getBufferTemplate()).slice(0, caretPos.begin).join("")) valueBeforeCaret = "";
1508
- if (valueAfterCaret == (isRTL ? getBufferTemplate().slice().reverse() : getBufferTemplate()).slice(caretPos.end).join("")) valueAfterCaret = "";
1509
-
1510
- if (window.clipboardData && window.clipboardData.getData) { // IE
1511
- inputValue = valueBeforeCaret + window.clipboardData.getData("Text") + valueAfterCaret;
1512
- } else if (e.clipboardData && e.clipboardData.getData) {
1513
- inputValue = valueBeforeCaret + e.clipboardData.getData("text/plain") + valueAfterCaret;
1514
- } else {
1515
- return true;
1516
- } //allow native paste event as fallback ~ masking will continue by inputfallback
1517
-
1518
- var pasteValue = inputValue;
1519
- if ($.isFunction(opts.onBeforePaste)) {
1520
- pasteValue = opts.onBeforePaste.call(inputmask, inputValue, opts);
1521
- if (pasteValue === false) {
1522
- return e.preventDefault();
1523
- }
1524
- if (!pasteValue) {
1525
- pasteValue = inputValue;
1526
- }
1527
- }
1528
- checkVal(input, false, false, pasteValue.toString().split(""));
1529
- writeBuffer(input, getBuffer(), seekNext(getLastValidPosition()), e, undoValue !== getBuffer().join(""));
1530
- return e.preventDefault();
1531
- },
1532
- inputFallBackEvent: function (e) { //fallback when keypress is not triggered
1533
- function ieMobileHandler(input, inputValue, caretPos) {
1534
- if (iemobile) { //iemobile just sets the character at the end althought the caret position is correctly set
1535
- var inputChar = inputValue.replace(getBuffer().join(""), "");
1536
- if (inputChar.length === 1) {
1537
- var iv = inputValue.split("");
1538
- iv.splice(caretPos.begin, 0, inputChar);
1539
- inputValue = iv.join("");
1540
- }
1541
- }
1542
- return inputValue;
1543
- }
1544
-
1545
- function analyseChanges(inputValue, buffer, caretPos) {
1546
- var frontPart = inputValue.substr(0, caretPos.begin).split(""),
1547
- backPart = inputValue.substr(caretPos.begin).split(""),
1548
- frontBufferPart = buffer.substr(0, caretPos.begin).split(""),
1549
- backBufferPart = buffer.substr(caretPos.begin).split("");
1550
-
1551
- var fpl = frontPart.length >= frontBufferPart.length ? frontPart.length : frontBufferPart.length,
1552
- bpl = backPart.length >= backBufferPart.length ? backPart.length : backBufferPart.length,
1553
- bl, i, action = "", data = [], marker = "~", placeholder;
1554
-
1555
- //align buffers
1556
- while (frontPart.length < fpl) frontPart.push(marker);
1557
- while (frontBufferPart.length < fpl) frontBufferPart.push(marker);
1558
- while (backPart.length < bpl) backPart.unshift(marker);
1559
- while (backBufferPart.length < bpl) backBufferPart.unshift(marker);
1560
-
1561
- var newBuffer = frontPart.concat(backPart);
1562
- var oldBuffer = frontBufferPart.concat(backBufferPart);
1563
-
1564
- // console.log("N " + newBuffer);
1565
- // console.log("O " + oldBuffer);
1566
-
1567
- for (i = 0, bl = newBuffer.length; i < bl; i++) {
1568
- placeholder = getPlaceholder(translatePosition(i));
1569
- switch (action) {
1570
- case "insertText":
1571
- if (oldBuffer[i - 1] === newBuffer[i] && caretPos.begin == newBuffer.length - 1) {
1572
- data.push(newBuffer[i]);
1573
- }
1574
- i = bl;
1575
- break;
1576
- case "insertReplacementText":
1577
- if (newBuffer[i] === marker) { //extend selection
1578
- caretPos.end++;
1579
- } else {
1580
- // breakout loop
1581
- i = bl;
1582
- }
1583
- break;
1584
- case "deleteContentBackward":
1585
- if (newBuffer[i] === marker) {
1586
- caretPos.end++;
1587
- } else {
1588
- //breakout loop
1589
- i = bl;
1590
- }
1591
- break;
1592
- default:
1593
- if (newBuffer[i] !== oldBuffer[i]) {
1594
- if ((newBuffer[i + 1] === marker || newBuffer[i + 1] === placeholder || newBuffer[i + 1] === undefined) && ((oldBuffer[i] === placeholder && oldBuffer[i + 1] === marker) || oldBuffer[i] === marker)) { //basic insert
1595
- action = "insertText";
1596
- data.push(newBuffer[i]);
1597
- caretPos.begin--;
1598
- caretPos.end--;
1599
- } else if (oldBuffer[i + 1] === marker && oldBuffer[i] === newBuffer[i + 1]) { //insert between
1600
- action = "insertText";
1601
- data.push(newBuffer[i]);
1602
- caretPos.begin--;
1603
- caretPos.end--;
1604
- } else if (newBuffer[i] !== placeholder && newBuffer[i] !== marker &&
1605
- (newBuffer[i + 1] === marker || (oldBuffer[i] !== newBuffer[i] && oldBuffer[i + 1] === newBuffer[i + 1] /*single char replacement*/))) { //replace selection
1606
- action = "insertReplacementText";
1607
- data.push(newBuffer[i]);
1608
- caretPos.begin--;
1609
- } else if (newBuffer[i] === marker) { //delete~backspace
1610
- action = "deleteContentBackward";
1611
- if (isMask(translatePosition(i), true) || oldBuffer[i] === opts.radixPoint) caretPos.end++;
1612
- } else {
1613
- i = bl;
1614
- }
1615
- }
1616
- break;
1617
- }
1618
- }
1619
-
1620
- return {
1621
- action: action,
1622
- data: data,
1623
- caret: caretPos
1624
- };
1625
- }
1626
-
1627
- var input = this,
1628
- inputValue = input.inputmask._valueGet(true),
1629
- buffer = (isRTL ? getBuffer().slice().reverse() : getBuffer()).join(""),
1630
- caretPos = caret(input, undefined, undefined, true);
1631
-
1632
- if (buffer !== inputValue) {
1633
- // inputValue = radixPointHandler(input, inputValue, caretPos);
1634
- inputValue = ieMobileHandler(input, inputValue, caretPos);
1635
-
1636
- var changes = analyseChanges(inputValue, buffer, caretPos);
1637
-
1638
- // console.log(JSON.stringify(changes));
1639
- if ((input.inputmask.shadowRoot || document).activeElement !== input) {
1640
- input.focus();
1641
- }
1642
- writeBuffer(input, getBuffer());
1643
- caret(input, caretPos.begin, caretPos.end, true);
1644
- switch (changes.action) {
1645
- case "insertText":
1646
- case "insertReplacementText":
1647
- $.each(changes.data, function (ndx, entry) {
1648
- var keypress = new $.Event("keypress");
1649
- keypress.which = entry.charCodeAt(0);
1650
- ignorable = false; //make sure ignorable is ignored ;-)
1651
- EventHandlers.keypressEvent.call(input, keypress);
1652
- });
1653
- setTimeout(function () { //#2195 trigger keyup to help some other plugins to track changes
1654
- $el.trigger("keyup");
1655
- }, 0);
1656
- break;
1657
- case "deleteContentBackward":
1658
- var keydown = new $.Event("keydown");
1659
- keydown.keyCode = keyCode.BACKSPACE;
1660
- EventHandlers.keydownEvent.call(input, keydown);
1661
- break;
1662
- default:
1663
- applyInputValue(input, inputValue);
1664
- break;
1665
- }
1666
-
1667
- e.preventDefault();
1668
- }
1669
- },
1670
- compositionendEvent: function (e) {
1671
- $el.trigger("input");
1672
- },
1673
- setValueEvent: function (e) {
1674
- var input = this,
1675
- value = (e && e.detail) ? e.detail[0] : arguments[1];
1676
-
1677
- if (value === undefined) {
1678
- value = input.inputmask._valueGet(true);
1679
- }
1680
-
1681
- applyInputValue(input, value);
1682
-
1683
- if ((e.detail && e.detail[1] !== undefined) || arguments[2] !== undefined) {
1684
- caret(input, e.detail ? e.detail[1] : arguments[2]);
1685
- }
1686
- }
1687
- ,
1688
- focusEvent: function (e) {
1689
- var input = this,
1690
- nptValue = input.inputmask._valueGet();
1691
-
1692
- if (opts.showMaskOnFocus) {
1693
- if (nptValue !== getBuffer().join("")) {
1694
- writeBuffer(input, getBuffer(), seekNext(getLastValidPosition()));
1695
- } /*else if (mouseEnter === false) { //only executed on focus without mouseenter
1696
- caret(input, seekNext(getLastValidPosition()));
1697
- }*/
1698
- }
1699
- if (opts.positionCaretOnTab === true && mouseEnter === false && (!isComplete(getBuffer()) || getLastValidPosition() === -1)) {
1700
- EventHandlers.clickEvent.apply(input, [e, true]);
1701
- }
1702
- undoValue = getBuffer().join("");
1703
- }
1704
- ,
1705
- invalidEvent: function (e) {
1706
- validationEvent = true;
1707
- }
1708
- ,
1709
- mouseleaveEvent: function () {
1710
- var input = this;
1711
- mouseEnter = false;
1712
- if (opts.clearMaskOnLostFocus && (input.inputmask.shadowRoot || document).activeElement !== input) {
1713
- HandleNativePlaceholder(input, originalPlaceholder);
1714
- }
1715
- }
1716
- ,
1717
- clickEvent: function (e, tabbed) {
1718
- var input = this;
1719
- if ((input.inputmask.shadowRoot || document).activeElement === input) {
1720
- var newCaretPosition = determineNewCaretPosition(caret(input), tabbed);
1721
- if (newCaretPosition !== undefined) {
1722
- caret(input, newCaretPosition);
1723
- }
1724
- }
1725
- },
1726
- cutEvent: function (e) {
1727
- var input = this,
1728
- pos = caret(input);
1729
-
1730
- //correct clipboardData
1731
- var clipboardData = window.clipboardData || e.clipboardData,
1732
- clipData = isRTL ? getBuffer().slice(pos.end, pos.begin) : getBuffer().slice(pos.begin, pos.end);
1733
- clipboardData.setData("text", isRTL ? clipData.reverse().join("") : clipData.join(""));
1734
- if (document.execCommand) document.execCommand("copy"); // copy selected content to system clipbaord
1735
-
1736
- handleRemove(input, keyCode.DELETE, pos);
1737
- writeBuffer(input, getBuffer(), maskset.p, e, undoValue !== getBuffer().join(""));
1738
- }
1739
- ,
1740
- blurEvent: function (e) {
1741
- var $input = $(this),
1742
- input = this;
1743
- if (input.inputmask) {
1744
- HandleNativePlaceholder(input, originalPlaceholder);
1745
- var nptValue = input.inputmask._valueGet(),
1746
- buffer = getBuffer().slice();
1747
-
1748
- if (nptValue !== "") {
1749
- if (opts.clearMaskOnLostFocus) {
1750
- if (getLastValidPosition() === -1 && nptValue === getBufferTemplate().join("")) {
1751
- buffer = [];
1752
- } else { //clearout optional tail of the mask
1753
- clearOptionalTail(buffer);
1754
- }
1755
- }
1756
- if (isComplete(buffer) === false) {
1757
- setTimeout(function () {
1758
- $input.trigger("incomplete");
1759
- }, 0);
1760
- if (opts.clearIncomplete) {
1761
- resetMaskSet();
1762
- if (opts.clearMaskOnLostFocus) {
1763
- buffer = [];
1764
- } else {
1765
- buffer = getBufferTemplate().slice();
1766
- }
1767
- }
1768
- }
1769
-
1770
- writeBuffer(input, buffer, undefined, e);
1771
- }
1772
-
1773
- if (undoValue !== getBuffer().join("")) {
1774
- undoValue = getBuffer().join("");
1775
- $input.trigger("change");
1776
- }
1777
- }
1778
- }
1779
- ,
1780
- mouseenterEvent: function () {
1781
- var input = this;
1782
- mouseEnter = true;
1783
- if ((input.inputmask.shadowRoot || document).activeElement !== input) {
1784
- if (originalPlaceholder == undefined && input.placeholder !== originalPlaceholder) {
1785
- originalPlaceholder = input.placeholder;
1786
- }
1787
- if (opts.showMaskOnHover) {
1788
- HandleNativePlaceholder(input, (isRTL ? getBufferTemplate().slice().reverse() : getBufferTemplate()).join(""));
1789
- }
1790
- }
1791
- }
1792
- ,
1793
- submitEvent: function () { //trigger change on submit if any
1794
- if (undoValue !== getBuffer().join("")) {
1795
- $el.trigger("change");
1796
- }
1797
- if (opts.clearMaskOnLostFocus && getLastValidPosition() === -1 && el.inputmask._valueGet && el.inputmask._valueGet() === getBufferTemplate().join("")) {
1798
- el.inputmask._valueSet(""); //clear masktemplete on submit and still has focus
1799
- }
1800
- if (opts.clearIncomplete && isComplete(getBuffer()) === false) {
1801
- el.inputmask._valueSet("");
1802
- }
1803
- if (opts.removeMaskOnSubmit) {
1804
- el.inputmask._valueSet(el.inputmask.unmaskedvalue(), true);
1805
- setTimeout(function () {
1806
- writeBuffer(el, getBuffer());
1807
- }, 0);
1808
- }
1809
- }
1810
- ,
1811
- resetEvent: function () {
1812
- el.inputmask.refreshValue = true; //indicate a forced refresh when there is a call to the value before leaving the triggering event fn
1813
- setTimeout(function () {
1814
- applyInputValue(el, el.inputmask._valueGet(true));
1815
- }, 0);
1816
- }
1817
- ,
1818
- };
1819
-
1820
- function checkVal(input, writeOut, strict, nptvl, initiatingEvent) {
1821
- var inputmask = this || input.inputmask,
1822
- inputValue = nptvl.slice(),
1823
- charCodes = "",
1824
- initialNdx = -1,
1825
- result = undefined;
1826
-
1827
- // console.log(nptvl);
1828
-
1829
- function isTemplateMatch(ndx, charCodes) {
1830
- var targetTemplate = getMaskTemplate(true, 0).slice(ndx, seekNext(ndx)).join("").replace(/'/g, ""),
1831
- charCodeNdx = targetTemplate.indexOf(charCodes);
1832
- //strip spaces from targetTemplate
1833
- while (charCodeNdx > 0 && targetTemplate[charCodeNdx - 1] === " ") charCodeNdx--;
1834
-
1835
- var match = charCodeNdx === 0 && !isMask(ndx)
1836
- && (getTest(ndx).match.nativeDef === charCodes.charAt(0)
1837
- || (getTest(ndx).match.static === true && getTest(ndx).match.nativeDef === ("'" + charCodes.charAt(0)))
1838
- || (getTest(ndx).match.nativeDef === " " && (getTest(ndx + 1).match.nativeDef === charCodes.charAt(0)
1839
- || (getTest(ndx + 1).match.static === true && getTest(ndx + 1).match.nativeDef === ("'" + charCodes.charAt(0))))));
1840
-
1841
- if (!match && charCodeNdx > 0 && !isMask(ndx, false, true)) {
1842
- var nextPos = seekNext(ndx);
1843
- if (inputmask.caretPos.begin < nextPos) {
1844
- inputmask.caretPos = {begin: nextPos};
1845
- }
1846
- }
1847
- return match;
1848
- }
1849
-
1850
- resetMaskSet();
1851
- maskset.tests = {}; //reset tests ~ possible after alternating
1852
- initialNdx = opts.radixPoint ? determineNewCaretPosition({begin: 0, end: 0}).begin : 0;
1853
- maskset.p = initialNdx;
1854
- inputmask.caretPos = {begin: initialNdx};
1855
-
1856
- var staticMatches = [], prevCaretPos = inputmask.caretPos;
1857
- $.each(inputValue, function (ndx, charCode) {
1858
- if (charCode !== undefined) { //inputfallback strips some elements out of the inputarray. $.each logically presents them as undefined
1859
- if (maskset.validPositions[ndx] === undefined && inputValue[ndx] === getPlaceholder(ndx) && isMask(ndx, true) &&
1860
- isValid(ndx, inputValue[ndx], true, undefined, undefined, true) === false) {
1861
- maskset.p++;
1862
- } else {
1863
- var keypress = new $.Event("_checkval");
1864
- keypress.which = charCode.toString().charCodeAt(0);
1865
- charCodes += charCode;
1866
- var lvp = getLastValidPosition(undefined, true);
1867
- if (!isTemplateMatch(initialNdx, charCodes)) {
1868
- result = EventHandlers.keypressEvent.call(input, keypress, true, false, strict, inputmask.caretPos.begin);
1869
-
1870
- if (result) {
1871
- initialNdx = inputmask.caretPos.begin + 1;
1872
- charCodes = "";
1873
- }
1874
- } else {
1875
- result = EventHandlers.keypressEvent.call(input, keypress, true, false, strict, lvp + 1);
1876
- }
1877
- if (result) {
1878
- if (result.pos !== undefined && maskset.validPositions[result.pos] && maskset.validPositions[result.pos].match.static === true && maskset.validPositions[result.pos].alternation === undefined) {
1879
- staticMatches.push(result.pos);
1880
- if (!isRTL) {
1881
- result.forwardPosition = result.pos + 1;
1882
- }
1883
- }
1884
- writeBuffer(undefined, getBuffer(), result.forwardPosition, keypress, false);
1885
- inputmask.caretPos = {begin: result.forwardPosition, end: result.forwardPosition};
1886
- prevCaretPos = inputmask.caretPos;
1887
- } else {
1888
- inputmask.caretPos = prevCaretPos;
1889
- } //restore the caret position from before the failed validation
1890
- }
1891
- }
1892
- });
1893
- if (staticMatches.length > 0) {
1894
- var sndx, validPos, nextValid = seekNext(-1, undefined, false);
1895
- if ((!isComplete(getBuffer()) && staticMatches.length <= nextValid)
1896
- || (isComplete(getBuffer()) && staticMatches.length > 0 && (staticMatches.length !== nextValid && staticMatches[0] === 0))) { //should check if is sequence starting from 0
1897
- var nextSndx = nextValid;
1898
- while ((sndx = staticMatches.shift()) !== undefined) {
1899
- var keypress = new $.Event("_checkval");
1900
- validPos = maskset.validPositions[sndx];
1901
- validPos.generatedInput = true;
1902
- keypress.which = validPos.input.charCodeAt(0);
1903
- result = EventHandlers.keypressEvent.call(input, keypress, true, false, strict, nextSndx);
1904
- if (result && result.pos !== undefined && result.pos !== sndx && maskset.validPositions[result.pos] && maskset.validPositions[result.pos].match.static === true) {
1905
- staticMatches.push(result.pos);
1906
- } else if (!result) break;
1907
- nextSndx++;
1908
- }
1909
- } else { //mark al statics as generated
1910
- while ((sndx = staticMatches.pop())) {
1911
- validPos = maskset.validPositions[sndx];
1912
- if (validPos) {
1913
- validPos.generatedInput = true;
1914
- }
1915
- }
1916
- }
1917
- }
1918
- if (writeOut) {
1919
- writeBuffer(input, getBuffer(), result ? result.forwardPosition : undefined, initiatingEvent || new $.Event("checkval"), initiatingEvent && initiatingEvent.type === "input");
1920
- for (var vndx in maskset.validPositions) {
1921
- if (maskset.validPositions[vndx].match.generated !== true) { //only remove non forced generated
1922
- delete maskset.validPositions[vndx].generatedInput; //clear generated markings ~ consider initializing with a value as fully typed
1923
- }
1924
- }
1925
- }
1926
- }
1927
-
1928
- function unmaskedvalue(input) {
1929
- if (input) {
1930
- if (input.inputmask === undefined) {
1931
- return input.value;
1932
- }
1933
- if (input.inputmask && input.inputmask.refreshValue) { //forced refresh from the value form.reset
1934
- applyInputValue(input, input.inputmask._valueGet(true));
1935
- }
1936
- }
1937
- var umValue = [],
1938
- vps = maskset.validPositions;
1939
- for (var pndx in vps) {
1940
- if (vps[pndx] && vps[pndx].match && (vps[pndx].match.static != true || vps[pndx].generatedInput !== true)) {
1941
- umValue.push(vps[pndx].input);
1942
- }
1943
- }
1944
- var unmaskedValue = umValue.length === 0 ? "" : (isRTL ? umValue.reverse() : umValue).join("");
1945
- if ($.isFunction(opts.onUnMask)) {
1946
- var bufferValue = (isRTL ? getBuffer().slice().reverse() : getBuffer()).join("");
1947
- unmaskedValue = opts.onUnMask.call(inputmask, bufferValue, unmaskedValue, opts);
1948
- }
1949
- return unmaskedValue;
1950
- }
1951
-
1952
- function translatePosition(pos) {
1953
- if (isRTL && typeof pos === "number" && (!opts.greedy || opts.placeholder !== "") && el) {
1954
- pos = el.inputmask._valueGet().length - pos;
1955
- }
1956
- return pos;
1957
- }
1958
-
1959
- function caret(input, begin, end, notranslate, isDelete) {
1960
- var range;
1961
- if (begin !== undefined) {
1962
- if ($.isArray(begin)) {
1963
- end = isRTL ? begin[0] : begin[1];
1964
- begin = isRTL ? begin[1] : begin[0];
1965
- }
1966
- if (begin.begin !== undefined) {
1967
- end = isRTL ? begin.begin : begin.end;
1968
- begin = isRTL ? begin.end : begin.begin;
1969
- }
1970
- if (typeof begin === "number") {
1971
- begin = notranslate ? begin : translatePosition(begin);
1972
- end = notranslate ? end : translatePosition(end);
1973
- end = (typeof end == "number") ? end : begin;
1974
- // if (!$(input).is(":visible")) {
1975
- // return;
1976
- // }
1977
-
1978
- var scrollCalc = parseInt(((input.ownerDocument.defaultView || window).getComputedStyle ? (input.ownerDocument.defaultView || window).getComputedStyle(input, null) : input.currentStyle).fontSize) * end;
1979
- input.scrollLeft = scrollCalc > input.scrollWidth ? scrollCalc : 0;
1980
- input.inputmask.caretPos = {begin: begin, end: end}; //track caret internally
1981
- if (opts.insertModeVisual && opts.insertMode === false && begin === end) {
1982
- if (!isDelete) {
1983
- end++; //set visualization for insert/overwrite mode
1984
- }
1985
- }
1986
- if (input === (input.inputmask.shadowRoot || document).activeElement) {
1987
- if ("setSelectionRange" in input) {
1988
- input.setSelectionRange(begin, end);
1989
- } else if (window.getSelection) {
1990
- range = document.createRange();
1991
- if (input.firstChild === undefined || input.firstChild === null) {
1992
- var textNode = document.createTextNode("");
1993
- input.appendChild(textNode);
1994
- }
1995
- range.setStart(input.firstChild, begin < input.inputmask._valueGet().length ? begin : input.inputmask._valueGet().length);
1996
- range.setEnd(input.firstChild, end < input.inputmask._valueGet().length ? end : input.inputmask._valueGet().length);
1997
- range.collapse(true);
1998
- var sel = window.getSelection();
1999
- sel.removeAllRanges();
2000
- sel.addRange(range);
2001
- //input.focus();
2002
- } else if (input.createTextRange) {
2003
- range = input.createTextRange();
2004
- range.collapse(true);
2005
- range.moveEnd("character", end);
2006
- range.moveStart("character", begin);
2007
- range.select();
2008
- }
2009
- }
2010
- }
2011
- } else {
2012
- if ("selectionStart" in input && "selectionEnd" in input) {
2013
- begin = input.selectionStart;
2014
- end = input.selectionEnd;
2015
- } else if (window.getSelection) {
2016
- range = window.getSelection().getRangeAt(0);
2017
- if (range.commonAncestorContainer.parentNode === input || range.commonAncestorContainer === input) {
2018
- begin = range.startOffset;
2019
- end = range.endOffset;
2020
- }
2021
- } else if (document.selection && document.selection.createRange) {
2022
- range = document.selection.createRange();
2023
- begin = 0 - range.duplicate().moveStart("character", -input.inputmask._valueGet().length);
2024
- end = begin + range.text.length;
2025
- }
2026
-
2027
- // if (opts.insertModeVisual && opts.insertMode === false && begin === (end - 1)) end--; //correct caret for insert/overwrite mode
2028
-
2029
- /*eslint-disable consistent-return */
2030
- return {
2031
- "begin": notranslate ? begin : translatePosition(begin),
2032
- "end": notranslate ? end : translatePosition(end)
2033
- };
2034
- /*eslint-enable consistent-return */
2035
- }
2036
- }
2037
-
2038
- function determineLastRequiredPosition(returnDefinition) {
2039
- var buffer = getMaskTemplate(true, getLastValidPosition(), true, true),
2040
- bl = buffer.length,
2041
- pos, lvp = getLastValidPosition(),
2042
- positions = {},
2043
- lvTest = maskset.validPositions[lvp],
2044
- ndxIntlzr = lvTest !== undefined ? lvTest.locator.slice() : undefined,
2045
- testPos;
2046
- for (pos = lvp + 1; pos < buffer.length; pos++) {
2047
- testPos = getTestTemplate(pos, ndxIntlzr, pos - 1);
2048
- ndxIntlzr = testPos.locator.slice();
2049
- positions[pos] = $.extend(true, {}, testPos);
2050
- }
2051
-
2052
- var lvTestAlt = lvTest && lvTest.alternation !== undefined ? lvTest.locator[lvTest.alternation] : undefined;
2053
- for (pos = bl - 1; pos > lvp; pos--) {
2054
- testPos = positions[pos];
2055
- if ((testPos.match.optionality ||
2056
- (testPos.match.optionalQuantifier && testPos.match.newBlockMarker) ||
2057
- (lvTestAlt &&
2058
- (
2059
- (lvTestAlt !== positions[pos].locator[lvTest.alternation] && testPos.match.static != true) ||
2060
- (testPos.match.static === true &&
2061
- testPos.locator[lvTest.alternation] &&
2062
- checkAlternationMatch(testPos.locator[lvTest.alternation].toString().split(","), lvTestAlt.toString().split(",")) &&
2063
- getTests(pos)[0].def !== "")
2064
- )
2065
- )) &&
2066
- buffer[pos] === getPlaceholder(pos, testPos.match)) {
2067
- bl--;
2068
- } else {
2069
- break;
2070
- }
2071
- }
2072
- return returnDefinition ? {
2073
- "l": bl,
2074
- "def": positions[bl] ? positions[bl].match : undefined
2075
- } : bl;
2076
- }
2077
-
2078
- function clearOptionalTail(buffer) {
2079
- buffer.length = 0;
2080
- var template = getMaskTemplate(true, 0, true, undefined, true), lmnt;
2081
- while ((lmnt = template.shift()) !== undefined) buffer.push(lmnt);
2082
- return buffer;
2083
- }
2084
-
2085
- function isComplete(buffer) { //return true / false / undefined (repeat *)
2086
- if ($.isFunction(opts.isComplete)) return opts.isComplete(buffer, opts);
2087
- if (opts.repeat === "*") return undefined;
2088
- var complete = false,
2089
- lrp = determineLastRequiredPosition(true),
2090
- aml = seekPrevious(lrp.l);
2091
-
2092
- if (lrp.def === undefined || lrp.def.newBlockMarker || lrp.def.optionality || lrp.def.optionalQuantifier) {
2093
- complete = true;
2094
- for (var i = 0; i <= aml; i++) {
2095
- var test = getTestTemplate(i).match;
2096
- if ((test.static !== true && maskset.validPositions[i] === undefined && test.optionality !== true && test.optionalQuantifier !== true) || (test.static === true && buffer[i] !== getPlaceholder(i, test))) {
2097
- complete = false;
2098
- break;
2099
- }
2100
- }
2101
- }
2102
- return complete;
2103
- }
2104
-
2105
-
2106
- function handleRemove(input, k, pos, strict, fromIsValid) {
2107
- if (opts.numericInput || isRTL) {
2108
- if (k === keyCode.BACKSPACE) {
2109
- k = keyCode.DELETE;
2110
- } else if (k === keyCode.DELETE) {
2111
- k = keyCode.BACKSPACE;
2112
- }
2113
-
2114
- if (isRTL) {
2115
- var pend = pos.end;
2116
- pos.end = pos.begin;
2117
- pos.begin = pend;
2118
- }
2119
- }
2120
-
2121
- var lvp = getLastValidPosition(undefined, true);
2122
- if (pos.end >= getBuffer().length && lvp >= pos.end) { //handle numeric negate symbol offset, due to dynamic jit masking
2123
- pos.end = lvp + 1;
2124
- }
2125
-
2126
- if (k === keyCode.BACKSPACE) {
2127
- if ((pos.end - pos.begin < 1)) {
2128
- pos.begin = seekPrevious(pos.begin);
2129
- }
2130
- } else if (k === keyCode.DELETE) {
2131
- if (pos.begin === pos.end) {
2132
- pos.end = isMask(pos.end, true, true) ? pos.end + 1 : seekNext(pos.end) + 1;
2133
- }
2134
- }
2135
- var offset;
2136
- if ((offset = revalidateMask(pos)) !== false) {
2137
- if (strict !== true && opts.keepStatic !== false || (opts.regex !== null && getTest(pos.begin).match.def.indexOf("|") !== -1)) { //TODO NEEDS BETTER CHECK WHEN TO ALTERNATE ~ opts regex isn"t good enough
2138
- var result = alternate(true);
2139
- if (result) {
2140
- var newPos = result.caret !== undefined ? result.caret : (result.pos ? seekNext(result.pos.begin ? result.pos.begin : result.pos) : getLastValidPosition(-1, true));
2141
- if (k !== keyCode.DELETE || pos.begin > newPos) {
2142
- pos.begin == newPos;
2143
- }
2144
- }
2145
- }
2146
-
2147
- if (strict !== true) {
2148
- maskset.p = k === keyCode.DELETE ? pos.begin + offset : pos.begin;
2149
- }
2150
- }
2151
- }
2152
-
2153
- function applyInputValue(input, value) {
2154
- input.inputmask.refreshValue = false;
2155
- if ($.isFunction(opts.onBeforeMask)) value = opts.onBeforeMask.call(inputmask, value, opts) || value;
2156
- value = value.toString().split("");
2157
- checkVal(input, true, false, value);
2158
- undoValue = getBuffer().join("");
2159
- if ((opts.clearMaskOnLostFocus || opts.clearIncomplete) && input.inputmask._valueGet() === getBufferTemplate().join("") && getLastValidPosition() === -1) {
2160
- input.inputmask._valueSet("");
2161
- }
2162
- }
2163
-
2164
- function mask(elem) {
2165
- function isElementTypeSupported(input, opts) {
2166
- function patchValueProperty(npt) {
2167
- var valueGet;
2168
- var valueSet;
2169
-
2170
- function patchValhook(type) {
2171
- if ($.valHooks && ($.valHooks[type] === undefined || $.valHooks[type].inputmaskpatch !== true)) {
2172
- var valhookGet = $.valHooks[type] && $.valHooks[type].get ? $.valHooks[type].get : function (elem) {
2173
- return elem.value;
2174
- };
2175
- var valhookSet = $.valHooks[type] && $.valHooks[type].set ? $.valHooks[type].set : function (elem, value) {
2176
- elem.value = value;
2177
- return elem;
2178
- };
2179
-
2180
- $.valHooks[type] = {
2181
- get: function (elem) {
2182
- if (elem.inputmask) {
2183
- if (elem.inputmask.opts.autoUnmask) {
2184
- return elem.inputmask.unmaskedvalue();
2185
- } else {
2186
- var result = valhookGet(elem);
2187
- return getLastValidPosition(undefined, undefined, elem.inputmask.maskset.validPositions) !== -1 || opts.nullable !== true ? result : "";
2188
- }
2189
- } else {
2190
- return valhookGet(elem);
2191
- }
2192
- },
2193
- set: function (elem, value) {
2194
- var result = valhookSet(elem, value);
2195
- if (elem.inputmask) {
2196
- applyInputValue(elem, value);
2197
- }
2198
- return result;
2199
- },
2200
- inputmaskpatch: true
2201
- };
2202
- }
2203
- }
2204
-
2205
- function getter() {
2206
- if (this.inputmask) {
2207
- return this.inputmask.opts.autoUnmask ?
2208
- this.inputmask.unmaskedvalue() :
2209
- (getLastValidPosition() !== -1 || opts.nullable !== true ?
2210
- ((this.inputmask.shadowRoot || document.activeElement) === this && opts.clearMaskOnLostFocus ?
2211
- (isRTL ? clearOptionalTail(getBuffer().slice()).reverse() : clearOptionalTail(getBuffer().slice())).join("") :
2212
- valueGet.call(this)) :
2213
- "");
2214
- } else {
2215
- return valueGet.call(this);
2216
- }
2217
- }
2218
-
2219
- function setter(value) {
2220
- valueSet.call(this, value);
2221
- if (this.inputmask) {
2222
- applyInputValue(this, value);
2223
- }
2224
- }
2225
-
2226
- function installNativeValueSetFallback(npt) {
2227
- EventRuler.on(npt, "mouseenter", function () {
2228
- var input = this,
2229
- value = input.inputmask._valueGet(true);
2230
- if (value !== (isRTL ? getBuffer().reverse() : getBuffer()).join("")) { //Is this correct? to apply RTL? TOCHECK
2231
- applyInputValue(input, value);
2232
- }
2233
- });
2234
- }
2235
-
2236
- if (!npt.inputmask.__valueGet) {
2237
- if (opts.noValuePatching !== true) {
2238
- if (Object.getOwnPropertyDescriptor) {
2239
- if (typeof Object.getPrototypeOf !== "function") {
2240
- Object.getPrototypeOf = typeof "test".__proto__ === "object" ? function (object) {
2241
- return object.__proto__;
2242
- } : function (object) {
2243
- return object.constructor.prototype;
2244
- };
2245
- }
2246
-
2247
- var valueProperty = Object.getPrototypeOf ? Object.getOwnPropertyDescriptor(Object.getPrototypeOf(npt), "value") : undefined;
2248
- if (valueProperty && valueProperty.get && valueProperty.set) {
2249
- valueGet = valueProperty.get;
2250
- valueSet = valueProperty.set;
2251
- Object.defineProperty(npt, "value", {
2252
- get: getter,
2253
- set: setter,
2254
- configurable: true
2255
- });
2256
- } else if (npt.tagName.toLowerCase() !== "input") {
2257
- valueGet = function () {
2258
- return this.textContent;
2259
- };
2260
- valueSet = function (value) {
2261
- this.textContent = value;
2262
- };
2263
- Object.defineProperty(npt, "value", {
2264
- get: getter,
2265
- set: setter,
2266
- configurable: true
2267
- });
2268
- }
2269
- } else if (document.__lookupGetter__ && npt.__lookupGetter__("value")) {
2270
- valueGet = npt.__lookupGetter__("value");
2271
- valueSet = npt.__lookupSetter__("value");
2272
-
2273
- npt.__defineGetter__("value", getter);
2274
- npt.__defineSetter__("value", setter);
2275
- }
2276
- npt.inputmask.__valueGet = valueGet; //store native property getter
2277
- npt.inputmask.__valueSet = valueSet; //store native property setter
2278
- }
2279
- npt.inputmask._valueGet = function (overruleRTL) {
2280
- return isRTL && overruleRTL !== true ? valueGet.call(this.el).split("").reverse().join("") : valueGet.call(this.el);
2281
- };
2282
- npt.inputmask._valueSet = function (value, overruleRTL) { //null check is needed for IE8 => otherwise converts to "null"
2283
- valueSet.call(this.el, (value === null || value === undefined) ? "" : ((overruleRTL !== true && isRTL) ? value.split("").reverse().join("") : value));
2284
- };
2285
-
2286
- if (valueGet === undefined) { //jquery.val fallback
2287
- valueGet = function () {
2288
- return this.value;
2289
- };
2290
- valueSet = function (value) {
2291
- this.value = value;
2292
- };
2293
- patchValhook(npt.type);
2294
- installNativeValueSetFallback(npt);
2295
- }
2296
- }
2297
- }
2298
-
2299
- if (input.tagName.toLowerCase() !== "textarea") {
2300
- opts.ignorables.push(keyCode.ENTER);
2301
- }
2302
-
2303
- var elementType = input.getAttribute("type");
2304
- var isSupported = (input.tagName.toLowerCase() === "input" && $.inArray(elementType, opts.supportsInputType) !== -1) || input.isContentEditable || input.tagName.toLowerCase() === "textarea";
2305
- if (!isSupported) {
2306
- if (input.tagName.toLowerCase() === "input") {
2307
- var el = document.createElement("input");
2308
- el.setAttribute("type", elementType);
2309
- isSupported = el.type === "text"; //apply mask only if the type is not natively supported
2310
- el = null;
2311
- } else {
2312
- isSupported = "partial";
2313
- }
2314
- }
2315
- if (isSupported !== false) {
2316
- patchValueProperty(input);
2317
- } else {
2318
- input.inputmask = undefined;
2319
- }
2320
- return isSupported;
2321
- }
2322
-
2323
- //unbind all events - to make sure that no other mask will interfere when re-masking
2324
- EventRuler.off(elem);
2325
- var isSupported = isElementTypeSupported(elem, opts);
2326
- if (isSupported !== false) {
2327
- el = elem;
2328
- $el = $(el);
2329
-
2330
- originalPlaceholder = el.placeholder;
2331
-
2332
- //read maxlength prop from el
2333
- maxLength = el !== undefined ? el.maxLength : undefined;
2334
- if (maxLength === -1) maxLength = undefined;
2335
- if ("inputMode" in el && el.getAttribute("inputmode") === null) {
2336
- el.inputMode = opts.inputmode;
2337
- el.setAttribute("inputmode", opts.inputmode);
2338
- }
2339
-
2340
-
2341
- if (isSupported === true) {
2342
- opts.showMaskOnFocus = opts.showMaskOnFocus && ["cc-number", "cc-exp"].indexOf(el.autocomplete) === -1;
2343
- if (iphone) { //selecting the caret shows as a slection on iphone
2344
- opts.insertModeVisual = false;
2345
- }
2346
-
2347
- //bind events
2348
- EventRuler.on(el, "submit", EventHandlers.submitEvent);
2349
- EventRuler.on(el, "reset", EventHandlers.resetEvent);
2350
- EventRuler.on(el, "blur", EventHandlers.blurEvent);
2351
- EventRuler.on(el, "focus", EventHandlers.focusEvent);
2352
- EventRuler.on(el, "invalid", EventHandlers.invalidEvent);
2353
- EventRuler.on(el, "click", EventHandlers.clickEvent);
2354
- EventRuler.on(el, "mouseleave", EventHandlers.mouseleaveEvent);
2355
- EventRuler.on(el, "mouseenter", EventHandlers.mouseenterEvent);
2356
- EventRuler.on(el, "paste", EventHandlers.pasteEvent);
2357
- EventRuler.on(el, "cut", EventHandlers.cutEvent);
2358
- EventRuler.on(el, "complete", opts.oncomplete);
2359
- EventRuler.on(el, "incomplete", opts.onincomplete);
2360
- EventRuler.on(el, "cleared", opts.oncleared);
2361
- if (!mobile && opts.inputEventOnly !== true) {
2362
- EventRuler.on(el, "keydown", EventHandlers.keydownEvent);
2363
- EventRuler.on(el, "keypress", EventHandlers.keypressEvent);
2364
- } else {
2365
- el.removeAttribute("maxLength");
2366
- }
2367
- EventRuler.on(el, "input", EventHandlers.inputFallBackEvent);
2368
- EventRuler.on(el, "compositionend", EventHandlers.compositionendEvent);
2369
- // EventRuler.on(el, "beforeinput", EventHandlers.beforeInputEvent); //https://github.com/w3c/input-events - to implement
2370
- }
2371
- EventRuler.on(el, "setvalue", EventHandlers.setValueEvent);
2372
-
2373
- //apply mask
2374
- undoValue = getBufferTemplate().join(""); //initialize the buffer and getmasklength
2375
- var activeElement = (el.inputmask.shadowRoot || document).activeElement;
2376
- if (el.inputmask._valueGet(true) !== "" || opts.clearMaskOnLostFocus === false || activeElement === el) {
2377
- applyInputValue(el, el.inputmask._valueGet(true), opts);
2378
- var buffer = getBuffer().slice();
2379
- if (isComplete(buffer) === false) {
2380
- if (opts.clearIncomplete) {
2381
- resetMaskSet();
2382
- }
2383
- }
2384
- if (opts.clearMaskOnLostFocus && activeElement !== el) {
2385
- if (getLastValidPosition() === -1) {
2386
- buffer = [];
2387
- } else {
2388
- clearOptionalTail(buffer);
2389
- }
2390
- }
2391
- if (opts.clearMaskOnLostFocus === false || (opts.showMaskOnFocus && activeElement === el) || el.inputmask._valueGet(true) !== "") {
2392
- writeBuffer(el, buffer);
2393
- }
2394
- if (activeElement === el) { //position the caret when in focus
2395
- caret(el, seekNext(getLastValidPosition()));
2396
- }
2397
- }
2398
- }
2399
- }
2400
-
2401
- //action object
2402
- var valueBuffer;
2403
- if (actionObj !== undefined) {
2404
- switch (actionObj.action) {
2405
- case "isComplete":
2406
- el = actionObj.el;
2407
- return isComplete(getBuffer());
2408
- case "unmaskedvalue":
2409
- if (el === undefined || actionObj.value !== undefined) {
2410
- valueBuffer = actionObj.value;
2411
- valueBuffer = ($.isFunction(opts.onBeforeMask) ? (opts.onBeforeMask.call(inputmask, valueBuffer, opts) || valueBuffer) : valueBuffer).split("");
2412
- checkVal.call(this, undefined, false, false, valueBuffer);
2413
- if ($.isFunction(opts.onBeforeWrite)) opts.onBeforeWrite.call(inputmask, undefined, getBuffer(), 0, opts);
2414
- }
2415
- return unmaskedvalue(el);
2416
- case "mask":
2417
- mask(el);
2418
- break;
2419
- case "format":
2420
- valueBuffer = ($.isFunction(opts.onBeforeMask) ? (opts.onBeforeMask.call(inputmask, actionObj.value, opts) || actionObj.value) : actionObj.value).split("");
2421
- checkVal.call(this, undefined, true, false, valueBuffer);
2422
- if (actionObj.metadata) {
2423
- return {
2424
- value: isRTL ? getBuffer().slice().reverse().join("") : getBuffer().join(""),
2425
- metadata: maskScope.call(this, {
2426
- "action": "getmetadata"
2427
- }, maskset, opts)
2428
- };
2429
- }
2430
-
2431
- return isRTL ? getBuffer().slice().reverse().join("") : getBuffer().join("");
2432
- case "isValid":
2433
- if (actionObj.value) {
2434
- valueBuffer = ($.isFunction(opts.onBeforeMask) ? (opts.onBeforeMask.call(inputmask, actionObj.value, opts) || actionObj.value) : actionObj.value).split("");
2435
- checkVal.call(this, undefined, true, false, valueBuffer);
2436
- } else {
2437
- actionObj.value = isRTL ? getBuffer().slice().reverse().join("") : getBuffer().join("");
2438
- }
2439
- var buffer = getBuffer();
2440
- var rl = determineLastRequiredPosition(),
2441
- lmib = buffer.length - 1;
2442
- for (; lmib > rl; lmib--) {
2443
- if (isMask(lmib)) break;
2444
- }
2445
- buffer.splice(rl, lmib + 1 - rl);
2446
-
2447
- return isComplete(buffer) && actionObj.value === (isRTL ? getBuffer().slice().reverse().join("") : getBuffer().join(""));
2448
- case "getemptymask":
2449
- return getBufferTemplate().join("");
2450
- case "remove":
2451
- if (el && el.inputmask) {
2452
- $.data(el, "_inputmask_opts", null); //invalidate
2453
- $el = $(el);
2454
- //writeout the value
2455
- var cv = opts.autoUnmask ? unmaskedvalue(el) : el.inputmask._valueGet(opts.autoUnmask);
2456
- if (cv !== getBufferTemplate().join("")) el.inputmask._valueSet(cv, opts.autoUnmask); else el.inputmask._valueSet("");
2457
- //unbind all events
2458
- EventRuler.off(el);
2459
-
2460
- //restore the value property
2461
- var valueProperty;
2462
- if (Object.getOwnPropertyDescriptor && Object.getPrototypeOf) {
2463
- valueProperty = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(el), "value");
2464
- if (valueProperty) {
2465
- if (el.inputmask.__valueGet) {
2466
- Object.defineProperty(el, "value", {
2467
- get: el.inputmask.__valueGet,
2468
- set: el.inputmask.__valueSet,
2469
- configurable: true
2470
- });
2471
- }
2472
- }
2473
- } else if (document.__lookupGetter__ && el.__lookupGetter__("value")) {
2474
- if (el.inputmask.__valueGet) {
2475
- el.__defineGetter__("value", el.inputmask.__valueGet);
2476
- el.__defineSetter__("value", el.inputmask.__valueSet);
2477
- }
2478
- }
2479
- //clear data
2480
- el.inputmask = undefined;
2481
- }
2482
- return el;
2483
- case "getmetadata":
2484
- if ($.isArray(maskset.metadata)) {
2485
- var maskTarget = getMaskTemplate(true, 0, false).join("");
2486
- $.each(maskset.metadata, function (ndx, mtdt) {
2487
- if (mtdt.mask === maskTarget) {
2488
- maskTarget = mtdt;
2489
- return false;
2490
- }
2491
- });
2492
- return maskTarget;
2493
- }
2494
-
2495
- return maskset.metadata;
2496
- }
2497
- }
2498
- };