Skip to content

Commit

Permalink
Take into account PageOpen and PageClose actions which are present in…
Browse files Browse the repository at this point in the history
… some fields
  • Loading branch information
calixteman committed Jun 28, 2024
1 parent 8709202 commit dacf8bb
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
42 changes: 37 additions & 5 deletions src/scripting_api/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ class Doc extends PDFObject {
this._zoom = data.zoom || 100;
this._actions = createActionsMap(data.actions);
this._globalEval = data.globalEval;
this._pageActions = new Map();
this._pageActions = null;
this._userActivation = false;
this._disablePrinting = false;
this._disableSaving = false;
this._otherPageActions = null;
}

_initActions() {
Expand Down Expand Up @@ -155,16 +156,19 @@ class Doc extends PDFObject {

_dispatchPageEvent(name, actions, pageNumber) {
if (name === "PageOpen") {
this._pageActions ||= new Map();
if (!this._pageActions.has(pageNumber)) {
this._pageActions.set(pageNumber, createActionsMap(actions));
}
this._pageNum = pageNumber - 1;
}

actions = this._pageActions.get(pageNumber)?.get(name);
if (actions) {
for (const action of actions) {
this._globalEval(action);
for (const acts of [this._pageActions, this._otherPageActions]) {
actions = acts?.get(pageNumber)?.get(name);
if (actions) {
for (const action of actions) {
this._globalEval(action);
}
}
}
}
Expand All @@ -182,6 +186,34 @@ class Doc extends PDFObject {
this._fields.set(name, field);
this._fieldNames.push(name);
this._numFields++;

// Fields on a page can have PageOpen/PageClose actions.
const po = field.obj._actions.get("PageOpen");
const pc = field.obj._actions.get("PageClose");
if (po || pc) {
this._otherPageActions ||= new Map();
let actions = this._otherPageActions.get(field.obj._page + 1);
if (!actions) {
actions = new Map();
this._otherPageActions.set(field.obj._page + 1, actions);
}
if (po) {
let poActions = actions.get("PageOpen");
if (!poActions) {
poActions = [];
actions.set("PageOpen", poActions);
}
poActions.push(...po);
}
if (pc) {
let pcActions = actions.get("PageClose");
if (!pcActions) {
pcActions = [];
actions.set("PageClose", pcActions);
}
pcActions.push(...pc);
}
}
}

_getDate(date) {
Expand Down
54 changes: 54 additions & 0 deletions test/integration/scripting_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2456,4 +2456,58 @@ describe("Interaction", () => {
);
});
});

describe("PageOpen and PageClose actions in fields", () => {
let pages;
let otherPages;

beforeAll(async () => {
otherPages = await Promise.all(
global.integrationSessions.map(async session =>
session.browser.newPage()
)
);
pages = await loadAndWait("issue18305.pdf", getSelector("7R"));
});

afterAll(async () => {
await closePages(pages);
await Promise.all(otherPages.map(page => page.close()));
});

it("must check that PageOpen/PageClose actions are correctly executed", async () => {
await Promise.all(
pages.map(async ([browserName, page], i) => {
await page.waitForFunction(
"window.PDFViewerApplication.scriptingReady === true"
);

const buttonSelector = `[data-annotation-id="25R"`;
await page.waitForSelector(buttonSelector, {
timeout: 0,
});

const inputSelector = getSelector("7R");
let text = await page.$eval(inputSelector, el => el.value);
expect(text).withContext(`In ${browserName}`).toEqual("");

text = await actAndWaitForInput(
page,
inputSelector,
() => scrollIntoView(page, buttonSelector),
false
);
expect(text).withContext(`In ${browserName}`).toEqual("PageOpen");

text = await actAndWaitForInput(
page,
inputSelector,
() => scrollIntoView(page, inputSelector),
false
);
expect(text).withContext(`In ${browserName}`).toEqual("PageClose");
})
);
});
});
});
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -651,3 +651,4 @@
!pdfjs_wikipedia.pdf
!bug1539074.pdf
!bug1539074.1.pdf
!issue18305.pdf
Binary file added test/pdfs/issue18305.pdf
Binary file not shown.

0 comments on commit dacf8bb

Please sign in to comment.