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. return res;
  47. };
  48. }
  49. /**
  50. * implement Array.indexOf for browsers without native support
  51. */
  52. if (!Array.prototype.indexOf){
  53. Array.prototype.indexOf = function(elt /*, from*/)
  54. {
  55. var len = this.length;
  56. var from = Number(arguments[1]) || 0;
  57. from = (from < 0) ? Math.ceil(from) : Math.floor(from);
  58. if (from < 0){
  59. from += len;
  60. }
  61. for (; from < len; from++)
  62. {
  63. if (from in this && this[from] === elt){
  64. return from;
  65. }
  66. }
  67. return -1;
  68. };
  69. }
  70. /**
  71. * implement Array.map for browsers without native support
  72. */
  73. if (!Array.prototype.map){
  74. Array.prototype.map = function(fun /*, thisp */){
  75. "use strict";
  76. if (this === void 0 || this === null){
  77. throw new TypeError();
  78. }
  79. var t = Object(this);
  80. var len = t.length >>> 0;
  81. if (typeof fun !== "function"){
  82. throw new TypeError();
  83. }
  84. var res = new Array(len);
  85. var thisp = arguments[1];
  86. for (var i = 0; i < len; i++){
  87. if (i in t){
  88. res[i] = fun.call(thisp, t[i], i, t);
  89. }
  90. }
  91. return res;
  92. };
  93. }
  94. //https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/bind
  95. if (!Function.prototype.bind) {
  96. Function.prototype.bind = function (oThis) {
  97. if (typeof this !== "function") {
  98. // closest thing possible to the ECMAScript 5 internal IsCallable function
  99. throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
  100. }
  101. var aArgs = Array.prototype.slice.call(arguments, 1),
  102. fToBind = this,
  103. fNOP = function () {},
  104. fBound = function () {
  105. return fToBind.apply(this instanceof fNOP && oThis
  106. ? this
  107. : oThis,
  108. aArgs.concat(Array.prototype.slice.call(arguments)));
  109. };
  110. fNOP.prototype = this.prototype;
  111. fBound.prototype = new fNOP();
  112. return fBound;
  113. };
  114. }
  115. //https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/Trim
  116. if(!String.prototype.trim) {
  117. String.prototype.trim = function () {
  118. return this.replace(/^\s+|\s+$/g,'');
  119. };
  120. }
  121. // Older Firefoxes doesn't support outerHTML
  122. // From http://stackoverflow.com/questions/1700870/how-do-i-do-outerhtml-in-firefox#answer-3819589
  123. function outerHTML(node){
  124. // In newer browsers use the internal property otherwise build a wrapper.
  125. return node.outerHTML || (
  126. function(n){
  127. var div = document.createElement('div'), h;
  128. div.appendChild( n.cloneNode(true) );
  129. h = div.innerHTML;
  130. div = null;
  131. return h;
  132. })(node);
  133. }
  134. // devicePixelRatio for IE10
  135. window.devicePixelRatio = window.devicePixelRatio ||
  136. window.screen.deviceXDPI / window.screen.logicalXDPI || 1;