es5-sham.js 22 KB

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