1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

[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.
This commit is contained in:
Calixte Denizet 2022-01-08 00:11:40 +01:00
parent 290cbc5232
commit 6369617e6f
3 changed files with 185 additions and 16 deletions

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 () {
it("should validate an e-mail address", async () => {
let value = await myeval(`eMailValidate(123)`);