From 39f59547296a7c97597f3688e149c4f34785b419 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Tue, 3 Nov 2020 16:53:21 +0100 Subject: [PATCH] JS -- Add default value in annotation data * these values are used when a form is resetted --- src/core/annotation.js | 10 ++++++++++ test/unit/annotation_spec.js | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/core/annotation.js b/src/core/annotation.js index 3241152d76882..f609159437e4b 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -951,6 +951,13 @@ class WidgetAnnotation extends Annotation { }); data.fieldValue = this._decodeFormValue(fieldValue); + const defaultFieldValue = getInheritableProperty({ + dict, + key: "DV", + getArray: true, + }); + data.defaultFieldValue = this._decodeFormValue(defaultFieldValue); + data.alternativeText = stringToPDFString(dict.get("TU") || ""); data.defaultAppearance = getInheritableProperty({ dict, key: "DA" }) || @@ -1652,6 +1659,7 @@ class TextWidgetAnnotation extends WidgetAnnotation { return { id: this.data.id, value: this.data.fieldValue, + defaultValue: this.data.defaultFieldValue, multiline: this.data.multiLine, password: this.hasFieldFlag(AnnotationFieldFlag.PASSWORD), charLimit: this.data.maxLen, @@ -1976,6 +1984,7 @@ class ButtonWidgetAnnotation extends WidgetAnnotation { return { id: this.data.id, value, + defaultValue: this.data.defaultFieldValue, editable: !this.data.readOnly, name: this.data.fieldName, rect: this.data.rect, @@ -2052,6 +2061,7 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation { return { id: this.data.id, value, + defaultValue: this.data.defaultFieldValue, editable: !this.data.readOnly, name: this.data.fieldName, rect: this.data.rect, diff --git a/test/unit/annotation_spec.js b/test/unit/annotation_spec.js index d212c467e338e..79b7708ce34d0 100644 --- a/test/unit/annotation_spec.js +++ b/test/unit/annotation_spec.js @@ -1421,6 +1421,8 @@ describe("annotation", function () { }); it("should handle unknown text alignment, maximum length and flags", function (done) { + textWidgetDict.set("DV", "foo"); + const textWidgetRef = Ref.get(124, 0); const xref = new XRefMock([{ ref: textWidgetRef, data: textWidgetDict }]); @@ -1437,6 +1439,7 @@ describe("annotation", function () { expect(data.hidden).toEqual(false); expect(data.multiLine).toEqual(false); expect(data.comb).toEqual(false); + expect(data.defaultFieldValue).toEqual("foo"); done(); }, done.fail); }); @@ -2021,6 +2024,7 @@ describe("annotation", function () { it("should handle checkboxes with export value", function (done) { buttonWidgetDict.set("V", Name.get("1")); + buttonWidgetDict.set("DV", Name.get("2")); const appearanceStatesDict = new Dict(); const normalAppearanceDict = new Dict(); @@ -2044,6 +2048,7 @@ describe("annotation", function () { expect(data.annotationType).toEqual(AnnotationType.WIDGET); expect(data.checkBox).toEqual(true); expect(data.fieldValue).toEqual("1"); + expect(data.defaultFieldValue).toEqual("2"); expect(data.radioButton).toEqual(false); expect(data.exportValue).toEqual("Checked"); done(); @@ -2052,6 +2057,7 @@ describe("annotation", function () { it("should handle checkboxes without export value", function (done) { buttonWidgetDict.set("V", Name.get("1")); + buttonWidgetDict.set("DV", Name.get("2")); const buttonWidgetRef = Ref.get(124, 0); const xref = new XRefMock([ @@ -2067,6 +2073,7 @@ describe("annotation", function () { expect(data.annotationType).toEqual(AnnotationType.WIDGET); expect(data.checkBox).toEqual(true); expect(data.fieldValue).toEqual("1"); + expect(data.defaultFieldValue).toEqual("2"); expect(data.radioButton).toEqual(false); done(); }, done.fail); @@ -2074,6 +2081,7 @@ describe("annotation", function () { it("should handle checkboxes without /Off appearance", function (done) { buttonWidgetDict.set("V", Name.get("1")); + buttonWidgetDict.set("DV", Name.get("2")); const appearanceStatesDict = new Dict(); const normalAppearanceDict = new Dict(); @@ -2096,6 +2104,7 @@ describe("annotation", function () { expect(data.annotationType).toEqual(AnnotationType.WIDGET); expect(data.checkBox).toEqual(true); expect(data.fieldValue).toEqual("1"); + expect(data.defaultFieldValue).toEqual("2"); expect(data.radioButton).toEqual(false); expect(data.exportValue).toEqual("Checked"); done(); @@ -3093,6 +3102,7 @@ describe("annotation", function () { choiceWidgetDict.set("Opt", [encodedString]); choiceWidgetDict.set("V", encodedString); + choiceWidgetDict.set("DV", Name.get("foo")); const choiceWidgetRef = Ref.get(984, 0); const xref = new XRefMock([ @@ -3107,6 +3117,7 @@ describe("annotation", function () { ).then(({ data }) => { expect(data.annotationType).toEqual(AnnotationType.WIDGET); expect(data.fieldValue).toEqual([decodedString]); + expect(data.defaultFieldValue).toEqual("foo"); expect(data.options).toEqual([ { exportValue: decodedString, displayValue: decodedString }, ]);