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.
This commit is contained in:
Jonas Jenwald 2021-04-23 09:36:14 +02:00
parent 83f7009e4b
commit 4ec0a4fb43

View File

@ -894,18 +894,20 @@ class Catalog {
_collectJavaScript() { _collectJavaScript() {
const obj = this._catDict.get("Names"); const obj = this._catDict.get("Names");
let javaScript = null; let javaScript = null;
function appendIfJavaScriptDict(name, jsDict) { function appendIfJavaScriptDict(name, jsDict) {
const type = jsDict.get("S"); if (!(jsDict instanceof Dict)) {
if (!isName(type, "JavaScript")) { return;
}
if (!isName(jsDict.get("S"), "JavaScript")) {
return; return;
} }
let js = jsDict.get("JS"); let js = jsDict.get("JS");
if (isStream(js)) { if (isStream(js)) {
js = bytesToString(js.getBytes()); js = bytesToString(js.getBytes());
} else if (!isString(js)) { } else if (typeof js !== "string") {
return; return;
} }
@ -918,17 +920,12 @@ class Catalog {
if (obj instanceof Dict && obj.has("JavaScript")) { if (obj instanceof Dict && obj.has("JavaScript")) {
const nameTree = new NameTree(obj.getRaw("JavaScript"), this.xref); const nameTree = new NameTree(obj.getRaw("JavaScript"), this.xref);
for (const [key, value] of nameTree.getAll()) { for (const [key, value] of nameTree.getAll()) {
// We don't use most JavaScript in PDF documents. This code is appendIfJavaScriptDict(key, value);
// defensive so we don't cause errors on document load.
if (value instanceof Dict) {
appendIfJavaScriptDict(key, value);
}
} }
} }
// Append OpenAction "JavaScript" actions, if any, to the JavaScript map.
// Append OpenAction "JavaScript" actions to the JavaScript array.
const openAction = this._catDict.get("OpenAction"); const openAction = this._catDict.get("OpenAction");
if (isDict(openAction) && isName(openAction.get("S"), "JavaScript")) { if (openAction) {
appendIfJavaScriptDict("OpenAction", openAction); appendIfJavaScriptDict("OpenAction", openAction);
} }