Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JS] Format all the fields if any when the document is open (bug 1766987) #14950

Merged
merged 1 commit into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/scripting_api/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ class EventDispatcher {
event.name = baseEvent.name;
}
if (id === "doc") {
if (event.name === "Open") {
// Before running the Open event, we format all the fields
// (see bug 1766987).
this.formatAll();
}
this._document.obj._dispatchDocEvent(event.name);
} else if (id === "page") {
this._document.obj._dispatchPageEvent(
Expand Down Expand Up @@ -180,6 +185,20 @@ class EventDispatcher {
}
}

formatAll() {
// Run format actions if any for all the fields.
const event = (globalThis.event = new Event({}));
for (const source of Object.values(this._objects)) {
event.value = source.obj.value;
if (this.runActions(source, source, event, "Format")) {
source.obj._send({
id: source.obj._id,
formattedValue: event.value?.toString?.(),
});
}
}
}

runValidation(source, event) {
const didValidateRun = this.runActions(source, source, event, "Validate");
if (event.rc) {
Expand Down
34 changes: 34 additions & 0 deletions test/integration/scripting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1302,4 +1302,38 @@ describe("Interaction", () => {
);
});
});

describe("in bug1766987.pdf", () => {
let pages;

beforeAll(async () => {
pages = await loadAndWait("bug1766987.pdf", "#\\37 5R");
});

afterAll(async () => {
await closePages(pages);
});

it("must check that field value is correctly formatted", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await page.waitForFunction(
"window.PDFViewerApplication.scriptingReady === true"
);

let text = await page.$eval("#\\37 5R", el => el.value);
expect(text).withContext(`In ${browserName}`).toEqual("150.32 €");

text = await page.$eval("#\\38 2R", el => el.value);
expect(text).withContext(`In ${browserName}`).toEqual("12.74 Kwh");

text = await page.$eval("#\\39 1R", el => el.value);
expect(text).withContext(`In ${browserName}`).toEqual("352.19 Kwh");

text = await page.$eval("#\\31 01R", el => el.value);
expect(text).withContext(`In ${browserName}`).toEqual("20.57 €");
})
);
});
});
});
2 changes: 2 additions & 0 deletions test/pdfs/bug1766987.pdf.link
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
https://bugzilla.mozilla.org/attachment.cgi?id=9274388

7 changes: 7 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6494,5 +6494,12 @@
"value": "05/17/2022"
}
}
},
{ "id": "bug1766987",
"file": "pdfs/bug1766987.pdf",
"md5": "3ce134ead03d6158c3e8207453dcd21d",
"rounds": 1,
"link": true,
"type": "other"
}
]