Merge pull request #21691 from Snuffleupagus/Field-setAction-Map

Add scripts correctly in `Field.prototype.setAction` (PR 12569 follow-up)
This commit is contained in:
Jonas Jenwald 2026-08-02 21:24:24 +02:00 committed by GitHub
commit 923d48ead4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 76 additions and 5 deletions

View File

@ -14,9 +14,9 @@
*/
import { createActionsMap, FieldType, getFieldType } from "./common.js";
import { makeArr, serializeError } from "./app_utils.js";
import { Color } from "./color.js";
import { PDFObject } from "./pdf_object.js";
import { serializeError } from "./app_utils.js";
class Field extends PDFObject {
constructor(data) {
@ -496,10 +496,7 @@ class Field extends PDFObject {
if (typeof cTrigger !== "string" || typeof cScript !== "string") {
return;
}
if (!(cTrigger in this._actions)) {
this._actions[cTrigger] = [];
}
this._actions[cTrigger].push(cScript);
this._actions.getOrInsertComputed(cTrigger, makeArr).push(cScript);
}
setFocus() {

View File

@ -363,6 +363,80 @@ describe("Scripting", function () {
});
});
it("should trigger an event added with setAction", async () => {
const refId = getId();
const data = {
objects: {
field: [
{
id: refId,
value: "",
actions: {},
type: "text",
},
],
},
appInfo: { language: "en-US", platform: "Linux x86_64" },
calculationOrder: [],
};
sandbox.createSandbox(data);
await myeval(
`(this.getField("field").setAction("test", 'event.source.value = "abc";'), 0)`
);
await sandbox.dispatchEventInSandbox({
id: refId,
value: "",
name: "test",
willCommit: true,
});
expect(send_queue.has(refId)).toBeTrue();
expect(send_queue.get(refId)).toEqual({
id: refId,
value: "abc",
});
});
it("should trigger an event appended with setAction", async () => {
const refId = getId();
const data = {
objects: {
field: [
{
id: refId,
value: "",
actions: {
test: [`event.source.value = "a";`],
},
type: "text",
},
],
},
appInfo: { language: "en-US", platform: "Linux x86_64" },
calculationOrder: [],
};
sandbox.createSandbox(data);
await myeval(
`(this.getField("field").setAction("test", 'event.source.value += "b";'), 0)`
);
await sandbox.dispatchEventInSandbox({
id: refId,
value: "",
name: "test",
willCommit: true,
});
expect(send_queue.has(refId)).toBeTrue();
expect(send_queue.get(refId)).toEqual({
id: refId,
value: "ab",
});
});
it("should trigger a Keystroke event and invalidate it", async () => {
const refId = getId();
const data = {