es5-sham.js 16 KB

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