Merge pull request #14429 from calixteman/14306

[JS] Fix few errors around AFSpecial_Keystroke
This commit is contained in:
calixteman 2022-01-08 03:50:05 -08:00 committed by GitHub
commit f25e95a2b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 185 additions and 16 deletions

View File

@ -433,11 +433,8 @@ class AForm {
} }
psf = this.AFMakeNumber(psf); psf = this.AFMakeNumber(psf);
if (psf === null) {
throw new Error("Invalid psf in AFSpecial_Format");
}
let formatStr = ""; let formatStr;
switch (psf) { switch (psf) {
case 0: case 0:
formatStr = "99999"; formatStr = "99999";
@ -486,7 +483,7 @@ class AForm {
]); ]);
function _checkValidity(_value, _cMask) { function _checkValidity(_value, _cMask) {
for (let i = 0, ii = value.length; i < ii; i++) { for (let i = 0, ii = _value.length; i < ii; i++) {
const mask = _cMask.charAt(i); const mask = _cMask.charAt(i);
const char = _value.charAt(i); const char = _value.charAt(i);
const checker = checkers.get(mask); const checker = checkers.get(mask);
@ -546,9 +543,6 @@ class AForm {
} }
psf = this.AFMakeNumber(psf); psf = this.AFMakeNumber(psf);
if (psf === null) {
throw new Error("Invalid psf in AFSpecial_Keystroke");
}
let formatStr; let formatStr;
switch (psf) { switch (psf) {
@ -559,12 +553,8 @@ class AForm {
formatStr = "99999-9999"; formatStr = "99999-9999";
break; break;
case 2: case 2:
const finalLen = const value = this.AFMergeChange(event);
event.value.length + if (value.length > 8 || value.startsWith("(")) {
event.change.length +
event.selStart -
event.selEnd;
if (finalLen >= 8) {
formatStr = "(999) 999-9999"; formatStr = "(999) 999-9999";
} else { } else {
formatStr = "999-9999"; formatStr = "999-9999";

View File

@ -26,8 +26,8 @@ class Event {
this.richChange = data.richChange || []; this.richChange = data.richChange || [];
this.richChangeEx = data.richChangeEx || []; this.richChangeEx = data.richChangeEx || [];
this.richValue = data.richValue || []; this.richValue = data.richValue || [];
this.selEnd = data.selEnd || -1; this.selEnd = data.selEnd ?? -1;
this.selStart = data.selStart || -1; this.selStart = data.selStart ?? -1;
this.shift = data.shift || false; this.shift = data.shift || false;
this.source = data.source || null; this.source = data.source || null;
this.target = data.target || null; this.target = data.target || null;

View File

@ -1176,6 +1176,185 @@ describe("Scripting", function () {
}); });
}); });
describe("AFSpecial_Keystroke", function () {
it("should validate a zip code on a keystroke event", async () => {
const refId = getId();
const data = {
objects: {
field: [
{
id: refId,
value: "",
actions: {
Keystroke: [`AFSpecial_Keystroke(0);`],
},
type: "text",
},
],
},
appInfo: { language: "en-US", platform: "Linux x86_64" },
calculationOrder: [],
dispatchEventName: "_dispatchMe",
};
sandbox.createSandbox(data);
let value = "";
const changes = "12345";
let i = 0;
for (; i < changes.length; i++) {
const change = changes.charAt(i);
await sandbox.dispatchEventInSandbox({
id: refId,
value,
change,
name: "Keystroke",
willCommit: false,
selStart: i,
selEnd: i,
});
expect(send_queue.has(refId)).toEqual(false);
value += change;
}
await sandbox.dispatchEventInSandbox({
id: refId,
value,
change: "A",
name: "Keystroke",
willCommit: false,
selStart: i,
selEnd: i,
});
expect(send_queue.has(refId)).toEqual(true);
expect(send_queue.get(refId)).toEqual({
id: refId,
value,
selRange: [i, i],
});
send_queue.delete(refId);
});
it("should validate a US phone number (long) on a keystroke event", async () => {
const refId = getId();
const data = {
objects: {
field: [
{
id: refId,
value: "",
actions: {
Keystroke: [`AFSpecial_Keystroke(2);`],
},
type: "text",
},
],
},
appInfo: { language: "en-US", platform: "Linux x86_64" },
calculationOrder: [],
dispatchEventName: "_dispatchMe",
};
sandbox.createSandbox(data);
let value = "";
const changes = "(123) 456-7890";
let i = 0;
for (; i < changes.length; i++) {
const change = changes.charAt(i);
await sandbox.dispatchEventInSandbox({
id: refId,
value,
change,
name: "Keystroke",
willCommit: false,
selStart: i,
selEnd: i,
});
expect(send_queue.has(refId)).toEqual(false);
value += change;
}
await sandbox.dispatchEventInSandbox({
id: refId,
value,
change: "A",
name: "Keystroke",
willCommit: false,
selStart: i,
selEnd: i,
});
expect(send_queue.has(refId)).toEqual(true);
expect(send_queue.get(refId)).toEqual({
id: refId,
value,
selRange: [i, i],
});
send_queue.delete(refId);
});
it("should validate a US phone number (short) on a keystroke event", async () => {
const refId = getId();
const data = {
objects: {
field: [
{
id: refId,
value: "",
actions: {
Keystroke: [`AFSpecial_Keystroke(2);`],
},
type: "text",
},
],
},
appInfo: { language: "en-US", platform: "Linux x86_64" },
calculationOrder: [],
dispatchEventName: "_dispatchMe",
};
sandbox.createSandbox(data);
let value = "";
const changes = "123-4567";
let i = 0;
for (; i < changes.length; i++) {
const change = changes.charAt(i);
await sandbox.dispatchEventInSandbox({
id: refId,
value,
change,
name: "Keystroke",
willCommit: false,
selStart: i,
selEnd: i,
});
expect(send_queue.has(refId)).toEqual(false);
value += change;
}
await sandbox.dispatchEventInSandbox({
id: refId,
value,
change: "A",
name: "Keystroke",
willCommit: false,
selStart: i,
selEnd: i,
});
expect(send_queue.has(refId)).toEqual(true);
expect(send_queue.get(refId)).toEqual({
id: refId,
value,
selRange: [i, i],
});
send_queue.delete(refId);
});
});
describe("eMailValidate", function () { describe("eMailValidate", function () {
it("should validate an e-mail address", async () => { it("should validate an e-mail address", async () => {
let value = await myeval(`eMailValidate(123)`); let value = await myeval(`eMailValidate(123)`);