From 1cd1582cb9685e75fe8ea23366db65b3f2123a7e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 15 Oct 2017 22:13:58 +0200 Subject: [PATCH] [api-major] Change `getJavaScript` to return `null`, rather than an empty Array, when no JavaScript exists Other API methods already return `null`, rather than empty Arrays/Objects, hence it makes sense to change `getJavaScript` to be consistent. --- src/core/obj.js | 8 +++++++- src/display/api.js | 6 +++--- test/unit/api_spec.js | 2 +- web/app.js | 8 +++++--- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/core/obj.js b/src/core/obj.js index 34ea684ac..622602991 100644 --- a/src/core/obj.js +++ b/src/core/obj.js @@ -371,7 +371,7 @@ var Catalog = (function CatalogClosure() { var xref = this.xref; var obj = this.catDict.get('Names'); - var javaScript = []; + let javaScript = null; function appendIfJavaScriptDict(jsDict) { var type = jsDict.get('S'); if (!isName(type, 'JavaScript')) { @@ -383,6 +383,9 @@ var Catalog = (function CatalogClosure() { } else if (!isString(js)) { return; } + if (!javaScript) { + javaScript = []; + } javaScript.push(stringToPDFString(js)); } if (obj && obj.has('JavaScript')) { @@ -407,6 +410,9 @@ var Catalog = (function CatalogClosure() { // but is supported by many PDF readers/writers (including Adobe's). var action = openactionDict.get('N'); if (isName(action, 'Print')) { + if (!javaScript) { + javaScript = []; + } javaScript.push('print({});'); } } else { diff --git a/src/display/api.js b/src/display/api.js index 0f2fdf01a..b0b9179f9 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -574,10 +574,10 @@ var PDFDocumentProxy = (function PDFDocumentProxyClosure() { return this.transport.getAttachments(); }, /** - * @return {Promise} A promise that is resolved with an array of all the - * JavaScript strings in the name tree. + * @return {Promise} A promise that is resolved with an {Array} of all the + * JavaScript strings in the name tree, or `null` if no JavaScript exists. */ - getJavaScript: function PDFDocumentProxy_getJavaScript() { + getJavaScript() { return this.transport.getJavaScript(); }, /** diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index 660b56d4e..dda727c0e 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -684,7 +684,7 @@ describe('api', function() { it('gets javascript', function(done) { var promise = doc.getJavaScript(); promise.then(function (data) { - expect(data).toEqual([]); + expect(data).toEqual(null); done(); }).catch(function (reason) { done.fail(reason); diff --git a/web/app.js b/web/app.js index 7f6b058b3..a7f3f9f0f 100644 --- a/web/app.js +++ b/web/app.js @@ -1037,10 +1037,12 @@ let PDFViewerApplication = { return; } pdfDocument.getJavaScript().then((javaScript) => { - if (javaScript.length) { - console.warn('Warning: JavaScript is not supported'); - this.fallback(UNSUPPORTED_FEATURES.javaScript); + if (!javaScript) { + return; } + console.warn('Warning: JavaScript is not supported'); + this.fallback(UNSUPPORTED_FEATURES.javaScript); + // Hack to support auto printing. let regex = /\bprint\s*\(/; for (let i = 0, ii = javaScript.length; i < ii; i++) {