[JS] By default, a text field value must be treated as a number (bug 1802888)

This commit is contained in:
Calixte Denizet 2022-11-28 15:53:17 +01:00
parent 33f9d1aab2
commit ae7da6ae48
6 changed files with 57 additions and 8 deletions

View File

@ -80,7 +80,7 @@ class Field extends PDFObject {
this._page = data.page || 0;
this._strokeColor = data.strokeColor || ["G", 0];
this._textColor = data.textColor || ["G", 0];
this._value = data.value || "";
this._value = null;
this._kidIds = data.kidIds || null;
this._fieldType = getFieldType(this._actions);
this._siblings = data.siblings || null;
@ -88,6 +88,9 @@ class Field extends PDFObject {
this._globalEval = data.globalEval;
this._appObjects = data.appObjects;
// The value is set depending on the field type.
this.value = data.value || "";
}
get currentValueIndices() {
@ -243,12 +246,13 @@ class Field extends PDFObject {
this._value = "";
} else if (typeof value === "string") {
switch (this._fieldType) {
case FieldType.none:
this._value = !isNaN(value) ? parseFloat(value) : value;
break;
case FieldType.number:
case FieldType.percent:
value = parseFloat(value);
if (!isNaN(value)) {
this._value = value;
}
const number = parseFloat(value);
this._value = !isNaN(number) ? number : 0;
break;
default:
this._value = value;
@ -563,6 +567,9 @@ class RadioButtonField extends Field {
this._id = radioData.id;
}
}
this._hasBeenInitialized = true;
this._value = data.value || "";
}
get value() {
@ -570,6 +577,10 @@ class RadioButtonField extends Field {
}
set value(value) {
if (!this._hasBeenInitialized) {
return;
}
if (value === null || value === undefined) {
this._value = "";
}

View File

@ -297,6 +297,7 @@ class Util extends PDFObject {
printx(cFormat, cSource) {
// case
cSource = (cSource ?? "").toString();
const handlers = [x => x, x => x.toUpperCase(), x => x.toLowerCase()];
const buf = [];
let i = 0;

View File

@ -1562,4 +1562,40 @@ describe("Interaction", () => {
);
});
});
describe("in bug1802888.pdf", () => {
let pages;
beforeAll(async () => {
pages = await loadAndWait("bug1802888.pdf", getSelector("30R"));
});
afterAll(async () => {
await closePages(pages);
});
it("must check field value is treated by default as a number", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await page.waitForFunction(
"window.PDFViewerApplication.scriptingReady === true"
);
await page.type(getSelector("30R"), "123", {
delay: 10,
});
await page.click(getSelector("31R"));
await page.type(getSelector("31R"), "456", {
delay: 10,
});
await page.click(getSelector("26R"));
await page.click(getSelector("27R"));
await page.waitForFunction(`${getQuerySelector("26R")}.value !== ""`);
const value = await page.$eval(getSelector("26R"), el => el.value);
expect(value).withContext(`In ${browserName}`).toEqual("579");
})
);
});
});
});

View File

@ -556,3 +556,4 @@
!textfields.pdf
!freetext_no_appearance.pdf
!issue15690.pdf
!bug1802888.pdf

BIN
test/pdfs/bug1802888.pdf Executable file

Binary file not shown.

View File

@ -346,7 +346,7 @@ describe("Scripting", function () {
expect(send_queue.has(refId)).toEqual(true);
expect(send_queue.get(refId)).toEqual({
id: refId,
value: "123",
value: 123,
});
});
@ -826,7 +826,7 @@ describe("Scripting", function () {
expect(send_queue.get(refId)).toEqual({
id: refId,
siblings: null,
value: "123456.789",
value: 123456.789,
formattedValue: null,
});
});
@ -1006,7 +1006,7 @@ describe("Scripting", function () {
expect(send_queue.get(refId)).toEqual({
id: refId,
siblings: null,
value: "321",
value: 321,
formattedValue: null,
});
});