Merge pull request #13138 from Snuffleupagus/PDFHistory-pushPage-dest

[PDFHistory] Correctly simulate an internal destination in the `pushPage`-method (PR 12493 follow-up)
This commit is contained in:
Tim van der Meij 2021-03-26 21:57:51 +01:00 committed by GitHub
commit 69c88d119d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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.`
); );
@ -304,6 +292,8 @@ class PDFHistory {
} }
this._pushOrReplaceState({ this._pushOrReplaceState({
// Simulate an internal destination, for `this._tryPushCurrentPosition`:
dest: null,
hash: `page=${pageNumber}`, hash: `page=${pageNumber}`,
page: pageNumber, page: pageNumber,
rotation: this.linkService.rotation, rotation: this.linkService.rotation,
@ -470,7 +460,7 @@ class PDFHistory {
// - contains an internal destination, since in this case we // - contains an internal destination, since in this case we
// cannot ensure that the document position has actually changed. // cannot ensure that the document position has actually changed.
// - was set through the user changing the hash of the document. // - was set through the user changing the hash of the document.
if (this._destination.dest || !this._destination.first) { if (this._destination.dest !== undefined || !this._destination.first) {
return; return;
} }
// To avoid "flooding" the browser history, replace the current entry. // To avoid "flooding" the browser history, replace the current entry.
@ -479,6 +469,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 +547,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 };