Skip to content

Commit 5620456

Browse files
Merge pull request #15972 from Snuffleupagus/PDFViewerApplication-open-signature
Change the `PDFViewerApplication.open` method to only accept objects
2 parents ee3be2f + 6129be7 commit 5620456

File tree

1 file changed

+47
-38
lines changed

1 file changed

+47
-38
lines changed

web/app.js

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -741,19 +741,16 @@ const PDFViewerApplication = {
741741
}
742742
this.externalServices.initPassiveLoading({
743743
onOpenWithTransport: (url, length, transport) => {
744-
this.open(url, { length, range: transport });
744+
this.open({ url, length, range: transport });
745745
},
746746
onOpenWithData: (data, contentDispositionFilename) => {
747747
if (isPdfFile(contentDispositionFilename)) {
748748
this._contentDispositionFilename = contentDispositionFilename;
749749
}
750-
this.open(data);
750+
this.open({ data });
751751
},
752752
onOpenWithURL: (url, length, originalUrl) => {
753-
const file = originalUrl !== undefined ? { url, originalUrl } : url;
754-
const args = length !== undefined ? { length } : null;
755-
756-
this.open(file, args);
753+
this.open({ url, length, originalUrl });
757754
},
758755
onError: err => {
759756
this.l10n.get("loading_error").then(msg => {
@@ -890,15 +887,28 @@ const PDFViewerApplication = {
890887
},
891888

892889
/**
893-
* Opens PDF document specified by URL or array with additional arguments.
894-
* @param {string|TypedArray|ArrayBuffer} file - PDF location or binary data.
895-
* @param {Object} [args] - Additional arguments for the getDocument call,
896-
* e.g. HTTP headers ('httpHeaders') or alternative
897-
* data transport ('range').
898-
* @returns {Promise} - Returns the promise, which is resolved when document
899-
* is opened.
890+
* Opens a new PDF document.
891+
* @param {Object} args - Accepts any/all of the properties from
892+
* {@link DocumentInitParameters}, and also a `originalUrl` string.
893+
* @returns {Promise} - Promise that is resolved when the document is opened.
900894
*/
901-
async open(file, args) {
895+
async open(args) {
896+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
897+
let deprecatedArgs = false;
898+
if (typeof args === "string") {
899+
args = { url: args }; // URL
900+
deprecatedArgs = true;
901+
} else if (args?.byteLength) {
902+
args = { data: args }; // ArrayBuffer
903+
deprecatedArgs = true;
904+
}
905+
if (deprecatedArgs) {
906+
console.error(
907+
"The `PDFViewerApplication.open` signature was updated, please use an object instead."
908+
);
909+
}
910+
}
911+
902912
if (this.pdfLoadingTask) {
903913
// We need to destroy already opened document.
904914
await this.close();
@@ -910,36 +920,36 @@ const PDFViewerApplication = {
910920
}
911921

912922
const parameters = Object.create(null);
913-
if (typeof file === "string") {
914-
// URL
915-
this.setTitleUsingUrl(file, /* downloadUrl = */ file);
916-
parameters.url = file;
917-
} else if (file && "byteLength" in file) {
918-
// ArrayBuffer
919-
parameters.data = file;
920-
} else if (file.url && file.originalUrl) {
921-
this.setTitleUsingUrl(file.originalUrl, /* downloadUrl = */ file.url);
922-
parameters.url = file.url;
923+
if (
924+
(typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) &&
925+
args.url
926+
) {
927+
// The Firefox built-in viewer always calls `setTitleUsingUrl`, before
928+
// `initPassiveLoading`, and it never provides an `originalUrl` here.
929+
if (args.originalUrl) {
930+
this.setTitleUsingUrl(args.originalUrl, /* downloadUrl = */ args.url);
931+
delete args.originalUrl;
932+
} else {
933+
this.setTitleUsingUrl(args.url, /* downloadUrl = */ args.url);
934+
}
923935
}
924936
// Set the necessary API parameters, using the available options.
925937
const apiParameters = AppOptions.getAll(OptionKind.API);
926938
for (const key in apiParameters) {
927939
let value = apiParameters[key];
928940

929-
if (key === "docBaseUrl" && !value) {
941+
if (key === "docBaseUrl") {
930942
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
931-
value = document.URL.split("#")[0];
943+
value ||= document.URL.split("#")[0];
932944
} else if (PDFJSDev.test("MOZCENTRAL || CHROME")) {
933-
value = this.baseUrl;
945+
value ||= this.baseUrl;
934946
}
935947
}
936948
parameters[key] = value;
937949
}
938-
// Finally, update the API parameters with the arguments (if they exist).
939-
if (args) {
940-
for (const key in args) {
941-
parameters[key] = args[key];
942-
}
950+
// Finally, update the API parameters with the arguments.
951+
for (const key in args) {
952+
parameters[key] = args[key];
943953
}
944954

945955
const loadingTask = getDocument(parameters);
@@ -2242,7 +2252,7 @@ function webViewerInitialized() {
22422252
try {
22432253
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
22442254
if (file) {
2245-
PDFViewerApplication.open(file);
2255+
PDFViewerApplication.open({ url: file });
22462256
} else {
22472257
PDFViewerApplication._hideViewBookmark();
22482258
}
@@ -2452,11 +2462,10 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
24522462
}
24532463
const file = evt.fileInput.files[0];
24542464

2455-
let url = URL.createObjectURL(file);
2456-
if (file.name) {
2457-
url = { url, originalUrl: file.name };
2458-
}
2459-
PDFViewerApplication.open(url);
2465+
PDFViewerApplication.open({
2466+
url: URL.createObjectURL(file),
2467+
originalUrl: file.name,
2468+
});
24602469
};
24612470

24622471
// eslint-disable-next-line no-var

0 commit comments

Comments
 (0)