From 098cc16c460549534fec9b31f07a623fbe0982a8 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Wed, 6 Dec 2023 09:44:30 +0100 Subject: [PATCH] Set text field value as a string when it's for a date or a time (bug 1868503) --- src/scripting_api/common.js | 2 +- src/scripting_api/field.js | 9 +++-- test/unit/scripting_spec.js | 70 +++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/src/scripting_api/common.js b/src/scripting_api/common.js index 3728689cf71bd..64d8edf55394c 100644 --- a/src/scripting_api/common.js +++ b/src/scripting_api/common.js @@ -49,7 +49,7 @@ function getFieldType(actions) { if (format.startsWith("AFDate_")) { return FieldType.date; } - if (format.startsWith("AFTime__")) { + if (format.startsWith("AFTime_")) { return FieldType.time; } return FieldType.none; diff --git a/src/scripting_api/field.js b/src/scripting_api/field.js index 5a692c9993d10..816824ce980e4 100644 --- a/src/scripting_api/field.js +++ b/src/scripting_api/field.js @@ -13,7 +13,7 @@ * limitations under the License. */ -import { createActionsMap, getFieldType } from "./common.js"; +import { createActionsMap, FieldType, getFieldType } from "./common.js"; import { Color } from "./color.js"; import { PDFObject } from "./pdf_object.js"; @@ -245,7 +245,12 @@ class Field extends PDFObject { return; } - if (value === "" || typeof value !== "string") { + if ( + value === "" || + typeof value !== "string" || + // When the field type is date or time, the value must be a string. + this._fieldType >= FieldType.date + ) { this._originalValue = undefined; this._value = value; return; diff --git a/test/unit/scripting_spec.js b/test/unit/scripting_spec.js index dbeb58d9b8f9d..d3d85dc5118c0 100644 --- a/test/unit/scripting_spec.js +++ b/test/unit/scripting_spec.js @@ -971,6 +971,76 @@ describe("Scripting", function () { value: "4/15/07 3:14 am", }); }); + + it("should format a date", async () => { + const refId = getId(); + const data = { + objects: { + field: [ + { + id: refId, + value: "", + actions: { + Format: [`AFDate_FormatEx("mmddyyyy");`], + Keystroke: [`AFDate_KeystrokeEx("mmddyyyy");`], + }, + type: "text", + }, + ], + }, + appInfo: { language: "en-US", platform: "Linux x86_64" }, + calculationOrder: [], + dispatchEventName: "_dispatchMe", + }; + + sandbox.createSandbox(data); + await sandbox.dispatchEventInSandbox({ + id: refId, + value: "12062023", + name: "Keystroke", + willCommit: true, + }); + expect(send_queue.has(refId)).toEqual(true); + expect(send_queue.get(refId)).toEqual({ + id: refId, + siblings: null, + value: "12062023", + formattedValue: "12062023", + }); + send_queue.delete(refId); + + await sandbox.dispatchEventInSandbox({ + id: refId, + value: "1206202", + name: "Keystroke", + willCommit: true, + }); + expect(send_queue.has(refId)).toEqual(true); + expect(send_queue.get(refId)).toEqual({ + id: refId, + siblings: null, + value: "", + formattedValue: null, + selRange: [0, 0], + }); + send_queue.delete(refId); + + sandbox.createSandbox(data); + await sandbox.dispatchEventInSandbox({ + id: refId, + value: "02062023", + name: "Keystroke", + willCommit: true, + }); + expect(send_queue.has(refId)).toEqual(true); + expect(send_queue.get(refId)).toEqual({ + id: refId, + siblings: null, + value: "02062023", + formattedValue: "02062023", + }); + send_queue.delete(refId); + }); }); describe("AFRange_Validate", function () {