Skip to content

Commit

Permalink
Merge pull request #10114 from Snuffleupagus/setDocument-optional-url
Browse files Browse the repository at this point in the history
Update `{PDFLinkService, PDFDocumentProperties}.setDocument` to make the "url" parameter optional
  • Loading branch information
timvandermeij authored Sep 30, 2018
2 parents b40fb38 + 1c814e2 commit 8d4c79c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
6 changes: 6 additions & 0 deletions test/unit/ui_utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions web/pdf_document_properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion web/pdf_link_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down

0 comments on commit 8d4c79c

Please sign in to comment.