Skip to content

Commit 0a4225b

Browse files
committed
[JS] Fix few errors around AFSpecial_Keystroke
- @cincodenada found some errors which are fixed in this patch; - it partially fixes issue #14306; - add some tests.
1 parent 290cbc5 commit 0a4225b

File tree

3 files changed

+183
-4
lines changed

3 files changed

+183
-4
lines changed

src/scripting_api/aform.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ class AForm {
486486
]);
487487

488488
function _checkValidity(_value, _cMask) {
489-
for (let i = 0, ii = value.length; i < ii; i++) {
489+
for (let i = 0, ii = _value.length; i < ii; i++) {
490490
const mask = _cMask.charAt(i);
491491
const char = _value.charAt(i);
492492
const checker = checkers.get(mask);
@@ -564,7 +564,7 @@ class AForm {
564564
event.change.length +
565565
event.selStart -
566566
event.selEnd;
567-
if (finalLen >= 8) {
567+
if (finalLen > 8 || event.value[0] === "(") {
568568
formatStr = "(999) 999-9999";
569569
} else {
570570
formatStr = "999-9999";

src/scripting_api/event.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class Event {
2626
this.richChange = data.richChange || [];
2727
this.richChangeEx = data.richChangeEx || [];
2828
this.richValue = data.richValue || [];
29-
this.selEnd = data.selEnd || -1;
30-
this.selStart = data.selStart || -1;
29+
this.selEnd = data.selEnd ?? -1;
30+
this.selStart = data.selStart ?? -1;
3131
this.shift = data.shift || false;
3232
this.source = data.source || null;
3333
this.target = data.target || null;

test/unit/scripting_spec.js

+179
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,185 @@ describe("Scripting", function () {
11761176
});
11771177
});
11781178

1179+
describe("AFSpecial_Keystroke", function () {
1180+
it("should validate a zip code on a keystroke event", async () => {
1181+
const refId = getId();
1182+
const data = {
1183+
objects: {
1184+
field: [
1185+
{
1186+
id: refId,
1187+
value: "",
1188+
actions: {
1189+
Keystroke: [`AFSpecial_Keystroke(0);`],
1190+
},
1191+
type: "text",
1192+
},
1193+
],
1194+
},
1195+
appInfo: { language: "en-US", platform: "Linux x86_64" },
1196+
calculationOrder: [],
1197+
dispatchEventName: "_dispatchMe",
1198+
};
1199+
sandbox.createSandbox(data);
1200+
1201+
let value = "";
1202+
const changes = "12345";
1203+
let i = 0;
1204+
1205+
for (; i < changes.length; i++) {
1206+
const change = changes.charAt(i);
1207+
await sandbox.dispatchEventInSandbox({
1208+
id: refId,
1209+
value,
1210+
change,
1211+
name: "Keystroke",
1212+
willCommit: false,
1213+
selStart: i,
1214+
selEnd: i,
1215+
});
1216+
expect(send_queue.has(refId)).toEqual(false);
1217+
value += change;
1218+
}
1219+
1220+
await sandbox.dispatchEventInSandbox({
1221+
id: refId,
1222+
value,
1223+
change: "A",
1224+
name: "Keystroke",
1225+
willCommit: false,
1226+
selStart: i,
1227+
selEnd: i,
1228+
});
1229+
expect(send_queue.has(refId)).toEqual(true);
1230+
expect(send_queue.get(refId)).toEqual({
1231+
id: refId,
1232+
value,
1233+
selRange: [i, i],
1234+
});
1235+
1236+
send_queue.delete(refId);
1237+
});
1238+
1239+
it("should validate a US phone number (long) on a keystroke event", async () => {
1240+
const refId = getId();
1241+
const data = {
1242+
objects: {
1243+
field: [
1244+
{
1245+
id: refId,
1246+
value: "",
1247+
actions: {
1248+
Keystroke: [`AFSpecial_Keystroke(2);`],
1249+
},
1250+
type: "text",
1251+
},
1252+
],
1253+
},
1254+
appInfo: { language: "en-US", platform: "Linux x86_64" },
1255+
calculationOrder: [],
1256+
dispatchEventName: "_dispatchMe",
1257+
};
1258+
sandbox.createSandbox(data);
1259+
1260+
let value = "";
1261+
const changes = "(123) 456-7890";
1262+
let i = 0;
1263+
1264+
for (; i < changes.length; i++) {
1265+
const change = changes.charAt(i);
1266+
await sandbox.dispatchEventInSandbox({
1267+
id: refId,
1268+
value,
1269+
change,
1270+
name: "Keystroke",
1271+
willCommit: false,
1272+
selStart: i,
1273+
selEnd: i,
1274+
});
1275+
expect(send_queue.has(refId)).toEqual(false);
1276+
value += change;
1277+
}
1278+
1279+
await sandbox.dispatchEventInSandbox({
1280+
id: refId,
1281+
value,
1282+
change: "A",
1283+
name: "Keystroke",
1284+
willCommit: false,
1285+
selStart: i,
1286+
selEnd: i,
1287+
});
1288+
expect(send_queue.has(refId)).toEqual(true);
1289+
expect(send_queue.get(refId)).toEqual({
1290+
id: refId,
1291+
value,
1292+
selRange: [i, i],
1293+
});
1294+
1295+
send_queue.delete(refId);
1296+
});
1297+
1298+
it("should validate a US phone number (short) on a keystroke event", async () => {
1299+
const refId = getId();
1300+
const data = {
1301+
objects: {
1302+
field: [
1303+
{
1304+
id: refId,
1305+
value: "",
1306+
actions: {
1307+
Keystroke: [`AFSpecial_Keystroke(2);`],
1308+
},
1309+
type: "text",
1310+
},
1311+
],
1312+
},
1313+
appInfo: { language: "en-US", platform: "Linux x86_64" },
1314+
calculationOrder: [],
1315+
dispatchEventName: "_dispatchMe",
1316+
};
1317+
sandbox.createSandbox(data);
1318+
1319+
let value = "";
1320+
const changes = "123-4567";
1321+
let i = 0;
1322+
1323+
for (; i < changes.length; i++) {
1324+
const change = changes.charAt(i);
1325+
await sandbox.dispatchEventInSandbox({
1326+
id: refId,
1327+
value,
1328+
change,
1329+
name: "Keystroke",
1330+
willCommit: false,
1331+
selStart: i,
1332+
selEnd: i,
1333+
});
1334+
expect(send_queue.has(refId)).toEqual(false);
1335+
value += change;
1336+
}
1337+
1338+
await sandbox.dispatchEventInSandbox({
1339+
id: refId,
1340+
value,
1341+
change: "A",
1342+
name: "Keystroke",
1343+
willCommit: false,
1344+
selStart: i,
1345+
selEnd: i,
1346+
});
1347+
expect(send_queue.has(refId)).toEqual(true);
1348+
expect(send_queue.get(refId)).toEqual({
1349+
id: refId,
1350+
value,
1351+
selRange: [i, i],
1352+
});
1353+
1354+
send_queue.delete(refId);
1355+
});
1356+
});
1357+
11791358
describe("eMailValidate", function () {
11801359
it("should validate an e-mail address", async () => {
11811360
let value = await myeval(`eMailValidate(123)`);

0 commit comments

Comments
 (0)