mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-04-09 23:04:02 +02:00
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:
parent
fa28ca1468
commit
520928719c
@ -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.
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user