es5-sham.js 18 KB

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