[PDFHistory] Reduce unnecessary duplication, by introducing a helper-method for validating pageNumbers

This commit is contained in:
Jonas Jenwald 2021-03-24 13:55:56 +01:00
parent 9d0ce6e79f
commit 685a60055e

View File

@ -205,13 +205,7 @@ class PDFHistory {
`"${explicitDest}" is not a valid explicitDest parameter.` `"${explicitDest}" is not a valid explicitDest parameter.`
); );
return; return;
} else if ( } else if (!this._isValidPage(pageNumber)) {
!(
Number.isInteger(pageNumber) &&
pageNumber > 0 &&
pageNumber <= this.linkService.pagesCount
)
) {
// Allow an unset `pageNumber` if and only if the history is still empty; // Allow an unset `pageNumber` if and only if the history is still empty;
// please refer to the `this._destination.page = null;` comment above. // please refer to the `this._destination.page = null;` comment above.
if (pageNumber !== null || this._destination) { if (pageNumber !== null || this._destination) {
@ -281,13 +275,7 @@ class PDFHistory {
if (!this._initialized) { if (!this._initialized) {
return; return;
} }
if ( if (!this._isValidPage(pageNumber)) {
!(
Number.isInteger(pageNumber) &&
pageNumber > 0 &&
pageNumber <= this.linkService.pagesCount
)
) {
console.error( console.error(
`PDFHistory.pushPage: "${pageNumber}" is not a valid page number.` `PDFHistory.pushPage: "${pageNumber}" is not a valid page number.`
); );
@ -479,6 +467,15 @@ class PDFHistory {
this._pushOrReplaceState(position, forceReplace); this._pushOrReplaceState(position, forceReplace);
} }
/**
* @private
*/
_isValidPage(val) {
return (
Number.isInteger(val) && val > 0 && val <= this.linkService.pagesCount
);
}
/** /**
* @private * @private
*/ */
@ -548,14 +545,7 @@ class PDFHistory {
const nameddest = params.nameddest || ""; const nameddest = params.nameddest || "";
let page = params.page | 0; let page = params.page | 0;
if ( if (!this._isValidPage(page) || (checkNameddest && nameddest.length > 0)) {
!(
Number.isInteger(page) &&
page > 0 &&
page <= this.linkService.pagesCount
) ||
(checkNameddest && nameddest.length > 0)
) {
page = null; page = null;
} }
return { hash, page, rotation: this.linkService.rotation }; return { hash, page, rotation: this.linkService.rotation };