Allow to have an URL as file parameter when loading the viewer

This commit is contained in:
Calixte Denizet 2025-06-21 14:57:24 +02:00
parent c7796c7f8d
commit eafc040734
3 changed files with 39 additions and 4 deletions

View File

@ -53,9 +53,11 @@ function loadAndWait(filename, selector, zoom, setups, options, viewport) {
app_options += `&${key}=${encodeURIComponent(value)}`;
}
}
const url = `${
global.integrationBaseUrl
}?file=/test/pdfs/${filename}#zoom=${zoom ?? "page-fit"}${app_options}`;
const fileParam = filename.startsWith("http")
? filename
: `/test/pdfs/${filename}`;
const url = `${global.integrationBaseUrl}?file=${fileParam}#zoom=${zoom ?? "page-fit"}${app_options}`;
if (setups) {
// page.evaluateOnNewDocument allows us to run code before the

View File

@ -1244,4 +1244,33 @@ describe("PDF viewer", () => {
);
});
});
describe("File param with an URL", () => {
let pages;
beforeEach(async () => {
const baseURL = new URL(global.integrationBaseUrl);
const url = `${baseURL.origin}/build/generic/web/compressed.tracemonkey-pldi-09.pdf`;
pages = await loadAndWait(
encodeURIComponent(url),
".textLayer .endOfContent"
);
});
afterEach(async () => {
await closePages(pages);
});
it("must load and extract the filename correctly", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
const filename = await page.evaluate(() => document.title);
expect(filename)
.withContext(`In ${browserName}`)
.toBe("compressed.tracemonkey-pldi-09.pdf");
})
);
});
});
});

View File

@ -727,7 +727,11 @@ const PDFViewerApplication = {
const queryString = document.location.search.substring(1);
const params = parseQueryString(queryString);
file = params.get("file") ?? AppOptions.get("defaultUrl");
try {
file = new URL(decodeURIComponent(file)).href;
} catch {
file = encodeURIComponent(file).replaceAll("%2F", "/");
}
validateFileURL(file);
} else if (PDFJSDev.test("MOZCENTRAL")) {
file = window.location.href;