From 878432784c35a27dd115ff888a2459df1344da69 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 11 Nov 2019 17:48:04 +0100 Subject: [PATCH] [PDFHistory] Move the IE11 `pushState`/`replaceState` work-around to `src/shared/compatibility.js` (PR 10461 follow-up) I've always disliked the solution in PR 10461, since it required changes to the `PDFHistory` code itself to deal with a bug in IE11. Now that IE11 support is limited, it seems reasonable to remove these `pushState`/`replaceState` hacks from the main code-base and simply use polyfills instead. --- src/shared/compatibility.js | 23 +++++++++++++++++++++++ web/pdf_history.js | 12 ++---------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/shared/compatibility.js b/src/shared/compatibility.js index fafa9d7a40abe..2bc09b1cc49f5 100644 --- a/src/shared/compatibility.js +++ b/src/shared/compatibility.js @@ -25,6 +25,9 @@ globalScope._pdfjsCompatibilityChecked = true; const { isNodeJS, } = require('./is_node'); const hasDOM = typeof window === 'object' && typeof document === 'object'; +const userAgent = + (typeof navigator !== 'undefined' && navigator.userAgent) || ''; +const isIE = /Trident/.test(userAgent); // Support: Node.js (function checkNodeBtoa() { @@ -112,6 +115,26 @@ const hasDOM = typeof window === 'object' && typeof document === 'object'; }; })(); +// Provides support for window.history.{pushState, replaceState}, with the +// `url` parameter set to `undefined`, without breaking the document URL. +// Support: IE +(function checkWindowHistoryPushStateReplaceState() { + if (!hasDOM || !isIE) { + return; + } + const OriginalPushState = window.history.pushState; + const OriginalReplaceState = window.history.replaceState; + + window.history.pushState = function(state, title, url) { + const args = (url === undefined ? [state, title] : [state, title, url]); + OriginalPushState.apply(this, args); + }; + window.history.replaceState = function(state, title, url) { + const args = (url === undefined ? [state, title] : [state, title, url]); + OriginalReplaceState.apply(this, args); + }; +})(); + // Provides support for String.prototype.startsWith in legacy browsers. // Support: IE, Chrome<41 (function checkStringStartsWith() { diff --git a/web/pdf_history.js b/web/pdf_history.js index e8f29a83dd738..2e67e2758c299 100644 --- a/web/pdf_history.js +++ b/web/pdf_history.js @@ -306,18 +306,10 @@ class PDFHistory { } } if (shouldReplace) { - if (newUrl) { - window.history.replaceState(newState, '', newUrl); - } else { - window.history.replaceState(newState, ''); - } + window.history.replaceState(newState, '', newUrl); } else { this._maxUid = this._uid; - if (newUrl) { - window.history.pushState(newState, '', newUrl); - } else { - window.history.pushState(newState, ''); - } + window.history.pushState(newState, '', newUrl); } if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('CHROME') &&