Skip to content

Commit

Permalink
Merge pull request #17541 from calixteman/issue17540
Browse files Browse the repository at this point in the history
Use the original value of a field when propagating event (fixes #17540)
  • Loading branch information
calixteman authored Jan 20, 2024
2 parents 03aa8a1 + 5732c0c commit f242461
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 8 deletions.
4 changes: 0 additions & 4 deletions src/scripting_api/aform.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,6 @@ class AForm {
bCurrencyPrepend
) {
const event = globalThis.event;
if (!event.value) {
return;
}

let value = this.AFMakeNumber(event.value);
if (value === null) {
event.value = "";
Expand Down
13 changes: 9 additions & 4 deletions src/scripting_api/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ class EventDispatcher {

event.value = null;
const target = this._objects[targetId];
let savedValue = target.obj.value;
let savedValue = target.obj._getValue();
this.runActions(source, target, event, "Calculate");
if (!event.rc) {
continue;
Expand All @@ -344,18 +344,23 @@ class EventDispatcher {
if (event.value !== null) {
// A new value has been calculated so set it.
target.obj.value = event.value;
} else {
event.value = target.obj._getValue();
}

event.value = target.obj.value;
this.runActions(target, target, event, "Validate");
if (!event.rc) {
if (target.obj.value !== savedValue) {
if (target.obj._getValue() !== savedValue) {
target.wrapped.value = savedValue;
}
continue;
}

savedValue = event.value = target.obj.value;
if (event.value === null) {
event.value = target.obj._getValue();
}

savedValue = target.obj._getValue();
let formattedValue = null;
if (this.runActions(target, target, event, "Format")) {
formattedValue = event.value?.toString?.();
Expand Down
42 changes: 42 additions & 0 deletions test/integration/scripting_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2324,4 +2324,46 @@ describe("Interaction", () => {
);
});
});

describe("Textfield with a number and some decimals", () => {
let pages;
let otherPages;

beforeAll(async () => {
otherPages = await Promise.all(
global.integrationSessions.map(async session =>
session.browser.newPage()
)
);
pages = await loadAndWait("issue17540.pdf", getSelector("15R"));
});

afterAll(async () => {
await closePages(pages);
await Promise.all(otherPages.map(page => page.close()));
});

it("must check the number has the correct number of decimals", async () => {
await Promise.all(
pages.map(async ([browserName, page], i) => {
await page.waitForFunction(
"window.PDFViewerApplication.scriptingReady === true"
);

await page.click(getSelector("15R"));
await page.type(getSelector("15R"), "3");
await page.keyboard.press("Enter");

await page.waitForFunction(
sel => document.querySelector(sel).value !== "",
{},
getSelector("16R")
);

const text = await page.$eval(getSelector("16R"), el => el.value);
expect(text).withContext(`In ${browserName}`).toEqual("0.900");
})
);
});
});
});
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -625,3 +625,4 @@
!bug1871353.1.pdf
!file_pdfjs_form.pdf
!issue17492.pdf
!issue17540.pdf
Binary file added test/pdfs/issue17540.pdf
Binary file not shown.
29 changes: 29 additions & 0 deletions test/unit/scripting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,11 @@ describe("Scripting", function () {
`AFNumber_Format(2, 0, 3, 0, "€", false);` +
`event.source.value = event.value;`,
],
test6: [
`event.value = 0;` +
`AFNumber_Format(2, 0, 0, 0, "€", false);` +
`event.source.value = event.value;`,
],
},
type: "text",
},
Expand All @@ -727,6 +732,30 @@ describe("Scripting", function () {
};

sandbox.createSandbox(data);
await sandbox.dispatchEventInSandbox({
id: refId,
value: "0",
name: "test1",
});
expect(send_queue.has(refId)).toEqual(true);
expect(send_queue.get(refId)).toEqual({
id: refId,
value: "0.00€",
});
send_queue.delete(refId);

await sandbox.dispatchEventInSandbox({
id: refId,
value: "",
name: "test6",
});
expect(send_queue.has(refId)).toEqual(true);
expect(send_queue.get(refId)).toEqual({
id: refId,
value: "0.00€",
});
send_queue.delete(refId);

await sandbox.dispatchEventInSandbox({
id: refId,
value: "123456.789",
Expand Down

0 comments on commit f242461

Please sign in to comment.