From 9407adc41667a79cd9f463d2493b8db8f44d2eb6 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Sun, 22 May 2022 14:37:17 +0200 Subject: [PATCH] [JS] Format all the fields if any when the document is open (bug 1766987) - it aims to fix https://bugzilla.mozilla.org/show_bug.cgi?id=1766987. --- src/scripting_api/event.js | 19 +++++++++++++++++ test/integration/scripting_spec.js | 34 ++++++++++++++++++++++++++++++ test/pdfs/bug1766987.pdf.link | 2 ++ test/test_manifest.json | 7 ++++++ 4 files changed, 62 insertions(+) create mode 100644 test/pdfs/bug1766987.pdf.link diff --git a/src/scripting_api/event.js b/src/scripting_api/event.js index f4036bb2643e6..b0f3c26f72b2b 100644 --- a/src/scripting_api/event.js +++ b/src/scripting_api/event.js @@ -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( @@ -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) { diff --git a/test/integration/scripting_spec.js b/test/integration/scripting_spec.js index d7f7f65b2a3bc..e50f371ddb07b 100644 --- a/test/integration/scripting_spec.js +++ b/test/integration/scripting_spec.js @@ -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 €"); + }) + ); + }); + }); }); diff --git a/test/pdfs/bug1766987.pdf.link b/test/pdfs/bug1766987.pdf.link new file mode 100644 index 0000000000000..77ac6322842e2 --- /dev/null +++ b/test/pdfs/bug1766987.pdf.link @@ -0,0 +1,2 @@ +https://bugzilla.mozilla.org/attachment.cgi?id=9274388 + diff --git a/test/test_manifest.json b/test/test_manifest.json index 0bca13fe1e653..b46951ab9446c 100644 --- a/test/test_manifest.json +++ b/test/test_manifest.json @@ -6494,5 +6494,12 @@ "value": "05/17/2022" } } + }, + { "id": "bug1766987", + "file": "pdfs/bug1766987.pdf", + "md5": "3ce134ead03d6158c3e8207453dcd21d", + "rounds": 1, + "link": true, + "type": "other" } ]