Skip to content

Commit

Permalink
[api-minor] Fix the return value of `PDFDocumentProxy.getViewerPrefer…
Browse files Browse the repository at this point in the history
…ences` when no viewer preferences are present (PR 10738 follow-up)

This patch fixes yet another instalment in the never-ending series of "what the *bleep* was I thinking", by changing the `PDFDocumentProxy.getViewerPreferences` method to return `null` by default.
Not only is this method now consistent with many other API methods, for the data not present case, but it also avoids having to e.g. loop through an object to check if it's actually empty (note the old unit-test).
  • Loading branch information
Snuffleupagus committed Apr 12, 2020
1 parent a4dd081 commit 15036a7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/core/obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ class Catalog {
};

const obj = this.catDict.get("ViewerPreferences");
const prefs = Object.create(null);
let prefs = null;

if (isDict(obj)) {
for (const key in ViewerPreferencesValidators) {
Expand Down Expand Up @@ -583,6 +583,9 @@ class Catalog {
}

if (prefValue !== undefined) {
if (!prefs) {
prefs = Object.create(null);
}
prefs[key] = prefValue;
} else {
info(`Bad value in ViewerPreferences for "${key}".`);
Expand Down
3 changes: 2 additions & 1 deletion src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,8 @@ class PDFDocumentProxy {

/**
* @returns {Promise} A promise that is resolved with an {Object} containing
* the viewer preferences.
* the viewer preferences, or `null` when no viewer preferences are present
* in the PDF file.
*/
getViewerPreferences() {
return this._transport.getViewerPreferences();
Expand Down
5 changes: 1 addition & 4 deletions test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
createPromiseCapability,
FontType,
InvalidPDFException,
isEmptyObj,
MissingPDFException,
OPS,
PasswordException,
Expand Down Expand Up @@ -846,9 +845,7 @@ describe("api", function() {
return pdfDoc.getViewerPreferences();
})
.then(function(prefs) {
expect(
typeof prefs === "object" && prefs !== null && isEmptyObj(prefs)
).toEqual(true);
expect(prefs).toEqual(null);

loadingTask.destroy().then(done);
})
Expand Down

0 comments on commit 15036a7

Please sign in to comment.