compatibility.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * implement Object.create for browsers without native support
  3. */
  4. if (typeof Object.create !== 'function') {
  5. Object.create = function (o) {
  6. function F() {}
  7. F.prototype = o;
  8. return new F();
  9. };
  10. }
  11. /**
  12. * implement Object.keys for browsers without native support
  13. */
  14. if (typeof Object.keys !== 'function') {
  15. Object.keys = function(o) {
  16. if (o !== Object(o)) {
  17. throw new TypeError('Object.keys called on a non-object');
  18. }
  19. var k=[],p;
  20. for (p in o) {
  21. if (Object.prototype.hasOwnProperty.call(o,p)) {
  22. k.push(p);
  23. }
  24. }
  25. return k;
  26. };
  27. }
  28. /**
  29. * implement Array.filter for browsers without native support
  30. */
  31. if (!Array.prototype.filter) {
  32. Array.prototype.filter = function(fun /*, thisp*/) {
  33. var len = this.length >>> 0;
  34. if (typeof fun !== "function"){
  35. throw new TypeError();
  36. }
  37. var res = [];
  38. var thisp = arguments[1];
  39. for (var i = 0; i < len; i++) {
  40. if (i in this) {
  41. var val = this[i]; // in case fun mutates this
  42. if (fun.call(thisp, val, i, this)) {
  43. res.push(val);
  44. }
  45. }
  46. }
  47. return res;
  48. };
  49. }
  50. /**
  51. * implement Array.indexOf for browsers without native support
  52. */
  53. if (!Array.prototype.indexOf){
  54. Array.prototype.indexOf = function(elt /*, from*/)
  55. {
  56. var len = this.length;
  57. var from = Number(arguments[1]) || 0;
  58. from = (from < 0) ? Math.ceil(from) : Math.floor(from);
  59. if (from < 0){
  60. from += len;
  61. }
  62. for (; from < len; from++)
  63. {
  64. if (from in this && this[from] === elt){
  65. return from;
  66. }
  67. }
  68. return -1;
  69. };
  70. }
  71. /**
  72. * implement Array.map for browsers without native support
  73. */
  74. if (!Array.prototype.map){
  75. Array.prototype.map = function(fun /*, thisp */){
  76. "use strict";
  77. if (this === void 0 || this === null){
  78. throw new TypeError();
  79. }
  80. var t = Object(this);
  81. var len = t.length >>> 0;
  82. if (typeof fun !== "function"){
  83. throw new TypeError();
  84. }
  85. var res = new Array(len);
  86. var thisp = arguments[1];
  87. for (var i = 0; i < len; i++){
  88. if (i in t){
  89. res[i] = fun.call(thisp, t[i], i, t);
  90. }
  91. }
  92. return res;
  93. };
  94. }
  95. //https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/bind
  96. if (!Function.prototype.bind) {
  97. Function.prototype.bind = function (oThis) {
  98. if (typeof this !== "function") {
  99. // closest thing possible to the ECMAScript 5 internal IsCallable function
  100. throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
  101. }
  102. var aArgs = Array.prototype.slice.call(arguments, 1),
  103. fToBind = this,
  104. fNOP = function () {},
  105. fBound = function () {
  106. return fToBind.apply(this instanceof fNOP && oThis
  107. ? this
  108. : oThis,
  109. aArgs.concat(Array.prototype.slice.call(arguments)));
  110. };
  111. fNOP.prototype = this.prototype;
  112. fBound.prototype = new fNOP();
  113. return fBound;
  114. };
  115. }
  116. //https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/Trim
  117. if(!String.prototype.trim) {
  118. String.prototype.trim = function () {
  119. return this.replace(/^\s+|\s+$/g,'');
  120. };
  121. }
  122. // Older Firefoxes doesn't support outerHTML
  123. // From http://stackoverflow.com/questions/1700870/how-do-i-do-outerhtml-in-firefox#answer-3819589
  124. function outerHTML(node){
  125. // In newer browsers use the internal property otherwise build a wrapper.
  126. return node.outerHTML || (
  127. function(n){
  128. var div = document.createElement('div'), h;
  129. div.appendChild( n.cloneNode(true) );
  130. h = div.innerHTML;
  131. div = null;
  132. return h;
  133. })(node);
  134. }
  135. // devicePixelRatio for IE10
  136. window.devicePixelRatio = window.devicePixelRatio ||
  137. window.screen.deviceXDPI / window.screen.logicalXDPI || 1;