Move and re-use the stripPath helper function more

There's a couple of spots that essentially re-implement that function.
This commit is contained in:
Jonas Jenwald 2026-02-13 17:38:21 +01:00
parent fa28ca1468
commit 520928719c
3 changed files with 11 additions and 8 deletions

View File

@ -13,7 +13,7 @@
* limitations under the License. * 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 { BaseStream } from "./base_stream.js";
import { Dict } from "./primitives.js"; import { Dict } from "./primitives.js";
@ -29,10 +29,6 @@ function pickPlatformItem(dict) {
return null; 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 * "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. * Specification (PDF 1.1)", see the spec (7.11) for more details.

View File

@ -19,6 +19,7 @@ import {
FeatureTest, FeatureTest,
MathClamp, MathClamp,
shadow, shadow,
stripPath,
Util, Util,
warn, warn,
} from "../shared/util.js"; } from "../shared/util.js";
@ -325,7 +326,7 @@ function isPdfFile(filename) {
*/ */
function getFilenameFromUrl(url) { function getFilenameFromUrl(url) {
[url] = url.split(/[#?]/, 1); [url] = url.split(/[#?]/, 1);
return url.substring(url.lastIndexOf("/") + 1); return stripPath(url);
} }
/** /**
@ -375,7 +376,7 @@ function getPdfFilenameFromUrl(url, defaultFilename = "document.pdf") {
try { try {
let decoded = decodeURIComponent(name); let decoded = decodeURIComponent(name);
if (decoded.includes("/")) { if (decoded.includes("/")) {
decoded = decoded.split("/").at(-1); decoded = stripPath(decoded);
if (decoded.test(/^\.pdf$/i)) { if (decoded.test(/^\.pdf$/i)) {
return decoded; return decoded;
} }
@ -388,7 +389,7 @@ function getPdfFilenameFromUrl(url, defaultFilename = "document.pdf") {
}; };
const pdfRegex = /\.pdf$/i; const pdfRegex = /\.pdf$/i;
const filename = newURL.pathname.split("/").at(-1); const filename = stripPath(newURL.pathname);
if (pdfRegex.test(filename)) { if (pdfRegex.test(filename)) {
return decode(filename); return decode(filename);
} }

View File

@ -475,6 +475,11 @@ function updateUrlHash(url, hash, allowRel = false) {
return ""; 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) { function shadow(obj, prop, value, nonSerializable = false) {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
assert( assert(
@ -1343,6 +1348,7 @@ export {
stringToBytes, stringToBytes,
stringToPDFString, stringToPDFString,
stringToUTF8String, stringToUTF8String,
stripPath,
TextRenderingMode, TextRenderingMode,
UnknownErrorException, UnknownErrorException,
unreachable, unreachable,