diff --git a/src/scripting_api/event.js b/src/scripting_api/event.js index 30b5b1878..6a2047bc3 100644 --- a/src/scripting_api/event.js +++ b/src/scripting_api/event.js @@ -249,7 +249,8 @@ class EventDispatcher { this.runCalculate(source, event); - const savedValue = (event.value = source.obj.value); + const savedValue = source.obj._getValue(); + event.value = source.obj.value; let formattedValue = null; if (this.runActions(source, source, event, "Format")) { diff --git a/src/scripting_api/field.js b/src/scripting_api/field.js index af67c9931..869822222 100644 --- a/src/scripting_api/field.js +++ b/src/scripting_api/field.js @@ -251,14 +251,19 @@ class Field extends PDFObject { this._value = ""; } else if (typeof value === "string") { switch (this._fieldType) { - case FieldType.none: - this._value = !isNaN(value) ? parseFloat(value) : value; + case FieldType.none: { + this._originalValue = value; + const _value = value.trim().replace(",", "."); + this._value = !isNaN(_value) ? parseFloat(_value) : value; break; + } case FieldType.number: - case FieldType.percent: - const number = parseFloat(value); + case FieldType.percent: { + const _value = value.trim().replace(",", "."); + const number = parseFloat(_value); this._value = !isNaN(number) ? number : 0; break; + } default: this._value = value; } @@ -267,6 +272,10 @@ class Field extends PDFObject { } } + _getValue() { + return this._originalValue ?? this.value; + } + _setChoiceValue(value) { if (this.multipleSelection) { if (!Array.isArray(value)) { diff --git a/src/scripting_api/proxy.js b/src/scripting_api/proxy.js index a1de00278..c1f6e7809 100644 --- a/src/scripting_api/proxy.js +++ b/src/scripting_api/proxy.js @@ -63,7 +63,7 @@ class ProxyHandler { typeof old !== "function" ) { const data = { id: obj._id }; - data[prop] = obj[prop]; + data[prop] = prop === "value" ? obj._getValue() : obj[prop]; // send the updated value to the other side if (!obj._siblings) { diff --git a/test/integration/scripting_spec.js b/test/integration/scripting_spec.js index c60feb1cd..f335fc1b6 100644 --- a/test/integration/scripting_spec.js +++ b/test/integration/scripting_spec.js @@ -1780,4 +1780,78 @@ describe("Interaction", () => { ); }); }); + + describe("in bug1811694.pdf", () => { + let pages; + + beforeAll(async () => { + pages = await loadAndWait("bug1811694.pdf", getSelector("25R")); + }); + + afterAll(async () => { + await closePages(pages); + }); + + it("must check that a field value with a number isn't changed", async () => { + await Promise.all( + pages.map(async ([browserName, page]) => { + await page.waitForFunction( + "window.PDFViewerApplication.scriptingReady === true" + ); + + await page.click(getSelector("25R")); + await page.type(getSelector("25R"), "00000000123", { delay: 10 }); + + let text = await page.$eval(getSelector("25R"), el => el.value); + expect(text).withContext(`In ${browserName}`).toEqual("00000000123"); + + await page.click(getSelector("26R")); + await page.waitForTimeout(10); + + text = await page.$eval(getSelector("25R"), el => el.value); + expect(text).withContext(`In ${browserName}`).toEqual("00000000123"); + }) + ); + }); + }); + + describe("in bug1811510.pdf", () => { + let pages; + + beforeAll(async () => { + pages = await loadAndWait("bug1811510.pdf", getSelector("22R")); + }); + + afterAll(async () => { + await closePages(pages); + }); + + it("must check that a field value with a number with a comma has the correct value", async () => { + await Promise.all( + pages.map(async ([browserName, page]) => { + await page.waitForFunction( + "window.PDFViewerApplication.scriptingReady === true" + ); + + let text = await page.$eval(getSelector("22R"), el => el.value); + expect(text).withContext(`In ${browserName}`).toEqual("5,25"); + + await page.$eval(getSelector("31R"), el => el.value); + expect(text).withContext(`In ${browserName}`).toEqual("5,25"); + + await page.click(getSelector("22R")); + await page.waitForTimeout(10); + + text = await page.$eval(getSelector("22R"), el => el.value); + expect(text).withContext(`In ${browserName}`).toEqual("5,25"); + + await page.click(getSelector("31R")); + await page.waitForTimeout(10); + + text = await page.$eval(getSelector("31R"), el => el.value); + expect(text).withContext(`In ${browserName}`).toEqual("5.25"); + }) + ); + }); + }); }); diff --git a/test/pdfs/.gitignore b/test/pdfs/.gitignore index b83117441..d69ba9659 100644 --- a/test/pdfs/.gitignore +++ b/test/pdfs/.gitignore @@ -568,3 +568,5 @@ !issue15815.pdf !issue15818.pdf !autoprint.pdf +!bug1811694.pdf +!bug1811510.pdf diff --git a/test/pdfs/bug1811510.pdf b/test/pdfs/bug1811510.pdf new file mode 100755 index 000000000..31da02d65 Binary files /dev/null and b/test/pdfs/bug1811510.pdf differ diff --git a/test/pdfs/bug1811694.pdf b/test/pdfs/bug1811694.pdf new file mode 100755 index 000000000..1c5db2826 Binary files /dev/null and b/test/pdfs/bug1811694.pdf differ diff --git a/test/unit/scripting_spec.js b/test/unit/scripting_spec.js index 87274f7ee..bfcc10a56 100644 --- a/test/unit/scripting_spec.js +++ b/test/unit/scripting_spec.js @@ -346,7 +346,7 @@ describe("Scripting", function () { expect(send_queue.has(refId)).toEqual(true); expect(send_queue.get(refId)).toEqual({ id: refId, - value: 123, + value: "123", }); }); @@ -826,7 +826,7 @@ describe("Scripting", function () { expect(send_queue.get(refId)).toEqual({ id: refId, siblings: null, - value: 123456.789, + value: "123456.789", formattedValue: null, }); }); @@ -1006,7 +1006,7 @@ describe("Scripting", function () { expect(send_queue.get(refId)).toEqual({ id: refId, siblings: null, - value: 321, + value: "321", formattedValue: null, }); });