From 6da78bcc3f9332306ccaf5a4762ecc66541e9301 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 30 Sep 2018 12:11:27 +0200 Subject: [PATCH 1/2] Update `{PDFLinkService, PDFDocumentProperties}.setDocument` to make the "url" parameter optional This way the resetting of `PDFLinkService`/`PDFDocumentProperties` instances, as is done in `PDFViewerApplication.close`, only requires passing in *one* `null` argument instead of two. --- web/app.js | 4 ++-- web/pdf_document_properties.js | 4 ++-- web/pdf_link_service.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/web/app.js b/web/app.js index 1cc90cdb7..3ff363ba1 100644 --- a/web/app.js +++ b/web/app.js @@ -595,8 +595,8 @@ let PDFViewerApplication = { this.pdfThumbnailViewer.setDocument(null); this.pdfViewer.setDocument(null); - this.pdfLinkService.setDocument(null, null); - this.pdfDocumentProperties.setDocument(null, null); + this.pdfLinkService.setDocument(null); + this.pdfDocumentProperties.setDocument(null); } this.store = null; this.isInitialViewSet = false; diff --git a/web/pdf_document_properties.js b/web/pdf_document_properties.js index 25711af5d..329c8dddc 100644 --- a/web/pdf_document_properties.js +++ b/web/pdf_document_properties.js @@ -120,7 +120,7 @@ class PDFDocumentProperties { return Promise.all([ info, metadata, - contentDispositionFilename || getPDFFileNameFromURL(this.url), + contentDispositionFilename || getPDFFileNameFromURL(this.url || ''), this._parseFileSize(this.maybeFileSize), this._parseDate(info.CreationDate), this._parseDate(info.ModDate), @@ -187,7 +187,7 @@ class PDFDocumentProperties { * @param {Object} pdfDocument - A reference to the PDF document. * @param {string} url - The URL of the document. */ - setDocument(pdfDocument, url) { + setDocument(pdfDocument, url = null) { if (this.pdfDocument) { this._reset(); this._updateUI(true); diff --git a/web/pdf_link_service.js b/web/pdf_link_service.js index 3f6af1a72..a0130db7e 100644 --- a/web/pdf_link_service.js +++ b/web/pdf_link_service.js @@ -49,7 +49,7 @@ class PDFLinkService { this._pagesRefCache = null; } - setDocument(pdfDocument, baseUrl) { + setDocument(pdfDocument, baseUrl = null) { this.baseUrl = baseUrl; this.pdfDocument = pdfDocument; this._pagesRefCache = Object.create(null); From 1c814e208eb00eb9511e8b29dbe9f09ac5c4cc1b Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 30 Sep 2018 12:22:50 +0200 Subject: [PATCH 2/2] Prevent `getPDFFileNameFromURL` from breaking if the `url` parameter is not a string --- test/unit/ui_utils_spec.js | 6 ++++++ web/ui_utils.js | 3 +++ 2 files changed, 9 insertions(+) diff --git a/test/unit/ui_utils_spec.js b/test/unit/ui_utils_spec.js index 145b30d6a..1bbb175d6 100644 --- a/test/unit/ui_utils_spec.js +++ b/test/unit/ui_utils_spec.js @@ -80,6 +80,12 @@ describe('ui_utils', function() { expect(getPDFFileNameFromURL('/pdfs/file3.txt', '')).toEqual(''); }); + it('gets fallback filename when url is not a string', function() { + expect(getPDFFileNameFromURL(null)).toEqual('document.pdf'); + + expect(getPDFFileNameFromURL(null, 'file.pdf')).toEqual('file.pdf'); + }); + it('gets PDF filename from URL containing leading/trailing whitespace', function() { // Relative URL diff --git a/web/ui_utils.js b/web/ui_utils.js index d7806aa8a..940ca81db 100644 --- a/web/ui_utils.js +++ b/web/ui_utils.js @@ -548,6 +548,9 @@ function isDataSchema(url) { * @returns {string} Guessed PDF filename. */ function getPDFFileNameFromURL(url, defaultFilename = 'document.pdf') { + if (typeof url !== 'string') { + return defaultFilename; + } if (isDataSchema(url)) { console.warn('getPDFFileNameFromURL: ' + 'ignoring "data:" URL for performance reasons.');