validation.js 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. import { EventHandlers } from "./eventhandlers";
  2. import { keyCode, keys } from "./keycode.js";
  3. import {
  4. determineLastRequiredPosition,
  5. determineNewCaretPosition,
  6. getBuffer,
  7. getLastValidPosition,
  8. isMask,
  9. resetMaskSet,
  10. seekNext,
  11. seekPrevious
  12. } from "./positioning";
  13. import {
  14. determineTestTemplate,
  15. getDecisionTaker,
  16. getPlaceholder,
  17. getTest,
  18. getTests,
  19. getTestTemplate
  20. } from "./validation-tests";
  21. export {
  22. alternate,
  23. checkAlternationMatch,
  24. isComplete,
  25. isSelection,
  26. isValid,
  27. refreshFromBuffer,
  28. revalidateMask,
  29. handleRemove
  30. };
  31. // tobe put on prototype?
  32. function alternate(maskPos, c, strict, fromIsValid, rAltPos, selection) {
  33. // pos == true => generalize
  34. const inputmask = this,
  35. $ = this.dependencyLib,
  36. opts = this.opts,
  37. maskset = inputmask.maskset;
  38. if (!inputmask.hasAlternator) return false;
  39. let validPsClone = $.extend(true, [], maskset.validPositions),
  40. tstClone = $.extend(true, {}, maskset.tests),
  41. lastAlt,
  42. alternation,
  43. isValidRslt = false,
  44. returnRslt = false,
  45. altPos,
  46. prevAltPos,
  47. i,
  48. validPos,
  49. decisionPos,
  50. lAltPos =
  51. rAltPos !== undefined ? rAltPos : getLastValidPosition.call(inputmask),
  52. nextPos,
  53. input,
  54. begin,
  55. end;
  56. if (selection) {
  57. begin = selection.begin;
  58. end = selection.end;
  59. if (selection.begin > selection.end) {
  60. begin = selection.end;
  61. end = selection.begin;
  62. }
  63. }
  64. if (lAltPos === -1 && rAltPos === undefined) {
  65. // do not recurse when already paste the beginning
  66. lastAlt = 0;
  67. prevAltPos = getTest.call(inputmask, lastAlt);
  68. alternation = prevAltPos.alternation;
  69. } else {
  70. // find last modified alternation
  71. for (; lAltPos >= 0; lAltPos--) {
  72. altPos = maskset.validPositions[lAltPos];
  73. if (altPos && altPos.alternation !== undefined) {
  74. if (
  75. lAltPos <= (maskPos || 0) &&
  76. prevAltPos &&
  77. prevAltPos.locator[altPos.alternation] !==
  78. altPos.locator[altPos.alternation]
  79. ) {
  80. break;
  81. }
  82. lastAlt = lAltPos;
  83. alternation = maskset.validPositions[lastAlt].alternation;
  84. prevAltPos = altPos;
  85. }
  86. }
  87. }
  88. if (alternation !== undefined) {
  89. decisionPos = parseInt(lastAlt);
  90. maskset.excludes[decisionPos] = maskset.excludes[decisionPos] || [];
  91. if (maskPos !== true) {
  92. // generalize
  93. maskset.excludes[decisionPos].push(
  94. getDecisionTaker(prevAltPos) + ":" + prevAltPos.alternation
  95. );
  96. }
  97. let validInputs = [],
  98. resultPos = -1;
  99. for (
  100. i = decisionPos;
  101. decisionPos < getLastValidPosition.call(inputmask, undefined, true) + 1;
  102. i++
  103. ) {
  104. if (resultPos === -1 && maskPos <= i && c !== undefined) {
  105. validInputs.push(c);
  106. resultPos = validInputs.length - 1;
  107. }
  108. validPos = maskset.validPositions[decisionPos];
  109. if (
  110. validPos &&
  111. validPos.generatedInput !== true &&
  112. (selection === undefined || i < begin || i >= end)
  113. ) {
  114. validInputs.push(validPos.input);
  115. }
  116. // delete maskset.validPositions[i++];
  117. maskset.validPositions.splice(decisionPos, 1);
  118. }
  119. if (resultPos === -1 && c !== undefined) {
  120. validInputs.push(c);
  121. resultPos = validInputs.length - 1;
  122. }
  123. while (
  124. maskset.excludes[decisionPos] !== undefined &&
  125. maskset.excludes[decisionPos].length < 10
  126. ) {
  127. // maskset.tests[decisionPos] = undefined; //clear decisionPos
  128. maskset.tests = {}; // clear all
  129. resetMaskSet.call(inputmask, true); // clear getbuffer
  130. isValidRslt = true;
  131. for (i = 0; i < validInputs.length; i++) {
  132. nextPos =
  133. isValidRslt.caret ||
  134. (opts.insertMode == false && nextPos != undefined)
  135. ? seekNext.call(inputmask, nextPos)
  136. : getLastValidPosition.call(inputmask, undefined, true) + 1;
  137. input = validInputs[i];
  138. // nextPos = translatePosition.call(inputmask, nextPos);
  139. if (
  140. !(isValidRslt = isValid.call(
  141. inputmask,
  142. nextPos,
  143. input,
  144. false,
  145. fromIsValid,
  146. true
  147. ))
  148. ) {
  149. break;
  150. }
  151. if (i === resultPos) {
  152. returnRslt = isValidRslt;
  153. }
  154. if (maskPos == true && isValidRslt) {
  155. // return validposition on generalise
  156. returnRslt = { caretPos: i };
  157. }
  158. }
  159. if (!isValidRslt) {
  160. resetMaskSet.call(inputmask);
  161. prevAltPos = getTest.call(inputmask, decisionPos); // get the current decisionPos to exclude ~ needs to be before restoring the initial validation
  162. // reset & revert
  163. maskset.validPositions = $.extend(true, [], validPsClone);
  164. maskset.tests = $.extend(true, {}, tstClone); // refresh tests after possible alternating
  165. if (maskset.excludes[decisionPos]) {
  166. if (prevAltPos.alternation != undefined) {
  167. const decisionTaker = getDecisionTaker(prevAltPos);
  168. if (
  169. maskset.excludes[decisionPos].indexOf(
  170. decisionTaker + ":" + prevAltPos.alternation
  171. ) !== -1
  172. ) {
  173. returnRslt = alternate.call(
  174. inputmask,
  175. maskPos,
  176. c,
  177. strict,
  178. fromIsValid,
  179. decisionPos - 1,
  180. selection
  181. );
  182. break;
  183. }
  184. maskset.excludes[decisionPos].push(
  185. decisionTaker + ":" + prevAltPos.alternation
  186. );
  187. for (
  188. i = decisionPos;
  189. i < getLastValidPosition.call(inputmask, undefined, true) + 1;
  190. i++
  191. )
  192. maskset.validPositions.splice(decisionPos);
  193. } else delete maskset.excludes[decisionPos];
  194. } else {
  195. // latest alternation
  196. returnRslt = alternate.call(
  197. inputmask,
  198. maskPos,
  199. c,
  200. strict,
  201. fromIsValid,
  202. decisionPos - 1,
  203. selection
  204. );
  205. break;
  206. }
  207. } else {
  208. break;
  209. }
  210. }
  211. }
  212. // reset alternation excludes
  213. if (!returnRslt || opts.keepStatic !== false) {
  214. delete maskset.excludes[decisionPos];
  215. }
  216. return returnRslt;
  217. }
  218. function casing(elem, test, pos) {
  219. const opts = this.opts,
  220. maskset = this.maskset;
  221. switch (opts.casing || test.casing) {
  222. case "upper":
  223. elem = elem.toUpperCase();
  224. break;
  225. case "lower":
  226. elem = elem.toLowerCase();
  227. break;
  228. case "title":
  229. var posBefore = maskset.validPositions[pos - 1];
  230. if (
  231. pos === 0 ||
  232. (posBefore && posBefore.input === String.fromCharCode(keyCode.Space))
  233. ) {
  234. elem = elem.toUpperCase();
  235. } else {
  236. elem = elem.toLowerCase();
  237. }
  238. break;
  239. default:
  240. if (typeof opts.casing === "function") {
  241. const args = Array.prototype.slice.call(arguments);
  242. args.push(maskset.validPositions);
  243. elem = opts.casing.apply(this, args);
  244. }
  245. }
  246. return elem;
  247. }
  248. // tobe put on prototype?
  249. function checkAlternationMatch(altArr1, altArr2, na) {
  250. const opts = this.opts;
  251. let altArrC = opts.greedy ? altArr2 : altArr2.slice(0, 1),
  252. isMatch = false,
  253. naArr = na !== undefined ? na.split(",") : [],
  254. naNdx;
  255. // remove no alternate indexes from alternation array
  256. for (let i = 0; i < naArr.length; i++) {
  257. if ((naNdx = altArr1.indexOf(naArr[i])) !== -1) {
  258. altArr1.splice(naNdx, 1);
  259. }
  260. }
  261. for (let alndx = 0; alndx < altArr1.length; alndx++) {
  262. if (altArrC.includes(altArr1[alndx])) {
  263. isMatch = true;
  264. break;
  265. }
  266. }
  267. return isMatch;
  268. }
  269. // tobe put on prototype?
  270. function handleRemove(input, c, pos, strict, fromIsValid) {
  271. const inputmask = this,
  272. maskset = this.maskset,
  273. opts = this.opts;
  274. if (opts.numericInput || inputmask.isRTL) {
  275. if (c === keys.Backspace) {
  276. c = keys.Delete;
  277. } else if (c === keys.Delete) {
  278. c = keys.Backspace;
  279. }
  280. if (inputmask.isRTL) {
  281. const pend = pos.end;
  282. pos.end = pos.begin;
  283. pos.begin = pend;
  284. }
  285. }
  286. const lvp = getLastValidPosition.call(inputmask, undefined, true);
  287. if (pos.end >= getBuffer.call(inputmask).length && lvp >= pos.end) {
  288. // handle numeric negate symbol offset, due to dynamic jit masking
  289. pos.end = lvp + 1;
  290. }
  291. if (c === keys.Backspace) {
  292. if (pos.end - pos.begin < 1) {
  293. pos.begin = seekPrevious.call(inputmask, pos.begin);
  294. }
  295. } else if (c === keys.Delete) {
  296. if (pos.begin === pos.end) {
  297. pos.end = isMask.call(inputmask, pos.end, true, true)
  298. ? pos.end + 1
  299. : seekNext.call(inputmask, pos.end) + 1;
  300. }
  301. }
  302. let offset;
  303. if ((offset = revalidateMask.call(inputmask, pos)) !== false) {
  304. if (
  305. (strict !== true && opts.keepStatic !== false) ||
  306. (opts.regex !== null &&
  307. getTest.call(inputmask, pos.begin).match.def.indexOf("|") !== -1)
  308. ) {
  309. // TODO NEEDS BETTER CHECK WHEN TO ALTERNATE ~ opts regex isn"t good enough
  310. alternate.call(inputmask, true);
  311. }
  312. if (strict !== true) {
  313. maskset.p = c === keys.Delete ? pos.begin + offset : pos.begin;
  314. maskset.p = determineNewCaretPosition.call(
  315. inputmask,
  316. {
  317. begin: maskset.p,
  318. end: maskset.p
  319. },
  320. false,
  321. opts.insertMode === false && c === keys.Backspace ? "none" : undefined
  322. ).begin;
  323. }
  324. }
  325. }
  326. // tobe put on prototype?
  327. function isComplete(buffer) {
  328. // return true / false / undefined (repeat *)
  329. const inputmask = this,
  330. opts = this.opts,
  331. maskset = this.maskset;
  332. if (typeof opts.isComplete === "function")
  333. return opts.isComplete(buffer, opts);
  334. if (opts.repeat === "*") return undefined;
  335. let complete = false,
  336. lrp = determineLastRequiredPosition.call(inputmask, true),
  337. aml = lrp.l; // seekPrevious.call(inputmask, lrp.l);
  338. if (
  339. lrp.def === undefined ||
  340. lrp.def.newBlockMarker ||
  341. lrp.def.optionality ||
  342. lrp.def.optionalQuantifier
  343. ) {
  344. complete = true;
  345. for (let i = 0; i <= aml; i++) {
  346. const test = getTestTemplate.call(inputmask, i).match;
  347. if (
  348. (test.static !== true &&
  349. maskset.validPositions[i] === undefined &&
  350. (test.optionality === false ||
  351. test.optionality === undefined ||
  352. (test.optionality && test.newBlockMarker == false)) &&
  353. (test.optionalQuantifier === false ||
  354. test.optionalQuantifier === undefined)) ||
  355. (test.static === true &&
  356. test.def != "" &&
  357. buffer[i] !== getPlaceholder.call(inputmask, i, test))
  358. ) {
  359. complete = false;
  360. break;
  361. }
  362. }
  363. }
  364. return complete;
  365. }
  366. function isSelection(posObj) {
  367. const inputmask = this,
  368. opts = this.opts,
  369. insertModeOffset = opts.insertMode ? 0 : 1;
  370. return inputmask.isRTL
  371. ? posObj.begin - posObj.end > insertModeOffset
  372. : posObj.end - posObj.begin > insertModeOffset;
  373. }
  374. // tobe put on prototype?
  375. function isValid(
  376. pos,
  377. c,
  378. strict,
  379. fromIsValid,
  380. fromAlternate,
  381. validateOnly,
  382. fromCheckval
  383. ) {
  384. // strict true ~ no correction or autofill
  385. const inputmask = this,
  386. $ = this.dependencyLib,
  387. opts = this.opts,
  388. maskset = inputmask.maskset;
  389. strict = strict === true; // always set a value to strict to prevent possible strange behavior in the extensions
  390. let maskPos = pos;
  391. if (pos.begin !== undefined) {
  392. // position was a position object - used to handle a delete by typing over a selection
  393. maskPos = inputmask.isRTL ? pos.end : pos.begin;
  394. }
  395. function processCommandObject(commandObj) {
  396. if (commandObj !== undefined) {
  397. if (commandObj.remove !== undefined) {
  398. // remove position(s)
  399. if (!Array.isArray(commandObj.remove))
  400. commandObj.remove = [commandObj.remove];
  401. commandObj.remove
  402. .sort(function (a, b) {
  403. return inputmask.isRTL ? a.pos - b.pos : b.pos - a.pos;
  404. })
  405. .forEach(function (lmnt) {
  406. revalidateMask.call(inputmask, { begin: lmnt, end: lmnt + 1 });
  407. });
  408. commandObj.remove = undefined;
  409. }
  410. if (commandObj.insert !== undefined) {
  411. // insert position(s)
  412. if (!Array.isArray(commandObj.insert))
  413. commandObj.insert = [commandObj.insert];
  414. commandObj.insert
  415. .sort(function (a, b) {
  416. return inputmask.isRTL ? b.pos - a.pos : a.pos - b.pos;
  417. })
  418. .forEach(function (lmnt) {
  419. if (lmnt.c !== "") {
  420. isValid.call(
  421. inputmask,
  422. lmnt.pos,
  423. lmnt.c,
  424. lmnt.strict !== undefined ? lmnt.strict : true,
  425. lmnt.fromIsValid !== undefined ? lmnt.fromIsValid : fromIsValid
  426. );
  427. }
  428. });
  429. commandObj.insert = undefined;
  430. }
  431. if (commandObj.refreshFromBuffer && commandObj.buffer) {
  432. const refresh = commandObj.refreshFromBuffer;
  433. refreshFromBuffer.call(
  434. inputmask,
  435. refresh === true ? refresh : refresh.start,
  436. refresh.end,
  437. commandObj.buffer
  438. );
  439. commandObj.refreshFromBuffer = undefined;
  440. }
  441. if (commandObj.rewritePosition !== undefined) {
  442. maskPos = commandObj.rewritePosition;
  443. // commandObj.rewritePosition = undefined;
  444. commandObj = true; // see prevalidation in isValid
  445. }
  446. }
  447. return commandObj;
  448. }
  449. function _isValid(position, c, strict) {
  450. let rslt = false;
  451. getTests.call(inputmask, position).every(function (tst, ndx) {
  452. const test = tst.match;
  453. // make sure the buffer is set and correct
  454. getBuffer.call(inputmask, true);
  455. if (
  456. test.jit &&
  457. maskset.validPositions[seekPrevious.call(inputmask, position)] ===
  458. undefined
  459. ) {
  460. // ignore if jit is not desirable
  461. rslt = false;
  462. } else {
  463. // return is false or a json object => { pos: ??, c: ??} or true
  464. rslt =
  465. test.fn != null
  466. ? test.fn.test(
  467. c,
  468. maskset,
  469. position,
  470. strict,
  471. opts,
  472. isSelection.call(inputmask, pos)
  473. )
  474. : (c === test.def || c === opts.skipOptionalPartCharacter) &&
  475. test.def !== "" // non mask
  476. ? {
  477. c:
  478. getPlaceholder.call(inputmask, position, test, true) ||
  479. test.def,
  480. pos: position
  481. }
  482. : false;
  483. }
  484. if (rslt !== false) {
  485. let elem = rslt.c !== undefined ? rslt.c : c,
  486. validatedPos = position;
  487. elem =
  488. elem === opts.skipOptionalPartCharacter && test.static === true
  489. ? getPlaceholder.call(inputmask, position, test, true) || test.def
  490. : elem;
  491. rslt = processCommandObject(rslt);
  492. if (rslt !== true && rslt.pos !== undefined && rslt.pos !== position) {
  493. // their is a position offset
  494. validatedPos = rslt.pos;
  495. }
  496. if (rslt !== true && rslt.pos === undefined && rslt.c === undefined) {
  497. return false; // breakout if nothing to insert
  498. }
  499. if (
  500. revalidateMask.call(
  501. inputmask,
  502. pos,
  503. $.extend({}, tst, {
  504. input: casing.call(inputmask, elem, test, validatedPos)
  505. }),
  506. fromIsValid,
  507. validatedPos
  508. ) === false
  509. ) {
  510. rslt = false;
  511. }
  512. return false; // break from loop
  513. }
  514. return true;
  515. });
  516. return rslt;
  517. }
  518. let result = true,
  519. positionsClone = $.extend(true, [], maskset.validPositions); // clone the currentPositions
  520. if (
  521. opts.keepStatic === false &&
  522. maskset.excludes[maskPos] !== undefined &&
  523. fromAlternate !== true &&
  524. fromIsValid !== true
  525. ) {
  526. for (let i = maskPos; i < (inputmask.isRTL ? pos.begin : pos.end); i++) {
  527. if (maskset.excludes[i] !== undefined) {
  528. maskset.excludes[i] = undefined;
  529. delete maskset.tests[i];
  530. }
  531. }
  532. }
  533. if (
  534. typeof opts.preValidation === "function" &&
  535. fromIsValid !== true &&
  536. validateOnly !== true
  537. ) {
  538. result = opts.preValidation.call(
  539. inputmask,
  540. getBuffer.call(inputmask),
  541. maskPos,
  542. c,
  543. isSelection.call(inputmask, pos),
  544. opts,
  545. maskset,
  546. pos,
  547. strict || fromAlternate
  548. );
  549. result = processCommandObject(result);
  550. }
  551. if (result === true) {
  552. // preValidation result
  553. result = _isValid(maskPos, c, strict);
  554. if (
  555. (!strict || fromIsValid === true) &&
  556. result === false &&
  557. validateOnly !== true
  558. ) {
  559. const currentPosValid = maskset.validPositions[maskPos];
  560. if (
  561. currentPosValid &&
  562. currentPosValid.match.static === true &&
  563. (currentPosValid.match.def === c ||
  564. c === opts.skipOptionalPartCharacter)
  565. ) {
  566. result = {
  567. caret: seekNext.call(inputmask, maskPos)
  568. };
  569. } else {
  570. if (
  571. opts.insertMode ||
  572. maskset.validPositions[seekNext.call(inputmask, maskPos)] ===
  573. undefined ||
  574. pos.end > maskPos
  575. ) {
  576. // does the input match on a further position?
  577. let skip = false;
  578. if (
  579. maskset.jitOffset[maskPos] &&
  580. maskset.validPositions[seekNext.call(inputmask, maskPos)] ===
  581. undefined
  582. ) {
  583. result = isValid.call(
  584. inputmask,
  585. maskPos + maskset.jitOffset[maskPos],
  586. c,
  587. true,
  588. true
  589. );
  590. if (result !== false) {
  591. if (fromAlternate !== true) result.caret = maskPos;
  592. skip = true;
  593. }
  594. }
  595. if (pos.end > maskPos) {
  596. maskset.validPositions[maskPos] = undefined;
  597. }
  598. if (
  599. !skip &&
  600. !isMask.call(inputmask, maskPos, opts.keepStatic && maskPos === 0)
  601. ) {
  602. for (
  603. let nPos = maskPos + 1,
  604. snPos = seekNext.call(inputmask, maskPos, false, maskPos !== 0);
  605. nPos <= snPos;
  606. nPos++
  607. ) {
  608. // if (!isMask(nPos, true)) {
  609. // continue;
  610. // }
  611. result = _isValid(nPos, c, strict);
  612. if (result !== false) {
  613. result =
  614. trackbackPositions.call(
  615. inputmask,
  616. maskPos,
  617. result.pos !== undefined ? result.pos : nPos
  618. ) || result;
  619. maskPos = nPos;
  620. break;
  621. }
  622. }
  623. }
  624. }
  625. }
  626. }
  627. if (inputmask.hasAlternator && fromAlternate !== true && !strict) {
  628. fromAlternate = true; // stop possible loop
  629. if (
  630. result === false &&
  631. opts.keepStatic &&
  632. (isComplete.call(inputmask, getBuffer.call(inputmask)) || maskPos === 0)
  633. ) {
  634. // try fuzzy alternator logic
  635. result = alternate.call(
  636. inputmask,
  637. maskPos,
  638. c,
  639. strict,
  640. fromIsValid,
  641. undefined,
  642. pos
  643. );
  644. } else if (
  645. isSelection.call(inputmask, pos) &&
  646. maskset.tests[maskPos] &&
  647. maskset.tests[maskPos].length > 1 &&
  648. opts.keepStatic
  649. ) {
  650. // selection clears an alternated keepstatic mask ~ #2189
  651. result = alternate.call(inputmask, true);
  652. } else if (
  653. result == true &&
  654. opts.numericInput !== true &&
  655. maskset.tests[maskPos] &&
  656. maskset.tests[maskPos].length > 1 &&
  657. getLastValidPosition.call(inputmask, undefined, true) > maskPos
  658. ) {
  659. // console.log("Alternating");
  660. result = alternate.call(inputmask, true);
  661. }
  662. }
  663. if (result === true) {
  664. result = {
  665. pos: maskPos
  666. };
  667. }
  668. }
  669. if (
  670. typeof opts.postValidation === "function" &&
  671. fromIsValid !== true &&
  672. validateOnly !== true
  673. ) {
  674. const postResult = opts.postValidation.call(
  675. inputmask,
  676. getBuffer.call(inputmask, true),
  677. pos.begin !== undefined ? (inputmask.isRTL ? pos.end : pos.begin) : pos,
  678. c,
  679. result,
  680. opts,
  681. maskset,
  682. strict,
  683. fromCheckval
  684. );
  685. if (postResult !== undefined) {
  686. result = postResult === true ? result : postResult;
  687. }
  688. }
  689. if (result && result.pos === undefined) {
  690. result.pos = maskPos;
  691. }
  692. if (result === false || validateOnly === true) {
  693. resetMaskSet.call(inputmask, true);
  694. maskset.validPositions = $.extend(true, [], positionsClone); // revert validation changes
  695. } else {
  696. trackbackPositions.call(inputmask, undefined, maskPos, true);
  697. }
  698. let endResult = processCommandObject(result);
  699. // console.log("returned result " + JSON.stringify(endResult));
  700. if (inputmask.maxLength !== undefined) {
  701. const buffer = getBuffer.call(inputmask);
  702. if (buffer.length > inputmask.maxLength && !fromIsValid) {
  703. resetMaskSet.call(inputmask, true);
  704. maskset.validPositions = $.extend(true, [], positionsClone); // revert validation changes
  705. endResult = false;
  706. }
  707. }
  708. return endResult;
  709. }
  710. // tobe put on prototype?
  711. function positionCanMatchDefinition(pos, testDefinition, opts) {
  712. const inputmask = this,
  713. maskset = this.maskset;
  714. let valid = false,
  715. tests = getTests.call(inputmask, pos);
  716. for (let tndx = 0; tndx < tests.length; tndx++) {
  717. if (
  718. tests[tndx].match &&
  719. ((tests[tndx].match.nativeDef ===
  720. testDefinition.match[opts.shiftPositions ? "def" : "nativeDef"] &&
  721. (!opts.shiftPositions || !testDefinition.match.static)) ||
  722. tests[tndx].match.nativeDef === testDefinition.match.nativeDef ||
  723. (opts.regex &&
  724. !tests[tndx].match.static &&
  725. tests[tndx].match.fn.test(
  726. testDefinition.input,
  727. maskset,
  728. pos,
  729. false,
  730. opts
  731. )))
  732. ) {
  733. valid = true;
  734. break;
  735. } else if (
  736. tests[tndx].match &&
  737. tests[tndx].match.def === testDefinition.match.nativeDef
  738. ) {
  739. valid = undefined;
  740. break;
  741. }
  742. }
  743. if (valid === false) {
  744. if (maskset.jitOffset[pos] !== undefined) {
  745. valid = positionCanMatchDefinition.call(
  746. inputmask,
  747. pos + maskset.jitOffset[pos],
  748. testDefinition,
  749. opts
  750. );
  751. }
  752. }
  753. return valid;
  754. }
  755. // tobe put on prototype?
  756. function refreshFromBuffer(start, end, buffer) {
  757. const inputmask = this,
  758. maskset = this.maskset,
  759. opts = this.opts,
  760. $ = this.dependencyLib;
  761. // checkVal.call(inputmask, el, false, true, isRTL ? buffer.reverse() : buffer);
  762. let i,
  763. p,
  764. skipOptionalPartCharacter = opts.skipOptionalPartCharacter,
  765. bffr = inputmask.isRTL ? buffer.slice().reverse() : buffer;
  766. opts.skipOptionalPartCharacter = "";
  767. if (start === true) {
  768. resetMaskSet.call(inputmask, false);
  769. start = 0;
  770. end = buffer.length;
  771. p = determineNewCaretPosition.call(
  772. inputmask,
  773. { begin: 0, end: 0 },
  774. false
  775. ).begin;
  776. } else {
  777. for (i = start; i < end; i++) {
  778. maskset.validPositions.splice(start, 0);
  779. }
  780. p = start;
  781. }
  782. const keypress = new $.Event("keypress");
  783. for (i = start; i < end; i++) {
  784. keypress.key = bffr[i].toString();
  785. inputmask.ignorable = false; // make sure ignorable is ignored ;-)
  786. const valResult = EventHandlers.keypressEvent.call(
  787. inputmask,
  788. keypress,
  789. true,
  790. false,
  791. false,
  792. p
  793. );
  794. if (valResult !== false && valResult !== undefined) {
  795. p = valResult.forwardPosition;
  796. }
  797. }
  798. opts.skipOptionalPartCharacter = skipOptionalPartCharacter;
  799. }
  800. // tobe put on prototype?
  801. // fill in best positions according the current input
  802. function trackbackPositions(originalPos, newPos, fillOnly) {
  803. const inputmask = this,
  804. maskset = this.maskset,
  805. $ = this.dependencyLib;
  806. // console.log("trackbackPositions " + originalPos + " " + newPos);
  807. if (originalPos === undefined) {
  808. // find previous valid
  809. for (originalPos = newPos - 1; originalPos > 0; originalPos--) {
  810. if (maskset.validPositions[originalPos]) break;
  811. }
  812. }
  813. for (let ps = originalPos; ps < newPos; ps++) {
  814. if (
  815. maskset.validPositions[ps] === undefined &&
  816. !isMask.call(inputmask, ps, false)
  817. ) {
  818. const vp =
  819. ps == 0 ? getTest.call(inputmask, ps) : maskset.validPositions[ps - 1];
  820. if (vp) {
  821. const tests = getTests.call(inputmask, ps).slice();
  822. if (tests[tests.length - 1].match.def === "") tests.pop();
  823. var bestMatch = determineTestTemplate.call(inputmask, ps, tests),
  824. np;
  825. if (
  826. bestMatch &&
  827. (bestMatch.match.jit !== true ||
  828. (bestMatch.match.newBlockMarker === "master" &&
  829. (np = maskset.validPositions[ps + 1]) &&
  830. np.match.optionalQuantifier === true))
  831. ) {
  832. bestMatch = $.extend({}, bestMatch, {
  833. input:
  834. getPlaceholder.call(inputmask, ps, bestMatch.match, true) ||
  835. bestMatch.match.def
  836. });
  837. bestMatch.generatedInput = true;
  838. revalidateMask.call(inputmask, ps, bestMatch, true);
  839. if (fillOnly !== true) {
  840. // revalidate the new position to update the locator value
  841. const cvpInput = maskset.validPositions[newPos].input;
  842. maskset.validPositions[newPos] = undefined;
  843. return isValid.call(inputmask, newPos, cvpInput, true, true);
  844. }
  845. }
  846. }
  847. }
  848. }
  849. }
  850. // tobe put on prototype?
  851. function revalidateMask(pos, validTest, fromIsValid, validatedPos) {
  852. // console.log("revalidateMask " + fromIsValid);
  853. const inputmask = this,
  854. maskset = this.maskset,
  855. opts = this.opts,
  856. $ = this.dependencyLib;
  857. function IsEnclosedStatic(pos, valids, selection) {
  858. const posMatch = valids[pos];
  859. if (
  860. posMatch !== undefined &&
  861. posMatch.match.static === true &&
  862. posMatch.match.optionality !== true &&
  863. (valids[0] === undefined || valids[0].alternation === undefined)
  864. ) {
  865. const prevMatch =
  866. selection.begin <= pos - 1
  867. ? valids[pos - 1] &&
  868. valids[pos - 1].match.static === true &&
  869. valids[pos - 1]
  870. : valids[pos - 1],
  871. nextMatch =
  872. selection.end > pos + 1
  873. ? valids[pos + 1] &&
  874. valids[pos + 1].match.static === true &&
  875. valids[pos + 1]
  876. : valids[pos + 1];
  877. return prevMatch && nextMatch;
  878. }
  879. return false;
  880. }
  881. let offset = 0,
  882. begin = pos.begin !== undefined ? pos.begin : pos,
  883. end = pos.end !== undefined ? pos.end : pos,
  884. valid = true;
  885. if (pos.begin > pos.end) {
  886. begin = pos.end;
  887. end = pos.begin;
  888. }
  889. validatedPos = validatedPos !== undefined ? validatedPos : begin;
  890. if (
  891. fromIsValid === undefined &&
  892. (begin !== end ||
  893. (opts.insertMode && maskset.validPositions[validatedPos] !== undefined) ||
  894. validTest === undefined ||
  895. validTest.match.optionalQuantifier ||
  896. validTest.match.optionality)
  897. ) {
  898. // reposition & revalidate others
  899. let positionsClone = $.extend(true, [], maskset.validPositions),
  900. lvp = getLastValidPosition.call(inputmask, undefined, true),
  901. i;
  902. maskset.p = begin; // needed for alternated position after overtype selection
  903. const clearpos = isSelection.call(inputmask, pos) ? begin : validatedPos;
  904. for (i = lvp; i >= clearpos; i--) {
  905. maskset.validPositions.splice(i, 1);
  906. if (validTest === undefined) delete maskset.tests[i + 1];
  907. }
  908. let j = validatedPos,
  909. posMatch = j,
  910. t,
  911. canMatch,
  912. test;
  913. if (validTest) {
  914. maskset.validPositions[validatedPos] = $.extend(true, {}, validTest);
  915. posMatch++;
  916. j++;
  917. }
  918. if (positionsClone[end] == undefined && maskset.jitOffset[end]) {
  919. end += maskset.jitOffset[end] + 1;
  920. }
  921. for (i = validTest ? end : end - 1; i <= lvp; i++) {
  922. if (
  923. (t = positionsClone[i]) !== undefined &&
  924. t.generatedInput !== true &&
  925. (i >= end ||
  926. (i >= begin &&
  927. IsEnclosedStatic(i, positionsClone, {
  928. begin,
  929. end
  930. })))
  931. ) {
  932. while (
  933. ((test = getTest.call(inputmask, posMatch)), test.match.def !== "")
  934. ) {
  935. // loop needed to match further positions
  936. if (
  937. (canMatch = positionCanMatchDefinition.call(
  938. inputmask,
  939. posMatch,
  940. t,
  941. opts
  942. )) !== false ||
  943. t.match.def === "+"
  944. ) {
  945. // validated match //we still need some hackery for the + validator (numeric alias)
  946. if (t.match.def === "+") getBuffer.call(inputmask, true);
  947. const result = isValid.call(
  948. inputmask,
  949. posMatch,
  950. t.input,
  951. t.match.def !== "+",
  952. /* t.match.def !== "+" */ true
  953. );
  954. valid = result !== false;
  955. j = (result.pos || posMatch) + 1;
  956. if (!valid && canMatch) break;
  957. } else {
  958. valid = false;
  959. }
  960. if (valid) {
  961. if (validTest === undefined && t.match.static && i === pos.begin)
  962. offset++;
  963. break;
  964. }
  965. if (
  966. (!valid && getBuffer.call(inputmask), posMatch > maskset.maskLength)
  967. ) {
  968. break;
  969. }
  970. posMatch++;
  971. }
  972. if (getTest.call(inputmask, posMatch).match.def == "") {
  973. valid = false;
  974. }
  975. // restore position
  976. posMatch = j;
  977. }
  978. if (!valid) break;
  979. }
  980. if (!valid) {
  981. maskset.validPositions = $.extend(true, [], positionsClone);
  982. resetMaskSet.call(inputmask, true);
  983. return false;
  984. }
  985. } else if (
  986. validTest &&
  987. getTest.call(inputmask, validatedPos).match.cd === validTest.match.cd
  988. ) {
  989. maskset.validPositions[validatedPos] = $.extend(true, {}, validTest);
  990. }
  991. resetMaskSet.call(inputmask, true);
  992. return offset;
  993. }