Skip to content

Commit

Permalink
[JS] By default, a text field value must be treated as a number (bug …
Browse files Browse the repository at this point in the history
…1802888)
  • Loading branch information
calixteman authored and pull[bot] committed Dec 8, 2022
1 parent 42eb673 commit 1714183
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 8 deletions.
21 changes: 16 additions & 5 deletions src/scripting_api/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,17 @@ 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;
this._rotation = data.rotation || 0;

this._globalEval = data.globalEval;
this._appObjects = data.appObjects;

// The value is set depending on the field type.
this.value = data.value || "";
}

get currentValueIndices() {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -563,13 +567,20 @@ class RadioButtonField extends Field {
this._id = radioData.id;
}
}

this._hasBeenInitialized = true;
this._value = data.value || "";
}

get value() {
return this._value;
}

set value(value) {
if (!this._hasBeenInitialized) {
return;
}

if (value === null || value === undefined) {
this._value = "";
}
Expand Down
1 change: 1 addition & 0 deletions src/scripting_api/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions test/integration/scripting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
})
);
});
});
});
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,4 @@
!textfields.pdf
!freetext_no_appearance.pdf
!issue15690.pdf
!bug1802888.pdf
Binary file added test/pdfs/bug1802888.pdf
Binary file not shown.
6 changes: 3 additions & 3 deletions test/unit/scripting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});

Expand Down Expand Up @@ -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,
});
});
Expand Down Expand Up @@ -1006,7 +1006,7 @@ describe("Scripting", function () {
expect(send_queue.get(refId)).toEqual({
id: refId,
siblings: null,
value: "321",
value: 321,
formattedValue: null,
});
});
Expand Down

0 comments on commit 1714183

Please sign in to comment.