Skip to content

Commit

Permalink
Merge pull request #18718 from calixteman/bug1916714
Browse files Browse the repository at this point in the history
[JS] Let AFSpecial_KeystrokeEx match a format without 'decoration' (bug 1916714)
  • Loading branch information
calixteman authored Sep 9, 2024
2 parents 5f39b59 + bae32b4 commit 95cd848
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/scripting_api/aform.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,22 @@ class AForm {
}

AFSpecial_KeystrokeEx(cMask) {
const event = globalThis.event;

// Simplify the format string by removing all characters that are not
// specific to the format because the user could enter 1234567 when the
// format is 999-9999.
const simplifiedFormatStr = cMask.replaceAll(/[^9AOX]/g, "");
this.#AFSpecial_KeystrokeEx_helper(simplifiedFormatStr, false);
if (event.rc) {
return;
}

event.rc = true;
this.#AFSpecial_KeystrokeEx_helper(cMask, true);
}

#AFSpecial_KeystrokeEx_helper(cMask, warn) {
if (!cMask) {
return;
}
Expand Down Expand Up @@ -605,20 +621,26 @@ class AForm {
const err = `${GlobalConstants.IDS_INVALID_VALUE} = "${cMask}"`;

if (value.length > cMask.length) {
this._app.alert(err);
if (warn) {
this._app.alert(err);
}
event.rc = false;
return;
}

if (event.willCommit) {
if (value.length < cMask.length) {
this._app.alert(err);
if (warn) {
this._app.alert(err);
}
event.rc = false;
return;
}

if (!_checkValidity(value, cMask)) {
this._app.alert(err);
if (warn) {
this._app.alert(err);
}
event.rc = false;
return;
}
Expand All @@ -631,7 +653,9 @@ class AForm {
}

if (!_checkValidity(value, cMask)) {
this._app.alert(err);
if (warn) {
this._app.alert(err);
}
event.rc = false;
}
}
Expand All @@ -651,7 +675,7 @@ class AForm {
case 2:
const value = this.AFMergeChange(event);
formatStr =
value.length > 8 || value.startsWith("(")
value.startsWith("(") || (value.length > 7 && /^\p{N}+$/.test(value))
? "(999) 999-9999"
: "999-9999";
break;
Expand Down
122 changes: 122 additions & 0 deletions test/unit/scripting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,67 @@ describe("Scripting", function () {
send_queue.delete(refId);
});

it("should validate a US phone number with digits only (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 = "1234567890";
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(true);
send_queue.delete(refId);
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,
siblings: null,
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 = {
Expand Down Expand Up @@ -1631,6 +1692,67 @@ describe("Scripting", function () {

send_queue.delete(refId);
});

it("should validate a US phone number with digits only (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 = "1234567";
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(true);
send_queue.delete(refId);
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,
siblings: null,
value,
selRange: [i, i],
});

send_queue.delete(refId);
});
});

describe("eMailValidate", function () {
Expand Down

0 comments on commit 95cd848

Please sign in to comment.