OldiePolyfills.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* *
  2. *
  3. * (c) 2010-2021 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * Simple polyfills for array functions in old IE browsers (6, 7 and 8) in
  8. * Highcharts v7+. These polyfills are sufficient for Highcharts to work, but
  9. * for fully compatible polyfills, see MDN.
  10. *
  11. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  12. *
  13. * */
  14. /* global document */
  15. 'use strict';
  16. /* eslint-disable no-extend-native */
  17. if (!String.prototype.trim) {
  18. String.prototype.trim = function () {
  19. return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  20. };
  21. }
  22. if (!Array.prototype.forEach) {
  23. Array.prototype.forEach = function (fn, thisArg) {
  24. var i = 0, len = this.length;
  25. for (; i < len; i++) {
  26. if (typeof this[i] !== 'undefined' && // added check
  27. fn.call(thisArg, this[i], i, this) === false) {
  28. return i;
  29. }
  30. }
  31. };
  32. }
  33. if (!Array.prototype.map) {
  34. Array.prototype.map = function (fn
  35. // @todo support optional ctx
  36. ) {
  37. var results = [], i = 0, len = this.length;
  38. for (; i < len; i++) {
  39. results[i] = fn.call(this[i], this[i], i, this);
  40. }
  41. return results;
  42. };
  43. }
  44. if (!Array.prototype.indexOf) {
  45. Array.prototype.indexOf = function (member, fromIndex) {
  46. var arr = this, // #8874
  47. len, i = fromIndex || 0; // #8346
  48. if (arr) {
  49. len = arr.length;
  50. for (; i < len; i++) {
  51. if (arr[i] === member) {
  52. return i;
  53. }
  54. }
  55. }
  56. return -1;
  57. };
  58. }
  59. if (!Array.prototype.filter) {
  60. Array.prototype.filter = function (fn
  61. // @todo support optional ctx
  62. ) {
  63. var ret = [], i = 0, length = this.length;
  64. for (; i < length; i++) {
  65. if (fn(this[i], i)) {
  66. ret.push(this[i]);
  67. }
  68. }
  69. return ret;
  70. };
  71. }
  72. if (!Array.prototype.some) {
  73. Array.prototype.some = function (fn, thisArg) {
  74. var i = 0, len = this.length;
  75. for (; i < len; i++) {
  76. if (fn.call(thisArg, this[i], i, this) === true) {
  77. return true;
  78. }
  79. }
  80. return false;
  81. };
  82. }
  83. if (!Array.prototype.reduce) {
  84. Array.prototype.reduce = function (func, initialValue) {
  85. var context = this, i = arguments.length > 1 ? 0 : 1, accumulator = arguments.length > 1 ? initialValue : this[0], len = this.length;
  86. for (; i < len; ++i) {
  87. accumulator = func.call(context, accumulator, this[i], i, this);
  88. }
  89. return accumulator;
  90. };
  91. }
  92. if (!Function.prototype.bind) {
  93. Function.prototype.bind = function () {
  94. var thatFunc = this;
  95. var thatArg = arguments[0];
  96. var args = Array.prototype.slice.call(arguments, 1);
  97. if (typeof thatFunc !== 'function') {
  98. // closest thing possible to the ECMAScript 5
  99. // internal IsCallable function
  100. throw new TypeError('Function.prototype.bind - ' +
  101. 'what is trying to be bound is not callable');
  102. }
  103. return function () {
  104. var funcArgs = args.concat(Array.prototype.slice.call(arguments));
  105. return thatFunc.apply(thatArg, funcArgs);
  106. };
  107. };
  108. }
  109. // Adapted from https://johnresig.com/blog/objectgetprototypeof/
  110. if (!Object.getPrototypeOf) {
  111. if (typeof 'test'.__proto__ === 'object') { // eslint-disable-line no-proto
  112. Object.getPrototypeOf = function (object) {
  113. return object.__proto__; // eslint-disable-line no-proto
  114. };
  115. }
  116. else {
  117. Object.getPrototypeOf = function (object) {
  118. var proto = object.constructor.prototype;
  119. if (proto === object) {
  120. return {}.constructor.prototype;
  121. }
  122. // May break if the constructor has been tampered with
  123. return proto;
  124. };
  125. }
  126. }
  127. if (!Object.keys) {
  128. Object.keys = function (obj) {
  129. var result = [], prop;
  130. for (prop in obj) {
  131. if (Object.hasOwnProperty.call(obj, prop)) {
  132. result.push(prop);
  133. }
  134. }
  135. return result;
  136. };
  137. }
  138. // Add a getElementsByClassName function if the browser doesn't have one
  139. // Limitation: only works with one class name
  140. // Copyright: Eike Send https://eike.se/nd
  141. // License: MIT License
  142. if (!document.getElementsByClassName) {
  143. document.getElementsByClassName = function (search) {
  144. var d = document, elements, pattern, i, results = [];
  145. if (d.querySelectorAll) { // IE8
  146. return d.querySelectorAll('.' + search);
  147. }
  148. if (d.evaluate) { // IE6, IE7
  149. pattern = './/*[contains(concat(\' \', @class, \' \'), \' ' +
  150. search + ' \')]';
  151. elements = d.evaluate(pattern, d, null, 0, null);
  152. while ((i = elements.iterateNext())) {
  153. results.push(i);
  154. }
  155. }
  156. else {
  157. elements = d.getElementsByTagName('*');
  158. pattern = new RegExp('(^|\\s)' + search + '(\\s|$)');
  159. for (i = 0; i < elements.length; i++) {
  160. if (pattern.test(elements[i].className)) {
  161. results.push(elements[i]);
  162. }
  163. }
  164. }
  165. return results;
  166. };
  167. }