Skip to content

Commit 032f80e

Browse files
Merge pull request #20027 from calixteman/issue20024
Allow to have an URL as file parameter when loading the viewer
2 parents c7796c7 + eafc040 commit 032f80e

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

test/integration/test_utils.mjs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ function loadAndWait(filename, selector, zoom, setups, options, viewport) {
5353
app_options += `&${key}=${encodeURIComponent(value)}`;
5454
}
5555
}
56-
const url = `${
57-
global.integrationBaseUrl
58-
}?file=/test/pdfs/${filename}#zoom=${zoom ?? "page-fit"}${app_options}`;
56+
57+
const fileParam = filename.startsWith("http")
58+
? filename
59+
: `/test/pdfs/${filename}`;
60+
const url = `${global.integrationBaseUrl}?file=${fileParam}#zoom=${zoom ?? "page-fit"}${app_options}`;
5961

6062
if (setups) {
6163
// page.evaluateOnNewDocument allows us to run code before the

test/integration/viewer_spec.mjs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,4 +1244,33 @@ describe("PDF viewer", () => {
12441244
);
12451245
});
12461246
});
1247+
1248+
describe("File param with an URL", () => {
1249+
let pages;
1250+
1251+
beforeEach(async () => {
1252+
const baseURL = new URL(global.integrationBaseUrl);
1253+
const url = `${baseURL.origin}/build/generic/web/compressed.tracemonkey-pldi-09.pdf`;
1254+
pages = await loadAndWait(
1255+
encodeURIComponent(url),
1256+
".textLayer .endOfContent"
1257+
);
1258+
});
1259+
1260+
afterEach(async () => {
1261+
await closePages(pages);
1262+
});
1263+
1264+
it("must load and extract the filename correctly", async () => {
1265+
await Promise.all(
1266+
pages.map(async ([browserName, page]) => {
1267+
const filename = await page.evaluate(() => document.title);
1268+
1269+
expect(filename)
1270+
.withContext(`In ${browserName}`)
1271+
.toBe("compressed.tracemonkey-pldi-09.pdf");
1272+
})
1273+
);
1274+
});
1275+
});
12471276
});

web/app.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,11 @@ const PDFViewerApplication = {
727727
const queryString = document.location.search.substring(1);
728728
const params = parseQueryString(queryString);
729729
file = params.get("file") ?? AppOptions.get("defaultUrl");
730-
file = encodeURIComponent(file).replaceAll("%2F", "/");
730+
try {
731+
file = new URL(decodeURIComponent(file)).href;
732+
} catch {
733+
file = encodeURIComponent(file).replaceAll("%2F", "/");
734+
}
731735
validateFileURL(file);
732736
} else if (PDFJSDev.test("MOZCENTRAL")) {
733737
file = window.location.href;

0 commit comments

Comments
 (0)