1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

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
),
};