polyfillsIE.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. (function () {
  2. 'use strict';
  3. /* Polyfill service v3.16.0
  4. * For detailed credits and licence information see https://github.com/financial-times/polyfill-service.
  5. *
  6. * UA detected: ie/9.0.0
  7. * Features requested: CustomEvent
  8. *
  9. * - Event, License: CC0 (required by "CustomEvent")
  10. * - CustomEvent, License: CC0 */
  11. function CustomEventPolyfill() {
  12. (function (undefined$1) {
  13. if (!((function (global) {
  14. if (!('Event' in global)) return false;
  15. if (typeof global.Event === 'function') return true;
  16. try {
  17. // In IE 9-11, the Event object exists but cannot be instantiated
  18. new Event('click');
  19. return true;
  20. } catch (e) {
  21. return false;
  22. }
  23. }(this)))) {
  24. // Event
  25. (function () {
  26. var unlistenableWindowEvents = {
  27. click: 1,
  28. dblclick: 1,
  29. keyup: 1,
  30. keypress: 1,
  31. keydown: 1,
  32. mousedown: 1,
  33. mouseup: 1,
  34. mousemove: 1,
  35. mouseover: 1,
  36. mouseenter: 1,
  37. mouseleave: 1,
  38. mouseout: 1,
  39. storage: 1,
  40. storagecommit: 1,
  41. textinput: 1
  42. };
  43. function indexOf(array, element) {
  44. var
  45. index = -1,
  46. length = array.length;
  47. while (++index < length) {
  48. if (index in array && array[index] === element) {
  49. return index;
  50. }
  51. }
  52. return -1;
  53. }
  54. var existingProto = (window.Event && window.Event.prototype) || null;
  55. window.Event = Window.prototype.Event = function Event(type, eventInitDict) {
  56. if (!type) {
  57. throw new Error('Not enough arguments');
  58. }
  59. // Shortcut if browser supports createEvent
  60. if ('createEvent' in document) {
  61. var event = document.createEvent('Event');
  62. var bubbles = eventInitDict && eventInitDict.bubbles !== undefined$1 ?
  63. eventInitDict.bubbles : false;
  64. var cancelable = eventInitDict && eventInitDict.cancelable !== undefined$1 ?
  65. eventInitDict.cancelable : false;
  66. event.initEvent(type, bubbles, cancelable);
  67. return event;
  68. }
  69. var event = document.createEventObject();
  70. event.type = type;
  71. event.bubbles =
  72. eventInitDict && eventInitDict.bubbles !== undefined$1 ? eventInitDict.bubbles : false;
  73. event.cancelable =
  74. eventInitDict && eventInitDict.cancelable !== undefined$1 ? eventInitDict.cancelable :
  75. false;
  76. return event;
  77. };
  78. if (existingProto) {
  79. Object.defineProperty(window.Event, 'prototype', {
  80. configurable: false,
  81. enumerable: false,
  82. writable: true,
  83. value: existingProto
  84. });
  85. }
  86. if (!('createEvent' in document)) {
  87. window.addEventListener = Window.prototype.addEventListener =
  88. Document.prototype.addEventListener =
  89. Element.prototype.addEventListener = function addEventListener() {
  90. var
  91. element = this,
  92. type = arguments[0],
  93. listener = arguments[1];
  94. if (element === window && type in unlistenableWindowEvents) {
  95. throw new Error('In IE8 the event: ' + type +
  96. ' is not available on the window object.');
  97. }
  98. if (!element._events) {
  99. element._events = {};
  100. }
  101. if (!element._events[type]) {
  102. element._events[type] = function (event) {
  103. var
  104. list = element._events[event.type].list,
  105. events = list.slice(),
  106. index = -1,
  107. length = events.length,
  108. eventElement;
  109. event.preventDefault = function preventDefault() {
  110. if (event.cancelable !== false) {
  111. event.returnValue = false;
  112. }
  113. };
  114. event.stopPropagation = function stopPropagation() {
  115. event.cancelBubble = true;
  116. };
  117. event.stopImmediatePropagation = function stopImmediatePropagation() {
  118. event.cancelBubble = true;
  119. event.cancelImmediate = true;
  120. };
  121. event.currentTarget = element;
  122. event.relatedTarget = event.fromElement || null;
  123. event.target = event.target || event.srcElement || element;
  124. event.timeStamp = new Date().getTime();
  125. if (event.clientX) {
  126. event.pageX = event.clientX + document.documentElement.scrollLeft;
  127. event.pageY = event.clientY + document.documentElement.scrollTop;
  128. }
  129. while (++index < length && !event.cancelImmediate) {
  130. if (index in events) {
  131. eventElement = events[index];
  132. if (indexOf(list, eventElement) !== -1 &&
  133. typeof eventElement === 'function') {
  134. eventElement.call(element, event);
  135. }
  136. }
  137. }
  138. };
  139. element._events[type].list = [];
  140. if (element.attachEvent) {
  141. element.attachEvent('on' + type, element._events[type]);
  142. }
  143. }
  144. element._events[type].list.push(listener);
  145. };
  146. window.removeEventListener = Window.prototype.removeEventListener =
  147. Document.prototype.removeEventListener =
  148. Element.prototype.removeEventListener = function removeEventListener() {
  149. var
  150. element = this,
  151. type = arguments[0],
  152. listener = arguments[1],
  153. index;
  154. if (element._events && element._events[type] && element._events[type].list) {
  155. index = indexOf(element._events[type].list, listener);
  156. if (index !== -1) {
  157. element._events[type].list.splice(index, 1);
  158. if (!element._events[type].list.length) {
  159. if (element.detachEvent) {
  160. element.detachEvent('on' + type, element._events[type]);
  161. }
  162. delete element._events[type];
  163. }
  164. }
  165. }
  166. };
  167. window.dispatchEvent = Window.prototype.dispatchEvent = Document.prototype.dispatchEvent =
  168. Element.prototype.dispatchEvent = function dispatchEvent(event) {
  169. if (!arguments.length) {
  170. throw new Error('Not enough arguments');
  171. }
  172. if (!event || typeof event.type !== 'string') {
  173. throw new Error('DOM Events Exception 0');
  174. }
  175. var element = this, type = event.type;
  176. try {
  177. if (!event.bubbles) {
  178. event.cancelBubble = true;
  179. var cancelBubbleEvent = function (event) {
  180. event.cancelBubble = true;
  181. (element || window).detachEvent('on' + type, cancelBubbleEvent);
  182. };
  183. this.attachEvent('on' + type, cancelBubbleEvent);
  184. }
  185. this.fireEvent('on' + type, event);
  186. } catch (error) {
  187. event.target = element;
  188. do {
  189. event.currentTarget = element;
  190. if ('_events' in element && typeof element._events[type] === 'function') {
  191. element._events[type].call(element, event);
  192. }
  193. if (typeof element['on' + type] === 'function') {
  194. element['on' + type].call(element, event);
  195. }
  196. element = element.nodeType === 9 ? element.parentWindow : element.parentNode;
  197. } while (element && !event.cancelBubble);
  198. }
  199. return true;
  200. };
  201. // Add the DOMContentLoaded Event
  202. document.attachEvent('onreadystatechange', function () {
  203. if (document.readyState === 'complete') {
  204. document.dispatchEvent(new Event('DOMContentLoaded', {
  205. bubbles: true
  206. }));
  207. }
  208. });
  209. }
  210. }());
  211. }
  212. if (!('CustomEvent' in this &&
  213. // In Safari, typeof CustomEvent == 'object' but it otherwise works fine
  214. (typeof this.CustomEvent === 'function' ||
  215. (this.CustomEvent.toString().indexOf('CustomEventConstructor') > -1)))) {
  216. // CustomEvent
  217. this.CustomEvent = function CustomEvent(type, eventInitDict) {
  218. if (!type) {
  219. throw Error(
  220. 'TypeError: Failed to construct "CustomEvent": An event name must be provided.');
  221. }
  222. var event;
  223. eventInitDict = eventInitDict || {bubbles: false, cancelable: false, detail: null};
  224. if ('createEvent' in document) {
  225. try {
  226. event = document.createEvent('CustomEvent');
  227. event.initCustomEvent(type, eventInitDict.bubbles, eventInitDict.cancelable,
  228. eventInitDict.detail);
  229. } catch (error) {
  230. // for browsers which don't support CustomEvent at all, we use a regular event instead
  231. event = document.createEvent('Event');
  232. event.initEvent(type, eventInitDict.bubbles, eventInitDict.cancelable);
  233. event.detail = eventInitDict.detail;
  234. }
  235. } else {
  236. // IE8
  237. event = new Event(type, eventInitDict);
  238. event.detail = eventInitDict && eventInitDict.detail || null;
  239. }
  240. return event;
  241. };
  242. CustomEvent.prototype = Event.prototype;
  243. }
  244. }).call('object' === typeof window && window || 'object' === typeof self && self ||
  245. 'object' === typeof global && global || {});
  246. }
  247. // Map function
  248. // Filter function
  249. function filter (array, block) {
  250. let i;
  251. const il = array.length;
  252. const result = [];
  253. for (i = 0; i < il; i++) {
  254. if (block(array[i])) {
  255. result.push(array[i]);
  256. }
  257. }
  258. return result
  259. }
  260. // IE11: children does not work for svg nodes
  261. function children (node) {
  262. return filter(node.childNodes, function (child) {
  263. return child.nodeType === 1
  264. })
  265. }
  266. /* globals SVGElement, DOMParser */
  267. (function () {
  268. try {
  269. if (SVGElement.prototype.innerHTML) return
  270. } catch (e) { return }
  271. const serializeXML = function (node, output) {
  272. const nodeType = node.nodeType;
  273. if (nodeType === 3) {
  274. output.push(node.textContent.replace(/&/, '&amp;').replace(/</, '&lt;').replace('>', '&gt;'));
  275. } else if (nodeType === 1) {
  276. output.push('<', node.tagName);
  277. if (node.hasAttributes()) {
  278. [].forEach.call(node.attributes, function (attrNode) {
  279. output.push(' ', attrNode.name, '="', attrNode.value, '"');
  280. });
  281. }
  282. output.push('>');
  283. if (node.hasChildNodes()) {
  284. [].forEach.call(node.childNodes, function (childNode) {
  285. serializeXML(childNode, output);
  286. });
  287. }
  288. output.push('</', node.tagName, '>');
  289. } else if (nodeType === 8) {
  290. output.push('<!--', node.nodeValue, '-->');
  291. }
  292. };
  293. Object.defineProperty(SVGElement.prototype, 'innerHTML', {
  294. get: function () {
  295. const output = [];
  296. let childNode = this.firstChild;
  297. while (childNode) {
  298. serializeXML(childNode, output);
  299. childNode = childNode.nextSibling;
  300. }
  301. return output.join('')
  302. },
  303. set: function (markupText) {
  304. while (this.firstChild) {
  305. this.removeChild(this.firstChild);
  306. }
  307. try {
  308. const dXML = new DOMParser();
  309. dXML.async = false;
  310. const sXML = '<svg xmlns=\'http://www.w3.org/2000/svg\' xmlns:xlink=\'http://www.w3.org/1999/xlink\'>' + markupText + '</svg>';
  311. const svgDocElement = dXML.parseFromString(sXML, 'text/xml').documentElement;
  312. let childNode = svgDocElement.firstChild;
  313. while (childNode) {
  314. this.appendChild(this.ownerDocument.importNode(childNode, true));
  315. childNode = childNode.nextSibling;
  316. }
  317. } catch (e) {
  318. throw new Error('Can not set innerHTML on node')
  319. } }
  320. });
  321. Object.defineProperty(SVGElement.prototype, 'outerHTML', {
  322. get: function () {
  323. const output = [];
  324. serializeXML(this, output);
  325. return output.join('')
  326. },
  327. set: function (markupText) {
  328. while (this.firstChild) {
  329. this.removeChild(this.firstChild);
  330. }
  331. try {
  332. const dXML = new DOMParser();
  333. dXML.async = false;
  334. const sXML = '<svg xmlns=\'http://www.w3.org/2000/svg\' xmlns:xlink=\'http://www.w3.org/1999/xlink\'>' + markupText + '</svg>';
  335. const svgDocElement = dXML.parseFromString(sXML, 'text/xml').documentElement;
  336. let childNode = svgDocElement.firstChild;
  337. while (childNode) {
  338. this.parentNode.insertBefore(this.ownerDocument.importNode(childNode, true), this);
  339. // this.appendChild(this.ownerDocument.importNode(childNode, true));
  340. childNode = childNode.nextSibling;
  341. }
  342. } catch (e) {
  343. throw new Error('Can not set outerHTML on node')
  344. } }
  345. });
  346. })();
  347. /* global SVGElement */
  348. /* IE 11 has no correct CustomEvent implementation */
  349. CustomEventPolyfill();
  350. /* IE 11 has no children on SVGElement */
  351. try {
  352. if (!SVGElement.prototype.children) {
  353. Object.defineProperty(SVGElement.prototype, 'children', {
  354. get: function () { return children(this) }
  355. });
  356. }
  357. } catch (e) {}
  358. /* IE 11 cannot handle getPrototypeOf(not_obj) */
  359. try {
  360. delete Object.getPrototypeOf('test');
  361. } catch (e) {
  362. var old = Object.getPrototypeOf;
  363. Object.getPrototypeOf = function (o) {
  364. if (typeof o !== 'object') o = new Object(o);
  365. return old.call(this, o)
  366. };
  367. }
  368. })();