From 520928719ce32664ad92c4635cd79f2f3da85b84 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 13 Feb 2026 17:38:21 +0100 Subject: [PATCH] Move and re-use the `stripPath` helper function more There's a couple of spots that essentially re-implement that function. --- src/core/file_spec.js | 6 +----- src/display/display_utils.js | 7 ++++--- src/shared/util.js | 6 ++++++ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/core/file_spec.js b/src/core/file_spec.js index c5b367950..890f34749 100644 --- a/src/core/file_spec.js +++ b/src/core/file_spec.js @@ -13,7 +13,7 @@ * limitations under the License. */ -import { stringToPDFString, warn } from "../shared/util.js"; +import { stringToPDFString, stripPath, warn } from "../shared/util.js"; import { BaseStream } from "./base_stream.js"; import { Dict } from "./primitives.js"; @@ -29,10 +29,6 @@ function pickPlatformItem(dict) { return null; } -function stripPath(str) { - return str.substring(str.lastIndexOf("/") + 1); -} - /** * "A PDF file can refer to the contents of another file by using a File * Specification (PDF 1.1)", see the spec (7.11) for more details. diff --git a/src/display/display_utils.js b/src/display/display_utils.js index 072366315..b3b5752d8 100644 --- a/src/display/display_utils.js +++ b/src/display/display_utils.js @@ -19,6 +19,7 @@ import { FeatureTest, MathClamp, shadow, + stripPath, Util, warn, } from "../shared/util.js"; @@ -325,7 +326,7 @@ function isPdfFile(filename) { */ function getFilenameFromUrl(url) { [url] = url.split(/[#?]/, 1); - return url.substring(url.lastIndexOf("/") + 1); + return stripPath(url); } /** @@ -375,7 +376,7 @@ function getPdfFilenameFromUrl(url, defaultFilename = "document.pdf") { try { let decoded = decodeURIComponent(name); if (decoded.includes("/")) { - decoded = decoded.split("/").at(-1); + decoded = stripPath(decoded); if (decoded.test(/^\.pdf$/i)) { return decoded; } @@ -388,7 +389,7 @@ function getPdfFilenameFromUrl(url, defaultFilename = "document.pdf") { }; const pdfRegex = /\.pdf$/i; - const filename = newURL.pathname.split("/").at(-1); + const filename = stripPath(newURL.pathname); if (pdfRegex.test(filename)) { return decode(filename); } diff --git a/src/shared/util.js b/src/shared/util.js index 039efa7d6..7bada9fb8 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -475,6 +475,11 @@ function updateUrlHash(url, hash, allowRel = false) { return ""; } +// Extract the final component from a path string. +function stripPath(str) { + return str.substring(str.lastIndexOf("/") + 1); +} + function shadow(obj, prop, value, nonSerializable = false) { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) { assert( @@ -1343,6 +1348,7 @@ export { stringToBytes, stringToPDFString, stringToUTF8String, + stripPath, TextRenderingMode, UnknownErrorException, unreachable,