Prevent getPDFFileNameFromURL from breaking if the url parameter is not a string

This commit is contained in:
Jonas Jenwald 2018-09-30 12:22:50 +02:00
parent 6da78bcc3f
commit 1c814e208e
2 changed files with 9 additions and 0 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

@ -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.');