Decode widget form values consistently

The helper method `_decodeFormValue` is used to ensure that it happens
in one place. Note that form values are field values, display values
and export values.
This commit is contained in:
Tim van der Meij 2020-08-22 15:02:29 +02:00
parent 36e149800e
commit 1b82ad8fff
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762
2 changed files with 43 additions and 19 deletions

View File

@ -803,11 +803,14 @@ class WidgetAnnotation extends Annotation {
data.annotationType = AnnotationType.WIDGET;
data.fieldName = this._constructFieldName(dict);
data.fieldValue = getInheritableProperty({
const fieldValue = getInheritableProperty({
dict,
key: "V",
getArray: true,
});
data.fieldValue = this._decodeFormValue(fieldValue);
data.alternativeText = stringToPDFString(dict.get("TU") || "");
data.defaultAppearance =
getInheritableProperty({ dict, key: "DA" }) ||
@ -882,6 +885,28 @@ class WidgetAnnotation extends Annotation {
return fieldName.join(".");
}
/**
* Decode the given form value.
*
* @private
* @memberof WidgetAnnotation
* @param {Array<string>|Name|string} formValue - The (possibly encoded)
* form value.
* @returns {Array<string>|string|null}
*/
_decodeFormValue(formValue) {
if (Array.isArray(formValue)) {
return formValue
.filter(item => isString(item))
.map(item => stringToPDFString(item));
} else if (isName(formValue)) {
return stringToPDFString(formValue.name);
} else if (isString(formValue)) {
return stringToPDFString(formValue);
}
return null;
}
/**
* Check if a provided field flag is set.
*
@ -1194,7 +1219,9 @@ class TextWidgetAnnotation extends WidgetAnnotation {
const dict = params.dict;
// The field value is always a string.
this.data.fieldValue = stringToPDFString(this.data.fieldValue || "");
if (!isString(this.data.fieldValue)) {
this.data.fieldValue = "";
}
// Determine the alignment of text in the field.
let alignment = getInheritableProperty({ dict, key: "Q" });
@ -1499,10 +1526,6 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
}
_processCheckBox(params) {
if (isName(this.data.fieldValue)) {
this.data.fieldValue = this.data.fieldValue.name;
}
const customAppearance = params.dict.get("AP");
if (!isDict(customAppearance)) {
return;
@ -1541,7 +1564,7 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
const fieldParentValue = fieldParent.get("V");
if (isName(fieldParentValue)) {
this.parent = params.dict.getRaw("Parent");
this.data.fieldValue = fieldParentValue.name;
this.data.fieldValue = this._decodeFormValue(fieldParentValue);
}
}
@ -1602,8 +1625,10 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
const isOptionArray = Array.isArray(option);
this.data.options[i] = {
exportValue: isOptionArray ? xref.fetchIfRef(option[0]) : option,
displayValue: stringToPDFString(
exportValue: this._decodeFormValue(
isOptionArray ? xref.fetchIfRef(option[0]) : option
),
displayValue: this._decodeFormValue(
isOptionArray ? xref.fetchIfRef(option[1]) : option
),
};

View File

@ -2450,16 +2450,12 @@ describe("annotation", function () {
}, done.fail);
});
it("should sanitize display values in option arrays (issue 8947)", function (done) {
// The option value is a UTF-16BE string. The display value should be
// sanitized, but the export value should remain the same since that
// may be used as a unique identifier when exporting form values.
const options = ["\xFE\xFF\x00F\x00o\x00o"];
const expected = [
{ exportValue: "\xFE\xFF\x00F\x00o\x00o", displayValue: "Foo" },
];
it("should decode form values", function (done) {
const encodedString = "\xFE\xFF\x00F\x00o\x00o";
const decodedString = "Foo";
choiceWidgetDict.set("Opt", options);
choiceWidgetDict.set("Opt", [encodedString]);
choiceWidgetDict.set("V", encodedString);
const choiceWidgetRef = Ref.get(984, 0);
const xref = new XRefMock([
@ -2473,7 +2469,10 @@ describe("annotation", function () {
idFactoryMock
).then(({ data }) => {
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.options).toEqual(expected);
expect(data.fieldValue).toEqual([decodedString]);
expect(data.options).toEqual([
{ exportValue: decodedString, displayValue: decodedString },
]);
done();
}, done.fail);
});