diff --git a/src/core/document.js b/src/core/document.js index 072ae3770..4db6248be 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -523,7 +523,6 @@ class Page { this.pageDict, PageActionEventType ); - return shadow(this, "jsActions", actions); } } @@ -1189,19 +1188,28 @@ class PDFDocument { } get hasJSActions() { - return shadow( - this, - "hasJSActions", - this.fieldObjects.then(fieldObjects => { - return ( - (fieldObjects !== null && - Object.values(fieldObjects).some(fieldObject => - fieldObject.some(object => object.actions !== null) - )) || - !!this.catalog.jsActions - ); - }) - ); + const promise = this.pdfManager.ensure(this, "_parseHasJSActions"); + return shadow(this, "hasJSActions", promise); + } + + /** + * @private + */ + async _parseHasJSActions() { + const [catalogJsActions, fieldObjects] = await Promise.all([ + this.pdfManager.ensureCatalog("jsActions"), + this.pdfManager.ensure(this, "fieldObjects"), + ]); + + if (catalogJsActions) { + return true; + } + if (fieldObjects) { + return Object.values(fieldObjects).some(fieldObject => + fieldObject.some(object => object.actions !== null) + ); + } + return false; } get calculationOrderIds() { diff --git a/src/core/obj.js b/src/core/obj.js index 717404fcb..c5b2c7b82 100644 --- a/src/core/obj.js +++ b/src/core/obj.js @@ -856,9 +856,6 @@ class Catalog { return shadow(this, "viewerPreferences", prefs); } - /** - * NOTE: "JavaScript" actions are, for now, handled by `get javaScript` below. - */ get openAction() { const obj = this._catDict.get("OpenAction"); const openAction = Object.create(null); @@ -923,9 +920,9 @@ class Catalog { } if (javaScript === null) { - javaScript = Object.create(null); + javaScript = new Map(); } - javaScript[name] = stringToPDFString(js); + javaScript.set(name, stringToPDFString(js)); } if (obj && obj.has("JavaScript")) { @@ -955,23 +952,23 @@ class Catalog { return shadow( this, "javaScript", - javaScript ? Object.values(javaScript) : null + javaScript ? [...javaScript.values()] : null ); } get jsActions() { - const js = this._collectJavaScript(); + const javaScript = this._collectJavaScript(); let actions = collectActions( this.xref, this._catDict, DocumentActionEventType ); - if (!actions && js) { - actions = Object.create(null); - } - if (actions && js) { - for (const [key, val] of Object.entries(js)) { + if (javaScript) { + if (!actions) { + actions = Object.create(null); + } + for (const [key, val] of javaScript) { if (key in actions) { actions[key].push(val); } else { @@ -979,7 +976,6 @@ class Catalog { } } } - return shadow(this, "jsActions", actions); } diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index af9a494bf..40958ff9e 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -1014,6 +1014,23 @@ describe("api", function () { .catch(done.fail); }); + it("gets hasJSActions, in document without javaScript", async function () { + const hasJSActions = await pdfDocument.hasJSActions(); + + expect(hasJSActions).toEqual(false); + }); + it("gets hasJSActions, in document with javaScript", async function () { + const loadingTask = getDocument( + buildGetDocumentParams("doc_actions.pdf") + ); + const pdfDoc = await loadingTask.promise; + const hasJSActions = await pdfDoc.hasJSActions(); + + expect(hasJSActions).toEqual(true); + + await loadingTask.destroy(); + }); + it("gets JSActions (none)", function (done) { const promise = pdfDocument.getJSActions(); promise