Fix coding style in web/compatibility.js

This commit is contained in:
Jonas Jenwald 2014-03-08 23:30:17 +01:00
parent 047a82a07e
commit 665b862b7f

View File

@ -38,9 +38,9 @@ if (typeof PDFJS === 'undefined') {
} }
// some mobile version might not support Float64Array // some mobile version might not support Float64Array
if (typeof Float64Array === 'undefined') if (typeof Float64Array === 'undefined') {
window.Float64Array = Float32Array; window.Float64Array = Float32Array;
}
return; return;
} }
@ -49,18 +49,21 @@ if (typeof PDFJS === 'undefined') {
} }
function setArrayOffset(array, offset) { function setArrayOffset(array, offset) {
if (arguments.length < 2) if (arguments.length < 2) {
offset = 0; offset = 0;
for (var i = 0, n = array.length; i < n; ++i, ++offset) }
for (var i = 0, n = array.length; i < n; ++i, ++offset) {
this[offset] = array[i] & 0xFF; this[offset] = array[i] & 0xFF;
}
} }
function TypedArray(arg1) { function TypedArray(arg1) {
var result; var result;
if (typeof arg1 === 'number') { if (typeof arg1 === 'number') {
result = []; result = [];
for (var i = 0; i < arg1; ++i) for (var i = 0; i < arg1; ++i) {
result[i] = 0; result[i] = 0;
}
} else if ('slice' in arg1) { } else if ('slice' in arg1) {
result = arg1.slice(0); result = arg1.slice(0);
} else { } else {
@ -75,9 +78,9 @@ if (typeof PDFJS === 'undefined') {
result.byteLength = result.length; result.byteLength = result.length;
result.set = setArrayOffset; result.set = setArrayOffset;
if (typeof arg1 === 'object' && arg1.buffer) if (typeof arg1 === 'object' && arg1.buffer) {
result.buffer = arg1.buffer; result.buffer = arg1.buffer;
}
return result; return result;
} }
@ -101,8 +104,9 @@ if (typeof PDFJS === 'undefined') {
// Object.create() ? // Object.create() ?
(function checkObjectCreateCompatibility() { (function checkObjectCreateCompatibility() {
if (typeof Object.create !== 'undefined') if (typeof Object.create !== 'undefined') {
return; return;
}
Object.create = function objectCreate(proto) { Object.create = function objectCreate(proto) {
function Constructor() {} function Constructor() {}
@ -127,15 +131,19 @@ if (typeof PDFJS === 'undefined') {
} catch (e) { } catch (e) {
definePropertyPossible = false; definePropertyPossible = false;
} }
if (definePropertyPossible) return; if (definePropertyPossible) {
return;
}
} }
Object.defineProperty = function objectDefineProperty(obj, name, def) { Object.defineProperty = function objectDefineProperty(obj, name, def) {
delete obj[name]; delete obj[name];
if ('get' in def) if ('get' in def) {
obj.__defineGetter__(name, def['get']); obj.__defineGetter__(name, def['get']);
if ('set' in def) }
if ('set' in def) {
obj.__defineSetter__(name, def['set']); obj.__defineSetter__(name, def['set']);
}
if ('value' in def) { if ('value' in def) {
obj.__defineSetter__(name, function objectDefinePropertySetter(value) { obj.__defineSetter__(name, function objectDefinePropertySetter(value) {
this.__defineGetter__(name, function objectDefinePropertyGetter() { this.__defineGetter__(name, function objectDefinePropertyGetter() {
@ -150,14 +158,16 @@ if (typeof PDFJS === 'undefined') {
// Object.keys() ? // Object.keys() ?
(function checkObjectKeysCompatibility() { (function checkObjectKeysCompatibility() {
if (typeof Object.keys !== 'undefined') if (typeof Object.keys !== 'undefined') {
return; return;
}
Object.keys = function objectKeys(obj) { Object.keys = function objectKeys(obj) {
var result = []; var result = [];
for (var i in obj) { for (var i in obj) {
if (obj.hasOwnProperty(i)) if (obj.hasOwnProperty(i)) {
result.push(i); result.push(i);
}
} }
return result; return result;
}; };
@ -165,12 +175,14 @@ if (typeof PDFJS === 'undefined') {
// No readAsArrayBuffer ? // No readAsArrayBuffer ?
(function checkFileReaderReadAsArrayBuffer() { (function checkFileReaderReadAsArrayBuffer() {
if (typeof FileReader === 'undefined') if (typeof FileReader === 'undefined') {
return; // FileReader is not implemented return; // FileReader is not implemented
}
var frPrototype = FileReader.prototype; var frPrototype = FileReader.prototype;
// Older versions of Firefox might not have readAsArrayBuffer // Older versions of Firefox might not have readAsArrayBuffer
if ('readAsArrayBuffer' in frPrototype) if ('readAsArrayBuffer' in frPrototype) {
return; // readAsArrayBuffer is implemented return; // readAsArrayBuffer is implemented
}
Object.defineProperty(frPrototype, 'readAsArrayBuffer', { Object.defineProperty(frPrototype, 'readAsArrayBuffer', {
value: function fileReaderReadAsArrayBuffer(blob) { value: function fileReaderReadAsArrayBuffer(blob) {
var fileReader = new FileReader(); var fileReader = new FileReader();
@ -180,8 +192,9 @@ if (typeof PDFJS === 'undefined') {
var buffer = new ArrayBuffer(data.length); var buffer = new ArrayBuffer(data.length);
var uint8Array = new Uint8Array(buffer); var uint8Array = new Uint8Array(buffer);
for (var i = 0, ii = data.length; i < ii; i++) for (var i = 0, ii = data.length; i < ii; i++) {
uint8Array[i] = data.charCodeAt(i); uint8Array[i] = data.charCodeAt(i);
}
Object.defineProperty(originalReader, 'result', { Object.defineProperty(originalReader, 'result', {
value: buffer, value: buffer,
@ -211,8 +224,9 @@ if (typeof PDFJS === 'undefined') {
if ('response' in xhrPrototype || if ('response' in xhrPrototype ||
'mozResponseArrayBuffer' in xhrPrototype || 'mozResponseArrayBuffer' in xhrPrototype ||
'mozResponse' in xhrPrototype || 'mozResponse' in xhrPrototype ||
'responseArrayBuffer' in xhrPrototype) 'responseArrayBuffer' in xhrPrototype) {
return; return;
}
// IE9 ? // IE9 ?
if (typeof VBArray !== 'undefined') { if (typeof VBArray !== 'undefined') {
Object.defineProperty(xhrPrototype, 'response', { Object.defineProperty(xhrPrototype, 'response', {
@ -236,8 +250,9 @@ if (typeof PDFJS === 'undefined') {
var text = this.responseText; var text = this.responseText;
var i, n = text.length; var i, n = text.length;
var result = new Uint8Array(n); var result = new Uint8Array(n);
for (i = 0; i < n; ++i) for (i = 0; i < n; ++i) {
result[i] = text.charCodeAt(i) & 0xFF; result[i] = text.charCodeAt(i) & 0xFF;
}
return result; return result;
} }
Object.defineProperty(xhrPrototype, 'response', { get: responseGetter }); Object.defineProperty(xhrPrototype, 'response', { get: responseGetter });
@ -245,8 +260,9 @@ if (typeof PDFJS === 'undefined') {
// window.btoa (base64 encode function) ? // window.btoa (base64 encode function) ?
(function checkWindowBtoaCompatibility() { (function checkWindowBtoaCompatibility() {
if ('btoa' in window) if ('btoa' in window) {
return; return;
}
var digits = var digits =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
@ -270,15 +286,18 @@ if (typeof PDFJS === 'undefined') {
// window.atob (base64 encode function) ? // window.atob (base64 encode function) ?
(function checkWindowAtobCompatibility() { (function checkWindowAtobCompatibility() {
if ('atob' in window) if ('atob' in window) {
return; return;
}
// https://github.com/davidchambers/Base64.js // https://github.com/davidchambers/Base64.js
var digits = var digits =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
window.atob = function (input) { window.atob = function (input) {
input = input.replace(/=+$/, ''); input = input.replace(/=+$/, '');
if (input.length % 4 == 1) throw new Error('bad atob input'); if (input.length % 4 == 1) {
throw new Error('bad atob input');
}
for ( for (
// initialize result and counters // initialize result and counters
var bc = 0, bs, buffer, idx = 0, output = ''; var bc = 0, bs, buffer, idx = 0, output = '';
@ -300,8 +319,9 @@ if (typeof PDFJS === 'undefined') {
// Function.prototype.bind ? // Function.prototype.bind ?
(function checkFunctionPrototypeBindCompatibility() { (function checkFunctionPrototypeBindCompatibility() {
if (typeof Function.prototype.bind !== 'undefined') if (typeof Function.prototype.bind !== 'undefined') {
return; return;
}
Function.prototype.bind = function functionPrototypeBind(obj) { Function.prototype.bind = function functionPrototypeBind(obj) {
var fn = this, headArgs = Array.prototype.slice.call(arguments, 1); var fn = this, headArgs = Array.prototype.slice.call(arguments, 1);
@ -316,21 +336,26 @@ if (typeof PDFJS === 'undefined') {
// HTMLElement dataset property // HTMLElement dataset property
(function checkDatasetProperty() { (function checkDatasetProperty() {
var div = document.createElement('div'); var div = document.createElement('div');
if ('dataset' in div) if ('dataset' in div) {
return; // dataset property exists return; // dataset property exists
}
Object.defineProperty(HTMLElement.prototype, 'dataset', { Object.defineProperty(HTMLElement.prototype, 'dataset', {
get: function() { get: function() {
if (this._dataset) if (this._dataset) {
return this._dataset; return this._dataset;
}
var dataset = {}; var dataset = {};
for (var j = 0, jj = this.attributes.length; j < jj; j++) { for (var j = 0, jj = this.attributes.length; j < jj; j++) {
var attribute = this.attributes[j]; var attribute = this.attributes[j];
if (attribute.name.substring(0, 5) != 'data-') if (attribute.name.substring(0, 5) != 'data-') {
continue; continue;
}
var key = attribute.name.substring(5).replace(/\-([a-z])/g, var key = attribute.name.substring(5).replace(/\-([a-z])/g,
function(all, ch) { return ch.toUpperCase(); }); function(all, ch) {
return ch.toUpperCase();
});
dataset[key] = attribute.value; dataset[key] = attribute.value;
} }
@ -348,18 +373,23 @@ if (typeof PDFJS === 'undefined') {
// HTMLElement classList property // HTMLElement classList property
(function checkClassListProperty() { (function checkClassListProperty() {
var div = document.createElement('div'); var div = document.createElement('div');
if ('classList' in div) if ('classList' in div) {
return; // classList property exists return; // classList property exists
}
function changeList(element, itemName, add, remove) { function changeList(element, itemName, add, remove) {
var s = element.className || ''; var s = element.className || '';
var list = s.split(/\s+/g); var list = s.split(/\s+/g);
if (list[0] === '') list.shift(); if (list[0] === '') {
list.shift();
}
var index = list.indexOf(itemName); var index = list.indexOf(itemName);
if (index < 0 && add) if (index < 0 && add) {
list.push(itemName); list.push(itemName);
if (index >= 0 && remove) }
if (index >= 0 && remove) {
list.splice(index, 1); list.splice(index, 1);
}
element.className = list.join(' '); element.className = list.join(' ');
return (index >= 0); return (index >= 0);
} }
@ -381,8 +411,9 @@ if (typeof PDFJS === 'undefined') {
Object.defineProperty(HTMLElement.prototype, 'classList', { Object.defineProperty(HTMLElement.prototype, 'classList', {
get: function() { get: function() {
if (this._classList) if (this._classList) {
return this._classList; return this._classList;
}
var classList = Object.create(classListPrototype, { var classList = Object.create(classListPrototype, {
element: { element: {