1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-24 09:08:07 +02:00

Move the isSameOrigin helper function

This function is currently placed in the `src/shared/util.js` file, which means that the code is duplicated in both of the *built* `pdf.js` and `pdf.worker.js` files. Furthermore, it only has a single call-site which is also specific to the `GENERIC`-build of the PDF.js library.

Hence this helper function is instead moved into the `src/display/api.js` file, in such a way that it's conditionally defined but still can be unit-tested.
This commit is contained in:
Jonas Jenwald 2022-03-10 13:37:21 +01:00
parent ee39499a5a
commit 537ed37835
4 changed files with 49 additions and 46 deletions

View file

@ -40,6 +40,7 @@ import {
PDFDocumentProxy,
PDFPageProxy,
PDFWorker,
PDFWorkerUtil,
RenderTask,
} from "../../src/display/api.js";
import {
@ -2967,4 +2968,33 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
}
);
});
describe("PDFWorkerUtil", function () {
describe("isSameOrigin", function () {
const { isSameOrigin } = PDFWorkerUtil;
it("handles invalid base URLs", function () {
// The base URL is not valid.
expect(isSameOrigin("/foo", "/bar")).toEqual(false);
// The base URL has no origin.
expect(isSameOrigin("blob:foo", "/bar")).toEqual(false);
});
it("correctly checks if the origin of both URLs matches", function () {
expect(
isSameOrigin(
"https://www.mozilla.org/foo",
"https://www.mozilla.org/bar"
)
).toEqual(true);
expect(
isSameOrigin(
"https://www.mozilla.org/foo",
"https://www.example.com/bar"
)
).toEqual(false);
});
});
});
});

View file

@ -21,7 +21,6 @@ import {
getModificationDate,
isArrayBuffer,
isAscii,
isSameOrigin,
string32,
stringToBytes,
stringToPDFString,
@ -165,31 +164,6 @@ describe("util", function () {
});
});
describe("isSameOrigin", function () {
it("handles invalid base URLs", function () {
// The base URL is not valid.
expect(isSameOrigin("/foo", "/bar")).toEqual(false);
// The base URL has no origin.
expect(isSameOrigin("blob:foo", "/bar")).toEqual(false);
});
it("correctly checks if the origin of both URLs matches", function () {
expect(
isSameOrigin(
"https://www.mozilla.org/foo",
"https://www.mozilla.org/bar"
)
).toEqual(true);
expect(
isSameOrigin(
"https://www.mozilla.org/foo",
"https://www.example.com/bar"
)
).toEqual(false);
});
});
describe("createValidAbsoluteUrl", function () {
it("handles invalid URLs", function () {
expect(createValidAbsoluteUrl(undefined, undefined)).toEqual(null);