Merge pull request #13234 from Snuffleupagus/hasJSActions-MissingDataException

[api-minor] Ensure that `PDFDocumentProxy.hasJSActions` won't fail if `MissingDataException`s are thrown during the associated worker-thread parsing
This commit is contained in:
Tim van der Meij 2021-04-13 20:44:58 +02:00 committed by GitHub
commit ebeb3f7999
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 27 deletions

View File

@ -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() {

View File

@ -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);
}

View File

@ -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