videojs-ie8.js 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600
  1. /**
  2. * HTML5 Element Shim for IE8
  3. *
  4. * **THIS CODE MUST BE LOADED IN THE <HEAD> OF THE DOCUMENT**
  5. *
  6. * Video.js uses the video tag as an embed code, even in IE8 which
  7. * doesn't have support for HTML5 video. The following code is needed
  8. * to make it possible to use the video tag. Otherwise IE8 ignores everything
  9. * inside the video tag.
  10. */
  11. if (typeof window.HTMLVideoElement === 'undefined') {
  12. document.createElement('video');
  13. document.createElement('audio');
  14. document.createElement('track');
  15. }
  16. /*!
  17. * https://github.com/es-shims/es5-shim
  18. * @license es5-shim Copyright 2009-2015 by contributors, MIT License
  19. * see https://github.com/es-shims/es5-shim/blob/master/LICENSE
  20. */
  21. // vim: ts=4 sts=4 sw=4 expandtab
  22. // Add semicolon to prevent IIFE from being passed as argument to concatenated code.
  23. ;
  24. // UMD (Universal Module Definition)
  25. // see https://github.com/umdjs/umd/blob/master/templates/returnExports.js
  26. (function (root, factory) {
  27. 'use strict';
  28. /* global define, exports, module */
  29. if (typeof define === 'function' && define.amd) {
  30. // AMD. Register as an anonymous module.
  31. define(factory);
  32. } else if (typeof exports === 'object') {
  33. // Node. Does not work with strict CommonJS, but
  34. // only CommonJS-like enviroments that support module.exports,
  35. // like Node.
  36. module.exports = factory();
  37. } else {
  38. // Browser globals (root is window)
  39. root.returnExports = factory();
  40. }
  41. }(this, function () {
  42. /**
  43. * Brings an environment as close to ECMAScript 5 compliance
  44. * as is possible with the facilities of erstwhile engines.
  45. *
  46. * Annotated ES5: http://es5.github.com/ (specific links below)
  47. * ES5 Spec: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
  48. * Required reading: http://javascriptweblog.wordpress.com/2011/12/05/extending-javascript-natives/
  49. */
  50. // Shortcut to an often accessed properties, in order to avoid multiple
  51. // dereference that costs universally. This also holds a reference to known-good
  52. // functions.
  53. var $Array = Array;
  54. var ArrayPrototype = $Array.prototype;
  55. var $Object = Object;
  56. var ObjectPrototype = $Object.prototype;
  57. var FunctionPrototype = Function.prototype;
  58. var $String = String;
  59. var StringPrototype = $String.prototype;
  60. var $Number = Number;
  61. var NumberPrototype = $Number.prototype;
  62. var array_slice = ArrayPrototype.slice;
  63. var array_splice = ArrayPrototype.splice;
  64. var array_push = ArrayPrototype.push;
  65. var array_unshift = ArrayPrototype.unshift;
  66. var array_concat = ArrayPrototype.concat;
  67. var call = FunctionPrototype.call;
  68. var apply = FunctionPrototype.apply;
  69. var max = Math.max;
  70. var min = Math.min;
  71. // Having a toString local variable name breaks in Opera so use to_string.
  72. var to_string = ObjectPrototype.toString;
  73. var hasToStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol';
  74. var isCallable; /* inlined from https://npmjs.com/is-callable */ var fnToStr = Function.prototype.toString, tryFunctionObject = function tryFunctionObject(value) { try { fnToStr.call(value); return true; } catch (e) { return false; } }, fnClass = '[object Function]', genClass = '[object GeneratorFunction]'; isCallable = function isCallable(value) { if (typeof value !== 'function') { return false; } if (hasToStringTag) { return tryFunctionObject(value); } var strClass = to_string.call(value); return strClass === fnClass || strClass === genClass; };
  75. var isRegex; /* inlined from https://npmjs.com/is-regex */ var regexExec = RegExp.prototype.exec, tryRegexExec = function tryRegexExec(value) { try { regexExec.call(value); return true; } catch (e) { return false; } }, regexClass = '[object RegExp]'; isRegex = function isRegex(value) { if (typeof value !== 'object') { return false; } return hasToStringTag ? tryRegexExec(value) : to_string.call(value) === regexClass; };
  76. var isString; /* inlined from https://npmjs.com/is-string */ var strValue = String.prototype.valueOf, tryStringObject = function tryStringObject(value) { try { strValue.call(value); return true; } catch (e) { return false; } }, stringClass = '[object String]'; isString = function isString(value) { if (typeof value === 'string') { return true; } if (typeof value !== 'object') { return false; } return hasToStringTag ? tryStringObject(value) : to_string.call(value) === stringClass; };
  77. /* inlined from http://npmjs.com/define-properties */
  78. var supportsDescriptors = $Object.defineProperty && (function () {
  79. try {
  80. var obj = {};
  81. $Object.defineProperty(obj, 'x', { enumerable: false, value: obj });
  82. for (var _ in obj) { return false; }
  83. return obj.x === obj;
  84. } catch (e) { /* this is ES3 */
  85. return false;
  86. }
  87. }());
  88. var defineProperties = (function (has) {
  89. // Define configurable, writable, and non-enumerable props
  90. // if they don't exist.
  91. var defineProperty;
  92. if (supportsDescriptors) {
  93. defineProperty = function (object, name, method, forceAssign) {
  94. if (!forceAssign && (name in object)) { return; }
  95. $Object.defineProperty(object, name, {
  96. configurable: true,
  97. enumerable: false,
  98. writable: true,
  99. value: method
  100. });
  101. };
  102. } else {
  103. defineProperty = function (object, name, method, forceAssign) {
  104. if (!forceAssign && (name in object)) { return; }
  105. object[name] = method;
  106. };
  107. }
  108. return function defineProperties(object, map, forceAssign) {
  109. for (var name in map) {
  110. if (has.call(map, name)) {
  111. defineProperty(object, name, map[name], forceAssign);
  112. }
  113. }
  114. };
  115. }(ObjectPrototype.hasOwnProperty));
  116. //
  117. // Util
  118. // ======
  119. //
  120. /* replaceable with https://npmjs.com/package/es-abstract /helpers/isPrimitive */
  121. var isPrimitive = function isPrimitive(input) {
  122. var type = typeof input;
  123. return input === null || (type !== 'object' && type !== 'function');
  124. };
  125. var isActualNaN = $Number.isNaN || function (x) { return x !== x; };
  126. var ES = {
  127. // ES5 9.4
  128. // http://es5.github.com/#x9.4
  129. // http://jsperf.com/to-integer
  130. /* replaceable with https://npmjs.com/package/es-abstract ES5.ToInteger */
  131. ToInteger: function ToInteger(num) {
  132. var n = +num;
  133. if (isActualNaN(n)) {
  134. n = 0;
  135. } else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) {
  136. n = (n > 0 || -1) * Math.floor(Math.abs(n));
  137. }
  138. return n;
  139. },
  140. /* replaceable with https://npmjs.com/package/es-abstract ES5.ToPrimitive */
  141. ToPrimitive: function ToPrimitive(input) {
  142. var val, valueOf, toStr;
  143. if (isPrimitive(input)) {
  144. return input;
  145. }
  146. valueOf = input.valueOf;
  147. if (isCallable(valueOf)) {
  148. val = valueOf.call(input);
  149. if (isPrimitive(val)) {
  150. return val;
  151. }
  152. }
  153. toStr = input.toString;
  154. if (isCallable(toStr)) {
  155. val = toStr.call(input);
  156. if (isPrimitive(val)) {
  157. return val;
  158. }
  159. }
  160. throw new TypeError();
  161. },
  162. // ES5 9.9
  163. // http://es5.github.com/#x9.9
  164. /* replaceable with https://npmjs.com/package/es-abstract ES5.ToObject */
  165. ToObject: function (o) {
  166. if (o == null) { // this matches both null and undefined
  167. throw new TypeError("can't convert " + o + ' to object');
  168. }
  169. return $Object(o);
  170. },
  171. /* replaceable with https://npmjs.com/package/es-abstract ES5.ToUint32 */
  172. ToUint32: function ToUint32(x) {
  173. return x >>> 0;
  174. }
  175. };
  176. //
  177. // Function
  178. // ========
  179. //
  180. // ES-5 15.3.4.5
  181. // http://es5.github.com/#x15.3.4.5
  182. var Empty = function Empty() {};
  183. defineProperties(FunctionPrototype, {
  184. bind: function bind(that) { // .length is 1
  185. // 1. Let Target be the this value.
  186. var target = this;
  187. // 2. If IsCallable(Target) is false, throw a TypeError exception.
  188. if (!isCallable(target)) {
  189. throw new TypeError('Function.prototype.bind called on incompatible ' + target);
  190. }
  191. // 3. Let A be a new (possibly empty) internal list of all of the
  192. // argument values provided after thisArg (arg1, arg2 etc), in order.
  193. // XXX slicedArgs will stand in for "A" if used
  194. var args = array_slice.call(arguments, 1); // for normal call
  195. // 4. Let F be a new native ECMAScript object.
  196. // 11. Set the [[Prototype]] internal property of F to the standard
  197. // built-in Function prototype object as specified in 15.3.3.1.
  198. // 12. Set the [[Call]] internal property of F as described in
  199. // 15.3.4.5.1.
  200. // 13. Set the [[Construct]] internal property of F as described in
  201. // 15.3.4.5.2.
  202. // 14. Set the [[HasInstance]] internal property of F as described in
  203. // 15.3.4.5.3.
  204. var bound;
  205. var binder = function () {
  206. if (this instanceof bound) {
  207. // 15.3.4.5.2 [[Construct]]
  208. // When the [[Construct]] internal method of a function object,
  209. // F that was created using the bind function is called with a
  210. // list of arguments ExtraArgs, the following steps are taken:
  211. // 1. Let target be the value of F's [[TargetFunction]]
  212. // internal property.
  213. // 2. If target has no [[Construct]] internal method, a
  214. // TypeError exception is thrown.
  215. // 3. Let boundArgs be the value of F's [[BoundArgs]] internal
  216. // property.
  217. // 4. Let args be a new list containing the same values as the
  218. // list boundArgs in the same order followed by the same
  219. // values as the list ExtraArgs in the same order.
  220. // 5. Return the result of calling the [[Construct]] internal
  221. // method of target providing args as the arguments.
  222. var result = target.apply(
  223. this,
  224. array_concat.call(args, array_slice.call(arguments))
  225. );
  226. if ($Object(result) === result) {
  227. return result;
  228. }
  229. return this;
  230. } else {
  231. // 15.3.4.5.1 [[Call]]
  232. // When the [[Call]] internal method of a function object, F,
  233. // which was created using the bind function is called with a
  234. // this value and a list of arguments ExtraArgs, the following
  235. // steps are taken:
  236. // 1. Let boundArgs be the value of F's [[BoundArgs]] internal
  237. // property.
  238. // 2. Let boundThis be the value of F's [[BoundThis]] internal
  239. // property.
  240. // 3. Let target be the value of F's [[TargetFunction]] internal
  241. // property.
  242. // 4. Let args be a new list containing the same values as the
  243. // list boundArgs in the same order followed by the same
  244. // values as the list ExtraArgs in the same order.
  245. // 5. Return the result of calling the [[Call]] internal method
  246. // of target providing boundThis as the this value and
  247. // providing args as the arguments.
  248. // equiv: target.call(this, ...boundArgs, ...args)
  249. return target.apply(
  250. that,
  251. array_concat.call(args, array_slice.call(arguments))
  252. );
  253. }
  254. };
  255. // 15. If the [[Class]] internal property of Target is "Function", then
  256. // a. Let L be the length property of Target minus the length of A.
  257. // b. Set the length own property of F to either 0 or L, whichever is
  258. // larger.
  259. // 16. Else set the length own property of F to 0.
  260. var boundLength = max(0, target.length - args.length);
  261. // 17. Set the attributes of the length own property of F to the values
  262. // specified in 15.3.5.1.
  263. var boundArgs = [];
  264. for (var i = 0; i < boundLength; i++) {
  265. array_push.call(boundArgs, '$' + i);
  266. }
  267. // XXX Build a dynamic function with desired amount of arguments is the only
  268. // way to set the length property of a function.
  269. // In environments where Content Security Policies enabled (Chrome extensions,
  270. // for ex.) all use of eval or Function costructor throws an exception.
  271. // However in all of these environments Function.prototype.bind exists
  272. // and so this code will never be executed.
  273. bound = Function('binder', 'return function (' + boundArgs.join(',') + '){ return binder.apply(this, arguments); }')(binder);
  274. if (target.prototype) {
  275. Empty.prototype = target.prototype;
  276. bound.prototype = new Empty();
  277. // Clean up dangling references.
  278. Empty.prototype = null;
  279. }
  280. // TODO
  281. // 18. Set the [[Extensible]] internal property of F to true.
  282. // TODO
  283. // 19. Let thrower be the [[ThrowTypeError]] function Object (13.2.3).
  284. // 20. Call the [[DefineOwnProperty]] internal method of F with
  285. // arguments "caller", PropertyDescriptor {[[Get]]: thrower, [[Set]]:
  286. // thrower, [[Enumerable]]: false, [[Configurable]]: false}, and
  287. // false.
  288. // 21. Call the [[DefineOwnProperty]] internal method of F with
  289. // arguments "arguments", PropertyDescriptor {[[Get]]: thrower,
  290. // [[Set]]: thrower, [[Enumerable]]: false, [[Configurable]]: false},
  291. // and false.
  292. // TODO
  293. // NOTE Function objects created using Function.prototype.bind do not
  294. // have a prototype property or the [[Code]], [[FormalParameters]], and
  295. // [[Scope]] internal properties.
  296. // XXX can't delete prototype in pure-js.
  297. // 22. Return F.
  298. return bound;
  299. }
  300. });
  301. // _Please note: Shortcuts are defined after `Function.prototype.bind` as we
  302. // use it in defining shortcuts.
  303. var owns = call.bind(ObjectPrototype.hasOwnProperty);
  304. var toStr = call.bind(ObjectPrototype.toString);
  305. var arraySlice = call.bind(array_slice);
  306. var arraySliceApply = apply.bind(array_slice);
  307. var strSlice = call.bind(StringPrototype.slice);
  308. var strSplit = call.bind(StringPrototype.split);
  309. var strIndexOf = call.bind(StringPrototype.indexOf);
  310. var pushCall = call.bind(array_push);
  311. var isEnum = call.bind(ObjectPrototype.propertyIsEnumerable);
  312. var arraySort = call.bind(ArrayPrototype.sort);
  313. //
  314. // Array
  315. // =====
  316. //
  317. var isArray = $Array.isArray || function isArray(obj) {
  318. return toStr(obj) === '[object Array]';
  319. };
  320. // ES5 15.4.4.12
  321. // http://es5.github.com/#x15.4.4.13
  322. // Return len+argCount.
  323. // [bugfix, ielt8]
  324. // IE < 8 bug: [].unshift(0) === undefined but should be "1"
  325. var hasUnshiftReturnValueBug = [].unshift(0) !== 1;
  326. defineProperties(ArrayPrototype, {
  327. unshift: function () {
  328. array_unshift.apply(this, arguments);
  329. return this.length;
  330. }
  331. }, hasUnshiftReturnValueBug);
  332. // ES5 15.4.3.2
  333. // http://es5.github.com/#x15.4.3.2
  334. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray
  335. defineProperties($Array, { isArray: isArray });
  336. // The IsCallable() check in the Array functions
  337. // has been replaced with a strict check on the
  338. // internal class of the object to trap cases where
  339. // the provided function was actually a regular
  340. // expression literal, which in V8 and
  341. // JavaScriptCore is a typeof "function". Only in
  342. // V8 are regular expression literals permitted as
  343. // reduce parameters, so it is desirable in the
  344. // general case for the shim to match the more
  345. // strict and common behavior of rejecting regular
  346. // expressions.
  347. // ES5 15.4.4.18
  348. // http://es5.github.com/#x15.4.4.18
  349. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/forEach
  350. // Check failure of by-index access of string characters (IE < 9)
  351. // and failure of `0 in boxedString` (Rhino)
  352. var boxedString = $Object('a');
  353. var splitString = boxedString[0] !== 'a' || !(0 in boxedString);
  354. var properlyBoxesContext = function properlyBoxed(method) {
  355. // Check node 0.6.21 bug where third parameter is not boxed
  356. var properlyBoxesNonStrict = true;
  357. var properlyBoxesStrict = true;
  358. var threwException = false;
  359. if (method) {
  360. try {
  361. method.call('foo', function (_, __, context) {
  362. if (typeof context !== 'object') { properlyBoxesNonStrict = false; }
  363. });
  364. method.call([1], function () {
  365. 'use strict';
  366. properlyBoxesStrict = typeof this === 'string';
  367. }, 'x');
  368. } catch (e) {
  369. threwException = true;
  370. }
  371. }
  372. return !!method && !threwException && properlyBoxesNonStrict && properlyBoxesStrict;
  373. };
  374. defineProperties(ArrayPrototype, {
  375. forEach: function forEach(callbackfn/*, thisArg*/) {
  376. var object = ES.ToObject(this);
  377. var self = splitString && isString(this) ? strSplit(this, '') : object;
  378. var i = -1;
  379. var length = ES.ToUint32(self.length);
  380. var T;
  381. if (arguments.length > 1) {
  382. T = arguments[1];
  383. }
  384. // If no callback function or if callback is not a callable function
  385. if (!isCallable(callbackfn)) {
  386. throw new TypeError('Array.prototype.forEach callback must be a function');
  387. }
  388. while (++i < length) {
  389. if (i in self) {
  390. // Invoke the callback function with call, passing arguments:
  391. // context, property value, property key, thisArg object
  392. if (typeof T === 'undefined') {
  393. callbackfn(self[i], i, object);
  394. } else {
  395. callbackfn.call(T, self[i], i, object);
  396. }
  397. }
  398. }
  399. }
  400. }, !properlyBoxesContext(ArrayPrototype.forEach));
  401. // ES5 15.4.4.19
  402. // http://es5.github.com/#x15.4.4.19
  403. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/map
  404. defineProperties(ArrayPrototype, {
  405. map: function map(callbackfn/*, thisArg*/) {
  406. var object = ES.ToObject(this);
  407. var self = splitString && isString(this) ? strSplit(this, '') : object;
  408. var length = ES.ToUint32(self.length);
  409. var result = $Array(length);
  410. var T;
  411. if (arguments.length > 1) {
  412. T = arguments[1];
  413. }
  414. // If no callback function or if callback is not a callable function
  415. if (!isCallable(callbackfn)) {
  416. throw new TypeError('Array.prototype.map callback must be a function');
  417. }
  418. for (var i = 0; i < length; i++) {
  419. if (i in self) {
  420. if (typeof T === 'undefined') {
  421. result[i] = callbackfn(self[i], i, object);
  422. } else {
  423. result[i] = callbackfn.call(T, self[i], i, object);
  424. }
  425. }
  426. }
  427. return result;
  428. }
  429. }, !properlyBoxesContext(ArrayPrototype.map));
  430. // ES5 15.4.4.20
  431. // http://es5.github.com/#x15.4.4.20
  432. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter
  433. defineProperties(ArrayPrototype, {
  434. filter: function filter(callbackfn/*, thisArg*/) {
  435. var object = ES.ToObject(this);
  436. var self = splitString && isString(this) ? strSplit(this, '') : object;
  437. var length = ES.ToUint32(self.length);
  438. var result = [];
  439. var value;
  440. var T;
  441. if (arguments.length > 1) {
  442. T = arguments[1];
  443. }
  444. // If no callback function or if callback is not a callable function
  445. if (!isCallable(callbackfn)) {
  446. throw new TypeError('Array.prototype.filter callback must be a function');
  447. }
  448. for (var i = 0; i < length; i++) {
  449. if (i in self) {
  450. value = self[i];
  451. if (typeof T === 'undefined' ? callbackfn(value, i, object) : callbackfn.call(T, value, i, object)) {
  452. pushCall(result, value);
  453. }
  454. }
  455. }
  456. return result;
  457. }
  458. }, !properlyBoxesContext(ArrayPrototype.filter));
  459. // ES5 15.4.4.16
  460. // http://es5.github.com/#x15.4.4.16
  461. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every
  462. defineProperties(ArrayPrototype, {
  463. every: function every(callbackfn/*, thisArg*/) {
  464. var object = ES.ToObject(this);
  465. var self = splitString && isString(this) ? strSplit(this, '') : object;
  466. var length = ES.ToUint32(self.length);
  467. var T;
  468. if (arguments.length > 1) {
  469. T = arguments[1];
  470. }
  471. // If no callback function or if callback is not a callable function
  472. if (!isCallable(callbackfn)) {
  473. throw new TypeError('Array.prototype.every callback must be a function');
  474. }
  475. for (var i = 0; i < length; i++) {
  476. if (i in self && !(typeof T === 'undefined' ? callbackfn(self[i], i, object) : callbackfn.call(T, self[i], i, object))) {
  477. return false;
  478. }
  479. }
  480. return true;
  481. }
  482. }, !properlyBoxesContext(ArrayPrototype.every));
  483. // ES5 15.4.4.17
  484. // http://es5.github.com/#x15.4.4.17
  485. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
  486. defineProperties(ArrayPrototype, {
  487. some: function some(callbackfn/*, thisArg */) {
  488. var object = ES.ToObject(this);
  489. var self = splitString && isString(this) ? strSplit(this, '') : object;
  490. var length = ES.ToUint32(self.length);
  491. var T;
  492. if (arguments.length > 1) {
  493. T = arguments[1];
  494. }
  495. // If no callback function or if callback is not a callable function
  496. if (!isCallable(callbackfn)) {
  497. throw new TypeError('Array.prototype.some callback must be a function');
  498. }
  499. for (var i = 0; i < length; i++) {
  500. if (i in self && (typeof T === 'undefined' ? callbackfn(self[i], i, object) : callbackfn.call(T, self[i], i, object))) {
  501. return true;
  502. }
  503. }
  504. return false;
  505. }
  506. }, !properlyBoxesContext(ArrayPrototype.some));
  507. // ES5 15.4.4.21
  508. // http://es5.github.com/#x15.4.4.21
  509. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduce
  510. var reduceCoercesToObject = false;
  511. if (ArrayPrototype.reduce) {
  512. reduceCoercesToObject = typeof ArrayPrototype.reduce.call('es5', function (_, __, ___, list) { return list; }) === 'object';
  513. }
  514. defineProperties(ArrayPrototype, {
  515. reduce: function reduce(callbackfn/*, initialValue*/) {
  516. var object = ES.ToObject(this);
  517. var self = splitString && isString(this) ? strSplit(this, '') : object;
  518. var length = ES.ToUint32(self.length);
  519. // If no callback function or if callback is not a callable function
  520. if (!isCallable(callbackfn)) {
  521. throw new TypeError('Array.prototype.reduce callback must be a function');
  522. }
  523. // no value to return if no initial value and an empty array
  524. if (length === 0 && arguments.length === 1) {
  525. throw new TypeError('reduce of empty array with no initial value');
  526. }
  527. var i = 0;
  528. var result;
  529. if (arguments.length >= 2) {
  530. result = arguments[1];
  531. } else {
  532. do {
  533. if (i in self) {
  534. result = self[i++];
  535. break;
  536. }
  537. // if array contains no values, no initial value to return
  538. if (++i >= length) {
  539. throw new TypeError('reduce of empty array with no initial value');
  540. }
  541. } while (true);
  542. }
  543. for (; i < length; i++) {
  544. if (i in self) {
  545. result = callbackfn(result, self[i], i, object);
  546. }
  547. }
  548. return result;
  549. }
  550. }, !reduceCoercesToObject);
  551. // ES5 15.4.4.22
  552. // http://es5.github.com/#x15.4.4.22
  553. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduceRight
  554. var reduceRightCoercesToObject = false;
  555. if (ArrayPrototype.reduceRight) {
  556. reduceRightCoercesToObject = typeof ArrayPrototype.reduceRight.call('es5', function (_, __, ___, list) { return list; }) === 'object';
  557. }
  558. defineProperties(ArrayPrototype, {
  559. reduceRight: function reduceRight(callbackfn/*, initial*/) {
  560. var object = ES.ToObject(this);
  561. var self = splitString && isString(this) ? strSplit(this, '') : object;
  562. var length = ES.ToUint32(self.length);
  563. // If no callback function or if callback is not a callable function
  564. if (!isCallable(callbackfn)) {
  565. throw new TypeError('Array.prototype.reduceRight callback must be a function');
  566. }
  567. // no value to return if no initial value, empty array
  568. if (length === 0 && arguments.length === 1) {
  569. throw new TypeError('reduceRight of empty array with no initial value');
  570. }
  571. var result;
  572. var i = length - 1;
  573. if (arguments.length >= 2) {
  574. result = arguments[1];
  575. } else {
  576. do {
  577. if (i in self) {
  578. result = self[i--];
  579. break;
  580. }
  581. // if array contains no values, no initial value to return
  582. if (--i < 0) {
  583. throw new TypeError('reduceRight of empty array with no initial value');
  584. }
  585. } while (true);
  586. }
  587. if (i < 0) {
  588. return result;
  589. }
  590. do {
  591. if (i in self) {
  592. result = callbackfn(result, self[i], i, object);
  593. }
  594. } while (i--);
  595. return result;
  596. }
  597. }, !reduceRightCoercesToObject);
  598. // ES5 15.4.4.14
  599. // http://es5.github.com/#x15.4.4.14
  600. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
  601. var hasFirefox2IndexOfBug = ArrayPrototype.indexOf && [0, 1].indexOf(1, 2) !== -1;
  602. defineProperties(ArrayPrototype, {
  603. indexOf: function indexOf(searchElement/*, fromIndex */) {
  604. var self = splitString && isString(this) ? strSplit(this, '') : ES.ToObject(this);
  605. var length = ES.ToUint32(self.length);
  606. if (length === 0) {
  607. return -1;
  608. }
  609. var i = 0;
  610. if (arguments.length > 1) {
  611. i = ES.ToInteger(arguments[1]);
  612. }
  613. // handle negative indices
  614. i = i >= 0 ? i : max(0, length + i);
  615. for (; i < length; i++) {
  616. if (i in self && self[i] === searchElement) {
  617. return i;
  618. }
  619. }
  620. return -1;
  621. }
  622. }, hasFirefox2IndexOfBug);
  623. // ES5 15.4.4.15
  624. // http://es5.github.com/#x15.4.4.15
  625. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/lastIndexOf
  626. var hasFirefox2LastIndexOfBug = ArrayPrototype.lastIndexOf && [0, 1].lastIndexOf(0, -3) !== -1;
  627. defineProperties(ArrayPrototype, {
  628. lastIndexOf: function lastIndexOf(searchElement/*, fromIndex */) {
  629. var self = splitString && isString(this) ? strSplit(this, '') : ES.ToObject(this);
  630. var length = ES.ToUint32(self.length);
  631. if (length === 0) {
  632. return -1;
  633. }
  634. var i = length - 1;
  635. if (arguments.length > 1) {
  636. i = min(i, ES.ToInteger(arguments[1]));
  637. }
  638. // handle negative indices
  639. i = i >= 0 ? i : length - Math.abs(i);
  640. for (; i >= 0; i--) {
  641. if (i in self && searchElement === self[i]) {
  642. return i;
  643. }
  644. }
  645. return -1;
  646. }
  647. }, hasFirefox2LastIndexOfBug);
  648. // ES5 15.4.4.12
  649. // http://es5.github.com/#x15.4.4.12
  650. var spliceNoopReturnsEmptyArray = (function () {
  651. var a = [1, 2];
  652. var result = a.splice();
  653. return a.length === 2 && isArray(result) && result.length === 0;
  654. }());
  655. defineProperties(ArrayPrototype, {
  656. // Safari 5.0 bug where .splice() returns undefined
  657. splice: function splice(start, deleteCount) {
  658. if (arguments.length === 0) {
  659. return [];
  660. } else {
  661. return array_splice.apply(this, arguments);
  662. }
  663. }
  664. }, !spliceNoopReturnsEmptyArray);
  665. var spliceWorksWithEmptyObject = (function () {
  666. var obj = {};
  667. ArrayPrototype.splice.call(obj, 0, 0, 1);
  668. return obj.length === 1;
  669. }());
  670. defineProperties(ArrayPrototype, {
  671. splice: function splice(start, deleteCount) {
  672. if (arguments.length === 0) { return []; }
  673. var args = arguments;
  674. this.length = max(ES.ToInteger(this.length), 0);
  675. if (arguments.length > 0 && typeof deleteCount !== 'number') {
  676. args = arraySlice(arguments);
  677. if (args.length < 2) {
  678. pushCall(args, this.length - start);
  679. } else {
  680. args[1] = ES.ToInteger(deleteCount);
  681. }
  682. }
  683. return array_splice.apply(this, args);
  684. }
  685. }, !spliceWorksWithEmptyObject);
  686. var spliceWorksWithLargeSparseArrays = (function () {
  687. // Per https://github.com/es-shims/es5-shim/issues/295
  688. // Safari 7/8 breaks with sparse arrays of size 1e5 or greater
  689. var arr = new $Array(1e5);
  690. // note: the index MUST be 8 or larger or the test will false pass
  691. arr[8] = 'x';
  692. arr.splice(1, 1);
  693. // note: this test must be defined *after* the indexOf shim
  694. // per https://github.com/es-shims/es5-shim/issues/313
  695. return arr.indexOf('x') === 7;
  696. }());
  697. var spliceWorksWithSmallSparseArrays = (function () {
  698. // Per https://github.com/es-shims/es5-shim/issues/295
  699. // Opera 12.15 breaks on this, no idea why.
  700. var n = 256;
  701. var arr = [];
  702. arr[n] = 'a';
  703. arr.splice(n + 1, 0, 'b');
  704. return arr[n] === 'a';
  705. }());
  706. defineProperties(ArrayPrototype, {
  707. splice: function splice(start, deleteCount) {
  708. var O = ES.ToObject(this);
  709. var A = [];
  710. var len = ES.ToUint32(O.length);
  711. var relativeStart = ES.ToInteger(start);
  712. var actualStart = relativeStart < 0 ? max((len + relativeStart), 0) : min(relativeStart, len);
  713. var actualDeleteCount = min(max(ES.ToInteger(deleteCount), 0), len - actualStart);
  714. var k = 0;
  715. var from;
  716. while (k < actualDeleteCount) {
  717. from = $String(actualStart + k);
  718. if (owns(O, from)) {
  719. A[k] = O[from];
  720. }
  721. k += 1;
  722. }
  723. var items = arraySlice(arguments, 2);
  724. var itemCount = items.length;
  725. var to;
  726. if (itemCount < actualDeleteCount) {
  727. k = actualStart;
  728. while (k < (len - actualDeleteCount)) {
  729. from = $String(k + actualDeleteCount);
  730. to = $String(k + itemCount);
  731. if (owns(O, from)) {
  732. O[to] = O[from];
  733. } else {
  734. delete O[to];
  735. }
  736. k += 1;
  737. }
  738. k = len;
  739. while (k > (len - actualDeleteCount + itemCount)) {
  740. delete O[k - 1];
  741. k -= 1;
  742. }
  743. } else if (itemCount > actualDeleteCount) {
  744. k = len - actualDeleteCount;
  745. while (k > actualStart) {
  746. from = $String(k + actualDeleteCount - 1);
  747. to = $String(k + itemCount - 1);
  748. if (owns(O, from)) {
  749. O[to] = O[from];
  750. } else {
  751. delete O[to];
  752. }
  753. k -= 1;
  754. }
  755. }
  756. k = actualStart;
  757. for (var i = 0; i < items.length; ++i) {
  758. O[k] = items[i];
  759. k += 1;
  760. }
  761. O.length = len - actualDeleteCount + itemCount;
  762. return A;
  763. }
  764. }, !spliceWorksWithLargeSparseArrays || !spliceWorksWithSmallSparseArrays);
  765. var originalJoin = ArrayPrototype.join;
  766. var hasStringJoinBug;
  767. try {
  768. hasStringJoinBug = Array.prototype.join.call('123', ',') !== '1,2,3';
  769. } catch (e) {
  770. hasStringJoinBug = true;
  771. }
  772. if (hasStringJoinBug) {
  773. defineProperties(ArrayPrototype, {
  774. join: function join(separator) {
  775. var sep = typeof separator === 'undefined' ? ',' : separator;
  776. return originalJoin.call(isString(this) ? strSplit(this, '') : this, sep);
  777. }
  778. }, hasStringJoinBug);
  779. }
  780. var hasJoinUndefinedBug = [1, 2].join(undefined) !== '1,2';
  781. if (hasJoinUndefinedBug) {
  782. defineProperties(ArrayPrototype, {
  783. join: function join(separator) {
  784. var sep = typeof separator === 'undefined' ? ',' : separator;
  785. return originalJoin.call(this, sep);
  786. }
  787. }, hasJoinUndefinedBug);
  788. }
  789. var pushShim = function push(item) {
  790. var O = ES.ToObject(this);
  791. var n = ES.ToUint32(O.length);
  792. var i = 0;
  793. while (i < arguments.length) {
  794. O[n + i] = arguments[i];
  795. i += 1;
  796. }
  797. O.length = n + i;
  798. return n + i;
  799. };
  800. var pushIsNotGeneric = (function () {
  801. var obj = {};
  802. var result = Array.prototype.push.call(obj, undefined);
  803. return result !== 1 || obj.length !== 1 || typeof obj[0] !== 'undefined' || !owns(obj, 0);
  804. }());
  805. defineProperties(ArrayPrototype, {
  806. push: function push(item) {
  807. if (isArray(this)) {
  808. return array_push.apply(this, arguments);
  809. }
  810. return pushShim.apply(this, arguments);
  811. }
  812. }, pushIsNotGeneric);
  813. // This fixes a very weird bug in Opera 10.6 when pushing `undefined
  814. var pushUndefinedIsWeird = (function () {
  815. var arr = [];
  816. var result = arr.push(undefined);
  817. return result !== 1 || arr.length !== 1 || typeof arr[0] !== 'undefined' || !owns(arr, 0);
  818. }());
  819. defineProperties(ArrayPrototype, { push: pushShim }, pushUndefinedIsWeird);
  820. // ES5 15.2.3.14
  821. // http://es5.github.io/#x15.4.4.10
  822. // Fix boxed string bug
  823. defineProperties(ArrayPrototype, {
  824. slice: function (start, end) {
  825. var arr = isString(this) ? strSplit(this, '') : this;
  826. return arraySliceApply(arr, arguments);
  827. }
  828. }, splitString);
  829. var sortIgnoresNonFunctions = (function () {
  830. try {
  831. [1, 2].sort(null);
  832. [1, 2].sort({});
  833. return true;
  834. } catch (e) { /**/ }
  835. return false;
  836. }());
  837. var sortThrowsOnRegex = (function () {
  838. // this is a problem in Firefox 4, in which `typeof /a/ === 'function'`
  839. try {
  840. [1, 2].sort(/a/);
  841. return false;
  842. } catch (e) { /**/ }
  843. return true;
  844. }());
  845. var sortIgnoresUndefined = (function () {
  846. // applies in IE 8, for one.
  847. try {
  848. [1, 2].sort(undefined);
  849. return true;
  850. } catch (e) { /**/ }
  851. return false;
  852. }());
  853. defineProperties(ArrayPrototype, {
  854. sort: function sort(compareFn) {
  855. if (typeof compareFn === 'undefined') {
  856. return arraySort(this);
  857. }
  858. if (!isCallable(compareFn)) {
  859. throw new TypeError('Array.prototype.sort callback must be a function');
  860. }
  861. return arraySort(this, compareFn);
  862. }
  863. }, sortIgnoresNonFunctions || !sortIgnoresUndefined || !sortThrowsOnRegex);
  864. //
  865. // Object
  866. // ======
  867. //
  868. // ES5 15.2.3.14
  869. // http://es5.github.com/#x15.2.3.14
  870. // http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation
  871. var hasDontEnumBug = !({ 'toString': null }).propertyIsEnumerable('toString');
  872. var hasProtoEnumBug = function () {}.propertyIsEnumerable('prototype');
  873. var hasStringEnumBug = !owns('x', '0');
  874. var equalsConstructorPrototype = function (o) {
  875. var ctor = o.constructor;
  876. return ctor && ctor.prototype === o;
  877. };
  878. var blacklistedKeys = {
  879. $window: true,
  880. $console: true,
  881. $parent: true,
  882. $self: true,
  883. $frame: true,
  884. $frames: true,
  885. $frameElement: true,
  886. $webkitIndexedDB: true,
  887. $webkitStorageInfo: true,
  888. $external: true
  889. };
  890. var hasAutomationEqualityBug = (function () {
  891. /* globals window */
  892. if (typeof window === 'undefined') { return false; }
  893. for (var k in window) {
  894. try {
  895. if (!blacklistedKeys['$' + k] && owns(window, k) && window[k] !== null && typeof window[k] === 'object') {
  896. equalsConstructorPrototype(window[k]);
  897. }
  898. } catch (e) {
  899. return true;
  900. }
  901. }
  902. return false;
  903. }());
  904. var equalsConstructorPrototypeIfNotBuggy = function (object) {
  905. if (typeof window === 'undefined' || !hasAutomationEqualityBug) { return equalsConstructorPrototype(object); }
  906. try {
  907. return equalsConstructorPrototype(object);
  908. } catch (e) {
  909. return false;
  910. }
  911. };
  912. var dontEnums = [
  913. 'toString',
  914. 'toLocaleString',
  915. 'valueOf',
  916. 'hasOwnProperty',
  917. 'isPrototypeOf',
  918. 'propertyIsEnumerable',
  919. 'constructor'
  920. ];
  921. var dontEnumsLength = dontEnums.length;
  922. // taken directly from https://github.com/ljharb/is-arguments/blob/master/index.js
  923. // can be replaced with require('is-arguments') if we ever use a build process instead
  924. var isStandardArguments = function isArguments(value) {
  925. return toStr(value) === '[object Arguments]';
  926. };
  927. var isLegacyArguments = function isArguments(value) {
  928. return value !== null &&
  929. typeof value === 'object' &&
  930. typeof value.length === 'number' &&
  931. value.length >= 0 &&
  932. !isArray(value) &&
  933. isCallable(value.callee);
  934. };
  935. var isArguments = isStandardArguments(arguments) ? isStandardArguments : isLegacyArguments;
  936. defineProperties($Object, {
  937. keys: function keys(object) {
  938. var isFn = isCallable(object);
  939. var isArgs = isArguments(object);
  940. var isObject = object !== null && typeof object === 'object';
  941. var isStr = isObject && isString(object);
  942. if (!isObject && !isFn && !isArgs) {
  943. throw new TypeError('Object.keys called on a non-object');
  944. }
  945. var theKeys = [];
  946. var skipProto = hasProtoEnumBug && isFn;
  947. if ((isStr && hasStringEnumBug) || isArgs) {
  948. for (var i = 0; i < object.length; ++i) {
  949. pushCall(theKeys, $String(i));
  950. }
  951. }
  952. if (!isArgs) {
  953. for (var name in object) {
  954. if (!(skipProto && name === 'prototype') && owns(object, name)) {
  955. pushCall(theKeys, $String(name));
  956. }
  957. }
  958. }
  959. if (hasDontEnumBug) {
  960. var skipConstructor = equalsConstructorPrototypeIfNotBuggy(object);
  961. for (var j = 0; j < dontEnumsLength; j++) {
  962. var dontEnum = dontEnums[j];
  963. if (!(skipConstructor && dontEnum === 'constructor') && owns(object, dontEnum)) {
  964. pushCall(theKeys, dontEnum);
  965. }
  966. }
  967. }
  968. return theKeys;
  969. }
  970. });
  971. var keysWorksWithArguments = $Object.keys && (function () {
  972. // Safari 5.0 bug
  973. return $Object.keys(arguments).length === 2;
  974. }(1, 2));
  975. var keysHasArgumentsLengthBug = $Object.keys && (function () {
  976. var argKeys = $Object.keys(arguments);
  977. return arguments.length !== 1 || argKeys.length !== 1 || argKeys[0] !== 1;
  978. }(1));
  979. var originalKeys = $Object.keys;
  980. defineProperties($Object, {
  981. keys: function keys(object) {
  982. if (isArguments(object)) {
  983. return originalKeys(arraySlice(object));
  984. } else {
  985. return originalKeys(object);
  986. }
  987. }
  988. }, !keysWorksWithArguments || keysHasArgumentsLengthBug);
  989. //
  990. // Date
  991. // ====
  992. //
  993. var hasNegativeMonthYearBug = new Date(-3509827329600292).getUTCMonth() !== 0;
  994. var aNegativeTestDate = new Date(-1509842289600292);
  995. var aPositiveTestDate = new Date(1449662400000);
  996. var hasToUTCStringFormatBug = aNegativeTestDate.toUTCString() !== 'Mon, 01 Jan -45875 11:59:59 GMT';
  997. var hasToDateStringFormatBug;
  998. var hasToStringFormatBug;
  999. var timeZoneOffset = aNegativeTestDate.getTimezoneOffset();
  1000. if (timeZoneOffset < -720) {
  1001. hasToDateStringFormatBug = aNegativeTestDate.toDateString() !== 'Tue Jan 02 -45875';
  1002. hasToStringFormatBug = !(/^Thu Dec 10 2015 \d\d:\d\d:\d\d GMT[-\+]\d\d\d\d(?: |$)/).test(aPositiveTestDate.toString());
  1003. } else {
  1004. hasToDateStringFormatBug = aNegativeTestDate.toDateString() !== 'Mon Jan 01 -45875';
  1005. hasToStringFormatBug = !(/^Wed Dec 09 2015 \d\d:\d\d:\d\d GMT[-\+]\d\d\d\d(?: |$)/).test(aPositiveTestDate.toString());
  1006. }
  1007. var originalGetFullYear = call.bind(Date.prototype.getFullYear);
  1008. var originalGetMonth = call.bind(Date.prototype.getMonth);
  1009. var originalGetDate = call.bind(Date.prototype.getDate);
  1010. var originalGetUTCFullYear = call.bind(Date.prototype.getUTCFullYear);
  1011. var originalGetUTCMonth = call.bind(Date.prototype.getUTCMonth);
  1012. var originalGetUTCDate = call.bind(Date.prototype.getUTCDate);
  1013. var originalGetUTCDay = call.bind(Date.prototype.getUTCDay);
  1014. var originalGetUTCHours = call.bind(Date.prototype.getUTCHours);
  1015. var originalGetUTCMinutes = call.bind(Date.prototype.getUTCMinutes);
  1016. var originalGetUTCSeconds = call.bind(Date.prototype.getUTCSeconds);
  1017. var originalGetUTCMilliseconds = call.bind(Date.prototype.getUTCMilliseconds);
  1018. var dayName = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri'];
  1019. var monthName = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  1020. var daysInMonth = function daysInMonth(month, year) {
  1021. return originalGetDate(new Date(year, month, 0));
  1022. };
  1023. defineProperties(Date.prototype, {
  1024. getFullYear: function getFullYear() {
  1025. if (!this || !(this instanceof Date)) {
  1026. throw new TypeError('this is not a Date object.');
  1027. }
  1028. var year = originalGetFullYear(this);
  1029. if (year < 0 && originalGetMonth(this) > 11) {
  1030. return year + 1;
  1031. }
  1032. return year;
  1033. },
  1034. getMonth: function getMonth() {
  1035. if (!this || !(this instanceof Date)) {
  1036. throw new TypeError('this is not a Date object.');
  1037. }
  1038. var year = originalGetFullYear(this);
  1039. var month = originalGetMonth(this);
  1040. if (year < 0 && month > 11) {
  1041. return 0;
  1042. }
  1043. return month;
  1044. },
  1045. getDate: function getDate() {
  1046. if (!this || !(this instanceof Date)) {
  1047. throw new TypeError('this is not a Date object.');
  1048. }
  1049. var year = originalGetFullYear(this);
  1050. var month = originalGetMonth(this);
  1051. var date = originalGetDate(this);
  1052. if (year < 0 && month > 11) {
  1053. if (month === 12) {
  1054. return date;
  1055. }
  1056. var days = daysInMonth(0, year + 1);
  1057. return (days - date) + 1;
  1058. }
  1059. return date;
  1060. },
  1061. getUTCFullYear: function getUTCFullYear() {
  1062. if (!this || !(this instanceof Date)) {
  1063. throw new TypeError('this is not a Date object.');
  1064. }
  1065. var year = originalGetUTCFullYear(this);
  1066. if (year < 0 && originalGetUTCMonth(this) > 11) {
  1067. return year + 1;
  1068. }
  1069. return year;
  1070. },
  1071. getUTCMonth: function getUTCMonth() {
  1072. if (!this || !(this instanceof Date)) {
  1073. throw new TypeError('this is not a Date object.');
  1074. }
  1075. var year = originalGetUTCFullYear(this);
  1076. var month = originalGetUTCMonth(this);
  1077. if (year < 0 && month > 11) {
  1078. return 0;
  1079. }
  1080. return month;
  1081. },
  1082. getUTCDate: function getUTCDate() {
  1083. if (!this || !(this instanceof Date)) {
  1084. throw new TypeError('this is not a Date object.');
  1085. }
  1086. var year = originalGetUTCFullYear(this);
  1087. var month = originalGetUTCMonth(this);
  1088. var date = originalGetUTCDate(this);
  1089. if (year < 0 && month > 11) {
  1090. if (month === 12) {
  1091. return date;
  1092. }
  1093. var days = daysInMonth(0, year + 1);
  1094. return (days - date) + 1;
  1095. }
  1096. return date;
  1097. }
  1098. }, hasNegativeMonthYearBug);
  1099. defineProperties(Date.prototype, {
  1100. toUTCString: function toUTCString() {
  1101. if (!this || !(this instanceof Date)) {
  1102. throw new TypeError('this is not a Date object.');
  1103. }
  1104. var day = originalGetUTCDay(this);
  1105. var date = originalGetUTCDate(this);
  1106. var month = originalGetUTCMonth(this);
  1107. var year = originalGetUTCFullYear(this);
  1108. var hour = originalGetUTCHours(this);
  1109. var minute = originalGetUTCMinutes(this);
  1110. var second = originalGetUTCSeconds(this);
  1111. return dayName[day] + ', ' +
  1112. (date < 10 ? '0' + date : date) + ' ' +
  1113. monthName[month] + ' ' +
  1114. year + ' ' +
  1115. (hour < 10 ? '0' + hour : hour) + ':' +
  1116. (minute < 10 ? '0' + minute : minute) + ':' +
  1117. (second < 10 ? '0' + second : second) + ' GMT';
  1118. }
  1119. }, hasNegativeMonthYearBug || hasToUTCStringFormatBug);
  1120. // Opera 12 has `,`
  1121. defineProperties(Date.prototype, {
  1122. toDateString: function toDateString() {
  1123. if (!this || !(this instanceof Date)) {
  1124. throw new TypeError('this is not a Date object.');
  1125. }
  1126. var day = this.getDay();
  1127. var date = this.getDate();
  1128. var month = this.getMonth();
  1129. var year = this.getFullYear();
  1130. return dayName[day] + ' ' +
  1131. monthName[month] + ' ' +
  1132. (date < 10 ? '0' + date : date) + ' ' +
  1133. year;
  1134. }
  1135. }, hasNegativeMonthYearBug || hasToDateStringFormatBug);
  1136. // can't use defineProperties here because of toString enumeration issue in IE <= 8
  1137. if (hasNegativeMonthYearBug || hasToStringFormatBug) {
  1138. Date.prototype.toString = function toString() {
  1139. if (!this || !(this instanceof Date)) {
  1140. throw new TypeError('this is not a Date object.');
  1141. }
  1142. var day = this.getDay();
  1143. var date = this.getDate();
  1144. var month = this.getMonth();
  1145. var year = this.getFullYear();
  1146. var hour = this.getHours();
  1147. var minute = this.getMinutes();
  1148. var second = this.getSeconds();
  1149. var timezoneOffset = this.getTimezoneOffset();
  1150. var hoursOffset = Math.floor(Math.abs(timezoneOffset) / 60);
  1151. var minutesOffset = Math.floor(Math.abs(timezoneOffset) % 60);
  1152. return dayName[day] + ' ' +
  1153. monthName[month] + ' ' +
  1154. (date < 10 ? '0' + date : date) + ' ' +
  1155. year + ' ' +
  1156. (hour < 10 ? '0' + hour : hour) + ':' +
  1157. (minute < 10 ? '0' + minute : minute) + ':' +
  1158. (second < 10 ? '0' + second : second) + ' GMT' +
  1159. (timezoneOffset > 0 ? '-' : '+') +
  1160. (hoursOffset < 10 ? '0' + hoursOffset : hoursOffset) +
  1161. (minutesOffset < 10 ? '0' + minutesOffset : minutesOffset);
  1162. };
  1163. if (supportsDescriptors) {
  1164. $Object.defineProperty(Date.prototype, 'toString', {
  1165. configurable: true,
  1166. enumerable: false,
  1167. writable: true
  1168. });
  1169. }
  1170. }
  1171. // ES5 15.9.5.43
  1172. // http://es5.github.com/#x15.9.5.43
  1173. // This function returns a String value represent the instance in time
  1174. // represented by this Date object. The format of the String is the Date Time
  1175. // string format defined in 15.9.1.15. All fields are present in the String.
  1176. // The time zone is always UTC, denoted by the suffix Z. If the time value of
  1177. // this object is not a finite Number a RangeError exception is thrown.
  1178. var negativeDate = -62198755200000;
  1179. var negativeYearString = '-000001';
  1180. var hasNegativeDateBug = Date.prototype.toISOString && new Date(negativeDate).toISOString().indexOf(negativeYearString) === -1;
  1181. var hasSafari51DateBug = Date.prototype.toISOString && new Date(-1).toISOString() !== '1969-12-31T23:59:59.999Z';
  1182. defineProperties(Date.prototype, {
  1183. toISOString: function toISOString() {
  1184. if (!isFinite(this)) {
  1185. throw new RangeError('Date.prototype.toISOString called on non-finite value.');
  1186. }
  1187. var year = originalGetUTCFullYear(this);
  1188. var month = originalGetUTCMonth(this);
  1189. // see https://github.com/es-shims/es5-shim/issues/111
  1190. year += Math.floor(month / 12);
  1191. month = (month % 12 + 12) % 12;
  1192. // the date time string format is specified in 15.9.1.15.
  1193. var result = [month + 1, originalGetUTCDate(this), originalGetUTCHours(this), originalGetUTCMinutes(this), originalGetUTCSeconds(this)];
  1194. year = (
  1195. (year < 0 ? '-' : (year > 9999 ? '+' : '')) +
  1196. strSlice('00000' + Math.abs(year), (0 <= year && year <= 9999) ? -4 : -6)
  1197. );
  1198. for (var i = 0; i < result.length; ++i) {
  1199. // pad months, days, hours, minutes, and seconds to have two digits.
  1200. result[i] = strSlice('00' + result[i], -2);
  1201. }
  1202. // pad milliseconds to have three digits.
  1203. return (
  1204. year + '-' + arraySlice(result, 0, 2).join('-') +
  1205. 'T' + arraySlice(result, 2).join(':') + '.' +
  1206. strSlice('000' + originalGetUTCMilliseconds(this), -3) + 'Z'
  1207. );
  1208. }
  1209. }, hasNegativeDateBug || hasSafari51DateBug);
  1210. // ES5 15.9.5.44
  1211. // http://es5.github.com/#x15.9.5.44
  1212. // This function provides a String representation of a Date object for use by
  1213. // JSON.stringify (15.12.3).
  1214. var dateToJSONIsSupported = (function () {
  1215. try {
  1216. return Date.prototype.toJSON &&
  1217. new Date(NaN).toJSON() === null &&
  1218. new Date(negativeDate).toJSON().indexOf(negativeYearString) !== -1 &&
  1219. Date.prototype.toJSON.call({ // generic
  1220. toISOString: function () { return true; }
  1221. });
  1222. } catch (e) {
  1223. return false;
  1224. }
  1225. }());
  1226. if (!dateToJSONIsSupported) {
  1227. Date.prototype.toJSON = function toJSON(key) {
  1228. // When the toJSON method is called with argument key, the following
  1229. // steps are taken:
  1230. // 1. Let O be the result of calling ToObject, giving it the this
  1231. // value as its argument.
  1232. // 2. Let tv be ES.ToPrimitive(O, hint Number).
  1233. var O = $Object(this);
  1234. var tv = ES.ToPrimitive(O);
  1235. // 3. If tv is a Number and is not finite, return null.
  1236. if (typeof tv === 'number' && !isFinite(tv)) {
  1237. return null;
  1238. }
  1239. // 4. Let toISO be the result of calling the [[Get]] internal method of
  1240. // O with argument "toISOString".
  1241. var toISO = O.toISOString;
  1242. // 5. If IsCallable(toISO) is false, throw a TypeError exception.
  1243. if (!isCallable(toISO)) {
  1244. throw new TypeError('toISOString property is not callable');
  1245. }
  1246. // 6. Return the result of calling the [[Call]] internal method of
  1247. // toISO with O as the this value and an empty argument list.
  1248. return toISO.call(O);
  1249. // NOTE 1 The argument is ignored.
  1250. // NOTE 2 The toJSON function is intentionally generic; it does not
  1251. // require that its this value be a Date object. Therefore, it can be
  1252. // transferred to other kinds of objects for use as a method. However,
  1253. // it does require that any such object have a toISOString method. An
  1254. // object is free to use the argument key to filter its
  1255. // stringification.
  1256. };
  1257. }
  1258. // ES5 15.9.4.2
  1259. // http://es5.github.com/#x15.9.4.2
  1260. // based on work shared by Daniel Friesen (dantman)
  1261. // http://gist.github.com/303249
  1262. var supportsExtendedYears = Date.parse('+033658-09-27T01:46:40.000Z') === 1e15;
  1263. var acceptsInvalidDates = !isNaN(Date.parse('2012-04-04T24:00:00.500Z')) || !isNaN(Date.parse('2012-11-31T23:59:59.000Z')) || !isNaN(Date.parse('2012-12-31T23:59:60.000Z'));
  1264. var doesNotParseY2KNewYear = isNaN(Date.parse('2000-01-01T00:00:00.000Z'));
  1265. if (doesNotParseY2KNewYear || acceptsInvalidDates || !supportsExtendedYears) {
  1266. // XXX global assignment won't work in embeddings that use
  1267. // an alternate object for the context.
  1268. /* global Date: true */
  1269. /* eslint-disable no-undef */
  1270. var maxSafeUnsigned32Bit = Math.pow(2, 31) - 1;
  1271. var hasSafariSignedIntBug = isActualNaN(new Date(1970, 0, 1, 0, 0, 0, maxSafeUnsigned32Bit + 1).getTime());
  1272. Date = (function (NativeDate) {
  1273. /* eslint-enable no-undef */
  1274. // Date.length === 7
  1275. var DateShim = function Date(Y, M, D, h, m, s, ms) {
  1276. var length = arguments.length;
  1277. var date;
  1278. if (this instanceof NativeDate) {
  1279. var seconds = s;
  1280. var millis = ms;
  1281. if (hasSafariSignedIntBug && length >= 7 && ms > maxSafeUnsigned32Bit) {
  1282. // work around a Safari 8/9 bug where it treats the seconds as signed
  1283. var msToShift = Math.floor(ms / maxSafeUnsigned32Bit) * maxSafeUnsigned32Bit;
  1284. var sToShift = Math.floor(msToShift / 1e3);
  1285. seconds += sToShift;
  1286. millis -= sToShift * 1e3;
  1287. }
  1288. date = length === 1 && $String(Y) === Y ? // isString(Y)
  1289. // We explicitly pass it through parse:
  1290. new NativeDate(DateShim.parse(Y)) :
  1291. // We have to manually make calls depending on argument
  1292. // length here
  1293. length >= 7 ? new NativeDate(Y, M, D, h, m, seconds, millis) :
  1294. length >= 6 ? new NativeDate(Y, M, D, h, m, seconds) :
  1295. length >= 5 ? new NativeDate(Y, M, D, h, m) :
  1296. length >= 4 ? new NativeDate(Y, M, D, h) :
  1297. length >= 3 ? new NativeDate(Y, M, D) :
  1298. length >= 2 ? new NativeDate(Y, M) :
  1299. length >= 1 ? new NativeDate(Y) :
  1300. new NativeDate();
  1301. } else {
  1302. date = NativeDate.apply(this, arguments);
  1303. }
  1304. if (!isPrimitive(date)) {
  1305. // Prevent mixups with unfixed Date object
  1306. defineProperties(date, { constructor: DateShim }, true);
  1307. }
  1308. return date;
  1309. };
  1310. // 15.9.1.15 Date Time String Format.
  1311. var isoDateExpression = new RegExp('^' +
  1312. '(\\d{4}|[+-]\\d{6})' + // four-digit year capture or sign +
  1313. // 6-digit extended year
  1314. '(?:-(\\d{2})' + // optional month capture
  1315. '(?:-(\\d{2})' + // optional day capture
  1316. '(?:' + // capture hours:minutes:seconds.milliseconds
  1317. 'T(\\d{2})' + // hours capture
  1318. ':(\\d{2})' + // minutes capture
  1319. '(?:' + // optional :seconds.milliseconds
  1320. ':(\\d{2})' + // seconds capture
  1321. '(?:(\\.\\d{1,}))?' + // milliseconds capture
  1322. ')?' +
  1323. '(' + // capture UTC offset component
  1324. 'Z|' + // UTC capture
  1325. '(?:' + // offset specifier +/-hours:minutes
  1326. '([-+])' + // sign capture
  1327. '(\\d{2})' + // hours offset capture
  1328. ':(\\d{2})' + // minutes offset capture
  1329. ')' +
  1330. ')?)?)?)?' +
  1331. '$');
  1332. var months = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365];
  1333. var dayFromMonth = function dayFromMonth(year, month) {
  1334. var t = month > 1 ? 1 : 0;
  1335. return (
  1336. months[month] +
  1337. Math.floor((year - 1969 + t) / 4) -
  1338. Math.floor((year - 1901 + t) / 100) +
  1339. Math.floor((year - 1601 + t) / 400) +
  1340. 365 * (year - 1970)
  1341. );
  1342. };
  1343. var toUTC = function toUTC(t) {
  1344. var s = 0;
  1345. var ms = t;
  1346. if (hasSafariSignedIntBug && ms > maxSafeUnsigned32Bit) {
  1347. // work around a Safari 8/9 bug where it treats the seconds as signed
  1348. var msToShift = Math.floor(ms / maxSafeUnsigned32Bit) * maxSafeUnsigned32Bit;
  1349. var sToShift = Math.floor(msToShift / 1e3);
  1350. s += sToShift;
  1351. ms -= sToShift * 1e3;
  1352. }
  1353. return $Number(new NativeDate(1970, 0, 1, 0, 0, s, ms));
  1354. };
  1355. // Copy any custom methods a 3rd party library may have added
  1356. for (var key in NativeDate) {
  1357. if (owns(NativeDate, key)) {
  1358. DateShim[key] = NativeDate[key];
  1359. }
  1360. }
  1361. // Copy "native" methods explicitly; they may be non-enumerable
  1362. defineProperties(DateShim, {
  1363. now: NativeDate.now,
  1364. UTC: NativeDate.UTC
  1365. }, true);
  1366. DateShim.prototype = NativeDate.prototype;
  1367. defineProperties(DateShim.prototype, {
  1368. constructor: DateShim
  1369. }, true);
  1370. // Upgrade Date.parse to handle simplified ISO 8601 strings
  1371. var parseShim = function parse(string) {
  1372. var match = isoDateExpression.exec(string);
  1373. if (match) {
  1374. // parse months, days, hours, minutes, seconds, and milliseconds
  1375. // provide default values if necessary
  1376. // parse the UTC offset component
  1377. var year = $Number(match[1]),
  1378. month = $Number(match[2] || 1) - 1,
  1379. day = $Number(match[3] || 1) - 1,
  1380. hour = $Number(match[4] || 0),
  1381. minute = $Number(match[5] || 0),
  1382. second = $Number(match[6] || 0),
  1383. millisecond = Math.floor($Number(match[7] || 0) * 1000),
  1384. // When time zone is missed, local offset should be used
  1385. // (ES 5.1 bug)
  1386. // see https://bugs.ecmascript.org/show_bug.cgi?id=112
  1387. isLocalTime = Boolean(match[4] && !match[8]),
  1388. signOffset = match[9] === '-' ? 1 : -1,
  1389. hourOffset = $Number(match[10] || 0),
  1390. minuteOffset = $Number(match[11] || 0),
  1391. result;
  1392. var hasMinutesOrSecondsOrMilliseconds = minute > 0 || second > 0 || millisecond > 0;
  1393. if (
  1394. hour < (hasMinutesOrSecondsOrMilliseconds ? 24 : 25) &&
  1395. minute < 60 && second < 60 && millisecond < 1000 &&
  1396. month > -1 && month < 12 && hourOffset < 24 &&
  1397. minuteOffset < 60 && // detect invalid offsets
  1398. day > -1 &&
  1399. day < (dayFromMonth(year, month + 1) - dayFromMonth(year, month))
  1400. ) {
  1401. result = (
  1402. (dayFromMonth(year, month) + day) * 24 +
  1403. hour +
  1404. hourOffset * signOffset
  1405. ) * 60;
  1406. result = (
  1407. (result + minute + minuteOffset * signOffset) * 60 +
  1408. second
  1409. ) * 1000 + millisecond;
  1410. if (isLocalTime) {
  1411. result = toUTC(result);
  1412. }
  1413. if (-8.64e15 <= result && result <= 8.64e15) {
  1414. return result;
  1415. }
  1416. }
  1417. return NaN;
  1418. }
  1419. return NativeDate.parse.apply(this, arguments);
  1420. };
  1421. defineProperties(DateShim, { parse: parseShim });
  1422. return DateShim;
  1423. }(Date));
  1424. /* global Date: false */
  1425. }
  1426. // ES5 15.9.4.4
  1427. // http://es5.github.com/#x15.9.4.4
  1428. if (!Date.now) {
  1429. Date.now = function now() {
  1430. return new Date().getTime();
  1431. };
  1432. }
  1433. //
  1434. // Number
  1435. // ======
  1436. //
  1437. // ES5.1 15.7.4.5
  1438. // http://es5.github.com/#x15.7.4.5
  1439. var hasToFixedBugs = NumberPrototype.toFixed && (
  1440. (0.00008).toFixed(3) !== '0.000' ||
  1441. (0.9).toFixed(0) !== '1' ||
  1442. (1.255).toFixed(2) !== '1.25' ||
  1443. (1000000000000000128).toFixed(0) !== '1000000000000000128'
  1444. );
  1445. var toFixedHelpers = {
  1446. base: 1e7,
  1447. size: 6,
  1448. data: [0, 0, 0, 0, 0, 0],
  1449. multiply: function multiply(n, c) {
  1450. var i = -1;
  1451. var c2 = c;
  1452. while (++i < toFixedHelpers.size) {
  1453. c2 += n * toFixedHelpers.data[i];
  1454. toFixedHelpers.data[i] = c2 % toFixedHelpers.base;
  1455. c2 = Math.floor(c2 / toFixedHelpers.base);
  1456. }
  1457. },
  1458. divide: function divide(n) {
  1459. var i = toFixedHelpers.size, c = 0;
  1460. while (--i >= 0) {
  1461. c += toFixedHelpers.data[i];
  1462. toFixedHelpers.data[i] = Math.floor(c / n);
  1463. c = (c % n) * toFixedHelpers.base;
  1464. }
  1465. },
  1466. numToString: function numToString() {
  1467. var i = toFixedHelpers.size;
  1468. var s = '';
  1469. while (--i >= 0) {
  1470. if (s !== '' || i === 0 || toFixedHelpers.data[i] !== 0) {
  1471. var t = $String(toFixedHelpers.data[i]);
  1472. if (s === '') {
  1473. s = t;
  1474. } else {
  1475. s += strSlice('0000000', 0, 7 - t.length) + t;
  1476. }
  1477. }
  1478. }
  1479. return s;
  1480. },
  1481. pow: function pow(x, n, acc) {
  1482. return (n === 0 ? acc : (n % 2 === 1 ? pow(x, n - 1, acc * x) : pow(x * x, n / 2, acc)));
  1483. },
  1484. log: function log(x) {
  1485. var n = 0;
  1486. var x2 = x;
  1487. while (x2 >= 4096) {
  1488. n += 12;
  1489. x2 /= 4096;
  1490. }
  1491. while (x2 >= 2) {
  1492. n += 1;
  1493. x2 /= 2;
  1494. }
  1495. return n;
  1496. }
  1497. };
  1498. var toFixedShim = function toFixed(fractionDigits) {
  1499. var f, x, s, m, e, z, j, k;
  1500. // Test for NaN and round fractionDigits down
  1501. f = $Number(fractionDigits);
  1502. f = isActualNaN(f) ? 0 : Math.floor(f);
  1503. if (f < 0 || f > 20) {
  1504. throw new RangeError('Number.toFixed called with invalid number of decimals');
  1505. }
  1506. x = $Number(this);
  1507. if (isActualNaN(x)) {
  1508. return 'NaN';
  1509. }
  1510. // If it is too big or small, return the string value of the number
  1511. if (x <= -1e21 || x >= 1e21) {
  1512. return $String(x);
  1513. }
  1514. s = '';
  1515. if (x < 0) {
  1516. s = '-';
  1517. x = -x;
  1518. }
  1519. m = '0';
  1520. if (x > 1e-21) {
  1521. // 1e-21 < x < 1e21
  1522. // -70 < log2(x) < 70
  1523. e = toFixedHelpers.log(x * toFixedHelpers.pow(2, 69, 1)) - 69;
  1524. z = (e < 0 ? x * toFixedHelpers.pow(2, -e, 1) : x / toFixedHelpers.pow(2, e, 1));
  1525. z *= 0x10000000000000; // Math.pow(2, 52);
  1526. e = 52 - e;
  1527. // -18 < e < 122
  1528. // x = z / 2 ^ e
  1529. if (e > 0) {
  1530. toFixedHelpers.multiply(0, z);
  1531. j = f;
  1532. while (j >= 7) {
  1533. toFixedHelpers.multiply(1e7, 0);
  1534. j -= 7;
  1535. }
  1536. toFixedHelpers.multiply(toFixedHelpers.pow(10, j, 1), 0);
  1537. j = e - 1;
  1538. while (j >= 23) {
  1539. toFixedHelpers.divide(1 << 23);
  1540. j -= 23;
  1541. }
  1542. toFixedHelpers.divide(1 << j);
  1543. toFixedHelpers.multiply(1, 1);
  1544. toFixedHelpers.divide(2);
  1545. m = toFixedHelpers.numToString();
  1546. } else {
  1547. toFixedHelpers.multiply(0, z);
  1548. toFixedHelpers.multiply(1 << (-e), 0);
  1549. m = toFixedHelpers.numToString() + strSlice('0.00000000000000000000', 2, 2 + f);
  1550. }
  1551. }
  1552. if (f > 0) {
  1553. k = m.length;
  1554. if (k <= f) {
  1555. m = s + strSlice('0.0000000000000000000', 0, f - k + 2) + m;
  1556. } else {
  1557. m = s + strSlice(m, 0, k - f) + '.' + strSlice(m, k - f);
  1558. }
  1559. } else {
  1560. m = s + m;
  1561. }
  1562. return m;
  1563. };
  1564. defineProperties(NumberPrototype, { toFixed: toFixedShim }, hasToFixedBugs);
  1565. var hasToPrecisionUndefinedBug = (function () {
  1566. try {
  1567. return 1.0.toPrecision(undefined) === '1';
  1568. } catch (e) {
  1569. return true;
  1570. }
  1571. }());
  1572. var originalToPrecision = NumberPrototype.toPrecision;
  1573. defineProperties(NumberPrototype, {
  1574. toPrecision: function toPrecision(precision) {
  1575. return typeof precision === 'undefined' ? originalToPrecision.call(this) : originalToPrecision.call(this, precision);
  1576. }
  1577. }, hasToPrecisionUndefinedBug);
  1578. //
  1579. // String
  1580. // ======
  1581. //
  1582. // ES5 15.5.4.14
  1583. // http://es5.github.com/#x15.5.4.14
  1584. // [bugfix, IE lt 9, firefox 4, Konqueror, Opera, obscure browsers]
  1585. // Many browsers do not split properly with regular expressions or they
  1586. // do not perform the split correctly under obscure conditions.
  1587. // See http://blog.stevenlevithan.com/archives/cross-browser-split
  1588. // I've tested in many browsers and this seems to cover the deviant ones:
  1589. // 'ab'.split(/(?:ab)*/) should be ["", ""], not [""]
  1590. // '.'.split(/(.?)(.?)/) should be ["", ".", "", ""], not ["", ""]
  1591. // 'tesst'.split(/(s)*/) should be ["t", undefined, "e", "s", "t"], not
  1592. // [undefined, "t", undefined, "e", ...]
  1593. // ''.split(/.?/) should be [], not [""]
  1594. // '.'.split(/()()/) should be ["."], not ["", "", "."]
  1595. if (
  1596. 'ab'.split(/(?:ab)*/).length !== 2 ||
  1597. '.'.split(/(.?)(.?)/).length !== 4 ||
  1598. 'tesst'.split(/(s)*/)[1] === 't' ||
  1599. 'test'.split(/(?:)/, -1).length !== 4 ||
  1600. ''.split(/.?/).length ||
  1601. '.'.split(/()()/).length > 1
  1602. ) {
  1603. (function () {
  1604. var compliantExecNpcg = typeof (/()??/).exec('')[1] === 'undefined'; // NPCG: nonparticipating capturing group
  1605. var maxSafe32BitInt = Math.pow(2, 32) - 1;
  1606. StringPrototype.split = function (separator, limit) {
  1607. var string = String(this);
  1608. if (typeof separator === 'undefined' && limit === 0) {
  1609. return [];
  1610. }
  1611. // If `separator` is not a regex, use native split
  1612. if (!isRegex(separator)) {
  1613. return strSplit(this, separator, limit);
  1614. }
  1615. var output = [];
  1616. var flags = (separator.ignoreCase ? 'i' : '') +
  1617. (separator.multiline ? 'm' : '') +
  1618. (separator.unicode ? 'u' : '') + // in ES6
  1619. (separator.sticky ? 'y' : ''), // Firefox 3+ and ES6
  1620. lastLastIndex = 0,
  1621. // Make `global` and avoid `lastIndex` issues by working with a copy
  1622. separator2, match, lastIndex, lastLength;
  1623. var separatorCopy = new RegExp(separator.source, flags + 'g');
  1624. if (!compliantExecNpcg) {
  1625. // Doesn't need flags gy, but they don't hurt
  1626. separator2 = new RegExp('^' + separatorCopy.source + '$(?!\\s)', flags);
  1627. }
  1628. /* Values for `limit`, per the spec:
  1629. * If undefined: 4294967295 // maxSafe32BitInt
  1630. * If 0, Infinity, or NaN: 0
  1631. * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296;
  1632. * If negative number: 4294967296 - Math.floor(Math.abs(limit))
  1633. * If other: Type-convert, then use the above rules
  1634. */
  1635. var splitLimit = typeof limit === 'undefined' ? maxSafe32BitInt : ES.ToUint32(limit);
  1636. match = separatorCopy.exec(string);
  1637. while (match) {
  1638. // `separatorCopy.lastIndex` is not reliable cross-browser
  1639. lastIndex = match.index + match[0].length;
  1640. if (lastIndex > lastLastIndex) {
  1641. pushCall(output, strSlice(string, lastLastIndex, match.index));
  1642. // Fix browsers whose `exec` methods don't consistently return `undefined` for
  1643. // nonparticipating capturing groups
  1644. if (!compliantExecNpcg && match.length > 1) {
  1645. /* eslint-disable no-loop-func */
  1646. match[0].replace(separator2, function () {
  1647. for (var i = 1; i < arguments.length - 2; i++) {
  1648. if (typeof arguments[i] === 'undefined') {
  1649. match[i] = void 0;
  1650. }
  1651. }
  1652. });
  1653. /* eslint-enable no-loop-func */
  1654. }
  1655. if (match.length > 1 && match.index < string.length) {
  1656. array_push.apply(output, arraySlice(match, 1));
  1657. }
  1658. lastLength = match[0].length;
  1659. lastLastIndex = lastIndex;
  1660. if (output.length >= splitLimit) {
  1661. break;
  1662. }
  1663. }
  1664. if (separatorCopy.lastIndex === match.index) {
  1665. separatorCopy.lastIndex++; // Avoid an infinite loop
  1666. }
  1667. match = separatorCopy.exec(string);
  1668. }
  1669. if (lastLastIndex === string.length) {
  1670. if (lastLength || !separatorCopy.test('')) {
  1671. pushCall(output, '');
  1672. }
  1673. } else {
  1674. pushCall(output, strSlice(string, lastLastIndex));
  1675. }
  1676. return output.length > splitLimit ? strSlice(output, 0, splitLimit) : output;
  1677. };
  1678. }());
  1679. // [bugfix, chrome]
  1680. // If separator is undefined, then the result array contains just one String,
  1681. // which is the this value (converted to a String). If limit is not undefined,
  1682. // then the output array is truncated so that it contains no more than limit
  1683. // elements.
  1684. // "0".split(undefined, 0) -> []
  1685. } else if ('0'.split(void 0, 0).length) {
  1686. StringPrototype.split = function split(separator, limit) {
  1687. if (typeof separator === 'undefined' && limit === 0) { return []; }
  1688. return strSplit(this, separator, limit);
  1689. };
  1690. }
  1691. var str_replace = StringPrototype.replace;
  1692. var replaceReportsGroupsCorrectly = (function () {
  1693. var groups = [];
  1694. 'x'.replace(/x(.)?/g, function (match, group) {
  1695. pushCall(groups, group);
  1696. });
  1697. return groups.length === 1 && typeof groups[0] === 'undefined';
  1698. }());
  1699. if (!replaceReportsGroupsCorrectly) {
  1700. StringPrototype.replace = function replace(searchValue, replaceValue) {
  1701. var isFn = isCallable(replaceValue);
  1702. var hasCapturingGroups = isRegex(searchValue) && (/\)[*?]/).test(searchValue.source);
  1703. if (!isFn || !hasCapturingGroups) {
  1704. return str_replace.call(this, searchValue, replaceValue);
  1705. } else {
  1706. var wrappedReplaceValue = function (match) {
  1707. var length = arguments.length;
  1708. var originalLastIndex = searchValue.lastIndex;
  1709. searchValue.lastIndex = 0;
  1710. var args = searchValue.exec(match) || [];
  1711. searchValue.lastIndex = originalLastIndex;
  1712. pushCall(args, arguments[length - 2], arguments[length - 1]);
  1713. return replaceValue.apply(this, args);
  1714. };
  1715. return str_replace.call(this, searchValue, wrappedReplaceValue);
  1716. }
  1717. };
  1718. }
  1719. // ECMA-262, 3rd B.2.3
  1720. // Not an ECMAScript standard, although ECMAScript 3rd Edition has a
  1721. // non-normative section suggesting uniform semantics and it should be
  1722. // normalized across all browsers
  1723. // [bugfix, IE lt 9] IE < 9 substr() with negative value not working in IE
  1724. var string_substr = StringPrototype.substr;
  1725. var hasNegativeSubstrBug = ''.substr && '0b'.substr(-1) !== 'b';
  1726. defineProperties(StringPrototype, {
  1727. substr: function substr(start, length) {
  1728. var normalizedStart = start;
  1729. if (start < 0) {
  1730. normalizedStart = max(this.length + start, 0);
  1731. }
  1732. return string_substr.call(this, normalizedStart, length);
  1733. }
  1734. }, hasNegativeSubstrBug);
  1735. // ES5 15.5.4.20
  1736. // whitespace from: http://es5.github.io/#x15.5.4.20
  1737. var ws = '\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003' +
  1738. '\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028' +
  1739. '\u2029\uFEFF';
  1740. var zeroWidth = '\u200b';
  1741. var wsRegexChars = '[' + ws + ']';
  1742. var trimBeginRegexp = new RegExp('^' + wsRegexChars + wsRegexChars + '*');
  1743. var trimEndRegexp = new RegExp(wsRegexChars + wsRegexChars + '*$');
  1744. var hasTrimWhitespaceBug = StringPrototype.trim && (ws.trim() || !zeroWidth.trim());
  1745. defineProperties(StringPrototype, {
  1746. // http://blog.stevenlevithan.com/archives/faster-trim-javascript
  1747. // http://perfectionkills.com/whitespace-deviations/
  1748. trim: function trim() {
  1749. if (typeof this === 'undefined' || this === null) {
  1750. throw new TypeError("can't convert " + this + ' to object');
  1751. }
  1752. return $String(this).replace(trimBeginRegexp, '').replace(trimEndRegexp, '');
  1753. }
  1754. }, hasTrimWhitespaceBug);
  1755. var trim = call.bind(String.prototype.trim);
  1756. var hasLastIndexBug = StringPrototype.lastIndexOf && 'abcあい'.lastIndexOf('あい', 2) !== -1;
  1757. defineProperties(StringPrototype, {
  1758. lastIndexOf: function lastIndexOf(searchString) {
  1759. if (typeof this === 'undefined' || this === null) {
  1760. throw new TypeError("can't convert " + this + ' to object');
  1761. }
  1762. var S = $String(this);
  1763. var searchStr = $String(searchString);
  1764. var numPos = arguments.length > 1 ? $Number(arguments[1]) : NaN;
  1765. var pos = isActualNaN(numPos) ? Infinity : ES.ToInteger(numPos);
  1766. var start = min(max(pos, 0), S.length);
  1767. var searchLen = searchStr.length;
  1768. var k = start + searchLen;
  1769. while (k > 0) {
  1770. k = max(0, k - searchLen);
  1771. var index = strIndexOf(strSlice(S, k, start + searchLen), searchStr);
  1772. if (index !== -1) {
  1773. return k + index;
  1774. }
  1775. }
  1776. return -1;
  1777. }
  1778. }, hasLastIndexBug);
  1779. var originalLastIndexOf = StringPrototype.lastIndexOf;
  1780. defineProperties(StringPrototype, {
  1781. lastIndexOf: function lastIndexOf(searchString) {
  1782. return originalLastIndexOf.apply(this, arguments);
  1783. }
  1784. }, StringPrototype.lastIndexOf.length !== 1);
  1785. // ES-5 15.1.2.2
  1786. /* eslint-disable radix */
  1787. if (parseInt(ws + '08') !== 8 || parseInt(ws + '0x16') !== 22) {
  1788. /* eslint-enable radix */
  1789. /* global parseInt: true */
  1790. parseInt = (function (origParseInt) {
  1791. var hexRegex = /^[\-+]?0[xX]/;
  1792. return function parseInt(str, radix) {
  1793. var string = trim(str);
  1794. var defaultedRadix = $Number(radix) || (hexRegex.test(string) ? 16 : 10);
  1795. return origParseInt(string, defaultedRadix);
  1796. };
  1797. }(parseInt));
  1798. }
  1799. // https://es5.github.io/#x15.1.2.3
  1800. if (1 / parseFloat('-0') !== -Infinity) {
  1801. /* global parseFloat: true */
  1802. parseFloat = (function (origParseFloat) {
  1803. return function parseFloat(string) {
  1804. var inputString = trim(string);
  1805. var result = origParseFloat(inputString);
  1806. return result === 0 && strSlice(inputString, 0, 1) === '-' ? -0 : result;
  1807. };
  1808. }(parseFloat));
  1809. }
  1810. if (String(new RangeError('test')) !== 'RangeError: test') {
  1811. var errorToStringShim = function toString() {
  1812. if (typeof this === 'undefined' || this === null) {
  1813. throw new TypeError("can't convert " + this + ' to object');
  1814. }
  1815. var name = this.name;
  1816. if (typeof name === 'undefined') {
  1817. name = 'Error';
  1818. } else if (typeof name !== 'string') {
  1819. name = $String(name);
  1820. }
  1821. var msg = this.message;
  1822. if (typeof msg === 'undefined') {
  1823. msg = '';
  1824. } else if (typeof msg !== 'string') {
  1825. msg = $String(msg);
  1826. }
  1827. if (!name) {
  1828. return msg;
  1829. }
  1830. if (!msg) {
  1831. return name;
  1832. }
  1833. return name + ': ' + msg;
  1834. };
  1835. // can't use defineProperties here because of toString enumeration issue in IE <= 8
  1836. Error.prototype.toString = errorToStringShim;
  1837. }
  1838. if (supportsDescriptors) {
  1839. var ensureNonEnumerable = function (obj, prop) {
  1840. if (isEnum(obj, prop)) {
  1841. var desc = Object.getOwnPropertyDescriptor(obj, prop);
  1842. desc.enumerable = false;
  1843. Object.defineProperty(obj, prop, desc);
  1844. }
  1845. };
  1846. ensureNonEnumerable(Error.prototype, 'message');
  1847. if (Error.prototype.message !== '') {
  1848. Error.prototype.message = '';
  1849. }
  1850. ensureNonEnumerable(Error.prototype, 'name');
  1851. }
  1852. if (String(/a/mig) !== '/a/gim') {
  1853. var regexToString = function toString() {
  1854. var str = '/' + this.source + '/';
  1855. if (this.global) {
  1856. str += 'g';
  1857. }
  1858. if (this.ignoreCase) {
  1859. str += 'i';
  1860. }
  1861. if (this.multiline) {
  1862. str += 'm';
  1863. }
  1864. return str;
  1865. };
  1866. // can't use defineProperties here because of toString enumeration issue in IE <= 8
  1867. RegExp.prototype.toString = regexToString;
  1868. }
  1869. }));
  1870. /*!
  1871. * https://github.com/es-shims/es5-shim
  1872. * @license es5-shim Copyright 2009-2015 by contributors, MIT License
  1873. * see https://github.com/es-shims/es5-shim/blob/master/LICENSE
  1874. */
  1875. // vim: ts=4 sts=4 sw=4 expandtab
  1876. // Add semicolon to prevent IIFE from being passed as argument to concatenated code.
  1877. ;
  1878. // UMD (Universal Module Definition)
  1879. // see https://github.com/umdjs/umd/blob/master/templates/returnExports.js
  1880. (function (root, factory) {
  1881. 'use strict';
  1882. /* global define, exports, module */
  1883. if (typeof define === 'function' && define.amd) {
  1884. // AMD. Register as an anonymous module.
  1885. define(factory);
  1886. } else if (typeof exports === 'object') {
  1887. // Node. Does not work with strict CommonJS, but
  1888. // only CommonJS-like enviroments that support module.exports,
  1889. // like Node.
  1890. module.exports = factory();
  1891. } else {
  1892. // Browser globals (root is window)
  1893. root.returnExports = factory();
  1894. }
  1895. }(this, function () {
  1896. var call = Function.call;
  1897. var prototypeOfObject = Object.prototype;
  1898. var owns = call.bind(prototypeOfObject.hasOwnProperty);
  1899. var isEnumerable = call.bind(prototypeOfObject.propertyIsEnumerable);
  1900. var toStr = call.bind(prototypeOfObject.toString);
  1901. // If JS engine supports accessors creating shortcuts.
  1902. var defineGetter;
  1903. var defineSetter;
  1904. var lookupGetter;
  1905. var lookupSetter;
  1906. var supportsAccessors = owns(prototypeOfObject, '__defineGetter__');
  1907. if (supportsAccessors) {
  1908. /* eslint-disable no-underscore-dangle */
  1909. defineGetter = call.bind(prototypeOfObject.__defineGetter__);
  1910. defineSetter = call.bind(prototypeOfObject.__defineSetter__);
  1911. lookupGetter = call.bind(prototypeOfObject.__lookupGetter__);
  1912. lookupSetter = call.bind(prototypeOfObject.__lookupSetter__);
  1913. /* eslint-enable no-underscore-dangle */
  1914. }
  1915. // ES5 15.2.3.2
  1916. // http://es5.github.com/#x15.2.3.2
  1917. if (!Object.getPrototypeOf) {
  1918. // https://github.com/es-shims/es5-shim/issues#issue/2
  1919. // http://ejohn.org/blog/objectgetprototypeof/
  1920. // recommended by fschaefer on github
  1921. //
  1922. // sure, and webreflection says ^_^
  1923. // ... this will nerever possibly return null
  1924. // ... Opera Mini breaks here with infinite loops
  1925. Object.getPrototypeOf = function getPrototypeOf(object) {
  1926. /* eslint-disable no-proto */
  1927. var proto = object.__proto__;
  1928. /* eslint-enable no-proto */
  1929. if (proto || proto === null) {
  1930. return proto;
  1931. } else if (toStr(object.constructor) === '[object Function]') {
  1932. return object.constructor.prototype;
  1933. } else if (object instanceof Object) {
  1934. return prototypeOfObject;
  1935. } else {
  1936. // Correctly return null for Objects created with `Object.create(null)`
  1937. // (shammed or native) or `{ __proto__: null}`. Also returns null for
  1938. // cross-realm objects on browsers that lack `__proto__` support (like
  1939. // IE <11), but that's the best we can do.
  1940. return null;
  1941. }
  1942. };
  1943. }
  1944. // ES5 15.2.3.3
  1945. // http://es5.github.com/#x15.2.3.3
  1946. var doesGetOwnPropertyDescriptorWork = function doesGetOwnPropertyDescriptorWork(object) {
  1947. try {
  1948. object.sentinel = 0;
  1949. return Object.getOwnPropertyDescriptor(object, 'sentinel').value === 0;
  1950. } catch (exception) {
  1951. return false;
  1952. }
  1953. };
  1954. // check whether getOwnPropertyDescriptor works if it's given. Otherwise, shim partially.
  1955. if (Object.defineProperty) {
  1956. var getOwnPropertyDescriptorWorksOnObject = doesGetOwnPropertyDescriptorWork({});
  1957. var getOwnPropertyDescriptorWorksOnDom = typeof document === 'undefined' ||
  1958. doesGetOwnPropertyDescriptorWork(document.createElement('div'));
  1959. if (!getOwnPropertyDescriptorWorksOnDom || !getOwnPropertyDescriptorWorksOnObject) {
  1960. var getOwnPropertyDescriptorFallback = Object.getOwnPropertyDescriptor;
  1961. }
  1962. }
  1963. if (!Object.getOwnPropertyDescriptor || getOwnPropertyDescriptorFallback) {
  1964. var ERR_NON_OBJECT = 'Object.getOwnPropertyDescriptor called on a non-object: ';
  1965. /* eslint-disable no-proto */
  1966. Object.getOwnPropertyDescriptor = function getOwnPropertyDescriptor(object, property) {
  1967. if ((typeof object !== 'object' && typeof object !== 'function') || object === null) {
  1968. throw new TypeError(ERR_NON_OBJECT + object);
  1969. }
  1970. // make a valiant attempt to use the real getOwnPropertyDescriptor
  1971. // for I8's DOM elements.
  1972. if (getOwnPropertyDescriptorFallback) {
  1973. try {
  1974. return getOwnPropertyDescriptorFallback.call(Object, object, property);
  1975. } catch (exception) {
  1976. // try the shim if the real one doesn't work
  1977. }
  1978. }
  1979. var descriptor;
  1980. // If object does not owns property return undefined immediately.
  1981. if (!owns(object, property)) {
  1982. return descriptor;
  1983. }
  1984. // If object has a property then it's for sure `configurable`, and
  1985. // probably `enumerable`. Detect enumerability though.
  1986. descriptor = {
  1987. enumerable: isEnumerable(object, property),
  1988. configurable: true
  1989. };
  1990. // If JS engine supports accessor properties then property may be a
  1991. // getter or setter.
  1992. if (supportsAccessors) {
  1993. // Unfortunately `__lookupGetter__` will return a getter even
  1994. // if object has own non getter property along with a same named
  1995. // inherited getter. To avoid misbehavior we temporary remove
  1996. // `__proto__` so that `__lookupGetter__` will return getter only
  1997. // if it's owned by an object.
  1998. var prototype = object.__proto__;
  1999. var notPrototypeOfObject = object !== prototypeOfObject;
  2000. // avoid recursion problem, breaking in Opera Mini when
  2001. // Object.getOwnPropertyDescriptor(Object.prototype, 'toString')
  2002. // or any other Object.prototype accessor
  2003. if (notPrototypeOfObject) {
  2004. object.__proto__ = prototypeOfObject;
  2005. }
  2006. var getter = lookupGetter(object, property);
  2007. var setter = lookupSetter(object, property);
  2008. if (notPrototypeOfObject) {
  2009. // Once we have getter and setter we can put values back.
  2010. object.__proto__ = prototype;
  2011. }
  2012. if (getter || setter) {
  2013. if (getter) {
  2014. descriptor.get = getter;
  2015. }
  2016. if (setter) {
  2017. descriptor.set = setter;
  2018. }
  2019. // If it was accessor property we're done and return here
  2020. // in order to avoid adding `value` to the descriptor.
  2021. return descriptor;
  2022. }
  2023. }
  2024. // If we got this far we know that object has an own property that is
  2025. // not an accessor so we set it as a value and return descriptor.
  2026. descriptor.value = object[property];
  2027. descriptor.writable = true;
  2028. return descriptor;
  2029. };
  2030. /* eslint-enable no-proto */
  2031. }
  2032. // ES5 15.2.3.4
  2033. // http://es5.github.com/#x15.2.3.4
  2034. if (!Object.getOwnPropertyNames) {
  2035. Object.getOwnPropertyNames = function getOwnPropertyNames(object) {
  2036. return Object.keys(object);
  2037. };
  2038. }
  2039. // ES5 15.2.3.5
  2040. // http://es5.github.com/#x15.2.3.5
  2041. if (!Object.create) {
  2042. // Contributed by Brandon Benvie, October, 2012
  2043. var createEmpty;
  2044. var supportsProto = !({ __proto__: null } instanceof Object);
  2045. // the following produces false positives
  2046. // in Opera Mini => not a reliable check
  2047. // Object.prototype.__proto__ === null
  2048. // Check for document.domain and active x support
  2049. // No need to use active x approach when document.domain is not set
  2050. // see https://github.com/es-shims/es5-shim/issues/150
  2051. // variation of https://github.com/kitcambridge/es5-shim/commit/4f738ac066346
  2052. /* global ActiveXObject */
  2053. var shouldUseActiveX = function shouldUseActiveX() {
  2054. // return early if document.domain not set
  2055. if (!document.domain) {
  2056. return false;
  2057. }
  2058. try {
  2059. return !!new ActiveXObject('htmlfile');
  2060. } catch (exception) {
  2061. return false;
  2062. }
  2063. };
  2064. // This supports IE8 when document.domain is used
  2065. // see https://github.com/es-shims/es5-shim/issues/150
  2066. // variation of https://github.com/kitcambridge/es5-shim/commit/4f738ac066346
  2067. var getEmptyViaActiveX = function getEmptyViaActiveX() {
  2068. var empty;
  2069. var xDoc;
  2070. xDoc = new ActiveXObject('htmlfile');
  2071. xDoc.write('<script><\/script>');
  2072. xDoc.close();
  2073. empty = xDoc.parentWindow.Object.prototype;
  2074. xDoc = null;
  2075. return empty;
  2076. };
  2077. // The original implementation using an iframe
  2078. // before the activex approach was added
  2079. // see https://github.com/es-shims/es5-shim/issues/150
  2080. var getEmptyViaIFrame = function getEmptyViaIFrame() {
  2081. var iframe = document.createElement('iframe');
  2082. var parent = document.body || document.documentElement;
  2083. var empty;
  2084. iframe.style.display = 'none';
  2085. parent.appendChild(iframe);
  2086. /* eslint-disable no-script-url */
  2087. iframe.src = 'javascript:';
  2088. /* eslint-enable no-script-url */
  2089. empty = iframe.contentWindow.Object.prototype;
  2090. parent.removeChild(iframe);
  2091. iframe = null;
  2092. return empty;
  2093. };
  2094. /* global document */
  2095. if (supportsProto || typeof document === 'undefined') {
  2096. createEmpty = function () {
  2097. return { __proto__: null };
  2098. };
  2099. } else {
  2100. // In old IE __proto__ can't be used to manually set `null`, nor does
  2101. // any other method exist to make an object that inherits from nothing,
  2102. // aside from Object.prototype itself. Instead, create a new global
  2103. // object and *steal* its Object.prototype and strip it bare. This is
  2104. // used as the prototype to create nullary objects.
  2105. createEmpty = function () {
  2106. // Determine which approach to use
  2107. // see https://github.com/es-shims/es5-shim/issues/150
  2108. var empty = shouldUseActiveX() ? getEmptyViaActiveX() : getEmptyViaIFrame();
  2109. delete empty.constructor;
  2110. delete empty.hasOwnProperty;
  2111. delete empty.propertyIsEnumerable;
  2112. delete empty.isPrototypeOf;
  2113. delete empty.toLocaleString;
  2114. delete empty.toString;
  2115. delete empty.valueOf;
  2116. var Empty = function Empty() {};
  2117. Empty.prototype = empty;
  2118. // short-circuit future calls
  2119. createEmpty = function () {
  2120. return new Empty();
  2121. };
  2122. return new Empty();
  2123. };
  2124. }
  2125. Object.create = function create(prototype, properties) {
  2126. var object;
  2127. var Type = function Type() {}; // An empty constructor.
  2128. if (prototype === null) {
  2129. object = createEmpty();
  2130. } else {
  2131. if (typeof prototype !== 'object' && typeof prototype !== 'function') {
  2132. // In the native implementation `parent` can be `null`
  2133. // OR *any* `instanceof Object` (Object|Function|Array|RegExp|etc)
  2134. // Use `typeof` tho, b/c in old IE, DOM elements are not `instanceof Object`
  2135. // like they are in modern browsers. Using `Object.create` on DOM elements
  2136. // is...err...probably inappropriate, but the native version allows for it.
  2137. throw new TypeError('Object prototype may only be an Object or null'); // same msg as Chrome
  2138. }
  2139. Type.prototype = prototype;
  2140. object = new Type();
  2141. // IE has no built-in implementation of `Object.getPrototypeOf`
  2142. // neither `__proto__`, but this manually setting `__proto__` will
  2143. // guarantee that `Object.getPrototypeOf` will work as expected with
  2144. // objects created using `Object.create`
  2145. /* eslint-disable no-proto */
  2146. object.__proto__ = prototype;
  2147. /* eslint-enable no-proto */
  2148. }
  2149. if (properties !== void 0) {
  2150. Object.defineProperties(object, properties);
  2151. }
  2152. return object;
  2153. };
  2154. }
  2155. // ES5 15.2.3.6
  2156. // http://es5.github.com/#x15.2.3.6
  2157. // Patch for WebKit and IE8 standard mode
  2158. // Designed by hax <hax.github.com>
  2159. // related issue: https://github.com/es-shims/es5-shim/issues#issue/5
  2160. // IE8 Reference:
  2161. // http://msdn.microsoft.com/en-us/library/dd282900.aspx
  2162. // http://msdn.microsoft.com/en-us/library/dd229916.aspx
  2163. // WebKit Bugs:
  2164. // https://bugs.webkit.org/show_bug.cgi?id=36423
  2165. var doesDefinePropertyWork = function doesDefinePropertyWork(object) {
  2166. try {
  2167. Object.defineProperty(object, 'sentinel', {});
  2168. return 'sentinel' in object;
  2169. } catch (exception) {
  2170. return false;
  2171. }
  2172. };
  2173. // check whether defineProperty works if it's given. Otherwise,
  2174. // shim partially.
  2175. if (Object.defineProperty) {
  2176. var definePropertyWorksOnObject = doesDefinePropertyWork({});
  2177. var definePropertyWorksOnDom = typeof document === 'undefined' ||
  2178. doesDefinePropertyWork(document.createElement('div'));
  2179. if (!definePropertyWorksOnObject || !definePropertyWorksOnDom) {
  2180. var definePropertyFallback = Object.defineProperty,
  2181. definePropertiesFallback = Object.defineProperties;
  2182. }
  2183. }
  2184. if (!Object.defineProperty || definePropertyFallback) {
  2185. var ERR_NON_OBJECT_DESCRIPTOR = 'Property description must be an object: ';
  2186. var ERR_NON_OBJECT_TARGET = 'Object.defineProperty called on non-object: ';
  2187. var ERR_ACCESSORS_NOT_SUPPORTED = 'getters & setters can not be defined on this javascript engine';
  2188. Object.defineProperty = function defineProperty(object, property, descriptor) {
  2189. if ((typeof object !== 'object' && typeof object !== 'function') || object === null) {
  2190. throw new TypeError(ERR_NON_OBJECT_TARGET + object);
  2191. }
  2192. if ((typeof descriptor !== 'object' && typeof descriptor !== 'function') || descriptor === null) {
  2193. throw new TypeError(ERR_NON_OBJECT_DESCRIPTOR + descriptor);
  2194. }
  2195. // make a valiant attempt to use the real defineProperty
  2196. // for I8's DOM elements.
  2197. if (definePropertyFallback) {
  2198. try {
  2199. return definePropertyFallback.call(Object, object, property, descriptor);
  2200. } catch (exception) {
  2201. // try the shim if the real one doesn't work
  2202. }
  2203. }
  2204. // If it's a data property.
  2205. if ('value' in descriptor) {
  2206. // fail silently if 'writable', 'enumerable', or 'configurable'
  2207. // are requested but not supported
  2208. /*
  2209. // alternate approach:
  2210. if ( // can't implement these features; allow false but not true
  2211. ('writable' in descriptor && !descriptor.writable) ||
  2212. ('enumerable' in descriptor && !descriptor.enumerable) ||
  2213. ('configurable' in descriptor && !descriptor.configurable)
  2214. ))
  2215. throw new RangeError(
  2216. 'This implementation of Object.defineProperty does not support configurable, enumerable, or writable.'
  2217. );
  2218. */
  2219. if (supportsAccessors && (lookupGetter(object, property) || lookupSetter(object, property))) {
  2220. // As accessors are supported only on engines implementing
  2221. // `__proto__` we can safely override `__proto__` while defining
  2222. // a property to make sure that we don't hit an inherited
  2223. // accessor.
  2224. /* eslint-disable no-proto */
  2225. var prototype = object.__proto__;
  2226. object.__proto__ = prototypeOfObject;
  2227. // Deleting a property anyway since getter / setter may be
  2228. // defined on object itself.
  2229. delete object[property];
  2230. object[property] = descriptor.value;
  2231. // Setting original `__proto__` back now.
  2232. object.__proto__ = prototype;
  2233. /* eslint-enable no-proto */
  2234. } else {
  2235. object[property] = descriptor.value;
  2236. }
  2237. } else {
  2238. if (!supportsAccessors && (('get' in descriptor) || ('set' in descriptor))) {
  2239. throw new TypeError(ERR_ACCESSORS_NOT_SUPPORTED);
  2240. }
  2241. // If we got that far then getters and setters can be defined !!
  2242. if ('get' in descriptor) {
  2243. defineGetter(object, property, descriptor.get);
  2244. }
  2245. if ('set' in descriptor) {
  2246. defineSetter(object, property, descriptor.set);
  2247. }
  2248. }
  2249. return object;
  2250. };
  2251. }
  2252. // ES5 15.2.3.7
  2253. // http://es5.github.com/#x15.2.3.7
  2254. if (!Object.defineProperties || definePropertiesFallback) {
  2255. Object.defineProperties = function defineProperties(object, properties) {
  2256. // make a valiant attempt to use the real defineProperties
  2257. if (definePropertiesFallback) {
  2258. try {
  2259. return definePropertiesFallback.call(Object, object, properties);
  2260. } catch (exception) {
  2261. // try the shim if the real one doesn't work
  2262. }
  2263. }
  2264. Object.keys(properties).forEach(function (property) {
  2265. if (property !== '__proto__') {
  2266. Object.defineProperty(object, property, properties[property]);
  2267. }
  2268. });
  2269. return object;
  2270. };
  2271. }
  2272. // ES5 15.2.3.8
  2273. // http://es5.github.com/#x15.2.3.8
  2274. if (!Object.seal) {
  2275. Object.seal = function seal(object) {
  2276. if (Object(object) !== object) {
  2277. throw new TypeError('Object.seal can only be called on Objects.');
  2278. }
  2279. // this is misleading and breaks feature-detection, but
  2280. // allows "securable" code to "gracefully" degrade to working
  2281. // but insecure code.
  2282. return object;
  2283. };
  2284. }
  2285. // ES5 15.2.3.9
  2286. // http://es5.github.com/#x15.2.3.9
  2287. if (!Object.freeze) {
  2288. Object.freeze = function freeze(object) {
  2289. if (Object(object) !== object) {
  2290. throw new TypeError('Object.freeze can only be called on Objects.');
  2291. }
  2292. // this is misleading and breaks feature-detection, but
  2293. // allows "securable" code to "gracefully" degrade to working
  2294. // but insecure code.
  2295. return object;
  2296. };
  2297. }
  2298. // detect a Rhino bug and patch it
  2299. try {
  2300. Object.freeze(function () {});
  2301. } catch (exception) {
  2302. Object.freeze = (function (freezeObject) {
  2303. return function freeze(object) {
  2304. if (typeof object === 'function') {
  2305. return object;
  2306. } else {
  2307. return freezeObject(object);
  2308. }
  2309. };
  2310. }(Object.freeze));
  2311. }
  2312. // ES5 15.2.3.10
  2313. // http://es5.github.com/#x15.2.3.10
  2314. if (!Object.preventExtensions) {
  2315. Object.preventExtensions = function preventExtensions(object) {
  2316. if (Object(object) !== object) {
  2317. throw new TypeError('Object.preventExtensions can only be called on Objects.');
  2318. }
  2319. // this is misleading and breaks feature-detection, but
  2320. // allows "securable" code to "gracefully" degrade to working
  2321. // but insecure code.
  2322. return object;
  2323. };
  2324. }
  2325. // ES5 15.2.3.11
  2326. // http://es5.github.com/#x15.2.3.11
  2327. if (!Object.isSealed) {
  2328. Object.isSealed = function isSealed(object) {
  2329. if (Object(object) !== object) {
  2330. throw new TypeError('Object.isSealed can only be called on Objects.');
  2331. }
  2332. return false;
  2333. };
  2334. }
  2335. // ES5 15.2.3.12
  2336. // http://es5.github.com/#x15.2.3.12
  2337. if (!Object.isFrozen) {
  2338. Object.isFrozen = function isFrozen(object) {
  2339. if (Object(object) !== object) {
  2340. throw new TypeError('Object.isFrozen can only be called on Objects.');
  2341. }
  2342. return false;
  2343. };
  2344. }
  2345. // ES5 15.2.3.13
  2346. // http://es5.github.com/#x15.2.3.13
  2347. if (!Object.isExtensible) {
  2348. Object.isExtensible = function isExtensible(object) {
  2349. // 1. If Type(O) is not Object throw a TypeError exception.
  2350. if (Object(object) !== object) {
  2351. throw new TypeError('Object.isExtensible can only be called on Objects.');
  2352. }
  2353. // 2. Return the Boolean value of the [[Extensible]] internal property of O.
  2354. var name = '';
  2355. while (owns(object, name)) {
  2356. name += '?';
  2357. }
  2358. object[name] = true;
  2359. var returnValue = owns(object, name);
  2360. delete object[name];
  2361. return returnValue;
  2362. };
  2363. }
  2364. }));