Merge pull request #12247 from timvandermeij/acroform-choice-null
Improve the field value parsing for choice widgets to handle `null` values
This commit is contained in:
commit
3c790936c1
@ -1610,11 +1610,14 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine the field value. In this case, it may be a string or an
|
// The field value can be `null` if no item is selected, a string if one
|
||||||
// array of strings. For convenience in the display layer, convert the
|
// item is selected or an array of strings if multiple items are selected.
|
||||||
// string to an array of one string as well.
|
// For consistency in the API and convenience in the display layer, we
|
||||||
if (!Array.isArray(this.data.fieldValue)) {
|
// always make the field value an array with zero, one or multiple items.
|
||||||
|
if (isString(this.data.fieldValue)) {
|
||||||
this.data.fieldValue = [this.data.fieldValue];
|
this.data.fieldValue = [this.data.fieldValue];
|
||||||
|
} else if (!this.data.fieldValue) {
|
||||||
|
this.data.fieldValue = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process field flags for the display layer.
|
// Process field flags for the display layer.
|
||||||
|
@ -2478,48 +2478,32 @@ describe("annotation", function () {
|
|||||||
}, done.fail);
|
}, done.fail);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle array field values", function (done) {
|
it("should convert the field value to an array", function (done) {
|
||||||
const fieldValue = ["Foo", "Bar"];
|
const inputs = [null, "Foo", ["Foo", "Bar"]];
|
||||||
|
const outputs = [[], ["Foo"], ["Foo", "Bar"]];
|
||||||
|
|
||||||
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(968, 0);
|
const choiceWidgetRef = Ref.get(968, 0);
|
||||||
const xref = new XRefMock([
|
const xref = new XRefMock([
|
||||||
{ ref: choiceWidgetRef, data: choiceWidgetDict },
|
{ ref: choiceWidgetRef, data: choiceWidgetDict },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
AnnotationFactory.create(
|
return AnnotationFactory.create(
|
||||||
xref,
|
xref,
|
||||||
choiceWidgetRef,
|
choiceWidgetRef,
|
||||||
pdfManagerMock,
|
pdfManagerMock,
|
||||||
idFactoryMock
|
idFactoryMock
|
||||||
).then(({ data }) => {
|
).then(({ data }) => {
|
||||||
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
|
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
|
||||||
expect(data.fieldValue).toEqual(fieldValue);
|
expect(data.fieldValue).toEqual(outputs[i]);
|
||||||
done();
|
});
|
||||||
}, done.fail);
|
});
|
||||||
});
|
}
|
||||||
|
promise.then(done, done.fail);
|
||||||
it("should handle string field values", function (done) {
|
|
||||||
const fieldValue = "Foo";
|
|
||||||
|
|
||||||
choiceWidgetDict.set("V", fieldValue);
|
|
||||||
|
|
||||||
const choiceWidgetRef = Ref.get(978, 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 unknown flags", function (done) {
|
it("should handle unknown flags", function (done) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user