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

Improve the field value parsing for choice widgets to handle null values #12247

Merged
merged 2 commits into from
Aug 21, 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
11 changes: 7 additions & 4 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1610,11 +1610,14 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
}
}

// Determine the field value. In this case, it may be a string or an
// array of strings. For convenience in the display layer, convert the
// string to an array of one string as well.
if (!Array.isArray(this.data.fieldValue)) {
// The field value can be `null` if no item is selected, a string if one
// item is selected or an array of strings if multiple items are selected.
// For consistency in the API and convenience in the display layer, we
// always make the field value an array with zero, one or multiple items.
if (isString(this.data.fieldValue)) {
this.data.fieldValue = [this.data.fieldValue];
timvandermeij marked this conversation as resolved.
Show resolved Hide resolved
} else if (!this.data.fieldValue) {
this.data.fieldValue = [];
}
timvandermeij marked this conversation as resolved.
Show resolved Hide resolved

// Process field flags for the display layer.
Expand Down
62 changes: 23 additions & 39 deletions test/unit/annotation_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2478,48 +2478,32 @@ describe("annotation", function () {
}, done.fail);
});

it("should handle array field values", function (done) {
const fieldValue = ["Foo", "Bar"];
it("should convert the field value to an array", function (done) {
const inputs = [null, "Foo", ["Foo", "Bar"]];
const outputs = [[], ["Foo"], ["Foo", "Bar"]];

choiceWidgetDict.set("V", fieldValue);

const choiceWidgetRef = Ref.get(968, 0);
const xref = new XRefMock([
{ ref: choiceWidgetRef, data: choiceWidgetDict },
]);

AnnotationFactory.create(
xref,
choiceWidgetRef,
pdfManagerMock,
idFactoryMock
).then(({ data }) => {
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.fieldValue).toEqual(fieldValue);
done();
}, done.fail);
});

it("should handle string field values", function (done) {
const fieldValue = "Foo";

choiceWidgetDict.set("V", fieldValue);
let promise = Promise.resolve();
for (let i = 0, ii = inputs.length; i < ii; i++) {
promise = promise.then(() => {
choiceWidgetDict.set("V", inputs[i]);

const choiceWidgetRef = Ref.get(978, 0);
const xref = new XRefMock([
{ ref: choiceWidgetRef, data: choiceWidgetDict },
]);
const choiceWidgetRef = Ref.get(968, 0);
const xref = new XRefMock([
{ ref: choiceWidgetRef, data: choiceWidgetDict },
]);

AnnotationFactory.create(
xref,
choiceWidgetRef,
pdfManagerMock,
idFactoryMock
).then(({ data }) => {
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.fieldValue).toEqual([fieldValue]);
done();
}, done.fail);
return AnnotationFactory.create(
xref,
choiceWidgetRef,
pdfManagerMock,
idFactoryMock
).then(({ data }) => {
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.fieldValue).toEqual(outputs[i]);
});
});
}
promise.then(done, done.fail);
});

it("should handle unknown flags", function (done) {
Expand Down