[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.
This commit is contained in:
Jonas Jenwald 2019-11-11 17:48:04 +01:00
parent 21895aa75a
commit 878432784c
2 changed files with 25 additions and 10 deletions

View File

@ -25,6 +25,9 @@ globalScope._pdfjsCompatibilityChecked = true;
const { isNodeJS, } = require('./is_node'); const { isNodeJS, } = require('./is_node');
const hasDOM = typeof window === 'object' && typeof document === 'object'; const hasDOM = typeof window === 'object' && typeof document === 'object';
const userAgent =
(typeof navigator !== 'undefined' && navigator.userAgent) || '';
const isIE = /Trident/.test(userAgent);
// Support: Node.js // Support: Node.js
(function checkNodeBtoa() { (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. // Provides support for String.prototype.startsWith in legacy browsers.
// Support: IE, Chrome<41 // Support: IE, Chrome<41
(function checkStringStartsWith() { (function checkStringStartsWith() {

View File

@ -306,18 +306,10 @@ class PDFHistory {
} }
} }
if (shouldReplace) { if (shouldReplace) {
if (newUrl) {
window.history.replaceState(newState, '', newUrl); window.history.replaceState(newState, '', newUrl);
} else {
window.history.replaceState(newState, '');
}
} else { } else {
this._maxUid = this._uid; this._maxUid = this._uid;
if (newUrl) {
window.history.pushState(newState, '', newUrl); window.history.pushState(newState, '', newUrl);
} else {
window.history.pushState(newState, '');
}
} }
if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('CHROME') && if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('CHROME') &&