From 4ec0a4fb430e90818bdf9eade2f45fc177f6dff3 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 23 Apr 2021 09:36:14 +0200 Subject: [PATCH] Re-factor the `Catalog._collectJavaScript` method slightly This patch first of all moves all checking/validation into the `appendIfJavaScriptDict` function, to avoid duplicating it in multiple places. Secondly, also removes what's now an outdated/incorrect comment since we have implemented scripting support. --- src/core/catalog.js | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/core/catalog.js b/src/core/catalog.js index 1b3d7e154..9fbb9d1fd 100644 --- a/src/core/catalog.js +++ b/src/core/catalog.js @@ -894,18 +894,20 @@ class Catalog { _collectJavaScript() { const obj = this._catDict.get("Names"); - let javaScript = null; + function appendIfJavaScriptDict(name, jsDict) { - const type = jsDict.get("S"); - if (!isName(type, "JavaScript")) { + if (!(jsDict instanceof Dict)) { + return; + } + if (!isName(jsDict.get("S"), "JavaScript")) { return; } let js = jsDict.get("JS"); if (isStream(js)) { js = bytesToString(js.getBytes()); - } else if (!isString(js)) { + } else if (typeof js !== "string") { return; } @@ -918,17 +920,12 @@ class Catalog { if (obj instanceof Dict && obj.has("JavaScript")) { const nameTree = new NameTree(obj.getRaw("JavaScript"), this.xref); for (const [key, value] of nameTree.getAll()) { - // We don't use most JavaScript in PDF documents. This code is - // defensive so we don't cause errors on document load. - if (value instanceof Dict) { - appendIfJavaScriptDict(key, value); - } + appendIfJavaScriptDict(key, value); } } - - // Append OpenAction "JavaScript" actions to the JavaScript array. + // Append OpenAction "JavaScript" actions, if any, to the JavaScript map. const openAction = this._catDict.get("OpenAction"); - if (isDict(openAction) && isName(openAction.get("S"), "JavaScript")) { + if (openAction) { appendIfJavaScriptDict("OpenAction", openAction); }