Merge pull request #10114 from Snuffleupagus/setDocument-optional-url

Update `{PDFLinkService, PDFDocumentProperties}.setDocument` to make the "url" parameter optional
This commit is contained in:
Tim van der Meij 2018-09-30 13:29:08 +02:00 committed by GitHub
commit 8d4c79c99c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 5 deletions

View File

@ -80,6 +80,12 @@ describe('ui_utils', function() {
expect(getPDFFileNameFromURL('/pdfs/file3.txt', '')).toEqual(''); 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', it('gets PDF filename from URL containing leading/trailing whitespace',
function() { function() {
// Relative URL // Relative URL

View File

@ -595,8 +595,8 @@ let PDFViewerApplication = {
this.pdfThumbnailViewer.setDocument(null); this.pdfThumbnailViewer.setDocument(null);
this.pdfViewer.setDocument(null); this.pdfViewer.setDocument(null);
this.pdfLinkService.setDocument(null, null); this.pdfLinkService.setDocument(null);
this.pdfDocumentProperties.setDocument(null, null); this.pdfDocumentProperties.setDocument(null);
} }
this.store = null; this.store = null;
this.isInitialViewSet = false; this.isInitialViewSet = false;

View File

@ -120,7 +120,7 @@ class PDFDocumentProperties {
return Promise.all([ return Promise.all([
info, info,
metadata, metadata,
contentDispositionFilename || getPDFFileNameFromURL(this.url), contentDispositionFilename || getPDFFileNameFromURL(this.url || ''),
this._parseFileSize(this.maybeFileSize), this._parseFileSize(this.maybeFileSize),
this._parseDate(info.CreationDate), this._parseDate(info.CreationDate),
this._parseDate(info.ModDate), this._parseDate(info.ModDate),
@ -187,7 +187,7 @@ class PDFDocumentProperties {
* @param {Object} pdfDocument - A reference to the PDF document. * @param {Object} pdfDocument - A reference to the PDF document.
* @param {string} url - The URL of the document. * @param {string} url - The URL of the document.
*/ */
setDocument(pdfDocument, url) { setDocument(pdfDocument, url = null) {
if (this.pdfDocument) { if (this.pdfDocument) {
this._reset(); this._reset();
this._updateUI(true); this._updateUI(true);

View File

@ -49,7 +49,7 @@ class PDFLinkService {
this._pagesRefCache = null; this._pagesRefCache = null;
} }
setDocument(pdfDocument, baseUrl) { setDocument(pdfDocument, baseUrl = null) {
this.baseUrl = baseUrl; this.baseUrl = baseUrl;
this.pdfDocument = pdfDocument; this.pdfDocument = pdfDocument;
this._pagesRefCache = Object.create(null); this._pagesRefCache = Object.create(null);

View File

@ -548,6 +548,9 @@ function isDataSchema(url) {
* @returns {string} Guessed PDF filename. * @returns {string} Guessed PDF filename.
*/ */
function getPDFFileNameFromURL(url, defaultFilename = 'document.pdf') { function getPDFFileNameFromURL(url, defaultFilename = 'document.pdf') {
if (typeof url !== 'string') {
return defaultFilename;
}
if (isDataSchema(url)) { if (isDataSchema(url)) {
console.warn('getPDFFileNameFromURL: ' + console.warn('getPDFFileNameFromURL: ' +
'ignoring "data:" URL for performance reasons.'); 'ignoring "data:" URL for performance reasons.');