es5-sham.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*!
  2. * https://github.com/es-shims/es5-shim
  3. * @license es5-shim Copyright 2009-2020 by contributors, MIT License
  4. * see https://github.com/es-shims/es5-shim/blob/master/LICENSE
  5. */
  6. // vim: ts=4 sts=4 sw=4 expandtab
  7. // Add semicolon to prevent IIFE from being passed as argument to concatenated code.
  8. ; // eslint-disable-line no-extra-semi
  9. // UMD (Universal Module Definition)
  10. // see https://github.com/umdjs/umd/blob/master/templates/returnExports.js
  11. (function (root, factory) {
  12. 'use strict';
  13. /* global define */
  14. if (typeof define === 'function' && define.amd) {
  15. // AMD. Register as an anonymous module.
  16. define(factory);
  17. } else if (typeof exports === 'object') {
  18. // Node. Does not work with strict CommonJS, but
  19. // only CommonJS-like enviroments that support module.exports,
  20. // like Node.
  21. module.exports = factory();
  22. } else {
  23. // Browser globals (root is window)
  24. root.returnExports = factory(); // eslint-disable-line no-param-reassign
  25. }
  26. }(this, function () {
  27. var call = Function.call;
  28. var prototypeOfObject = Object.prototype;
  29. var owns = call.bind(prototypeOfObject.hasOwnProperty);
  30. var isEnumerable = call.bind(prototypeOfObject.propertyIsEnumerable);
  31. var toStr = call.bind(prototypeOfObject.toString);
  32. // If JS engine supports accessors creating shortcuts.
  33. var defineGetter;
  34. var defineSetter;
  35. var lookupGetter;
  36. var lookupSetter;
  37. var supportsAccessors = owns(prototypeOfObject, '__defineGetter__');
  38. if (supportsAccessors) {
  39. /* eslint-disable no-underscore-dangle, no-restricted-properties */
  40. defineGetter = call.bind(prototypeOfObject.__defineGetter__);
  41. defineSetter = call.bind(prototypeOfObject.__defineSetter__);
  42. lookupGetter = call.bind(prototypeOfObject.__lookupGetter__);
  43. lookupSetter = call.bind(prototypeOfObject.__lookupSetter__);
  44. /* eslint-enable no-underscore-dangle, no-restricted-properties */
  45. }
  46. var isPrimitive = function isPrimitive(o) {
  47. return o == null || (typeof o !== 'object' && typeof o !== 'function');
  48. };
  49. // ES5 15.2.3.2
  50. // https://es5.github.io/#x15.2.3.2
  51. if (!Object.getPrototypeOf) {
  52. // https://github.com/es-shims/es5-shim/issues#issue/2
  53. // https://johnresig.com/blog/objectgetprototypeof/
  54. // recommended by fschaefer on github
  55. //
  56. // sure, and webreflection says ^_^
  57. // ... this will nerever possibly return null
  58. // ... Opera Mini breaks here with infinite loops
  59. Object.getPrototypeOf = function getPrototypeOf(object) {
  60. // eslint-disable-next-line no-proto
  61. var proto = object.__proto__;
  62. if (proto || proto == null) { // `undefined` is for pre-proto browsers
  63. return proto;
  64. } else if (toStr(object.constructor) === '[object Function]') {
  65. return object.constructor.prototype;
  66. } else if (object instanceof Object) {
  67. return prototypeOfObject;
  68. }
  69. // Correctly return null for Objects created with `Object.create(null)`
  70. // (shammed or native) or `{ __proto__: null}`. Also returns null for
  71. // cross-realm objects on browsers that lack `__proto__` support (like
  72. // IE <11), but that's the best we can do.
  73. return null;
  74. };
  75. }
  76. // ES5 15.2.3.3
  77. // https://es5.github.io/#x15.2.3.3
  78. // check whether getOwnPropertyDescriptor works if it's given. Otherwise, shim partially.
  79. if (Object.defineProperty) {
  80. var doesGetOwnPropertyDescriptorWork = function doesGetOwnPropertyDescriptorWork(object) {
  81. try {
  82. object.sentinel = 0; // eslint-disable-line no-param-reassign
  83. return Object.getOwnPropertyDescriptor(object, 'sentinel').value === 0;
  84. } catch (exception) {
  85. return false;
  86. }
  87. };
  88. var getOwnPropertyDescriptorWorksOnObject = doesGetOwnPropertyDescriptorWork({});
  89. var getOwnPropertyDescriptorWorksOnDom = typeof document === 'undefined'
  90. || doesGetOwnPropertyDescriptorWork(document.createElement('div'));
  91. if (!getOwnPropertyDescriptorWorksOnDom || !getOwnPropertyDescriptorWorksOnObject) {
  92. var getOwnPropertyDescriptorFallback = Object.getOwnPropertyDescriptor;
  93. }
  94. }
  95. if (!Object.getOwnPropertyDescriptor || getOwnPropertyDescriptorFallback) {
  96. var ERR_NON_OBJECT = 'Object.getOwnPropertyDescriptor called on a non-object: ';
  97. /* eslint-disable no-proto */
  98. Object.getOwnPropertyDescriptor = function getOwnPropertyDescriptor(object, property) {
  99. if (isPrimitive(object)) {
  100. throw new TypeError(ERR_NON_OBJECT + object);
  101. }
  102. // make a valiant attempt to use the real getOwnPropertyDescriptor
  103. // for I8's DOM elements.
  104. if (getOwnPropertyDescriptorFallback) {
  105. try {
  106. return getOwnPropertyDescriptorFallback.call(Object, object, property);
  107. } catch (exception) {
  108. // try the shim if the real one doesn't work
  109. }
  110. }
  111. var descriptor;
  112. // If object does not owns property return undefined immediately.
  113. if (!owns(object, property)) {
  114. return descriptor;
  115. }
  116. // If object has a property then it's for sure `configurable`, and
  117. // probably `enumerable`. Detect enumerability though.
  118. descriptor = {
  119. enumerable: isEnumerable(object, property),
  120. configurable: true
  121. };
  122. // If JS engine supports accessor properties then property may be a
  123. // getter or setter.
  124. if (supportsAccessors) {
  125. // Unfortunately `__lookupGetter__` will return a getter even
  126. // if object has own non getter property along with a same named
  127. // inherited getter. To avoid misbehavior we temporary remove
  128. // `__proto__` so that `__lookupGetter__` will return getter only
  129. // if it's owned by an object.
  130. var prototype = object.__proto__;
  131. var notPrototypeOfObject = object !== prototypeOfObject;
  132. // avoid recursion problem, breaking in Opera Mini when
  133. // Object.getOwnPropertyDescriptor(Object.prototype, 'toString')
  134. // or any other Object.prototype accessor
  135. if (notPrototypeOfObject) {
  136. object.__proto__ = prototypeOfObject; // eslint-disable-line no-param-reassign
  137. }
  138. var getter = lookupGetter(object, property);
  139. var setter = lookupSetter(object, property);
  140. if (notPrototypeOfObject) {
  141. // Once we have getter and setter we can put values back.
  142. object.__proto__ = prototype; // eslint-disable-line no-param-reassign
  143. }
  144. if (getter || setter) {
  145. if (getter) {
  146. descriptor.get = getter;
  147. }
  148. if (setter) {
  149. descriptor.set = setter;
  150. }
  151. // If it was accessor property we're done and return here
  152. // in order to avoid adding `value` to the descriptor.
  153. return descriptor;
  154. }
  155. }
  156. // If we got this far we know that object has an own property that is
  157. // not an accessor so we set it as a value and return descriptor.
  158. descriptor.value = object[property];
  159. descriptor.writable = true;
  160. return descriptor;
  161. };
  162. /* eslint-enable no-proto */
  163. }
  164. // ES5 15.2.3.4
  165. // https://es5.github.io/#x15.2.3.4
  166. if (!Object.getOwnPropertyNames) {
  167. Object.getOwnPropertyNames = function getOwnPropertyNames(object) {
  168. return Object.keys(object);
  169. };
  170. }
  171. // ES5 15.2.3.5
  172. // https://es5.github.io/#x15.2.3.5
  173. if (!Object.create) {
  174. // Contributed by Brandon Benvie, October, 2012
  175. var createEmpty;
  176. var supportsProto = !({ __proto__: null } instanceof Object);
  177. // the following produces false positives
  178. // in Opera Mini => not a reliable check
  179. // Object.prototype.__proto__ === null
  180. // Check for document.domain and active x support
  181. // No need to use active x approach when document.domain is not set
  182. // see https://github.com/es-shims/es5-shim/issues/150
  183. // variation of https://github.com/kitcambridge/es5-shim/commit/4f738ac066346
  184. /* global ActiveXObject */
  185. var shouldUseActiveX = function shouldUseActiveX() {
  186. // return early if document.domain not set
  187. if (!document.domain) {
  188. return false;
  189. }
  190. try {
  191. return !!new ActiveXObject('htmlfile');
  192. } catch (exception) {
  193. return false;
  194. }
  195. };
  196. // This supports IE8 when document.domain is used
  197. // see https://github.com/es-shims/es5-shim/issues/150
  198. // variation of https://github.com/kitcambridge/es5-shim/commit/4f738ac066346
  199. var getEmptyViaActiveX = function getEmptyViaActiveX() {
  200. var empty;
  201. var xDoc;
  202. xDoc = new ActiveXObject('htmlfile');
  203. var script = 'script';
  204. xDoc.write('<' + script + '></' + script + '>');
  205. xDoc.close();
  206. empty = xDoc.parentWindow.Object.prototype;
  207. xDoc = null;
  208. return empty;
  209. };
  210. // The original implementation using an iframe
  211. // before the activex approach was added
  212. // see https://github.com/es-shims/es5-shim/issues/150
  213. var getEmptyViaIFrame = function getEmptyViaIFrame() {
  214. var iframe = document.createElement('iframe');
  215. var parent = document.body || document.documentElement;
  216. var empty;
  217. iframe.style.display = 'none';
  218. parent.appendChild(iframe);
  219. // eslint-disable-next-line no-script-url
  220. iframe.src = 'javascript:';
  221. empty = iframe.contentWindow.Object.prototype;
  222. parent.removeChild(iframe);
  223. iframe = null;
  224. return empty;
  225. };
  226. /* global document */
  227. if (supportsProto || typeof document === 'undefined') {
  228. createEmpty = function () {
  229. return { __proto__: null };
  230. };
  231. } else {
  232. // In old IE __proto__ can't be used to manually set `null`, nor does
  233. // any other method exist to make an object that inherits from nothing,
  234. // aside from Object.prototype itself. Instead, create a new global
  235. // object and *steal* its Object.prototype and strip it bare. This is
  236. // used as the prototype to create nullary objects.
  237. createEmpty = function () {
  238. // Determine which approach to use
  239. // see https://github.com/es-shims/es5-shim/issues/150
  240. var empty = shouldUseActiveX() ? getEmptyViaActiveX() : getEmptyViaIFrame();
  241. delete empty.constructor;
  242. delete empty.hasOwnProperty;
  243. delete empty.propertyIsEnumerable;
  244. delete empty.isPrototypeOf;
  245. delete empty.toLocaleString;
  246. delete empty.toString;
  247. delete empty.valueOf;
  248. var Empty = function Empty() {};
  249. Empty.prototype = empty;
  250. // short-circuit future calls
  251. createEmpty = function () {
  252. return new Empty();
  253. };
  254. return new Empty();
  255. };
  256. }
  257. Object.create = function create(prototype, properties) {
  258. var object;
  259. var Type = function Type() {}; // An empty constructor.
  260. if (prototype === null) {
  261. object = createEmpty();
  262. } else if (isPrimitive(prototype)) {
  263. // In the native implementation `parent` can be `null`
  264. // OR *any* `instanceof Object` (Object|Function|Array|RegExp|etc)
  265. // Use `typeof` tho, b/c in old IE, DOM elements are not `instanceof Object`
  266. // like they are in modern browsers. Using `Object.create` on DOM elements
  267. // is...err...probably inappropriate, but the native version allows for it.
  268. throw new TypeError('Object prototype may only be an Object or null'); // same msg as Chrome
  269. } else {
  270. Type.prototype = prototype;
  271. object = new Type();
  272. // IE has no built-in implementation of `Object.getPrototypeOf`
  273. // neither `__proto__`, but this manually setting `__proto__` will
  274. // guarantee that `Object.getPrototypeOf` will work as expected with
  275. // objects created using `Object.create`
  276. // eslint-disable-next-line no-proto
  277. object.__proto__ = prototype;
  278. }
  279. if (properties !== void 0) {
  280. Object.defineProperties(object, properties);
  281. }
  282. return object;
  283. };
  284. }
  285. // ES5 15.2.3.6
  286. // https://es5.github.io/#x15.2.3.6
  287. // Patch for WebKit and IE8 standard mode
  288. // Designed by hax <hax.github.com>
  289. // related issue: https://github.com/es-shims/es5-shim/issues#issue/5
  290. // IE8 Reference:
  291. // https://msdn.microsoft.com/en-us/library/dd282900.aspx
  292. // https://msdn.microsoft.com/en-us/library/dd229916.aspx
  293. // WebKit Bugs:
  294. // https://bugs.webkit.org/show_bug.cgi?id=36423
  295. var doesDefinePropertyWork = function doesDefinePropertyWork(object) {
  296. try {
  297. Object.defineProperty(object, 'sentinel', {});
  298. return 'sentinel' in object;
  299. } catch (exception) {
  300. return false;
  301. }
  302. };
  303. // check whether defineProperty works if it's given. Otherwise,
  304. // shim partially.
  305. if (Object.defineProperty) {
  306. var definePropertyWorksOnObject = doesDefinePropertyWork({});
  307. var definePropertyWorksOnDom = typeof document === 'undefined'
  308. || doesDefinePropertyWork(document.createElement('div'));
  309. if (!definePropertyWorksOnObject || !definePropertyWorksOnDom) {
  310. var definePropertyFallback = Object.defineProperty,
  311. definePropertiesFallback = Object.defineProperties;
  312. }
  313. }
  314. if (!Object.defineProperty || definePropertyFallback) {
  315. var ERR_NON_OBJECT_DESCRIPTOR = 'Property description must be an object: ';
  316. var ERR_NON_OBJECT_TARGET = 'Object.defineProperty called on non-object: ';
  317. var ERR_ACCESSORS_NOT_SUPPORTED = 'getters & setters can not be defined on this javascript engine';
  318. Object.defineProperty = function defineProperty(object, property, descriptor) {
  319. if (isPrimitive(object)) {
  320. throw new TypeError(ERR_NON_OBJECT_TARGET + object);
  321. }
  322. if (isPrimitive(descriptor)) {
  323. throw new TypeError(ERR_NON_OBJECT_DESCRIPTOR + descriptor);
  324. }
  325. // make a valiant attempt to use the real defineProperty
  326. // for I8's DOM elements.
  327. if (definePropertyFallback) {
  328. try {
  329. return definePropertyFallback.call(Object, object, property, descriptor);
  330. } catch (exception) {
  331. // try the shim if the real one doesn't work
  332. }
  333. }
  334. // If it's a data property.
  335. if ('value' in descriptor) {
  336. // fail silently if 'writable', 'enumerable', or 'configurable'
  337. // are requested but not supported
  338. /*
  339. // alternate approach:
  340. if ( // can't implement these features; allow false but not true
  341. ('writable' in descriptor && !descriptor.writable) ||
  342. ('enumerable' in descriptor && !descriptor.enumerable) ||
  343. ('configurable' in descriptor && !descriptor.configurable)
  344. ))
  345. throw new RangeError(
  346. 'This implementation of Object.defineProperty does not support configurable, enumerable, or writable.'
  347. );
  348. */
  349. if (supportsAccessors && (lookupGetter(object, property) || lookupSetter(object, property))) {
  350. // As accessors are supported only on engines implementing
  351. // `__proto__` we can safely override `__proto__` while defining
  352. // a property to make sure that we don't hit an inherited
  353. // accessor.
  354. /* eslint-disable no-proto, no-param-reassign */
  355. var prototype = object.__proto__;
  356. object.__proto__ = prototypeOfObject;
  357. // Deleting a property anyway since getter / setter may be
  358. // defined on object itself.
  359. delete object[property];
  360. object[property] = descriptor.value;
  361. // Setting original `__proto__` back now.
  362. object.__proto__ = prototype;
  363. /* eslint-enable no-proto, no-param-reassign */
  364. } else {
  365. object[property] = descriptor.value; // eslint-disable-line no-param-reassign
  366. }
  367. } else {
  368. var hasGetter = 'get' in descriptor;
  369. var hasSetter = 'set' in descriptor;
  370. if (!supportsAccessors && (hasGetter || hasSetter)) {
  371. throw new TypeError(ERR_ACCESSORS_NOT_SUPPORTED);
  372. }
  373. // If we got that far then getters and setters can be defined !!
  374. if (hasGetter) {
  375. defineGetter(object, property, descriptor.get);
  376. }
  377. if (hasSetter) {
  378. defineSetter(object, property, descriptor.set);
  379. }
  380. }
  381. return object;
  382. };
  383. }
  384. // ES5 15.2.3.7
  385. // https://es5.github.io/#x15.2.3.7
  386. if (!Object.defineProperties || definePropertiesFallback) {
  387. Object.defineProperties = function defineProperties(object, properties) {
  388. // make a valiant attempt to use the real defineProperties
  389. if (definePropertiesFallback) {
  390. try {
  391. return definePropertiesFallback.call(Object, object, properties);
  392. } catch (exception) {
  393. // try the shim if the real one doesn't work
  394. }
  395. }
  396. Object.keys(properties).forEach(function (property) {
  397. if (property !== '__proto__') {
  398. Object.defineProperty(object, property, properties[property]);
  399. }
  400. });
  401. return object;
  402. };
  403. }
  404. // ES5 15.2.3.8
  405. // https://es5.github.io/#x15.2.3.8
  406. if (!Object.seal) {
  407. Object.seal = function seal(object) {
  408. if (Object(object) !== object) {
  409. throw new TypeError('Object.seal can only be called on Objects.');
  410. }
  411. // this is misleading and breaks feature-detection, but
  412. // allows "securable" code to "gracefully" degrade to working
  413. // but insecure code.
  414. return object;
  415. };
  416. }
  417. // ES5 15.2.3.9
  418. // https://es5.github.io/#x15.2.3.9
  419. if (!Object.freeze) {
  420. Object.freeze = function freeze(object) {
  421. if (Object(object) !== object) {
  422. throw new TypeError('Object.freeze can only be called on Objects.');
  423. }
  424. // this is misleading and breaks feature-detection, but
  425. // allows "securable" code to "gracefully" degrade to working
  426. // but insecure code.
  427. return object;
  428. };
  429. }
  430. // detect a Rhino bug and patch it
  431. try {
  432. Object.freeze(function () {});
  433. } catch (exception) {
  434. Object.freeze = (function (freezeObject) {
  435. return function freeze(object) {
  436. if (typeof object === 'function') {
  437. return object;
  438. }
  439. return freezeObject(object);
  440. };
  441. }(Object.freeze));
  442. }
  443. // ES5 15.2.3.10
  444. // https://es5.github.io/#x15.2.3.10
  445. if (!Object.preventExtensions) {
  446. Object.preventExtensions = function preventExtensions(object) {
  447. if (Object(object) !== object) {
  448. throw new TypeError('Object.preventExtensions can only be called on Objects.');
  449. }
  450. // this is misleading and breaks feature-detection, but
  451. // allows "securable" code to "gracefully" degrade to working
  452. // but insecure code.
  453. return object;
  454. };
  455. }
  456. // ES5 15.2.3.11
  457. // https://es5.github.io/#x15.2.3.11
  458. if (!Object.isSealed) {
  459. Object.isSealed = function isSealed(object) {
  460. if (Object(object) !== object) {
  461. throw new TypeError('Object.isSealed can only be called on Objects.');
  462. }
  463. return false;
  464. };
  465. }
  466. // ES5 15.2.3.12
  467. // https://es5.github.io/#x15.2.3.12
  468. if (!Object.isFrozen) {
  469. Object.isFrozen = function isFrozen(object) {
  470. if (Object(object) !== object) {
  471. throw new TypeError('Object.isFrozen can only be called on Objects.');
  472. }
  473. return false;
  474. };
  475. }
  476. // ES5 15.2.3.13
  477. // https://es5.github.io/#x15.2.3.13
  478. if (!Object.isExtensible) {
  479. Object.isExtensible = function isExtensible(object) {
  480. // 1. If Type(O) is not Object throw a TypeError exception.
  481. if (Object(object) !== object) {
  482. throw new TypeError('Object.isExtensible can only be called on Objects.');
  483. }
  484. // 2. Return the Boolean value of the [[Extensible]] internal property of O.
  485. var name = '';
  486. while (owns(object, name)) {
  487. name += '?';
  488. }
  489. object[name] = true; // eslint-disable-line no-param-reassign
  490. var returnValue = owns(object, name);
  491. delete object[name]; // eslint-disable-line no-param-reassign
  492. return returnValue;
  493. };
  494. }
  495. }));