Merge pull request #21222 from Snuffleupagus/getPdfFilenameFromUrl-tweaks

A couple of small tweaks of the `getPdfFilenameFromUrl` helper
This commit is contained in:
Tim van der Meij 2026-05-05 20:27:55 +02:00 committed by GitHub
commit ac51bdf745
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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;
}
}