diff --git a/src/scripting_api/field.js b/src/scripting_api/field.js index bdb2add5f..59501b912 100644 --- a/src/scripting_api/field.js +++ b/src/scripting_api/field.js @@ -80,7 +80,7 @@ class Field extends PDFObject { this._page = data.page || 0; this._strokeColor = data.strokeColor || ["G", 0]; this._textColor = data.textColor || ["G", 0]; - this._value = data.value || ""; + this._value = null; this._kidIds = data.kidIds || null; this._fieldType = getFieldType(this._actions); this._siblings = data.siblings || null; @@ -88,6 +88,9 @@ class Field extends PDFObject { this._globalEval = data.globalEval; this._appObjects = data.appObjects; + + // The value is set depending on the field type. + this.value = data.value || ""; } get currentValueIndices() { @@ -243,12 +246,13 @@ class Field extends PDFObject { this._value = ""; } else if (typeof value === "string") { switch (this._fieldType) { + case FieldType.none: + this._value = !isNaN(value) ? parseFloat(value) : value; + break; case FieldType.number: case FieldType.percent: - value = parseFloat(value); - if (!isNaN(value)) { - this._value = value; - } + const number = parseFloat(value); + this._value = !isNaN(number) ? number : 0; break; default: this._value = value; @@ -563,6 +567,9 @@ class RadioButtonField extends Field { this._id = radioData.id; } } + + this._hasBeenInitialized = true; + this._value = data.value || ""; } get value() { @@ -570,6 +577,10 @@ class RadioButtonField extends Field { } set value(value) { + if (!this._hasBeenInitialized) { + return; + } + if (value === null || value === undefined) { this._value = ""; } diff --git a/src/scripting_api/util.js b/src/scripting_api/util.js index 262c1f0aa..8496683e3 100644 --- a/src/scripting_api/util.js +++ b/src/scripting_api/util.js @@ -297,6 +297,7 @@ class Util extends PDFObject { printx(cFormat, cSource) { // case + cSource = (cSource ?? "").toString(); const handlers = [x => x, x => x.toUpperCase(), x => x.toLowerCase()]; const buf = []; let i = 0; diff --git a/test/integration/scripting_spec.js b/test/integration/scripting_spec.js index 64467f644..81afb33d7 100644 --- a/test/integration/scripting_spec.js +++ b/test/integration/scripting_spec.js @@ -1562,4 +1562,40 @@ describe("Interaction", () => { ); }); }); + + describe("in bug1802888.pdf", () => { + let pages; + + beforeAll(async () => { + pages = await loadAndWait("bug1802888.pdf", getSelector("30R")); + }); + + afterAll(async () => { + await closePages(pages); + }); + + it("must check field value is treated by default as a number", async () => { + await Promise.all( + pages.map(async ([browserName, page]) => { + await page.waitForFunction( + "window.PDFViewerApplication.scriptingReady === true" + ); + + await page.type(getSelector("30R"), "123", { + delay: 10, + }); + await page.click(getSelector("31R")); + await page.type(getSelector("31R"), "456", { + delay: 10, + }); + await page.click(getSelector("26R")); + await page.click(getSelector("27R")); + await page.waitForFunction(`${getQuerySelector("26R")}.value !== ""`); + + const value = await page.$eval(getSelector("26R"), el => el.value); + expect(value).withContext(`In ${browserName}`).toEqual("579"); + }) + ); + }); + }); }); diff --git a/test/pdfs/.gitignore b/test/pdfs/.gitignore index a2f82e23b..f5b148498 100644 --- a/test/pdfs/.gitignore +++ b/test/pdfs/.gitignore @@ -556,3 +556,4 @@ !textfields.pdf !freetext_no_appearance.pdf !issue15690.pdf +!bug1802888.pdf diff --git a/test/pdfs/bug1802888.pdf b/test/pdfs/bug1802888.pdf new file mode 100755 index 000000000..08ddcb5a3 Binary files /dev/null and b/test/pdfs/bug1802888.pdf differ diff --git a/test/unit/scripting_spec.js b/test/unit/scripting_spec.js index bfcc10a56..87274f7ee 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, }); });