Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-minor] JS -- Add default value in annotation data #12568

Merged
merged 1 commit into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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