Merge pull request #20656 from Snuffleupagus/mv-stripPath

Move and re-use the `stripPath` helper function more
This commit is contained in:
Tim van der Meij 2026-02-13 19:49:57 +01:00 committed by GitHub
commit 3302b3d5b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 11 additions and 8 deletions

View File

@ -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.

View File

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

View File

@ -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,