Skip to content

Commit 5209287

Browse files
committed
Move and re-use the stripPath helper function more
There's a couple of spots that essentially re-implement that function.
1 parent fa28ca1 commit 5209287

3 files changed

Lines changed: 11 additions & 8 deletions

File tree

src/core/file_spec.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
import { stringToPDFString, warn } from "../shared/util.js";
16+
import { stringToPDFString, stripPath, warn } from "../shared/util.js";
1717
import { BaseStream } from "./base_stream.js";
1818
import { Dict } from "./primitives.js";
1919

@@ -29,10 +29,6 @@ function pickPlatformItem(dict) {
2929
return null;
3030
}
3131

32-
function stripPath(str) {
33-
return str.substring(str.lastIndexOf("/") + 1);
34-
}
35-
3632
/**
3733
* "A PDF file can refer to the contents of another file by using a File
3834
* Specification (PDF 1.1)", see the spec (7.11) for more details.

src/display/display_utils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
FeatureTest,
2020
MathClamp,
2121
shadow,
22+
stripPath,
2223
Util,
2324
warn,
2425
} from "../shared/util.js";
@@ -325,7 +326,7 @@ function isPdfFile(filename) {
325326
*/
326327
function getFilenameFromUrl(url) {
327328
[url] = url.split(/[#?]/, 1);
328-
return url.substring(url.lastIndexOf("/") + 1);
329+
return stripPath(url);
329330
}
330331

331332
/**
@@ -375,7 +376,7 @@ function getPdfFilenameFromUrl(url, defaultFilename = "document.pdf") {
375376
try {
376377
let decoded = decodeURIComponent(name);
377378
if (decoded.includes("/")) {
378-
decoded = decoded.split("/").at(-1);
379+
decoded = stripPath(decoded);
379380
if (decoded.test(/^\.pdf$/i)) {
380381
return decoded;
381382
}
@@ -388,7 +389,7 @@ function getPdfFilenameFromUrl(url, defaultFilename = "document.pdf") {
388389
};
389390

390391
const pdfRegex = /\.pdf$/i;
391-
const filename = newURL.pathname.split("/").at(-1);
392+
const filename = stripPath(newURL.pathname);
392393
if (pdfRegex.test(filename)) {
393394
return decode(filename);
394395
}

src/shared/util.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,11 @@ function updateUrlHash(url, hash, allowRel = false) {
475475
return "";
476476
}
477477

478+
// Extract the final component from a path string.
479+
function stripPath(str) {
480+
return str.substring(str.lastIndexOf("/") + 1);
481+
}
482+
478483
function shadow(obj, prop, value, nonSerializable = false) {
479484
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
480485
assert(
@@ -1343,6 +1348,7 @@ export {
13431348
stringToBytes,
13441349
stringToPDFString,
13451350
stringToUTF8String,
1351+
stripPath,
13461352
TextRenderingMode,
13471353
UnknownErrorException,
13481354
unreachable,

0 commit comments

Comments
 (0)