Skip to content

Commit

Permalink
Merge pull request #15570 from calixteman/15568
Browse files Browse the repository at this point in the history
[JS] Take into account all the required fields for some computations
  • Loading branch information
calixteman authored Oct 13, 2022
2 parents c6cc7c6 + e756bb6 commit 09d077d
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 10 deletions.
10 changes: 7 additions & 3 deletions src/scripting_api/aform.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,14 +500,18 @@ class AForm {

const event = globalThis.event;
const values = [];

cFields = this.AFMakeArrayFromList(cFields);
for (const cField of cFields) {
const field = this._document.getField(cField);
if (!field) {
continue;
}
const number = this.AFMakeNumber(field.value);
if (number !== null) {
values.push(number);
for (const child of field.getArray()) {
const number = this.AFMakeNumber(child.value);
if (number !== null) {
values.push(number);
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/scripting_api/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,24 @@ class Doc extends PDFObject {
return children;
}

_getTerminalChildren(fieldName) {
// Get all the descendants which have a value.
const children = [];
const len = fieldName.length;
for (const [name, field] of this._fields.entries()) {
if (name.startsWith(fieldName)) {
const finalPart = name.slice(len);
if (
field.obj._hasValue &&
(finalPart === "" || finalPart.startsWith("."))
) {
children.push(field.wrapped);
}
}
}
return children;
}

getIcon() {
/* Not implemented */
}
Expand Down
26 changes: 22 additions & 4 deletions src/scripting_api/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Field extends PDFObject {
this._fillColor = data.fillColor || ["T"];
this._isChoice = Array.isArray(data.items);
this._items = data.items || [];
this._hasValue = data.hasOwnProperty("value");
this._page = data.page || 0;
this._strokeColor = data.strokeColor || ["G", 0];
this._textColor = data.textColor || ["G", 0];
Expand Down Expand Up @@ -393,15 +394,32 @@ class Field extends PDFObject {
}

getArray() {
// Gets the array of terminal child fields (that is, fields that can have
// a value for this Field object, the parent field).
if (this._kidIds) {
return this._kidIds.map(id => this._appObjects[id].wrapped);
const array = [];
const fillArrayWithKids = kidIds => {
for (const id of kidIds) {
const obj = this._appObjects[id];
if (!obj) {
continue;
}
if (obj.obj._hasValue) {
array.push(obj.wrapped);
}
if (obj.obj._kidIds) {
fillArrayWithKids(obj.obj._kidIds);
}
}
};
fillArrayWithKids(this._kidIds);
return array;
}

if (this._children === null) {
this._children = this._document.obj
._getChildren(this._fieldPath)
.map(child => child.wrapped);
this._children = this._document.obj._getTerminalChildren(this._fieldPath);
}

return this._children;
}

Expand Down
110 changes: 107 additions & 3 deletions test/unit/scripting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ describe("Scripting", function () {
value = await myeval(
`this.getField("A.B.C.D").getArray().map((x) => x.value)`
);
expect(value).toEqual([5, 7]);
expect(value).toEqual([4, 5, 6, 7]);
});
});

Expand Down Expand Up @@ -1037,7 +1037,7 @@ describe("Scripting", function () {

describe("AFSimple_Calculate", function () {
it("should compute the sum of several fields", async () => {
const refIds = [0, 1, 2, 3].map(_ => getId());
const refIds = [0, 1, 2, 3, 4].map(_ => getId());
const data = {
objects: {
field1: [
Expand Down Expand Up @@ -1076,9 +1076,21 @@ describe("Scripting", function () {
type: "text",
},
],
field5: [
{
id: refIds[4],
value: "",
actions: {
Calculate: [
`AFSimple_Calculate("SUM", "field1, field2, field3, unknown");`,
],
},
type: "text",
},
],
},
appInfo: { language: "en-US", platform: "Linux x86_64" },
calculationOrder: [refIds[3]],
calculationOrder: [refIds[3], refIds[4]],
dispatchEventName: "_dispatchMe",
};

Expand Down Expand Up @@ -1124,6 +1136,98 @@ describe("Scripting", function () {
value: 6,
formattedValue: null,
});

expect(send_queue.has(refIds[4])).toEqual(true);
expect(send_queue.get(refIds[4])).toEqual({
id: refIds[4],
siblings: null,
value: 6,
formattedValue: null,
});
});

it("should compute the sum of several fields in fields tree", async () => {
const refIds = [0, 1, 2, 3, 4, 5].map(_ => getId());
const data = {
objects: {
field1: [
{
id: refIds[0],
kidIds: [refIds[1], refIds[2]],
},
],
"field1.field2": [
{
id: refIds[1],
kidIds: [refIds[3]],
},
],
"field1.field3": [
{
id: refIds[2],
value: "",
actions: {},
type: "text",
},
],
"field1.field2.field4": [
{
id: refIds[3],
kidIds: [refIds[4]],
},
],
"field1.field2.field4.field5": [
{
id: refIds[4],
value: "",
actions: {},
type: "text",
},
],
field6: [
{
id: refIds[5],
value: "",
actions: {
Calculate: [`AFSimple_Calculate("SUM", "field1");`],
},
type: "text",
},
],
},
appInfo: { language: "en-US", platform: "Linux x86_64" },
calculationOrder: [refIds[5]],
dispatchEventName: "_dispatchMe",
};

sandbox.createSandbox(data);
await sandbox.dispatchEventInSandbox({
id: refIds[2],
value: "123",
name: "Keystroke",
willCommit: true,
});
expect(send_queue.has(refIds[5])).toEqual(true);
expect(send_queue.get(refIds[5])).toEqual({
id: refIds[5],
siblings: null,
value: 123,
formattedValue: null,
});

await sandbox.dispatchEventInSandbox({
id: refIds[4],
value: "456",
name: "Keystroke",
willCommit: true,
});
expect(send_queue.has(refIds[5])).toEqual(true);
expect(send_queue.get(refIds[5])).toEqual({
id: refIds[5],
siblings: null,
value: 579,
formattedValue: null,
});
});
});

Expand Down

0 comments on commit 09d077d

Please sign in to comment.