Skip to content

Commit

Permalink
Merge pull request #17385 from calixteman/bug1868503
Browse files Browse the repository at this point in the history
Set text field value as a string when it's for a date or a time (bug 1868503)
  • Loading branch information
calixteman authored Dec 6, 2023
2 parents c8f6b39 + 098cc16 commit 7e64f82
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/scripting_api/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions src/scripting_api/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
Expand Down
70 changes: 70 additions & 0 deletions test/unit/scripting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 7e64f82

Please sign in to comment.