Skip to content

Commit

Permalink
JS -- Add default value in annotation data
Browse files Browse the repository at this point in the history
 * these values are used when a form is resetted
  • Loading branch information
calixteman committed Nov 5, 2020
1 parent 4e13559 commit 39f5954
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" }) ||
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions test/unit/annotation_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }]);

Expand All @@ -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);
});
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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([
Expand All @@ -2067,13 +2073,15 @@ 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);
});

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();
Expand All @@ -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();
Expand Down Expand Up @@ -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([
Expand All @@ -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 },
]);
Expand Down

0 comments on commit 39f5954

Please sign in to comment.