From 8fc56772e8a79ba5def9b3326e893f9d55ef3614 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 5 May 2026 12:01:51 +0200 Subject: [PATCH] A couple of small tweaks of the `getPdfFilenameFromUrl` helper - Shorten the `getURL` function slightly, by re-factoring the try-catch blocks. - Change how the `decode` function looks for a decoded ".pdf" name, to skip the regular expression matching when it's not needed and to allow re-using the already defined `pdfRegex`. --- src/display/display_utils.js | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/display/display_utils.js b/src/display/display_utils.js index a4bd75133..2d96f1b4d 100644 --- a/src/display/display_utils.js +++ b/src/display/display_utils.js @@ -347,22 +347,19 @@ function getPdfFilenameFromUrl(url, defaultFilename = "document.pdf") { const getURL = urlString => { try { return new URL(urlString); - } catch { - try { - return new URL(decodeURIComponent(urlString)); - } catch { - try { - // Attempt to parse the URL using the document's base URI. - return new URL(urlString, "https://foo.bar"); - } catch { - try { - return new URL(decodeURIComponent(urlString), "https://foo.bar"); - } catch { - return null; - } - } - } - } + } catch {} + try { + return new URL(decodeURIComponent(urlString)); + } catch {} + try { + // Attempt to parse the URL using the document's base URI. + return new URL(urlString, "https://foo.bar"); + } catch {} + try { + return new URL(decodeURIComponent(urlString), "https://foo.bar"); + } catch {} + + return null; }; const newURL = getURL(url); @@ -376,7 +373,8 @@ function getPdfFilenameFromUrl(url, defaultFilename = "document.pdf") { let decoded = decodeURIComponent(name); if (decoded.includes("/")) { decoded = stripPath(decoded); - if (/^\.pdf$/i.test(decoded)) { + // Ignore the decoded name if it's identical to ".pdf". + if (decoded.length === 4 && pdfRegex.test(decoded)) { return name; } }