Merge pull request #4803 from mzgol/compatibility
[WIP] Update web/compatibility.js
This commit is contained in:
commit
825762f22a
@ -25,9 +25,10 @@ if (typeof PDFJS === 'undefined') {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Checking if the typed arrays are supported
|
// Checking if the typed arrays are supported
|
||||||
|
// Support: iOS<6.0 (subarray), IE<10, Android<4.0
|
||||||
(function checkTypedArrayCompatibility() {
|
(function checkTypedArrayCompatibility() {
|
||||||
if (typeof Uint8Array !== 'undefined') {
|
if (typeof Uint8Array !== 'undefined') {
|
||||||
// some mobile versions do not support subarray (e.g. safari 5 / iOS)
|
// Support: iOS<6.0
|
||||||
if (typeof Uint8Array.prototype.subarray === 'undefined') {
|
if (typeof Uint8Array.prototype.subarray === 'undefined') {
|
||||||
Uint8Array.prototype.subarray = function subarray(start, end) {
|
Uint8Array.prototype.subarray = function subarray(start, end) {
|
||||||
return new Uint8Array(this.slice(start, end));
|
return new Uint8Array(this.slice(start, end));
|
||||||
@ -37,7 +38,7 @@ if (typeof PDFJS === 'undefined') {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// some mobile version might not support Float64Array
|
// Support: Android<4.1
|
||||||
if (typeof Float64Array === 'undefined') {
|
if (typeof Float64Array === 'undefined') {
|
||||||
window.Float64Array = Float32Array;
|
window.Float64Array = Float32Array;
|
||||||
}
|
}
|
||||||
@ -97,26 +98,15 @@ if (typeof PDFJS === 'undefined') {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
// URL = URL || webkitURL
|
// URL = URL || webkitURL
|
||||||
|
// Support: Safari<7, Android 4.2+
|
||||||
(function normalizeURLObject() {
|
(function normalizeURLObject() {
|
||||||
if (!window.URL) {
|
if (!window.URL) {
|
||||||
window.URL = window.webkitURL;
|
window.URL = window.webkitURL;
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
// Object.create() ?
|
// Object.defineProperty()?
|
||||||
(function checkObjectCreateCompatibility() {
|
// Support: Android<4.0, Safari<5.1
|
||||||
if (typeof Object.create !== 'undefined') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.create = function objectCreate(proto) {
|
|
||||||
function Constructor() {}
|
|
||||||
Constructor.prototype = proto;
|
|
||||||
return new Constructor();
|
|
||||||
};
|
|
||||||
})();
|
|
||||||
|
|
||||||
// Object.defineProperty() ?
|
|
||||||
(function checkObjectDefinePropertyCompatibility() {
|
(function checkObjectDefinePropertyCompatibility() {
|
||||||
if (typeof Object.defineProperty !== 'undefined') {
|
if (typeof Object.defineProperty !== 'undefined') {
|
||||||
var definePropertyPossible = true;
|
var definePropertyPossible = true;
|
||||||
@ -157,78 +147,23 @@ if (typeof PDFJS === 'undefined') {
|
|||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
||||||
// Object.keys() ?
|
|
||||||
(function checkObjectKeysCompatibility() {
|
|
||||||
if (typeof Object.keys !== 'undefined') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.keys = function objectKeys(obj) {
|
// No XMLHttpRequest#response?
|
||||||
var result = [];
|
// Support: IE<11, Android <4.0
|
||||||
for (var i in obj) {
|
|
||||||
if (obj.hasOwnProperty(i)) {
|
|
||||||
result.push(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
};
|
|
||||||
})();
|
|
||||||
|
|
||||||
// No readAsArrayBuffer ?
|
|
||||||
(function checkFileReaderReadAsArrayBuffer() {
|
|
||||||
if (typeof FileReader === 'undefined') {
|
|
||||||
return; // FileReader is not implemented
|
|
||||||
}
|
|
||||||
var frPrototype = FileReader.prototype;
|
|
||||||
// Older versions of Firefox might not have readAsArrayBuffer
|
|
||||||
if ('readAsArrayBuffer' in frPrototype) {
|
|
||||||
return; // readAsArrayBuffer is implemented
|
|
||||||
}
|
|
||||||
Object.defineProperty(frPrototype, 'readAsArrayBuffer', {
|
|
||||||
value: function fileReaderReadAsArrayBuffer(blob) {
|
|
||||||
var fileReader = new FileReader();
|
|
||||||
var originalReader = this;
|
|
||||||
fileReader.onload = function fileReaderOnload(evt) {
|
|
||||||
var data = evt.target.result;
|
|
||||||
var buffer = new ArrayBuffer(data.length);
|
|
||||||
var uint8Array = new Uint8Array(buffer);
|
|
||||||
|
|
||||||
for (var i = 0, ii = data.length; i < ii; i++) {
|
|
||||||
uint8Array[i] = data.charCodeAt(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.defineProperty(originalReader, 'result', {
|
|
||||||
value: buffer,
|
|
||||||
enumerable: true,
|
|
||||||
writable: false,
|
|
||||||
configurable: true
|
|
||||||
});
|
|
||||||
|
|
||||||
var event = document.createEvent('HTMLEvents');
|
|
||||||
event.initEvent('load', false, false);
|
|
||||||
originalReader.dispatchEvent(event);
|
|
||||||
};
|
|
||||||
fileReader.readAsBinaryString(blob);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})();
|
|
||||||
|
|
||||||
// No XMLHttpRequest.response ?
|
|
||||||
(function checkXMLHttpRequestResponseCompatibility() {
|
(function checkXMLHttpRequestResponseCompatibility() {
|
||||||
var xhrPrototype = XMLHttpRequest.prototype;
|
var xhrPrototype = XMLHttpRequest.prototype;
|
||||||
if (!('overrideMimeType' in xhrPrototype)) {
|
var xhr = new XMLHttpRequest();
|
||||||
|
if (!('overrideMimeType' in xhr)) {
|
||||||
// IE10 might have response, but not overrideMimeType
|
// IE10 might have response, but not overrideMimeType
|
||||||
|
// Support: IE10
|
||||||
Object.defineProperty(xhrPrototype, 'overrideMimeType', {
|
Object.defineProperty(xhrPrototype, 'overrideMimeType', {
|
||||||
value: function xmlHttpRequestOverrideMimeType(mimeType) {}
|
value: function xmlHttpRequestOverrideMimeType(mimeType) {}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if ('response' in xhrPrototype ||
|
if ('response' in xhr || 'responseArrayBuffer' in xhr) {
|
||||||
'mozResponseArrayBuffer' in xhrPrototype ||
|
|
||||||
'mozResponse' in xhrPrototype ||
|
|
||||||
'responseArrayBuffer' in xhrPrototype) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// IE9 ?
|
// Support: IE9
|
||||||
if (typeof VBArray !== 'undefined') {
|
if (typeof VBArray !== 'undefined') {
|
||||||
Object.defineProperty(xhrPrototype, 'response', {
|
Object.defineProperty(xhrPrototype, 'response', {
|
||||||
get: function xmlHttpRequestResponseGet() {
|
get: function xmlHttpRequestResponseGet() {
|
||||||
@ -243,7 +178,7 @@ if (typeof PDFJS === 'undefined') {
|
|||||||
// will be only called to set "arraybuffer"
|
// will be only called to set "arraybuffer"
|
||||||
this.overrideMimeType('text/plain; charset=x-user-defined');
|
this.overrideMimeType('text/plain; charset=x-user-defined');
|
||||||
}
|
}
|
||||||
if (typeof xhrPrototype.overrideMimeType === 'function') {
|
if (typeof xhr.overrideMimeType === 'function') {
|
||||||
Object.defineProperty(xhrPrototype, 'responseType',
|
Object.defineProperty(xhrPrototype, 'responseType',
|
||||||
{ set: responseTypeSetter });
|
{ set: responseTypeSetter });
|
||||||
}
|
}
|
||||||
@ -260,6 +195,7 @@ if (typeof PDFJS === 'undefined') {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
// window.btoa (base64 encode function) ?
|
// window.btoa (base64 encode function) ?
|
||||||
|
// Support: IE<10
|
||||||
(function checkWindowBtoaCompatibility() {
|
(function checkWindowBtoaCompatibility() {
|
||||||
if ('btoa' in window) {
|
if ('btoa' in window) {
|
||||||
return;
|
return;
|
||||||
@ -285,7 +221,8 @@ if (typeof PDFJS === 'undefined') {
|
|||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
||||||
// window.atob (base64 encode function) ?
|
// window.atob (base64 encode function)?
|
||||||
|
// Support: IE<10
|
||||||
(function checkWindowAtobCompatibility() {
|
(function checkWindowAtobCompatibility() {
|
||||||
if ('atob' in window) {
|
if ('atob' in window) {
|
||||||
return;
|
return;
|
||||||
@ -318,7 +255,8 @@ if (typeof PDFJS === 'undefined') {
|
|||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
||||||
// Function.prototype.bind ?
|
// Function.prototype.bind?
|
||||||
|
// Support: Android<4.0, iOS<6.0
|
||||||
(function checkFunctionPrototypeBindCompatibility() {
|
(function checkFunctionPrototypeBindCompatibility() {
|
||||||
if (typeof Function.prototype.bind !== 'undefined') {
|
if (typeof Function.prototype.bind !== 'undefined') {
|
||||||
return;
|
return;
|
||||||
@ -335,6 +273,7 @@ if (typeof PDFJS === 'undefined') {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
// HTMLElement dataset property
|
// HTMLElement dataset property
|
||||||
|
// Support: IE<11, Safari<5.1, Android<4.0
|
||||||
(function checkDatasetProperty() {
|
(function checkDatasetProperty() {
|
||||||
var div = document.createElement('div');
|
var div = document.createElement('div');
|
||||||
if ('dataset' in div) {
|
if ('dataset' in div) {
|
||||||
@ -372,6 +311,7 @@ if (typeof PDFJS === 'undefined') {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
// HTMLElement classList property
|
// HTMLElement classList property
|
||||||
|
// Support: IE<10, Android<4.0, iOS<5.0
|
||||||
(function checkClassListProperty() {
|
(function checkClassListProperty() {
|
||||||
var div = document.createElement('div');
|
var div = document.createElement('div');
|
||||||
if ('classList' in div) {
|
if ('classList' in div) {
|
||||||
@ -435,6 +375,9 @@ if (typeof PDFJS === 'undefined') {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
// Check console compatibility
|
// Check console compatibility
|
||||||
|
// In older IE versions the console object is not available
|
||||||
|
// unless console is open.
|
||||||
|
// Support: IE<10
|
||||||
(function checkConsoleCompatibility() {
|
(function checkConsoleCompatibility() {
|
||||||
if (!('console' in window)) {
|
if (!('console' in window)) {
|
||||||
window.console = {
|
window.console = {
|
||||||
@ -457,6 +400,7 @@ if (typeof PDFJS === 'undefined') {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
// Check onclick compatibility in Opera
|
// Check onclick compatibility in Opera
|
||||||
|
// Support: Opera<15
|
||||||
(function checkOnClickCompatibility() {
|
(function checkOnClickCompatibility() {
|
||||||
// workaround for reported Opera bug DSK-354448:
|
// workaround for reported Opera bug DSK-354448:
|
||||||
// onclick fires on disabled buttons with opaque content
|
// onclick fires on disabled buttons with opaque content
|
||||||
@ -475,6 +419,7 @@ if (typeof PDFJS === 'undefined') {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
// Checks if possible to use URL.createObjectURL()
|
// Checks if possible to use URL.createObjectURL()
|
||||||
|
// Support: IE
|
||||||
(function checkOnBlobSupport() {
|
(function checkOnBlobSupport() {
|
||||||
// sometimes IE loosing the data created with createObjectURL(), see #3977
|
// sometimes IE loosing the data created with createObjectURL(), see #3977
|
||||||
if (navigator.userAgent.indexOf('Trident') >= 0) {
|
if (navigator.userAgent.indexOf('Trident') >= 0) {
|
||||||
@ -504,6 +449,7 @@ if (typeof PDFJS === 'undefined') {
|
|||||||
// Safari has issues with cached range requests see:
|
// Safari has issues with cached range requests see:
|
||||||
// https://github.com/mozilla/pdf.js/issues/3260
|
// https://github.com/mozilla/pdf.js/issues/3260
|
||||||
// Last tested with version 6.0.4.
|
// Last tested with version 6.0.4.
|
||||||
|
// Support: Safari 6.0+
|
||||||
var isSafari = Object.prototype.toString.call(
|
var isSafari = Object.prototype.toString.call(
|
||||||
window.HTMLElement).indexOf('Constructor') > 0;
|
window.HTMLElement).indexOf('Constructor') > 0;
|
||||||
|
|
||||||
@ -511,6 +457,7 @@ if (typeof PDFJS === 'undefined') {
|
|||||||
// https://github.com/mozilla/pdf.js/issues/3381.
|
// https://github.com/mozilla/pdf.js/issues/3381.
|
||||||
// Make sure that we only match webkit-based Android browsers,
|
// Make sure that we only match webkit-based Android browsers,
|
||||||
// since Firefox/Fennec works as expected.
|
// since Firefox/Fennec works as expected.
|
||||||
|
// Support: Android<3.0
|
||||||
var regex = /Android\s[0-2][^\d]/;
|
var regex = /Android\s[0-2][^\d]/;
|
||||||
var isOldAndroid = regex.test(navigator.userAgent);
|
var isOldAndroid = regex.test(navigator.userAgent);
|
||||||
|
|
||||||
@ -520,12 +467,18 @@ if (typeof PDFJS === 'undefined') {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
// Check if the browser supports manipulation of the history.
|
// Check if the browser supports manipulation of the history.
|
||||||
|
// Support: IE<10, Android<4.2
|
||||||
(function checkHistoryManipulation() {
|
(function checkHistoryManipulation() {
|
||||||
if (!window.history.pushState) {
|
// Android 2.x has so buggy pushState support that it was removed in
|
||||||
|
// Android 3.0 and restored as late as in Android 4.2.
|
||||||
|
// Support: Android 2.x
|
||||||
|
if (!history.pushState || navigator.userAgent.indexOf('Android 2.') >= 0) {
|
||||||
PDFJS.disableHistory = true;
|
PDFJS.disableHistory = true;
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
// TODO CanvasPixelArray is deprecated; use Uint8ClampedArray
|
||||||
|
// once it's supported.
|
||||||
(function checkSetPresenceInImageData() {
|
(function checkSetPresenceInImageData() {
|
||||||
if (window.CanvasPixelArray) {
|
if (window.CanvasPixelArray) {
|
||||||
if (typeof window.CanvasPixelArray.prototype.set !== 'function') {
|
if (typeof window.CanvasPixelArray.prototype.set !== 'function') {
|
||||||
@ -538,39 +491,12 @@ if (typeof PDFJS === 'undefined') {
|
|||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
(function checkStorages() {
|
// Support: IE<10, Android<4.0, iOS<5.0
|
||||||
// Feature test as per http://diveintohtml5.info/storage.html
|
|
||||||
// The additional localStorage call is to get around a FF quirk, see
|
|
||||||
// bug #495747 in bugzilla
|
|
||||||
try {
|
|
||||||
if ('localStorage' in window && window['localStorage'] !== null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
// When the generic viewer is used in Firefox the following code will fail
|
|
||||||
// when the preference 'network.cookie.lifetimePolicy' is set to 1,
|
|
||||||
// see Mozilla bug 365772.
|
|
||||||
try {
|
|
||||||
window.localStorage = {
|
|
||||||
data: Object.create(null),
|
|
||||||
getItem: function (key) {
|
|
||||||
return this.data[key];
|
|
||||||
},
|
|
||||||
setItem: function (key, value) {
|
|
||||||
this.data[key] = value;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} catch (e) {
|
|
||||||
console.log('Unable to create polyfill for localStorage');
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
(function checkRequestAnimationFrame() {
|
(function checkRequestAnimationFrame() {
|
||||||
if ('requestAnimationFrame' in window) {
|
if ('requestAnimationFrame' in window) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
window.requestAnimationFrame =
|
window.requestAnimationFrame = window.webkitRequestAnimationFrame ||
|
||||||
window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame ||
|
|
||||||
(function fakeRequestAnimationFrame(callback) {
|
(function fakeRequestAnimationFrame(callback) {
|
||||||
window.setTimeout(callback, 20);
|
window.setTimeout(callback, 20);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user