Compare commits

...

6 Commits

Author SHA1 Message Date
Tim van der Meij
72feb4c256
Merge pull request #19775 from Snuffleupagus/AFSimple_Calculate-MIN-MAX-destructuring
Use Array-destructuring when computing MIN/MAX in `AFSimple_Calculate`
2025-04-06 18:32:37 +02:00
Tim van der Meij
36ece9af8d
Merge pull request #19777 from Snuffleupagus/Node-engines-20.16.0
Tweak the Node.js version listed in "engines", to ensure that `process.getBuiltinModule` is available
2025-04-06 18:25:26 +02:00
Tim van der Meij
082485f094
Merge pull request #19776 from Snuffleupagus/rm-reduce-unit-tests
Remove `Array.prototype.reduce` usage from the unit-tests
2025-04-06 18:24:29 +02:00
Jonas Jenwald
18617eb792 Tweak the Node.js version listed in "engines", to ensure that process.getBuiltinModule is available
In order to use the PDF.js library in Node.js environments the `process.getBuiltinModule` functionality must be available, which was released in [version `20.16.0`](https://nodejs.org/en/blog/release/v20.16.0), however we've seen repeated issues filed by users on older `20.x` versions.
2025-04-06 14:32:36 +02:00
Jonas Jenwald
ec5b5184d3 Remove Array.prototype.reduce usage from the unit-tests
Using `Array.prototype.reduce` often leads to less readable code, and in these cases we can replace it with other Array-methods instead.
2025-04-06 12:12:37 +02:00
Jonas Jenwald
0845552ff9 Use Array-destructuring when computing MIN/MAX in AFSimple_Calculate
This appears to work fine with QuickJS, as evident by the added unit-test, and allows us to remove more `Array.prototype.reduce` usage.
2025-04-06 11:44:08 +02:00
6 changed files with 123 additions and 18 deletions

View File

@ -2333,7 +2333,7 @@ function packageJson() {
url: `git+${DIST_GIT_URL}`,
},
engines: {
node: ">=20",
node: ">=20.16.0",
},
scripts: {},
};

2
package-lock.json generated
View File

@ -63,7 +63,7 @@
"yargs": "^17.7.2"
},
"engines": {
"node": ">=20"
"node": ">=20.16.0"
}
},
"node_modules/@aashutoshrathi/word-wrap": {

View File

@ -62,7 +62,7 @@
"url": "git://github.com/mozilla/pdf.js.git"
},
"engines": {
"node": ">=20"
"node": ">=20.16.0"
},
"license": "Apache-2.0"
}

View File

@ -392,10 +392,8 @@ class AForm {
AVG: args => args.reduce((acc, value) => acc + value, 0) / args.length,
SUM: args => args.reduce((acc, value) => acc + value, 0),
PRD: args => args.reduce((acc, value) => acc * value, 1),
MIN: args =>
args.reduce((acc, value) => Math.min(acc, value), Number.MAX_VALUE),
MAX: args =>
args.reduce((acc, value) => Math.max(acc, value), Number.MIN_VALUE),
MIN: args => Math.min(...args),
MAX: args => Math.max(...args),
};
if (!(cFunction in actions)) {

View File

@ -1394,6 +1394,116 @@ describe("Scripting", function () {
formattedValue: null,
});
});
it("should compute the max of several fields", async () => {
const refIds = [0, 1, 2, 3, 4].map(_ => getId());
const data = {
objects: {
field1: [
{
id: refIds[0],
value: "",
actions: {},
type: "text",
},
],
field2: [
{
id: refIds[1],
value: "",
actions: {},
type: "text",
},
],
field3: [
{
id: refIds[2],
value: "",
actions: {},
type: "text",
},
],
field4: [
{
id: refIds[3],
value: "",
actions: {
Calculate: [
`AFSimple_Calculate("MAX", ["field1", "field2", "field3", "unknown"]);`,
],
},
type: "text",
},
],
field5: [
{
id: refIds[4],
value: "",
actions: {
Calculate: [
`AFSimple_Calculate("MAX", "field1, field2, field3, unknown");`,
],
},
type: "text",
},
],
},
appInfo: { language: "en-US", platform: "Linux x86_64" },
calculationOrder: [refIds[3], refIds[4]],
dispatchEventName: "_dispatchMe",
};
sandbox.createSandbox(data);
await sandbox.dispatchEventInSandbox({
id: refIds[0],
value: "1",
name: "Keystroke",
willCommit: true,
});
expect(send_queue.has(refIds[3])).toEqual(true);
expect(send_queue.get(refIds[3])).toEqual({
id: refIds[3],
siblings: null,
value: 1,
formattedValue: null,
});
await sandbox.dispatchEventInSandbox({
id: refIds[1],
value: "2",
name: "Keystroke",
willCommit: true,
});
expect(send_queue.has(refIds[3])).toEqual(true);
expect(send_queue.get(refIds[3])).toEqual({
id: refIds[3],
siblings: null,
value: 2,
formattedValue: null,
});
await sandbox.dispatchEventInSandbox({
id: refIds[2],
value: "3",
name: "Keystroke",
willCommit: true,
});
expect(send_queue.has(refIds[3])).toEqual(true);
expect(send_queue.get(refIds[3])).toEqual({
id: refIds[3],
siblings: null,
value: 3,
formattedValue: null,
});
expect(send_queue.has(refIds[4])).toEqual(true);
expect(send_queue.get(refIds[4])).toEqual({
id: refIds[4],
siblings: null,
value: 3,
formattedValue: null,
});
});
});
describe("AFSpecial_KeystrokeEx", function () {

View File

@ -234,9 +234,8 @@ describe("ui_utils", function () {
let lineTop = 0,
id = 0;
for (const line of lines) {
const lineHeight = line.reduce(function (maxHeight, pair) {
return Math.max(maxHeight, pair[1]);
}, 0);
const heights = line.map(pair => pair[1]);
const lineHeight = Math.max(...heights);
let offsetLeft = -BORDER_WIDTH;
for (const [clientWidth, clientHeight] of line) {
const offsetTop =
@ -320,14 +319,12 @@ describe("ui_utils", function () {
// test to the slower implementation above, for a range of scroll viewport
// sizes and positions.
function scrollOverDocument(pages, horizontal = false, rtl = false) {
const size = pages.reduce(function (max, { div }) {
return Math.max(
max,
horizontal
? Math.abs(div.offsetLeft + div.clientLeft + div.clientWidth)
: div.offsetTop + div.clientTop + div.clientHeight
);
}, 0);
const sizes = pages.map(({ div }) =>
horizontal
? Math.abs(div.offsetLeft + div.clientLeft + div.clientWidth)
: div.offsetTop + div.clientTop + div.clientHeight
);
const size = Math.max(...sizes);
// The numbers (7 and 5) are mostly arbitrary, not magic: increase them to
// make scrollOverDocument tests faster, decrease them to make the tests
// more scrupulous, and keep them coprime to reduce the chance of missing