You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

worker-json.js 92KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904
  1. "no use strict";
  2. var console = {
  3. log: function(msg) {
  4. postMessage({type: "log", data: msg});
  5. }
  6. };
  7. var window = {
  8. console: console
  9. };
  10. var normalizeModule = function(parentId, moduleName) {
  11. // normalize plugin requires
  12. if (moduleName.indexOf("!") !== -1) {
  13. var chunks = moduleName.split("!");
  14. return normalizeModule(parentId, chunks[0]) + "!" + normalizeModule(parentId, chunks[1]);
  15. }
  16. // normalize relative requires
  17. if (moduleName.charAt(0) == ".") {
  18. var base = parentId.split("/").slice(0, -1).join("/");
  19. var moduleName = base + "/" + moduleName;
  20. while(moduleName.indexOf(".") !== -1 && previous != moduleName) {
  21. var previous = moduleName;
  22. var moduleName = moduleName.replace(/\/\.\//, "/").replace(/[^\/]+\/\.\.\//, "");
  23. }
  24. }
  25. return moduleName;
  26. };
  27. var require = function(parentId, id) {
  28. var id = normalizeModule(parentId, id);
  29. var module = require.modules[id];
  30. if (module) {
  31. if (!module.initialized) {
  32. module.exports = module.factory().exports;
  33. module.initialized = true;
  34. }
  35. return module.exports;
  36. }
  37. var chunks = id.split("/");
  38. chunks[0] = require.tlns[chunks[0]] || chunks[0];
  39. var path = chunks.join("/") + ".js";
  40. require.id = id;
  41. importScripts(path);
  42. return require(parentId, id);
  43. };
  44. require.modules = {};
  45. require.tlns = {};
  46. var define = function(id, deps, factory) {
  47. if (arguments.length == 2) {
  48. factory = deps;
  49. } else if (arguments.length == 1) {
  50. factory = id;
  51. id = require.id;
  52. }
  53. if (id.indexOf("text!") === 0)
  54. return;
  55. var req = function(deps, factory) {
  56. return require(id, deps, factory);
  57. };
  58. require.modules[id] = {
  59. factory: function() {
  60. var module = {
  61. exports: {}
  62. };
  63. var returnExports = factory(req, module.exports, module);
  64. if (returnExports)
  65. module.exports = returnExports;
  66. return module;
  67. }
  68. };
  69. };
  70. function initBaseUrls(topLevelNamespaces) {
  71. require.tlns = topLevelNamespaces;
  72. }
  73. function initSender() {
  74. var EventEmitter = require(null, "ace/lib/event_emitter").EventEmitter;
  75. var oop = require(null, "ace/lib/oop");
  76. var Sender = function() {};
  77. (function() {
  78. oop.implement(this, EventEmitter);
  79. this.callback = function(data, callbackId) {
  80. postMessage({
  81. type: "call",
  82. id: callbackId,
  83. data: data
  84. });
  85. };
  86. this.emit = function(name, data) {
  87. postMessage({
  88. type: "event",
  89. name: name,
  90. data: data
  91. });
  92. };
  93. }).call(Sender.prototype);
  94. return new Sender();
  95. }
  96. var main;
  97. var sender;
  98. onmessage = function(e) {
  99. var msg = e.data;
  100. if (msg.command) {
  101. main[msg.command].apply(main, msg.args);
  102. }
  103. else if (msg.init) {
  104. initBaseUrls(msg.tlns);
  105. require(null, "ace/lib/fixoldbrowsers");
  106. sender = initSender();
  107. var clazz = require(null, msg.module)[msg.classname];
  108. main = new clazz(sender);
  109. }
  110. else if (msg.event && sender) {
  111. sender._emit(msg.event, msg.data);
  112. }
  113. };
  114. // vim:set ts=4 sts=4 sw=4 st:
  115. // -- kriskowal Kris Kowal Copyright (C) 2009-2010 MIT License
  116. // -- tlrobinson Tom Robinson Copyright (C) 2009-2010 MIT License (Narwhal Project)
  117. // -- dantman Daniel Friesen Copyright(C) 2010 XXX No License Specified
  118. // -- fschaefer Florian Schäfer Copyright (C) 2010 MIT License
  119. // -- Irakli Gozalishvili Copyright (C) 2010 MIT License
  120. /*!
  121. Copyright (c) 2009, 280 North Inc. http://280north.com/
  122. MIT License. http://github.com/280north/narwhal/blob/master/README.md
  123. */
  124. define('ace/lib/fixoldbrowsers', ['require', 'exports', 'module' , 'ace/lib/regexp', 'ace/lib/es5-shim'], function(require, exports, module) {
  125. require("./regexp");
  126. require("./es5-shim");
  127. });
  128. define('ace/lib/regexp', ['require', 'exports', 'module' ], function(require, exports, module) {
  129. //---------------------------------
  130. // Private variables
  131. //---------------------------------
  132. var real = {
  133. exec: RegExp.prototype.exec,
  134. test: RegExp.prototype.test,
  135. match: String.prototype.match,
  136. replace: String.prototype.replace,
  137. split: String.prototype.split
  138. },
  139. compliantExecNpcg = real.exec.call(/()??/, "")[1] === undefined, // check `exec` handling of nonparticipating capturing groups
  140. compliantLastIndexIncrement = function () {
  141. var x = /^/g;
  142. real.test.call(x, "");
  143. return !x.lastIndex;
  144. }();
  145. if (compliantLastIndexIncrement && compliantExecNpcg)
  146. return;
  147. //---------------------------------
  148. // Overriden native methods
  149. //---------------------------------
  150. // Adds named capture support (with backreferences returned as `result.name`), and fixes two
  151. // cross-browser issues per ES3:
  152. // - Captured values for nonparticipating capturing groups should be returned as `undefined`,
  153. // rather than the empty string.
  154. // - `lastIndex` should not be incremented after zero-length matches.
  155. RegExp.prototype.exec = function (str) {
  156. var match = real.exec.apply(this, arguments),
  157. name, r2;
  158. if ( typeof(str) == 'string' && match) {
  159. // Fix browsers whose `exec` methods don't consistently return `undefined` for
  160. // nonparticipating capturing groups
  161. if (!compliantExecNpcg && match.length > 1 && indexOf(match, "") > -1) {
  162. r2 = RegExp(this.source, real.replace.call(getNativeFlags(this), "g", ""));
  163. // Using `str.slice(match.index)` rather than `match[0]` in case lookahead allowed
  164. // matching due to characters outside the match
  165. real.replace.call(str.slice(match.index), r2, function () {
  166. for (var i = 1; i < arguments.length - 2; i++) {
  167. if (arguments[i] === undefined)
  168. match[i] = undefined;
  169. }
  170. });
  171. }
  172. // Attach named capture properties
  173. if (this._xregexp && this._xregexp.captureNames) {
  174. for (var i = 1; i < match.length; i++) {
  175. name = this._xregexp.captureNames[i - 1];
  176. if (name)
  177. match[name] = match[i];
  178. }
  179. }
  180. // Fix browsers that increment `lastIndex` after zero-length matches
  181. if (!compliantLastIndexIncrement && this.global && !match[0].length && (this.lastIndex > match.index))
  182. this.lastIndex--;
  183. }
  184. return match;
  185. };
  186. // Don't override `test` if it won't change anything
  187. if (!compliantLastIndexIncrement) {
  188. // Fix browser bug in native method
  189. RegExp.prototype.test = function (str) {
  190. // Use the native `exec` to skip some processing overhead, even though the overriden
  191. // `exec` would take care of the `lastIndex` fix
  192. var match = real.exec.call(this, str);
  193. // Fix browsers that increment `lastIndex` after zero-length matches
  194. if (match && this.global && !match[0].length && (this.lastIndex > match.index))
  195. this.lastIndex--;
  196. return !!match;
  197. };
  198. }
  199. //---------------------------------
  200. // Private helper functions
  201. //---------------------------------
  202. function getNativeFlags (regex) {
  203. return (regex.global ? "g" : "") +
  204. (regex.ignoreCase ? "i" : "") +
  205. (regex.multiline ? "m" : "") +
  206. (regex.extended ? "x" : "") + // Proposed for ES4; included in AS3
  207. (regex.sticky ? "y" : "");
  208. };
  209. function indexOf (array, item, from) {
  210. if (Array.prototype.indexOf) // Use the native array method if available
  211. return array.indexOf(item, from);
  212. for (var i = from || 0; i < array.length; i++) {
  213. if (array[i] === item)
  214. return i;
  215. }
  216. return -1;
  217. };
  218. });
  219. // vim: ts=4 sts=4 sw=4 expandtab
  220. // -- kriskowal Kris Kowal Copyright (C) 2009-2011 MIT License
  221. // -- tlrobinson Tom Robinson Copyright (C) 2009-2010 MIT License (Narwhal Project)
  222. // -- dantman Daniel Friesen Copyright (C) 2010 XXX TODO License or CLA
  223. // -- fschaefer Florian Schäfer Copyright (C) 2010 MIT License
  224. // -- Gozala Irakli Gozalishvili Copyright (C) 2010 MIT License
  225. // -- kitcambridge Kit Cambridge Copyright (C) 2011 MIT License
  226. // -- kossnocorp Sasha Koss XXX TODO License or CLA
  227. // -- bryanforbes Bryan Forbes XXX TODO License or CLA
  228. // -- killdream Quildreen Motta Copyright (C) 2011 MIT Licence
  229. // -- michaelficarra Michael Ficarra Copyright (C) 2011 3-clause BSD License
  230. // -- sharkbrainguy Gerard Paapu Copyright (C) 2011 MIT License
  231. // -- bbqsrc Brendan Molloy (C) 2011 Creative Commons Zero (public domain)
  232. // -- iwyg XXX TODO License or CLA
  233. // -- DomenicDenicola Domenic Denicola Copyright (C) 2011 MIT License
  234. // -- xavierm02 Montillet Xavier XXX TODO License or CLA
  235. // -- Raynos Raynos XXX TODO License or CLA
  236. // -- samsonjs Sami Samhuri Copyright (C) 2010 MIT License
  237. // -- rwldrn Rick Waldron Copyright (C) 2011 MIT License
  238. // -- lexer Alexey Zakharov XXX TODO License or CLA
  239. /*!
  240. Copyright (c) 2009, 280 North Inc. http://280north.com/
  241. MIT License. http://github.com/280north/narwhal/blob/master/README.md
  242. */
  243. define('ace/lib/es5-shim', ['require', 'exports', 'module' ], function(require, exports, module) {
  244. /*
  245. * Brings an environment as close to ECMAScript 5 compliance
  246. * as is possible with the facilities of erstwhile engines.
  247. *
  248. * Annotated ES5: http://es5.github.com/ (specific links below)
  249. * ES5 Spec: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
  250. *
  251. * @module
  252. */
  253. /*whatsupdoc*/
  254. //
  255. // Function
  256. // ========
  257. //
  258. // ES-5 15.3.4.5
  259. // http://es5.github.com/#x15.3.4.5
  260. if (!Function.prototype.bind) {
  261. Function.prototype.bind = function bind(that) { // .length is 1
  262. // 1. Let Target be the this value.
  263. var target = this;
  264. // 2. If IsCallable(Target) is false, throw a TypeError exception.
  265. if (typeof target != "function")
  266. throw new TypeError(); // TODO message
  267. // 3. Let A be a new (possibly empty) internal list of all of the
  268. // argument values provided after thisArg (arg1, arg2 etc), in order.
  269. // XXX slicedArgs will stand in for "A" if used
  270. var args = slice.call(arguments, 1); // for normal call
  271. // 4. Let F be a new native ECMAScript object.
  272. // 11. Set the [[Prototype]] internal property of F to the standard
  273. // built-in Function prototype object as specified in 15.3.3.1.
  274. // 12. Set the [[Call]] internal property of F as described in
  275. // 15.3.4.5.1.
  276. // 13. Set the [[Construct]] internal property of F as described in
  277. // 15.3.4.5.2.
  278. // 14. Set the [[HasInstance]] internal property of F as described in
  279. // 15.3.4.5.3.
  280. var bound = function () {
  281. if (this instanceof bound) {
  282. // 15.3.4.5.2 [[Construct]]
  283. // When the [[Construct]] internal method of a function object,
  284. // F that was created using the bind function is called with a
  285. // list of arguments ExtraArgs, the following steps are taken:
  286. // 1. Let target be the value of F's [[TargetFunction]]
  287. // internal property.
  288. // 2. If target has no [[Construct]] internal method, a
  289. // TypeError exception is thrown.
  290. // 3. Let boundArgs be the value of F's [[BoundArgs]] internal
  291. // property.
  292. // 4. Let args be a new list containing the same values as the
  293. // list boundArgs in the same order followed by the same
  294. // values as the list ExtraArgs in the same order.
  295. // 5. Return the result of calling the [[Construct]] internal
  296. // method of target providing args as the arguments.
  297. var F = function(){};
  298. F.prototype = target.prototype;
  299. var self = new F;
  300. var result = target.apply(
  301. self,
  302. args.concat(slice.call(arguments))
  303. );
  304. if (result !== null && Object(result) === result)
  305. return result;
  306. return self;
  307. } else {
  308. // 15.3.4.5.1 [[Call]]
  309. // When the [[Call]] internal method of a function object, F,
  310. // which was created using the bind function is called with a
  311. // this value and a list of arguments ExtraArgs, the following
  312. // steps are taken:
  313. // 1. Let boundArgs be the value of F's [[BoundArgs]] internal
  314. // property.
  315. // 2. Let boundThis be the value of F's [[BoundThis]] internal
  316. // property.
  317. // 3. Let target be the value of F's [[TargetFunction]] internal
  318. // property.
  319. // 4. Let args be a new list containing the same values as the
  320. // list boundArgs in the same order followed by the same
  321. // values as the list ExtraArgs in the same order.
  322. // 5. Return the result of calling the [[Call]] internal method
  323. // of target providing boundThis as the this value and
  324. // providing args as the arguments.
  325. // equiv: target.call(this, ...boundArgs, ...args)
  326. return target.apply(
  327. that,
  328. args.concat(slice.call(arguments))
  329. );
  330. }
  331. };
  332. // XXX bound.length is never writable, so don't even try
  333. //
  334. // 15. If the [[Class]] internal property of Target is "Function", then
  335. // a. Let L be the length property of Target minus the length of A.
  336. // b. Set the length own property of F to either 0 or L, whichever is
  337. // larger.
  338. // 16. Else set the length own property of F to 0.
  339. // 17. Set the attributes of the length own property of F to the values
  340. // specified in 15.3.5.1.
  341. // TODO
  342. // 18. Set the [[Extensible]] internal property of F to true.
  343. // TODO
  344. // 19. Let thrower be the [[ThrowTypeError]] function Object (13.2.3).
  345. // 20. Call the [[DefineOwnProperty]] internal method of F with
  346. // arguments "caller", PropertyDescriptor {[[Get]]: thrower, [[Set]]:
  347. // thrower, [[Enumerable]]: false, [[Configurable]]: false}, and
  348. // false.
  349. // 21. Call the [[DefineOwnProperty]] internal method of F with
  350. // arguments "arguments", PropertyDescriptor {[[Get]]: thrower,
  351. // [[Set]]: thrower, [[Enumerable]]: false, [[Configurable]]: false},
  352. // and false.
  353. // TODO
  354. // NOTE Function objects created using Function.prototype.bind do not
  355. // have a prototype property or the [[Code]], [[FormalParameters]], and
  356. // [[Scope]] internal properties.
  357. // XXX can't delete prototype in pure-js.
  358. // 22. Return F.
  359. return bound;
  360. };
  361. }
  362. // Shortcut to an often accessed properties, in order to avoid multiple
  363. // dereference that costs universally.
  364. // _Please note: Shortcuts are defined after `Function.prototype.bind` as we
  365. // us it in defining shortcuts.
  366. var call = Function.prototype.call;
  367. var prototypeOfArray = Array.prototype;
  368. var prototypeOfObject = Object.prototype;
  369. var slice = prototypeOfArray.slice;
  370. var toString = call.bind(prototypeOfObject.toString);
  371. var owns = call.bind(prototypeOfObject.hasOwnProperty);
  372. // If JS engine supports accessors creating shortcuts.
  373. var defineGetter;
  374. var defineSetter;
  375. var lookupGetter;
  376. var lookupSetter;
  377. var supportsAccessors;
  378. if ((supportsAccessors = owns(prototypeOfObject, "__defineGetter__"))) {
  379. defineGetter = call.bind(prototypeOfObject.__defineGetter__);
  380. defineSetter = call.bind(prototypeOfObject.__defineSetter__);
  381. lookupGetter = call.bind(prototypeOfObject.__lookupGetter__);
  382. lookupSetter = call.bind(prototypeOfObject.__lookupSetter__);
  383. }
  384. //
  385. // Array
  386. // =====
  387. //
  388. // ES5 15.4.3.2
  389. // http://es5.github.com/#x15.4.3.2
  390. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray
  391. if (!Array.isArray) {
  392. Array.isArray = function isArray(obj) {
  393. return toString(obj) == "[object Array]";
  394. };
  395. }
  396. // The IsCallable() check in the Array functions
  397. // has been replaced with a strict check on the
  398. // internal class of the object to trap cases where
  399. // the provided function was actually a regular
  400. // expression literal, which in V8 and
  401. // JavaScriptCore is a typeof "function". Only in
  402. // V8 are regular expression literals permitted as
  403. // reduce parameters, so it is desirable in the
  404. // general case for the shim to match the more
  405. // strict and common behavior of rejecting regular
  406. // expressions.
  407. // ES5 15.4.4.18
  408. // http://es5.github.com/#x15.4.4.18
  409. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/forEach
  410. if (!Array.prototype.forEach) {
  411. Array.prototype.forEach = function forEach(fun /*, thisp*/) {
  412. var self = toObject(this),
  413. thisp = arguments[1],
  414. i = 0,
  415. length = self.length >>> 0;
  416. // If no callback function or if callback is not a callable function
  417. if (toString(fun) != "[object Function]") {
  418. throw new TypeError(); // TODO message
  419. }
  420. while (i < length) {
  421. if (i in self) {
  422. // Invoke the callback function with call, passing arguments:
  423. // context, property value, property key, thisArg object context
  424. fun.call(thisp, self[i], i, self);
  425. }
  426. i++;
  427. }
  428. };
  429. }
  430. // ES5 15.4.4.19
  431. // http://es5.github.com/#x15.4.4.19
  432. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/map
  433. if (!Array.prototype.map) {
  434. Array.prototype.map = function map(fun /*, thisp*/) {
  435. var self = toObject(this),
  436. length = self.length >>> 0,
  437. result = Array(length),
  438. thisp = arguments[1];
  439. // If no callback function or if callback is not a callable function
  440. if (toString(fun) != "[object Function]") {
  441. throw new TypeError(); // TODO message
  442. }
  443. for (var i = 0; i < length; i++) {
  444. if (i in self)
  445. result[i] = fun.call(thisp, self[i], i, self);
  446. }
  447. return result;
  448. };
  449. }
  450. // ES5 15.4.4.20
  451. // http://es5.github.com/#x15.4.4.20
  452. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter
  453. if (!Array.prototype.filter) {
  454. Array.prototype.filter = function filter(fun /*, thisp */) {
  455. var self = toObject(this),
  456. length = self.length >>> 0,
  457. result = [],
  458. thisp = arguments[1];
  459. // If no callback function or if callback is not a callable function
  460. if (toString(fun) != "[object Function]") {
  461. throw new TypeError(); // TODO message
  462. }
  463. for (var i = 0; i < length; i++) {
  464. if (i in self && fun.call(thisp, self[i], i, self))
  465. result.push(self[i]);
  466. }
  467. return result;
  468. };
  469. }
  470. // ES5 15.4.4.16
  471. // http://es5.github.com/#x15.4.4.16
  472. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every
  473. if (!Array.prototype.every) {
  474. Array.prototype.every = function every(fun /*, thisp */) {
  475. var self = toObject(this),
  476. length = self.length >>> 0,
  477. thisp = arguments[1];
  478. // If no callback function or if callback is not a callable function
  479. if (toString(fun) != "[object Function]") {
  480. throw new TypeError(); // TODO message
  481. }
  482. for (var i = 0; i < length; i++) {
  483. if (i in self && !fun.call(thisp, self[i], i, self))
  484. return false;
  485. }
  486. return true;
  487. };
  488. }
  489. // ES5 15.4.4.17
  490. // http://es5.github.com/#x15.4.4.17
  491. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
  492. if (!Array.prototype.some) {
  493. Array.prototype.some = function some(fun /*, thisp */) {
  494. var self = toObject(this),
  495. length = self.length >>> 0,
  496. thisp = arguments[1];
  497. // If no callback function or if callback is not a callable function
  498. if (toString(fun) != "[object Function]") {
  499. throw new TypeError(); // TODO message
  500. }
  501. for (var i = 0; i < length; i++) {
  502. if (i in self && fun.call(thisp, self[i], i, self))
  503. return true;
  504. }
  505. return false;
  506. };
  507. }
  508. // ES5 15.4.4.21
  509. // http://es5.github.com/#x15.4.4.21
  510. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduce
  511. if (!Array.prototype.reduce) {
  512. Array.prototype.reduce = function reduce(fun /*, initial*/) {
  513. var self = toObject(this),
  514. length = self.length >>> 0;
  515. // If no callback function or if callback is not a callable function
  516. if (toString(fun) != "[object Function]") {
  517. throw new TypeError(); // TODO message
  518. }
  519. // no value to return if no initial value and an empty array
  520. if (!length && arguments.length == 1)
  521. throw new TypeError(); // TODO message
  522. var i = 0;
  523. var result;
  524. if (arguments.length >= 2) {
  525. result = arguments[1];
  526. } else {
  527. do {
  528. if (i in self) {
  529. result = self[i++];
  530. break;
  531. }
  532. // if array contains no values, no initial value to return
  533. if (++i >= length)
  534. throw new TypeError(); // TODO message
  535. } while (true);
  536. }
  537. for (; i < length; i++) {
  538. if (i in self)
  539. result = fun.call(void 0, result, self[i], i, self);
  540. }
  541. return result;
  542. };
  543. }
  544. // ES5 15.4.4.22
  545. // http://es5.github.com/#x15.4.4.22
  546. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduceRight
  547. if (!Array.prototype.reduceRight) {
  548. Array.prototype.reduceRight = function reduceRight(fun /*, initial*/) {
  549. var self = toObject(this),
  550. length = self.length >>> 0;
  551. // If no callback function or if callback is not a callable function
  552. if (toString(fun) != "[object Function]") {
  553. throw new TypeError(); // TODO message
  554. }
  555. // no value to return if no initial value, empty array
  556. if (!length && arguments.length == 1)
  557. throw new TypeError(); // TODO message
  558. var result, i = length - 1;
  559. if (arguments.length >= 2) {
  560. result = arguments[1];
  561. } else {
  562. do {
  563. if (i in self) {
  564. result = self[i--];
  565. break;
  566. }
  567. // if array contains no values, no initial value to return
  568. if (--i < 0)
  569. throw new TypeError(); // TODO message
  570. } while (true);
  571. }
  572. do {
  573. if (i in this)
  574. result = fun.call(void 0, result, self[i], i, self);
  575. } while (i--);
  576. return result;
  577. };
  578. }
  579. // ES5 15.4.4.14
  580. // http://es5.github.com/#x15.4.4.14
  581. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
  582. if (!Array.prototype.indexOf) {
  583. Array.prototype.indexOf = function indexOf(sought /*, fromIndex */ ) {
  584. var self = toObject(this),
  585. length = self.length >>> 0;
  586. if (!length)
  587. return -1;
  588. var i = 0;
  589. if (arguments.length > 1)
  590. i = toInteger(arguments[1]);
  591. // handle negative indices
  592. i = i >= 0 ? i : Math.max(0, length + i);
  593. for (; i < length; i++) {
  594. if (i in self && self[i] === sought) {
  595. return i;
  596. }
  597. }
  598. return -1;
  599. };
  600. }
  601. // ES5 15.4.4.15
  602. // http://es5.github.com/#x15.4.4.15
  603. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/lastIndexOf
  604. if (!Array.prototype.lastIndexOf) {
  605. Array.prototype.lastIndexOf = function lastIndexOf(sought /*, fromIndex */) {
  606. var self = toObject(this),
  607. length = self.length >>> 0;
  608. if (!length)
  609. return -1;
  610. var i = length - 1;
  611. if (arguments.length > 1)
  612. i = Math.min(i, toInteger(arguments[1]));
  613. // handle negative indices
  614. i = i >= 0 ? i : length - Math.abs(i);
  615. for (; i >= 0; i--) {
  616. if (i in self && sought === self[i])
  617. return i;
  618. }
  619. return -1;
  620. };
  621. }
  622. //
  623. // Object
  624. // ======
  625. //
  626. // ES5 15.2.3.2
  627. // http://es5.github.com/#x15.2.3.2
  628. if (!Object.getPrototypeOf) {
  629. // https://github.com/kriskowal/es5-shim/issues#issue/2
  630. // http://ejohn.org/blog/objectgetprototypeof/
  631. // recommended by fschaefer on github
  632. Object.getPrototypeOf = function getPrototypeOf(object) {
  633. return object.__proto__ || (
  634. object.constructor ?
  635. object.constructor.prototype :
  636. prototypeOfObject
  637. );
  638. };
  639. }
  640. // ES5 15.2.3.3
  641. // http://es5.github.com/#x15.2.3.3
  642. if (!Object.getOwnPropertyDescriptor) {
  643. var ERR_NON_OBJECT = "Object.getOwnPropertyDescriptor called on a " +
  644. "non-object: ";
  645. Object.getOwnPropertyDescriptor = function getOwnPropertyDescriptor(object, property) {
  646. if ((typeof object != "object" && typeof object != "function") || object === null)
  647. throw new TypeError(ERR_NON_OBJECT + object);
  648. // If object does not owns property return undefined immediately.
  649. if (!owns(object, property))
  650. return;
  651. var descriptor, getter, setter;
  652. // If object has a property then it's for sure both `enumerable` and
  653. // `configurable`.
  654. descriptor = { enumerable: true, configurable: true };
  655. // If JS engine supports accessor properties then property may be a
  656. // getter or setter.
  657. if (supportsAccessors) {
  658. // Unfortunately `__lookupGetter__` will return a getter even
  659. // if object has own non getter property along with a same named
  660. // inherited getter. To avoid misbehavior we temporary remove
  661. // `__proto__` so that `__lookupGetter__` will return getter only
  662. // if it's owned by an object.
  663. var prototype = object.__proto__;
  664. object.__proto__ = prototypeOfObject;
  665. var getter = lookupGetter(object, property);
  666. var setter = lookupSetter(object, property);
  667. // Once we have getter and setter we can put values back.
  668. object.__proto__ = prototype;
  669. if (getter || setter) {
  670. if (getter) descriptor.get = getter;
  671. if (setter) descriptor.set = setter;
  672. // If it was accessor property we're done and return here
  673. // in order to avoid adding `value` to the descriptor.
  674. return descriptor;
  675. }
  676. }
  677. // If we got this far we know that object has an own property that is
  678. // not an accessor so we set it as a value and return descriptor.
  679. descriptor.value = object[property];
  680. return descriptor;
  681. };
  682. }
  683. // ES5 15.2.3.4
  684. // http://es5.github.com/#x15.2.3.4
  685. if (!Object.getOwnPropertyNames) {
  686. Object.getOwnPropertyNames = function getOwnPropertyNames(object) {
  687. return Object.keys(object);
  688. };
  689. }
  690. // ES5 15.2.3.5
  691. // http://es5.github.com/#x15.2.3.5
  692. if (!Object.create) {
  693. Object.create = function create(prototype, properties) {
  694. var object;
  695. if (prototype === null) {
  696. object = { "__proto__": null };
  697. } else {
  698. if (typeof prototype != "object")
  699. throw new TypeError("typeof prototype["+(typeof prototype)+"] != 'object'");
  700. var Type = function () {};
  701. Type.prototype = prototype;
  702. object = new Type();
  703. // IE has no built-in implementation of `Object.getPrototypeOf`
  704. // neither `__proto__`, but this manually setting `__proto__` will
  705. // guarantee that `Object.getPrototypeOf` will work as expected with
  706. // objects created using `Object.create`
  707. object.__proto__ = prototype;
  708. }
  709. if (properties !== void 0)
  710. Object.defineProperties(object, properties);
  711. return object;
  712. };
  713. }
  714. // ES5 15.2.3.6
  715. // http://es5.github.com/#x15.2.3.6
  716. // Patch for WebKit and IE8 standard mode
  717. // Designed by hax <hax.github.com>
  718. // related issue: https://github.com/kriskowal/es5-shim/issues#issue/5
  719. // IE8 Reference:
  720. // http://msdn.microsoft.com/en-us/library/dd282900.aspx
  721. // http://msdn.microsoft.com/en-us/library/dd229916.aspx
  722. // WebKit Bugs:
  723. // https://bugs.webkit.org/show_bug.cgi?id=36423
  724. function doesDefinePropertyWork(object) {
  725. try {
  726. Object.defineProperty(object, "sentinel", {});
  727. return "sentinel" in object;
  728. } catch (exception) {
  729. // returns falsy
  730. }
  731. }
  732. // check whether defineProperty works if it's given. Otherwise,
  733. // shim partially.
  734. if (Object.defineProperty) {
  735. var definePropertyWorksOnObject = doesDefinePropertyWork({});
  736. var definePropertyWorksOnDom = typeof document == "undefined" ||
  737. doesDefinePropertyWork(document.createElement("div"));
  738. if (!definePropertyWorksOnObject || !definePropertyWorksOnDom) {
  739. var definePropertyFallback = Object.defineProperty;
  740. }
  741. }
  742. if (!Object.defineProperty || definePropertyFallback) {
  743. var ERR_NON_OBJECT_DESCRIPTOR = "Property description must be an object: ";
  744. var ERR_NON_OBJECT_TARGET = "Object.defineProperty called on non-object: "
  745. var ERR_ACCESSORS_NOT_SUPPORTED = "getters & setters can not be defined " +
  746. "on this javascript engine";
  747. Object.defineProperty = function defineProperty(object, property, descriptor) {
  748. if ((typeof object != "object" && typeof object != "function") || object === null)
  749. throw new TypeError(ERR_NON_OBJECT_TARGET + object);
  750. if ((typeof descriptor != "object" && typeof descriptor != "function") || descriptor === null)
  751. throw new TypeError(ERR_NON_OBJECT_DESCRIPTOR + descriptor);
  752. // make a valiant attempt to use the real defineProperty
  753. // for I8's DOM elements.
  754. if (definePropertyFallback) {
  755. try {
  756. return definePropertyFallback.call(Object, object, property, descriptor);
  757. } catch (exception) {
  758. // try the shim if the real one doesn't work
  759. }
  760. }
  761. // If it's a data property.
  762. if (owns(descriptor, "value")) {
  763. // fail silently if "writable", "enumerable", or "configurable"
  764. // are requested but not supported
  765. /*
  766. // alternate approach:
  767. if ( // can't implement these features; allow false but not true
  768. !(owns(descriptor, "writable") ? descriptor.writable : true) ||
  769. !(owns(descriptor, "enumerable") ? descriptor.enumerable : true) ||
  770. !(owns(descriptor, "configurable") ? descriptor.configurable : true)
  771. )
  772. throw new RangeError(
  773. "This implementation of Object.defineProperty does not " +
  774. "support configurable, enumerable, or writable."
  775. );
  776. */
  777. if (supportsAccessors && (lookupGetter(object, property) ||
  778. lookupSetter(object, property)))
  779. {
  780. // As accessors are supported only on engines implementing
  781. // `__proto__` we can safely override `__proto__` while defining
  782. // a property to make sure that we don't hit an inherited
  783. // accessor.
  784. var prototype = object.__proto__;
  785. object.__proto__ = prototypeOfObject;
  786. // Deleting a property anyway since getter / setter may be
  787. // defined on object itself.
  788. delete object[property];
  789. object[property] = descriptor.value;
  790. // Setting original `__proto__` back now.
  791. object.__proto__ = prototype;
  792. } else {
  793. object[property] = descriptor.value;
  794. }
  795. } else {
  796. if (!supportsAccessors)
  797. throw new TypeError(ERR_ACCESSORS_NOT_SUPPORTED);
  798. // If we got that far then getters and setters can be defined !!
  799. if (owns(descriptor, "get"))
  800. defineGetter(object, property, descriptor.get);
  801. if (owns(descriptor, "set"))
  802. defineSetter(object, property, descriptor.set);
  803. }
  804. return object;
  805. };
  806. }
  807. // ES5 15.2.3.7
  808. // http://es5.github.com/#x15.2.3.7
  809. if (!Object.defineProperties) {
  810. Object.defineProperties = function defineProperties(object, properties) {
  811. for (var property in properties) {
  812. if (owns(properties, property))
  813. Object.defineProperty(object, property, properties[property]);
  814. }
  815. return object;
  816. };
  817. }
  818. // ES5 15.2.3.8
  819. // http://es5.github.com/#x15.2.3.8
  820. if (!Object.seal) {
  821. Object.seal = function seal(object) {
  822. // this is misleading and breaks feature-detection, but
  823. // allows "securable" code to "gracefully" degrade to working
  824. // but insecure code.
  825. return object;
  826. };
  827. }
  828. // ES5 15.2.3.9
  829. // http://es5.github.com/#x15.2.3.9
  830. if (!Object.freeze) {
  831. Object.freeze = function freeze(object) {
  832. // this is misleading and breaks feature-detection, but
  833. // allows "securable" code to "gracefully" degrade to working
  834. // but insecure code.
  835. return object;
  836. };
  837. }
  838. // detect a Rhino bug and patch it
  839. try {
  840. Object.freeze(function () {});
  841. } catch (exception) {
  842. Object.freeze = (function freeze(freezeObject) {
  843. return function freeze(object) {
  844. if (typeof object == "function") {
  845. return object;
  846. } else {
  847. return freezeObject(object);
  848. }
  849. };
  850. })(Object.freeze);
  851. }
  852. // ES5 15.2.3.10
  853. // http://es5.github.com/#x15.2.3.10
  854. if (!Object.preventExtensions) {
  855. Object.preventExtensions = function preventExtensions(object) {
  856. // this is misleading and breaks feature-detection, but
  857. // allows "securable" code to "gracefully" degrade to working
  858. // but insecure code.
  859. return object;
  860. };
  861. }
  862. // ES5 15.2.3.11
  863. // http://es5.github.com/#x15.2.3.11
  864. if (!Object.isSealed) {
  865. Object.isSealed = function isSealed(object) {
  866. return false;
  867. };
  868. }
  869. // ES5 15.2.3.12
  870. // http://es5.github.com/#x15.2.3.12
  871. if (!Object.isFrozen) {
  872. Object.isFrozen = function isFrozen(object) {
  873. return false;
  874. };
  875. }
  876. // ES5 15.2.3.13
  877. // http://es5.github.com/#x15.2.3.13
  878. if (!Object.isExtensible) {
  879. Object.isExtensible = function isExtensible(object) {
  880. // 1. If Type(O) is not Object throw a TypeError exception.
  881. if (Object(object) === object) {
  882. throw new TypeError(); // TODO message
  883. }
  884. // 2. Return the Boolean value of the [[Extensible]] internal property of O.
  885. var name = '';
  886. while (owns(object, name)) {
  887. name += '?';
  888. }
  889. object[name] = true;
  890. var returnValue = owns(object, name);
  891. delete object[name];
  892. return returnValue;
  893. };
  894. }
  895. // ES5 15.2.3.14
  896. // http://es5.github.com/#x15.2.3.14
  897. if (!Object.keys) {
  898. // http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation
  899. var hasDontEnumBug = true,
  900. dontEnums = [
  901. "toString",
  902. "toLocaleString",
  903. "valueOf",
  904. "hasOwnProperty",
  905. "isPrototypeOf",
  906. "propertyIsEnumerable",
  907. "constructor"
  908. ],
  909. dontEnumsLength = dontEnums.length;
  910. for (var key in {"toString": null})
  911. hasDontEnumBug = false;
  912. Object.keys = function keys(object) {
  913. if ((typeof object != "object" && typeof object != "function") || object === null)
  914. throw new TypeError("Object.keys called on a non-object");
  915. var keys = [];
  916. for (var name in object) {
  917. if (owns(object, name)) {
  918. keys.push(name);
  919. }
  920. }
  921. if (hasDontEnumBug) {
  922. for (var i = 0, ii = dontEnumsLength; i < ii; i++) {
  923. var dontEnum = dontEnums[i];
  924. if (owns(object, dontEnum)) {
  925. keys.push(dontEnum);
  926. }
  927. }
  928. }
  929. return keys;
  930. };
  931. }
  932. //
  933. // Date
  934. // ====
  935. //
  936. // ES5 15.9.5.43
  937. // http://es5.github.com/#x15.9.5.43
  938. // This function returns a String value represent the instance in time
  939. // represented by this Date object. The format of the String is the Date Time
  940. // string format defined in 15.9.1.15. All fields are present in the String.
  941. // The time zone is always UTC, denoted by the suffix Z. If the time value of
  942. // this object is not a finite Number a RangeError exception is thrown.
  943. if (!Date.prototype.toISOString || (new Date(-62198755200000).toISOString().indexOf('-000001') === -1)) {
  944. Date.prototype.toISOString = function toISOString() {
  945. var result, length, value, year;
  946. if (!isFinite(this))
  947. throw new RangeError;
  948. // the date time string format is specified in 15.9.1.15.
  949. result = [this.getUTCMonth() + 1, this.getUTCDate(),
  950. this.getUTCHours(), this.getUTCMinutes(), this.getUTCSeconds()];
  951. year = this.getUTCFullYear();
  952. year = (year < 0 ? '-' : (year > 9999 ? '+' : '')) + ('00000' + Math.abs(year)).slice(0 <= year && year <= 9999 ? -4 : -6);
  953. length = result.length;
  954. while (length--) {
  955. value = result[length];
  956. // pad months, days, hours, minutes, and seconds to have two digits.
  957. if (value < 10)
  958. result[length] = "0" + value;
  959. }
  960. // pad milliseconds to have three digits.
  961. return year + "-" + result.slice(0, 2).join("-") + "T" + result.slice(2).join(":") + "." +
  962. ("000" + this.getUTCMilliseconds()).slice(-3) + "Z";
  963. }
  964. }
  965. // ES5 15.9.4.4
  966. // http://es5.github.com/#x15.9.4.4
  967. if (!Date.now) {
  968. Date.now = function now() {
  969. return new Date().getTime();
  970. };
  971. }
  972. // ES5 15.9.5.44
  973. // http://es5.github.com/#x15.9.5.44
  974. // This function provides a String representation of a Date object for use by
  975. // JSON.stringify (15.12.3).
  976. if (!Date.prototype.toJSON) {
  977. Date.prototype.toJSON = function toJSON(key) {
  978. // When the toJSON method is called with argument key, the following
  979. // steps are taken:
  980. // 1. Let O be the result of calling ToObject, giving it the this
  981. // value as its argument.
  982. // 2. Let tv be ToPrimitive(O, hint Number).
  983. // 3. If tv is a Number and is not finite, return null.
  984. // XXX
  985. // 4. Let toISO be the result of calling the [[Get]] internal method of
  986. // O with argument "toISOString".
  987. // 5. If IsCallable(toISO) is false, throw a TypeError exception.
  988. if (typeof this.toISOString != "function")
  989. throw new TypeError(); // TODO message
  990. // 6. Return the result of calling the [[Call]] internal method of
  991. // toISO with O as the this value and an empty argument list.
  992. return this.toISOString();
  993. // NOTE 1 The argument is ignored.
  994. // NOTE 2 The toJSON function is intentionally generic; it does not
  995. // require that its this value be a Date object. Therefore, it can be
  996. // transferred to other kinds of objects for use as a method. However,
  997. // it does require that any such object have a toISOString method. An
  998. // object is free to use the argument key to filter its
  999. // stringification.
  1000. };
  1001. }
  1002. // ES5 15.9.4.2
  1003. // http://es5.github.com/#x15.9.4.2
  1004. // based on work shared by Daniel Friesen (dantman)
  1005. // http://gist.github.com/303249
  1006. if (Date.parse("+275760-09-13T00:00:00.000Z") !== 8.64e15) {
  1007. // XXX global assignment won't work in embeddings that use
  1008. // an alternate object for the context.
  1009. Date = (function(NativeDate) {
  1010. // Date.length === 7
  1011. var Date = function Date(Y, M, D, h, m, s, ms) {
  1012. var length = arguments.length;
  1013. if (this instanceof NativeDate) {
  1014. var date = length == 1 && String(Y) === Y ? // isString(Y)
  1015. // We explicitly pass it through parse:
  1016. new NativeDate(Date.parse(Y)) :
  1017. // We have to manually make calls depending on argument
  1018. // length here
  1019. length >= 7 ? new NativeDate(Y, M, D, h, m, s, ms) :
  1020. length >= 6 ? new NativeDate(Y, M, D, h, m, s) :
  1021. length >= 5 ? new NativeDate(Y, M, D, h, m) :
  1022. length >= 4 ? new NativeDate(Y, M, D, h) :
  1023. length >= 3 ? new NativeDate(Y, M, D) :
  1024. length >= 2 ? new NativeDate(Y, M) :
  1025. length >= 1 ? new NativeDate(Y) :
  1026. new NativeDate();
  1027. // Prevent mixups with unfixed Date object
  1028. date.constructor = Date;
  1029. return date;
  1030. }
  1031. return NativeDate.apply(this, arguments);
  1032. };
  1033. // 15.9.1.15 Date Time String Format.
  1034. var isoDateExpression = new RegExp("^" +
  1035. "(\\d{4}|[\+\-]\\d{6})" + // four-digit year capture or sign + 6-digit extended year
  1036. "(?:-(\\d{2})" + // optional month capture
  1037. "(?:-(\\d{2})" + // optional day capture
  1038. "(?:" + // capture hours:minutes:seconds.milliseconds
  1039. "T(\\d{2})" + // hours capture
  1040. ":(\\d{2})" + // minutes capture
  1041. "(?:" + // optional :seconds.milliseconds
  1042. ":(\\d{2})" + // seconds capture
  1043. "(?:\\.(\\d{3}))?" + // milliseconds capture
  1044. ")?" +
  1045. "(?:" + // capture UTC offset component
  1046. "Z|" + // UTC capture
  1047. "(?:" + // offset specifier +/-hours:minutes
  1048. "([-+])" + // sign capture
  1049. "(\\d{2})" + // hours offset capture
  1050. ":(\\d{2})" + // minutes offset capture
  1051. ")" +
  1052. ")?)?)?)?" +
  1053. "$");
  1054. // Copy any custom methods a 3rd party library may have added
  1055. for (var key in NativeDate)
  1056. Date[key] = NativeDate[key];
  1057. // Copy "native" methods explicitly; they may be non-enumerable
  1058. Date.now = NativeDate.now;
  1059. Date.UTC = NativeDate.UTC;
  1060. Date.prototype = NativeDate.prototype;
  1061. Date.prototype.constructor = Date;
  1062. // Upgrade Date.parse to handle simplified ISO 8601 strings
  1063. Date.parse = function parse(string) {
  1064. var match = isoDateExpression.exec(string);
  1065. if (match) {
  1066. match.shift(); // kill match[0], the full match
  1067. // parse months, days, hours, minutes, seconds, and milliseconds
  1068. for (var i = 1; i < 7; i++) {
  1069. // provide default values if necessary
  1070. match[i] = +(match[i] || (i < 3 ? 1 : 0));
  1071. // match[1] is the month. Months are 0-11 in JavaScript
  1072. // `Date` objects, but 1-12 in ISO notation, so we
  1073. // decrement.
  1074. if (i == 1)
  1075. match[i]--;
  1076. }
  1077. // parse the UTC offset component
  1078. var minuteOffset = +match.pop(), hourOffset = +match.pop(), sign = match.pop();
  1079. // compute the explicit time zone offset if specified
  1080. var offset = 0;
  1081. if (sign) {
  1082. // detect invalid offsets and return early
  1083. if (hourOffset > 23 || minuteOffset > 59)
  1084. return NaN;
  1085. // express the provided time zone offset in minutes. The offset is
  1086. // negative for time zones west of UTC; positive otherwise.
  1087. offset = (hourOffset * 60 + minuteOffset) * 6e4 * (sign == "+" ? -1 : 1);
  1088. }
  1089. // Date.UTC for years between 0 and 99 converts year to 1900 + year
  1090. // The Gregorian calendar has a 400-year cycle, so
  1091. // to Date.UTC(year + 400, .... ) - 12622780800000 == Date.UTC(year, ...),
  1092. // where 12622780800000 - number of milliseconds in Gregorian calendar 400 years
  1093. var year = +match[0];
  1094. if (0 <= year && year <= 99) {
  1095. match[0] = year + 400;
  1096. return NativeDate.UTC.apply(this, match) + offset - 12622780800000;
  1097. }
  1098. // compute a new UTC date value, accounting for the optional offset
  1099. return NativeDate.UTC.apply(this, match) + offset;
  1100. }
  1101. return NativeDate.parse.apply(this, arguments);
  1102. };
  1103. return Date;
  1104. })(Date);
  1105. }
  1106. //
  1107. // String
  1108. // ======
  1109. //
  1110. // ES5 15.5.4.20
  1111. // http://es5.github.com/#x15.5.4.20
  1112. var ws = "\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003" +
  1113. "\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028" +
  1114. "\u2029\uFEFF";
  1115. if (!String.prototype.trim || ws.trim()) {
  1116. // http://blog.stevenlevithan.com/archives/faster-trim-javascript
  1117. // http://perfectionkills.com/whitespace-deviations/
  1118. ws = "[" + ws + "]";
  1119. var trimBeginRegexp = new RegExp("^" + ws + ws + "*"),
  1120. trimEndRegexp = new RegExp(ws + ws + "*$");
  1121. String.prototype.trim = function trim() {
  1122. return String(this).replace(trimBeginRegexp, "").replace(trimEndRegexp, "");
  1123. };
  1124. }
  1125. //
  1126. // Util
  1127. // ======
  1128. //
  1129. // ES5 9.4
  1130. // http://es5.github.com/#x9.4
  1131. // http://jsperf.com/to-integer
  1132. var toInteger = function (n) {
  1133. n = +n;
  1134. if (n !== n) // isNaN
  1135. n = 0;
  1136. else if (n !== 0 && n !== (1/0) && n !== -(1/0))
  1137. n = (n > 0 || -1) * Math.floor(Math.abs(n));
  1138. return n;
  1139. };
  1140. var prepareString = "a"[0] != "a",
  1141. // ES5 9.9
  1142. // http://es5.github.com/#x9.9
  1143. toObject = function (o) {
  1144. if (o == null) { // this matches both null and undefined
  1145. throw new TypeError(); // TODO message
  1146. }
  1147. // If the implementation doesn't support by-index access of
  1148. // string characters (ex. IE < 7), split the string
  1149. if (prepareString && typeof o == "string" && o) {
  1150. return o.split("");
  1151. }
  1152. return Object(o);
  1153. };
  1154. });
  1155. define('ace/lib/event_emitter', ['require', 'exports', 'module' ], function(require, exports, module) {
  1156. var EventEmitter = {};
  1157. EventEmitter._emit =
  1158. EventEmitter._dispatchEvent = function(eventName, e) {
  1159. this._eventRegistry = this._eventRegistry || {};
  1160. this._defaultHandlers = this._defaultHandlers || {};
  1161. var listeners = this._eventRegistry[eventName] || [];
  1162. var defaultHandler = this._defaultHandlers[eventName];
  1163. if (!listeners.length && !defaultHandler)
  1164. return;
  1165. e = e || {};
  1166. e.type = eventName;
  1167. if (!e.stopPropagation) {
  1168. e.stopPropagation = function() {
  1169. this.propagationStopped = true;
  1170. };
  1171. }
  1172. if (!e.preventDefault) {
  1173. e.preventDefault = function() {
  1174. this.defaultPrevented = true;
  1175. };
  1176. }
  1177. for (var i=0; i<listeners.length; i++) {
  1178. listeners[i](e);
  1179. if (e.propagationStopped)
  1180. break;
  1181. }
  1182. if (defaultHandler && !e.defaultPrevented)
  1183. return defaultHandler(e);
  1184. };
  1185. EventEmitter.setDefaultHandler = function(eventName, callback) {
  1186. this._defaultHandlers = this._defaultHandlers || {};
  1187. if (this._defaultHandlers[eventName])
  1188. throw new Error("The default handler for '" + eventName + "' is already set");
  1189. this._defaultHandlers[eventName] = callback;
  1190. };
  1191. EventEmitter.on =
  1192. EventEmitter.addEventListener = function(eventName, callback) {
  1193. this._eventRegistry = this._eventRegistry || {};
  1194. var listeners = this._eventRegistry[eventName];
  1195. if (!listeners)
  1196. var listeners = this._eventRegistry[eventName] = [];
  1197. if (listeners.indexOf(callback) == -1)
  1198. listeners.push(callback);
  1199. };
  1200. EventEmitter.removeListener =
  1201. EventEmitter.removeEventListener = function(eventName, callback) {
  1202. this._eventRegistry = this._eventRegistry || {};
  1203. var listeners = this._eventRegistry[eventName];
  1204. if (!listeners)
  1205. return;
  1206. var index = listeners.indexOf(callback);
  1207. if (index !== -1)
  1208. listeners.splice(index, 1);
  1209. };
  1210. EventEmitter.removeAllListeners = function(eventName) {
  1211. if (this._eventRegistry) this._eventRegistry[eventName] = [];
  1212. };
  1213. exports.EventEmitter = EventEmitter;
  1214. });
  1215. define('ace/lib/oop', ['require', 'exports', 'module' ], function(require, exports, module) {
  1216. exports.inherits = (function() {
  1217. var tempCtor = function() {};
  1218. return function(ctor, superCtor) {
  1219. tempCtor.prototype = superCtor.prototype;
  1220. ctor.super_ = superCtor.prototype;
  1221. ctor.prototype = new tempCtor();
  1222. ctor.prototype.constructor = ctor;
  1223. };
  1224. }());
  1225. exports.mixin = function(obj, mixin) {
  1226. for (var key in mixin) {
  1227. obj[key] = mixin[key];
  1228. }
  1229. };
  1230. exports.implement = function(proto, mixin) {
  1231. exports.mixin(proto, mixin);
  1232. };
  1233. });
  1234. define('ace/mode/json_worker', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/worker/mirror', 'ace/mode/json/json_parse'], function(require, exports, module) {
  1235. var oop = require("../lib/oop");
  1236. var Mirror = require("../worker/mirror").Mirror;
  1237. var parse = require("./json/json_parse");
  1238. var JsonWorker = exports.JsonWorker = function(sender) {
  1239. Mirror.call(this, sender);
  1240. this.setTimeout(200);
  1241. };
  1242. oop.inherits(JsonWorker, Mirror);
  1243. (function() {
  1244. this.onUpdate = function() {
  1245. var value = this.doc.getValue();
  1246. try {
  1247. var result = parse(value);
  1248. } catch (e) {
  1249. var pos = this.charToDocumentPosition(e.at-1);
  1250. this.sender.emit("error", {
  1251. row: pos.row,
  1252. column: pos.column,
  1253. text: e.message,
  1254. type: "error"
  1255. });
  1256. return;
  1257. }
  1258. this.sender.emit("ok");
  1259. };
  1260. this.charToDocumentPosition = function(charPos) {
  1261. var i = 0;
  1262. var len = this.doc.getLength();
  1263. var nl = this.doc.getNewLineCharacter().length;
  1264. if (!len) {
  1265. return { row: 0, column: 0};
  1266. }
  1267. var lineStart = 0;
  1268. while (i < len) {
  1269. var line = this.doc.getLine(i);
  1270. var lineLength = line.length + nl;
  1271. if (lineStart + lineLength > charPos)
  1272. return {
  1273. row: i,
  1274. column: charPos - lineStart
  1275. };
  1276. lineStart += lineLength;
  1277. i += 1;
  1278. }
  1279. return {
  1280. row: i-1,
  1281. column: line.length
  1282. };
  1283. };
  1284. }).call(JsonWorker.prototype);
  1285. });
  1286. define('ace/worker/mirror', ['require', 'exports', 'module' , 'ace/document', 'ace/lib/lang'], function(require, exports, module) {
  1287. var Document = require("../document").Document;
  1288. var lang = require("../lib/lang");
  1289. var Mirror = exports.Mirror = function(sender) {
  1290. this.sender = sender;
  1291. var doc = this.doc = new Document("");
  1292. var deferredUpdate = this.deferredUpdate = lang.deferredCall(this.onUpdate.bind(this));
  1293. var _self = this;
  1294. sender.on("change", function(e) {
  1295. doc.applyDeltas([e.data]);
  1296. deferredUpdate.schedule(_self.$timeout);
  1297. });
  1298. };
  1299. (function() {
  1300. this.$timeout = 500;
  1301. this.setTimeout = function(timeout) {
  1302. this.$timeout = timeout;
  1303. };
  1304. this.setValue = function(value) {
  1305. this.doc.setValue(value);
  1306. this.deferredUpdate.schedule(this.$timeout);
  1307. };
  1308. this.getValue = function(callbackId) {
  1309. this.sender.callback(this.doc.getValue(), callbackId);
  1310. };
  1311. this.onUpdate = function() {
  1312. // abstract method
  1313. };
  1314. }).call(Mirror.prototype);
  1315. });
  1316. define('ace/document', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/lib/event_emitter', 'ace/range', 'ace/anchor'], function(require, exports, module) {
  1317. var oop = require("./lib/oop");
  1318. var EventEmitter = require("./lib/event_emitter").EventEmitter;
  1319. var Range = require("./range").Range;
  1320. var Anchor = require("./anchor").Anchor;
  1321. /**
  1322. * new Document([text])
  1323. * - text (String | Array): The starting text
  1324. *
  1325. * Creates a new `Document`. If `text` is included, the `Document` contains those strings; otherwise, it's empty.
  1326. *
  1327. **/
  1328. var Document = function(text) {
  1329. this.$lines = [];
  1330. // There has to be one line at least in the document. If you pass an empty
  1331. // string to the insert function, nothing will happen. Workaround.
  1332. if (text.length == 0) {
  1333. this.$lines = [""];
  1334. } else if (Array.isArray(text)) {
  1335. this.insertLines(0, text);
  1336. } else {
  1337. this.insert({row: 0, column:0}, text);
  1338. }
  1339. };
  1340. (function() {
  1341. oop.implement(this, EventEmitter);
  1342. this.setValue = function(text) {
  1343. var len = this.getLength();
  1344. this.remove(new Range(0, 0, len, this.getLine(len-1).length));
  1345. this.insert({row: 0, column:0}, text);
  1346. };
  1347. this.getValue = function() {
  1348. return this.getAllLines().join(this.getNewLineCharacter());
  1349. };
  1350. this.createAnchor = function(row, column) {
  1351. return new Anchor(this, row, column);
  1352. };
  1353. // check for IE split bug
  1354. if ("aaa".split(/a/).length == 0)
  1355. this.$split = function(text) {
  1356. return text.replace(/\r\n|\r/g, "\n").split("\n");
  1357. }
  1358. else
  1359. this.$split = function(text) {
  1360. return text.split(/\r\n|\r|\n/);
  1361. };
  1362. this.$detectNewLine = function(text) {
  1363. var match = text.match(/^.*?(\r\n|\r|\n)/m);
  1364. if (match) {
  1365. this.$autoNewLine = match[1];
  1366. } else {
  1367. this.$autoNewLine = "\n";
  1368. }
  1369. };
  1370. this.getNewLineCharacter = function() {
  1371. switch (this.$newLineMode) {
  1372. case "windows":
  1373. return "\r\n";
  1374. case "unix":
  1375. return "\n";
  1376. case "auto":
  1377. return this.$autoNewLine;
  1378. }
  1379. };
  1380. this.$autoNewLine = "\n";
  1381. this.$newLineMode = "auto";
  1382. this.setNewLineMode = function(newLineMode) {
  1383. if (this.$newLineMode === newLineMode)
  1384. return;
  1385. this.$newLineMode = newLineMode;
  1386. };
  1387. this.getNewLineMode = function() {
  1388. return this.$newLineMode;
  1389. };
  1390. this.isNewLine = function(text) {
  1391. return (text == "\r\n" || text == "\r" || text == "\n");
  1392. };
  1393. this.getLine = function(row) {
  1394. return this.$lines[row] || "";
  1395. };
  1396. this.getLines = function(firstRow, lastRow) {
  1397. return this.$lines.slice(firstRow, lastRow + 1);
  1398. };
  1399. this.getAllLines = function() {
  1400. return this.getLines(0, this.getLength());
  1401. };
  1402. this.getLength = function() {
  1403. return this.$lines.length;
  1404. };
  1405. this.getTextRange = function(range) {
  1406. if (range.start.row == range.end.row) {
  1407. return this.$lines[range.start.row].substring(range.start.column,
  1408. range.end.column);
  1409. }
  1410. else {
  1411. var lines = this.getLines(range.start.row+1, range.end.row-1);
  1412. lines.unshift((this.$lines[range.start.row] || "").substring(range.start.column));
  1413. lines.push((this.$lines[range.end.row] || "").substring(0, range.end.column));
  1414. return lines.join(this.getNewLineCharacter());
  1415. }
  1416. };
  1417. this.$clipPosition = function(position) {
  1418. var length = this.getLength();
  1419. if (position.row >= length) {
  1420. position.row = Math.max(0, length - 1);
  1421. position.column = this.getLine(length-1).length;
  1422. }
  1423. return position;
  1424. };
  1425. this.insert = function(position, text) {
  1426. if (!text || text.length === 0)
  1427. return position;
  1428. position = this.$clipPosition(position);
  1429. // only detect new lines if the document has no line break yet
  1430. if (this.getLength() <= 1)
  1431. this.$detectNewLine(text);
  1432. var lines = this.$split(text);
  1433. var firstLine = lines.splice(0, 1)[0];
  1434. var lastLine = lines.length == 0 ? null : lines.splice(lines.length - 1, 1)[0];
  1435. position = this.insertInLine(position, firstLine);
  1436. if (lastLine !== null) {
  1437. position = this.insertNewLine(position); // terminate first line
  1438. position = this.insertLines(position.row, lines);
  1439. position = this.insertInLine(position, lastLine || "");
  1440. }
  1441. return position;
  1442. };
  1443. this.insertLines = function(row, lines) {
  1444. if (lines.length == 0)
  1445. return {row: row, column: 0};
  1446. // apply doesn't work for big arrays (smallest threshold is on safari 0xFFFF)
  1447. // to circumvent that we have to break huge inserts into smaller chunks here
  1448. if (lines.length > 0xFFFF) {
  1449. var end = this.insertLines(row, lines.slice(0xFFFF));
  1450. lines = lines.slice(0, 0xFFFF);
  1451. }
  1452. var args = [row, 0];
  1453. args.push.apply(args, lines);
  1454. this.$lines.splice.apply(this.$lines, args);
  1455. var range = new Range(row, 0, row + lines.length, 0);
  1456. var delta = {
  1457. action: "insertLines",
  1458. range: range,
  1459. lines: lines
  1460. };
  1461. this._emit("change", { data: delta });
  1462. return end || range.end;
  1463. };
  1464. this.insertNewLine = function(position) {
  1465. position = this.$clipPosition(position);
  1466. var line = this.$lines[position.row] || "";
  1467. this.$lines[position.row] = line.substring(0, position.column);
  1468. this.$lines.splice(position.row + 1, 0, line.substring(position.column, line.length));
  1469. var end = {
  1470. row : position.row + 1,
  1471. column : 0
  1472. };
  1473. var delta = {
  1474. action: "insertText",
  1475. range: Range.fromPoints(position, end),
  1476. text: this.getNewLineCharacter()
  1477. };
  1478. this._emit("change", { data: delta });
  1479. return end;
  1480. };
  1481. this.insertInLine = function(position, text) {
  1482. if (text.length == 0)
  1483. return position;
  1484. var line = this.$lines[position.row] || "";
  1485. this.$lines[position.row] = line.substring(0, position.column) + text
  1486. + line.substring(position.column);
  1487. var end = {
  1488. row : position.row,
  1489. column : position.column + text.length
  1490. };
  1491. var delta = {
  1492. action: "insertText",
  1493. range: Range.fromPoints(position, end),
  1494. text: text
  1495. };
  1496. this._emit("change", { data: delta });
  1497. return end;
  1498. };
  1499. this.remove = function(range) {
  1500. // clip to document
  1501. range.start = this.$clipPosition(range.start);
  1502. range.end = this.$clipPosition(range.end);
  1503. if (range.isEmpty())
  1504. return range.start;
  1505. var firstRow = range.start.row;
  1506. var lastRow = range.end.row;
  1507. if (range.isMultiLine()) {
  1508. var firstFullRow = range.start.column == 0 ? firstRow : firstRow + 1;
  1509. var lastFullRow = lastRow - 1;
  1510. if (range.end.column > 0)
  1511. this.removeInLine(lastRow, 0, range.end.column);
  1512. if (lastFullRow >= firstFullRow)
  1513. this.removeLines(firstFullRow, lastFullRow);
  1514. if (firstFullRow != firstRow) {
  1515. this.removeInLine(firstRow, range.start.column, this.getLine(firstRow).length);
  1516. this.removeNewLine(range.start.row);
  1517. }
  1518. }
  1519. else {
  1520. this.removeInLine(firstRow, range.start.column, range.end.column);
  1521. }
  1522. return range.start;
  1523. };
  1524. this.removeInLine = function(row, startColumn, endColumn) {
  1525. if (startColumn == endColumn)
  1526. return;
  1527. var range = new Range(row, startColumn, row, endColumn);
  1528. var line = this.getLine(row);
  1529. var removed = line.substring(startColumn, endColumn);
  1530. var newLine = line.substring(0, startColumn) + line.substring(endColumn, line.length);
  1531. this.$lines.splice(row, 1, newLine);
  1532. var delta = {
  1533. action: "removeText",
  1534. range: range,
  1535. text: removed
  1536. };
  1537. this._emit("change", { data: delta });
  1538. return range.start;
  1539. };
  1540. this.removeLines = function(firstRow, lastRow) {
  1541. var range = new Range(firstRow, 0, lastRow + 1, 0);
  1542. var removed = this.$lines.splice(firstRow, lastRow - firstRow + 1);
  1543. var delta = {
  1544. action: "removeLines",
  1545. range: range,
  1546. nl: this.getNewLineCharacter(),
  1547. lines: removed
  1548. };
  1549. this._emit("change", { data: delta });
  1550. return removed;
  1551. };
  1552. this.removeNewLine = function(row) {
  1553. var firstLine = this.getLine(row);
  1554. var secondLine = this.getLine(row+1);
  1555. var range = new Range(row, firstLine.length, row+1, 0);
  1556. var line = firstLine + secondLine;
  1557. this.$lines.splice(row, 2, line);
  1558. var delta = {
  1559. action: "removeText",
  1560. range: range,
  1561. text: this.getNewLineCharacter()
  1562. };
  1563. this._emit("change", { data: delta });
  1564. };
  1565. this.replace = function(range, text) {
  1566. if (text.length == 0 && range.isEmpty())
  1567. return range.start;
  1568. // Shortcut: If the text we want to insert is the same as it is already
  1569. // in the document, we don't have to replace anything.
  1570. if (text == this.getTextRange(range))
  1571. return range.end;
  1572. this.remove(range);
  1573. if (text) {
  1574. var end = this.insert(range.start, text);
  1575. }
  1576. else {
  1577. end = range.start;
  1578. }
  1579. return end;
  1580. };
  1581. this.applyDeltas = function(deltas) {
  1582. for (var i=0; i<deltas.length; i++) {
  1583. var delta = deltas[i];
  1584. var range = Range.fromPoints(delta.range.start, delta.range.end);
  1585. if (delta.action == "insertLines")
  1586. this.insertLines(range.start.row, delta.lines);
  1587. else if (delta.action == "insertText")
  1588. this.insert(range.start, delta.text);
  1589. else if (delta.action == "removeLines")
  1590. this.removeLines(range.start.row, range.end.row - 1);
  1591. else if (delta.action == "removeText")
  1592. this.remove(range);
  1593. }
  1594. };
  1595. this.revertDeltas = function(deltas) {
  1596. for (var i=deltas.length-1; i>=0; i--) {
  1597. var delta = deltas[i];
  1598. var range = Range.fromPoints(delta.range.start, delta.range.end);
  1599. if (delta.action == "insertLines")
  1600. this.removeLines(range.start.row, range.end.row - 1);
  1601. else if (delta.action == "insertText")
  1602. this.remove(range);
  1603. else if (delta.action == "removeLines")
  1604. this.insertLines(range.start.row, delta.lines);
  1605. else if (delta.action == "removeText")
  1606. this.insert(range.start, delta.text);
  1607. }
  1608. };
  1609. }).call(Document.prototype);
  1610. exports.Document = Document;
  1611. });
  1612. define('ace/range', ['require', 'exports', 'module' ], function(require, exports, module) {
  1613. /**
  1614. * class Range
  1615. *
  1616. * This object is used in various places to indicate a region within the editor. To better visualize how this works, imagine a rectangle. Each quadrant of the rectangle is analogus to a range, as ranges contain a starting row and starting column, and an ending row, and ending column.
  1617. *
  1618. **/
  1619. /**
  1620. * new Range(startRow, startColumn, endRow, endColumn)
  1621. * - startRow (Number): The starting row
  1622. * - startColumn (Number): The starting column
  1623. * - endRow (Number): The ending row
  1624. * - endColumn (Number): The ending column
  1625. *
  1626. * Creates a new `Range` object with the given starting and ending row and column points.
  1627. *
  1628. **/
  1629. var Range = function(startRow, startColumn, endRow, endColumn) {
  1630. this.start = {
  1631. row: startRow,
  1632. column: startColumn
  1633. };
  1634. this.end = {
  1635. row: endRow,
  1636. column: endColumn
  1637. };
  1638. };
  1639. (function() {
  1640. /**
  1641. * Range.isEqual(range) -> Boolean
  1642. * - range (Range): A range to check against
  1643. *
  1644. * Returns `true` if and only if the starting row and column, and ending tow and column, are equivalent to those given by `range`.
  1645. *
  1646. **/
  1647. this.isEqual = function(range) {
  1648. return this.start.row == range.start.row &&
  1649. this.end.row == range.end.row &&
  1650. this.start.column == range.start.column &&
  1651. this.end.column == range.end.column
  1652. };
  1653. this.toString = function() {
  1654. return ("Range: [" + this.start.row + "/" + this.start.column +
  1655. "] -> [" + this.end.row + "/" + this.end.column + "]");
  1656. };
  1657. this.contains = function(row, column) {
  1658. return this.compare(row, column) == 0;
  1659. };
  1660. this.compareRange = function(range) {
  1661. var cmp,
  1662. end = range.end,
  1663. start = range.start;
  1664. cmp = this.compare(end.row, end.column);
  1665. if (cmp == 1) {
  1666. cmp = this.compare(start.row, start.column);
  1667. if (cmp == 1) {
  1668. return 2;
  1669. } else if (cmp == 0) {
  1670. return 1;
  1671. } else {
  1672. return 0;
  1673. }
  1674. } else if (cmp == -1) {
  1675. return -2;
  1676. } else {
  1677. cmp = this.compare(start.row, start.column);
  1678. if (cmp == -1) {
  1679. return -1;
  1680. } else if (cmp == 1) {
  1681. return 42;
  1682. } else {
  1683. return 0;
  1684. }
  1685. }
  1686. }
  1687. /** related to: Range.compare
  1688. * Range.comparePoint(p) -> Number
  1689. * - p (Range): A point to compare with
  1690. * + (Number): This method returns one of the following numbers:<br/>
  1691. * * `0` if the two points are exactly equal<br/>
  1692. * * `-1` if `p.row` is less then the calling range<br/>
  1693. * * `1` if `p.row` is greater than the calling range<br/>
  1694. * <br/>
  1695. * If the starting row of the calling range is equal to `p.row`, and:<br/>
  1696. * * `p.column` is greater than or equal to the calling range's starting column, this returns `0`<br/>
  1697. * * Otherwise, it returns -1<br/>
  1698. *<br/>
  1699. * If the ending row of the calling range is equal to `p.row`, and:<br/>
  1700. * * `p.column` is less than or equal to the calling range's ending column, this returns `0`<br/>
  1701. * * Otherwise, it returns 1<br/>
  1702. *
  1703. * Checks the row and column points of `p` with the row and column points of the calling range.
  1704. *
  1705. *
  1706. *
  1707. **/
  1708. this.comparePoint = function(p) {
  1709. return this.compare(p.row, p.column);
  1710. }
  1711. /** related to: Range.comparePoint
  1712. * Range.containsRange(range) -> Boolean
  1713. * - range (Range): A range to compare with
  1714. *
  1715. * Checks the start and end points of `range` and compares them to the calling range. Returns `true` if the `range` is contained within the caller's range.
  1716. *
  1717. **/
  1718. this.containsRange = function(range) {
  1719. return this.comparePoint(range.start) == 0 && this.comparePoint(range.end) == 0;
  1720. }
  1721. /**
  1722. * Range.intersects(range) -> Boolean
  1723. * - range (Range): A range to compare with
  1724. *
  1725. * Returns `true` if passed in `range` intersects with the one calling this method.
  1726. *
  1727. **/
  1728. this.intersects = function(range) {
  1729. var cmp = this.compareRange(range);
  1730. return (cmp == -1 || cmp == 0 || cmp == 1);
  1731. }
  1732. /**
  1733. * Range.isEnd(row, column) -> Boolean
  1734. * - row (Number): A row point to compare with
  1735. * - column (Number): A column point to compare with
  1736. *
  1737. * Returns `true` if the caller's ending row point is the same as `row`, and if the caller's ending column is the same as `column`.
  1738. *
  1739. **/
  1740. this.isEnd = function(row, column) {
  1741. return this.end.row == row && this.end.column == column;
  1742. }
  1743. /**
  1744. * Range.isStart(row, column) -> Boolean
  1745. * - row (Number): A row point to compare with
  1746. * - column (Number): A column point to compare with
  1747. *
  1748. * Returns `true` if the caller's starting row point is the same as `row`, and if the caller's starting column is the same as `column`.
  1749. *
  1750. **/
  1751. this.isStart = function(row, column) {
  1752. return this.start.row == row && this.start.column == column;
  1753. }
  1754. /**
  1755. * Range.setStart(row, column)
  1756. * - row (Number): A row point to set
  1757. * - column (Number): A column point to set
  1758. *
  1759. * Sets the starting row and column for the range.
  1760. *
  1761. **/
  1762. this.setStart = function(row, column) {
  1763. if (typeof row == "object") {
  1764. this.start.column = row.column;
  1765. this.start.row = row.row;
  1766. } else {
  1767. this.start.row = row;
  1768. this.start.column = column;
  1769. }
  1770. }
  1771. /**
  1772. * Range.setEnd(row, column)
  1773. * - row (Number): A row point to set
  1774. * - column (Number): A column point to set
  1775. *
  1776. * Sets the starting row and column for the range.
  1777. *
  1778. **/
  1779. this.setEnd = function(row, column) {
  1780. if (typeof row == "object") {
  1781. this.end.column = row.column;
  1782. this.end.row = row.row;
  1783. } else {
  1784. this.end.row = row;
  1785. this.end.column = column;
  1786. }
  1787. }
  1788. /** related to: Range.compare
  1789. * Range.inside(row, column) -> Boolean
  1790. * - row (Number): A row point to compare with
  1791. * - column (Number): A column point to compare with
  1792. *
  1793. * Returns `true` if the `row` and `column` are within the given range.
  1794. *
  1795. **/
  1796. this.inside = function(row, column) {
  1797. if (this.compare(row, column) == 0) {
  1798. if (this.isEnd(row, column) || this.isStart(row, column)) {
  1799. return false;
  1800. } else {
  1801. return true;
  1802. }
  1803. }
  1804. return false;
  1805. }
  1806. /** related to: Range.compare
  1807. * Range.insideStart(row, column) -> Boolean
  1808. * - row (Number): A row point to compare with
  1809. * - column (Number): A column point to compare with
  1810. *
  1811. * Returns `true` if the `row` and `column` are within the given range's starting points.
  1812. *
  1813. **/
  1814. this.insideStart = function(row, column) {
  1815. if (this.compare(row, column) == 0) {
  1816. if (this.isEnd(row, column)) {
  1817. return false;
  1818. } else {
  1819. return true;
  1820. }
  1821. }
  1822. return false;
  1823. }
  1824. /** related to: Range.compare
  1825. * Range.insideEnd(row, column) -> Boolean
  1826. * - row (Number): A row point to compare with
  1827. * - column (Number): A column point to compare with
  1828. *
  1829. * Returns `true` if the `row` and `column` are within the given range's ending points.
  1830. *
  1831. **/
  1832. this.insideEnd = function(row, column) {
  1833. if (this.compare(row, column) == 0) {
  1834. if (this.isStart(row, column)) {
  1835. return false;
  1836. } else {
  1837. return true;
  1838. }
  1839. }
  1840. return false;
  1841. }
  1842. /**
  1843. * Range.compare(row, column) -> Number
  1844. * - row (Number): A row point to compare with
  1845. * - column (Number): A column point to compare with
  1846. * + (Number): This method returns one of the following numbers:<br/>
  1847. * * `0` if the two points are exactly equal <br/>
  1848. * * `-1` if `p.row` is less then the calling range <br/>
  1849. * * `1` if `p.row` is greater than the calling range <br/>
  1850. * <br/>
  1851. * If the starting row of the calling range is equal to `p.row`, and: <br/>
  1852. * * `p.column` is greater than or equal to the calling range's starting column, this returns `0`<br/>
  1853. * * Otherwise, it returns -1<br/>
  1854. * <br/>
  1855. * If the ending row of the calling range is equal to `p.row`, and: <br/>
  1856. * * `p.column` is less than or equal to the calling range's ending column, this returns `0` <br/>
  1857. * * Otherwise, it returns 1
  1858. *
  1859. * Checks the row and column points with the row and column points of the calling range.
  1860. *
  1861. *
  1862. **/
  1863. this.compare = function(row, column) {
  1864. if (!this.isMultiLine()) {
  1865. if (row === this.start.row) {
  1866. return column < this.start.column ? -1 : (column > this.end.column ? 1 : 0);
  1867. };
  1868. }
  1869. if (row < this.start.row)
  1870. return -1;
  1871. if (row > this.end.row)
  1872. return 1;
  1873. if (this.start.row === row)
  1874. return column >= this.start.column ? 0 : -1;
  1875. if (this.end.row === row)
  1876. return column <= this.end.column ? 0 : 1;
  1877. return 0;
  1878. };
  1879. this.compareStart = function(row, column) {
  1880. if (this.start.row == row && this.start.column == column) {
  1881. return -1;
  1882. } else {
  1883. return this.compare(row, column);
  1884. }
  1885. }
  1886. /**
  1887. * Range.compareEnd(row, column) -> Number
  1888. * - row (Number): A row point to compare with
  1889. * - column (Number): A column point to compare with
  1890. * + (Number): This method returns one of the following numbers:<br/>
  1891. * * `0` if the two points are exactly equal<br/>
  1892. * * `-1` if `p.row` is less then the calling range<br/>
  1893. * * `1` if `p.row` is greater than the calling range, or if `isEnd` is `true.<br/>
  1894. * <br/>
  1895. * If the starting row of the calling range is equal to `p.row`, and:<br/>
  1896. * * `p.column` is greater than or equal to the calling range's starting column, this returns `0`<br/>
  1897. * * Otherwise, it returns -1<br/>
  1898. *<br/>
  1899. * If the ending row of the calling range is equal to `p.row`, and:<br/>
  1900. * * `p.column` is less than or equal to the calling range's ending column, this returns `0`<br/>
  1901. * * Otherwise, it returns 1
  1902. *
  1903. * Checks the row and column points with the row and column points of the calling range.
  1904. *
  1905. *
  1906. **/
  1907. this.compareEnd = function(row, column) {
  1908. if (this.end.row == row && this.end.column == column) {
  1909. return 1;
  1910. } else {
  1911. return this.compare(row, column);
  1912. }
  1913. }
  1914. /**
  1915. * Range.compareInside(row, column) -> Number
  1916. * - row (Number): A row point to compare with
  1917. * - column (Number): A column point to compare with
  1918. * + (Number): This method returns one of the following numbers:<br/>
  1919. * * `1` if the ending row of the calling range is equal to `row`, and the ending column of the calling range is equal to `column`<br/>
  1920. * * `-1` if the starting row of the calling range is equal to `row`, and the starting column of the calling range is equal to `column`<br/>
  1921. * <br/>
  1922. * Otherwise, it returns the value after calling [[Range.compare `compare()`]].
  1923. *
  1924. * Checks the row and column points with the row and column points of the calling range.
  1925. *
  1926. *
  1927. *
  1928. **/
  1929. this.compareInside = function(row, column) {
  1930. if (this.end.row == row && this.end.column == column) {
  1931. return 1;
  1932. } else if (this.start.row == row && this.start.column == column) {
  1933. return -1;
  1934. } else {
  1935. return this.compare(row, column);
  1936. }
  1937. }
  1938. /**
  1939. * Range.clipRows(firstRow, lastRow) -> Range
  1940. * - firstRow (Number): The starting row
  1941. * - lastRow (Number): The ending row
  1942. *
  1943. * Returns the part of the current `Range` that occurs within the boundaries of `firstRow` and `lastRow` as a new `Range` object.
  1944. *
  1945. **/
  1946. this.clipRows = function(firstRow, lastRow) {
  1947. if (this.end.row > lastRow) {
  1948. var end = {
  1949. row: lastRow+1,
  1950. column: 0
  1951. };
  1952. }
  1953. if (this.start.row > lastRow) {
  1954. var start = {
  1955. row: lastRow+1,
  1956. column: 0
  1957. };
  1958. }
  1959. if (this.start.row < firstRow) {
  1960. var start = {
  1961. row: firstRow,
  1962. column: 0
  1963. };
  1964. }
  1965. if (this.end.row < firstRow) {
  1966. var end = {
  1967. row: firstRow,
  1968. column: 0
  1969. };
  1970. }
  1971. return Range.fromPoints(start || this.start, end || this.end);
  1972. };
  1973. this.extend = function(row, column) {
  1974. var cmp = this.compare(row, column);
  1975. if (cmp == 0)
  1976. return this;
  1977. else if (cmp == -1)
  1978. var start = {row: row, column: column};
  1979. else
  1980. var end = {row: row, column: column};
  1981. return Range.fromPoints(start || this.start, end || this.end);
  1982. };
  1983. this.isEmpty = function() {
  1984. return (this.start.row == this.end.row && this.start.column == this.end.column);
  1985. };
  1986. this.isMultiLine = function() {
  1987. return (this.start.row !== this.end.row);
  1988. };
  1989. this.clone = function() {
  1990. return Range.fromPoints(this.start, this.end);
  1991. };
  1992. this.collapseRows = function() {
  1993. if (this.end.column == 0)
  1994. return new Range(this.start.row, 0, Math.max(this.start.row, this.end.row-1), 0)
  1995. else
  1996. return new Range(this.start.row, 0, this.end.row, 0)
  1997. };
  1998. this.toScreenRange = function(session) {
  1999. var screenPosStart =
  2000. session.documentToScreenPosition(this.start);
  2001. var screenPosEnd =
  2002. session.documentToScreenPosition(this.end);
  2003. return new Range(
  2004. screenPosStart.row, screenPosStart.column,
  2005. screenPosEnd.row, screenPosEnd.column
  2006. );
  2007. };
  2008. }).call(Range.prototype);
  2009. Range.fromPoints = function(start, end) {
  2010. return new Range(start.row, start.column, end.row, end.column);
  2011. };
  2012. exports.Range = Range;
  2013. });
  2014. define('ace/anchor', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/lib/event_emitter'], function(require, exports, module) {
  2015. var oop = require("./lib/oop");
  2016. var EventEmitter = require("./lib/event_emitter").EventEmitter;
  2017. /**
  2018. * new Anchor(doc, row, column)
  2019. * - doc (Document): The document to associate with the anchor
  2020. * - row (Number): The starting row position
  2021. * - column (Number): The starting column position
  2022. *
  2023. * Creates a new `Anchor` and associates it with a document.
  2024. *
  2025. **/
  2026. var Anchor = exports.Anchor = function(doc, row, column) {
  2027. this.document = doc;
  2028. if (typeof column == "undefined")
  2029. this.setPosition(row.row, row.column);
  2030. else
  2031. this.setPosition(row, column);
  2032. this.$onChange = this.onChange.bind(this);
  2033. doc.on("change", this.$onChange);
  2034. };
  2035. (function() {
  2036. oop.implement(this, EventEmitter);
  2037. this.getPosition = function() {
  2038. return this.$clipPositionToDocument(this.row, this.column);
  2039. };
  2040. this.getDocument = function() {
  2041. return this.document;
  2042. };
  2043. this.onChange = function(e) {
  2044. var delta = e.data;
  2045. var range = delta.range;
  2046. if (range.start.row == range.end.row && range.start.row != this.row)
  2047. return;
  2048. if (range.start.row > this.row)
  2049. return;
  2050. if (range.start.row == this.row && range.start.column > this.column)
  2051. return;
  2052. var row = this.row;
  2053. var column = this.column;
  2054. if (delta.action === "insertText") {
  2055. if (range.start.row === row && range.start.column <= column) {
  2056. if (range.start.row === range.end.row) {
  2057. column += range.end.column - range.start.column;
  2058. }
  2059. else {
  2060. column -= range.start.column;
  2061. row += range.end.row - range.start.row;
  2062. }
  2063. }
  2064. else if (range.start.row !== range.end.row && range.start.row < row) {
  2065. row += range.end.row - range.start.row;
  2066. }
  2067. } else if (delta.action === "insertLines") {
  2068. if (range.start.row <= row) {
  2069. row += range.end.row - range.start.row;
  2070. }
  2071. }
  2072. else if (delta.action == "removeText") {
  2073. if (range.start.row == row && range.start.column < column) {
  2074. if (range.end.column >= column)
  2075. column = range.start.column;
  2076. else
  2077. column = Math.max(0, column - (range.end.column - range.start.column));
  2078. } else if (range.start.row !== range.end.row && range.start.row < row) {
  2079. if (range.end.row == row) {
  2080. column = Math.max(0, column - range.end.column) + range.start.column;
  2081. }
  2082. row -= (range.end.row - range.start.row);
  2083. }
  2084. else if (range.end.row == row) {
  2085. row -= range.end.row - range.start.row;
  2086. column = Math.max(0, column - range.end.column) + range.start.column;
  2087. }
  2088. } else if (delta.action == "removeLines") {
  2089. if (range.start.row <= row) {
  2090. if (range.end.row <= row)
  2091. row -= range.end.row - range.start.row;
  2092. else {
  2093. row = range.start.row;
  2094. column = 0;
  2095. }
  2096. }
  2097. }
  2098. this.setPosition(row, column, true);
  2099. };
  2100. this.setPosition = function(row, column, noClip) {
  2101. var pos;
  2102. if (noClip) {
  2103. pos = {
  2104. row: row,
  2105. column: column
  2106. };
  2107. }
  2108. else {
  2109. pos = this.$clipPositionToDocument(row, column);
  2110. }
  2111. if (this.row == pos.row && this.column == pos.column)
  2112. return;
  2113. var old = {
  2114. row: this.row,
  2115. column: this.column
  2116. };
  2117. this.row = pos.row;
  2118. this.column = pos.column;
  2119. this._emit("change", {
  2120. old: old,
  2121. value: pos
  2122. });
  2123. };
  2124. this.detach = function() {
  2125. this.document.removeEventListener("change", this.$onChange);
  2126. };
  2127. this.$clipPositionToDocument = function(row, column) {
  2128. var pos = {};
  2129. if (row >= this.document.getLength()) {
  2130. pos.row = Math.max(0, this.document.getLength() - 1);
  2131. pos.column = this.document.getLine(pos.row).length;
  2132. }
  2133. else if (row < 0) {
  2134. pos.row = 0;
  2135. pos.column = 0;
  2136. }
  2137. else {
  2138. pos.row = row;
  2139. pos.column = Math.min(this.document.getLine(pos.row).length, Math.max(0, column));
  2140. }
  2141. if (column < 0)
  2142. pos.column = 0;
  2143. return pos;
  2144. };
  2145. }).call(Anchor.prototype);
  2146. });
  2147. define('ace/lib/lang', ['require', 'exports', 'module' ], function(require, exports, module) {
  2148. exports.stringReverse = function(string) {
  2149. return string.split("").reverse().join("");
  2150. };
  2151. exports.stringRepeat = function (string, count) {
  2152. return new Array(count + 1).join(string);
  2153. };
  2154. var trimBeginRegexp = /^\s\s*/;
  2155. var trimEndRegexp = /\s\s*$/;
  2156. exports.stringTrimLeft = function (string) {
  2157. return string.replace(trimBeginRegexp, '');
  2158. };
  2159. exports.stringTrimRight = function (string) {
  2160. return string.replace(trimEndRegexp, '');
  2161. };
  2162. exports.copyObject = function(obj) {
  2163. var copy = {};
  2164. for (var key in obj) {
  2165. copy[key] = obj[key];
  2166. }
  2167. return copy;
  2168. };
  2169. exports.copyArray = function(array){
  2170. var copy = [];
  2171. for (var i=0, l=array.length; i<l; i++) {
  2172. if (array[i] && typeof array[i] == "object")
  2173. copy[i] = this.copyObject( array[i] );
  2174. else
  2175. copy[i] = array[i];
  2176. }
  2177. return copy;
  2178. };
  2179. exports.deepCopy = function (obj) {
  2180. if (typeof obj != "object") {
  2181. return obj;
  2182. }
  2183. var copy = obj.constructor();
  2184. for (var key in obj) {
  2185. if (typeof obj[key] == "object") {
  2186. copy[key] = this.deepCopy(obj[key]);
  2187. } else {
  2188. copy[key] = obj[key];
  2189. }
  2190. }
  2191. return copy;
  2192. };
  2193. exports.arrayToMap = function(arr) {
  2194. var map = {};
  2195. for (var i=0; i<arr.length; i++) {
  2196. map[arr[i]] = 1;
  2197. }
  2198. return map;
  2199. };
  2200. exports.arrayRemove = function(array, value) {
  2201. for (var i = 0; i <= array.length; i++) {
  2202. if (value === array[i]) {
  2203. array.splice(i, 1);
  2204. }
  2205. }
  2206. };
  2207. exports.escapeRegExp = function(str) {
  2208. return str.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1');
  2209. };
  2210. exports.getMatchOffsets = function(string, regExp) {
  2211. var matches = [];
  2212. string.replace(regExp, function(str) {
  2213. matches.push({
  2214. offset: arguments[arguments.length-2],
  2215. length: str.length
  2216. });
  2217. });
  2218. return matches;
  2219. };
  2220. exports.deferredCall = function(fcn) {
  2221. var timer = null;
  2222. var callback = function() {
  2223. timer = null;
  2224. fcn();
  2225. };
  2226. var deferred = function(timeout) {
  2227. deferred.cancel();
  2228. timer = setTimeout(callback, timeout || 0);
  2229. return deferred;
  2230. };
  2231. deferred.schedule = deferred;
  2232. deferred.call = function() {
  2233. this.cancel();
  2234. fcn();
  2235. return deferred;
  2236. };
  2237. deferred.cancel = function() {
  2238. clearTimeout(timer);
  2239. timer = null;
  2240. return deferred;
  2241. };
  2242. return deferred;
  2243. };
  2244. });
  2245. /*members "", "\"", "\/", "\\", at, b, call, charAt, f, fromCharCode,
  2246. hasOwnProperty, message, n, name, push, r, t, text
  2247. */
  2248. define('ace/mode/json/json_parse', ['require', 'exports', 'module' ], function(require, exports, module) {
  2249. // This is a function that can parse a JSON text, producing a JavaScript
  2250. // data structure. It is a simple, recursive descent parser. It does not use
  2251. // eval or regular expressions, so it can be used as a model for implementing
  2252. // a JSON parser in other languages.
  2253. // We are defining the function inside of another function to avoid creating
  2254. // global variables.
  2255. var at, // The index of the current character
  2256. ch, // The current character
  2257. escapee = {
  2258. '"': '"',
  2259. '\\': '\\',
  2260. '/': '/',
  2261. b: '\b',
  2262. f: '\f',
  2263. n: '\n',
  2264. r: '\r',
  2265. t: '\t'
  2266. },
  2267. text,
  2268. error = function (m) {
  2269. // Call error when something is wrong.
  2270. throw {
  2271. name: 'SyntaxError',
  2272. message: m,
  2273. at: at,
  2274. text: text
  2275. };
  2276. },
  2277. next = function (c) {
  2278. // If a c parameter is provided, verify that it matches the current character.
  2279. if (c && c !== ch) {
  2280. error("Expected '" + c + "' instead of '" + ch + "'");
  2281. }
  2282. // Get the next character. When there are no more characters,
  2283. // return the empty string.
  2284. ch = text.charAt(at);
  2285. at += 1;
  2286. return ch;
  2287. },
  2288. number = function () {
  2289. // Parse a number value.
  2290. var number,
  2291. string = '';
  2292. if (ch === '-') {
  2293. string = '-';
  2294. next('-');
  2295. }
  2296. while (ch >= '0' && ch <= '9') {
  2297. string += ch;
  2298. next();
  2299. }
  2300. if (ch === '.') {
  2301. string += '.';
  2302. while (next() && ch >= '0' && ch <= '9') {
  2303. string += ch;
  2304. }
  2305. }
  2306. if (ch === 'e' || ch === 'E') {
  2307. string += ch;
  2308. next();
  2309. if (ch === '-' || ch === '+') {
  2310. string += ch;
  2311. next();
  2312. }
  2313. while (ch >= '0' && ch <= '9') {
  2314. string += ch;
  2315. next();
  2316. }
  2317. }
  2318. number = +string;
  2319. if (isNaN(number)) {
  2320. error("Bad number");
  2321. } else {
  2322. return number;
  2323. }
  2324. },
  2325. string = function () {
  2326. // Parse a string value.
  2327. var hex,
  2328. i,
  2329. string = '',
  2330. uffff;
  2331. // When parsing for string values, we must look for " and \ characters.
  2332. if (ch === '"') {
  2333. while (next()) {
  2334. if (ch === '"') {
  2335. next();
  2336. return string;
  2337. } else if (ch === '\\') {
  2338. next();
  2339. if (ch === 'u') {
  2340. uffff = 0;
  2341. for (i = 0; i < 4; i += 1) {
  2342. hex = parseInt(next(), 16);
  2343. if (!isFinite(hex)) {
  2344. break;
  2345. }
  2346. uffff = uffff * 16 + hex;
  2347. }
  2348. string += String.fromCharCode(uffff);
  2349. } else if (typeof escapee[ch] === 'string') {
  2350. string += escapee[ch];
  2351. } else {
  2352. break;
  2353. }
  2354. } else {
  2355. string += ch;
  2356. }
  2357. }
  2358. }
  2359. error("Bad string");
  2360. },
  2361. white = function () {
  2362. // Skip whitespace.
  2363. while (ch && ch <= ' ') {
  2364. next();
  2365. }
  2366. },
  2367. word = function () {
  2368. // true, false, or null.
  2369. switch (ch) {
  2370. case 't':
  2371. next('t');
  2372. next('r');
  2373. next('u');
  2374. next('e');
  2375. return true;
  2376. case 'f':
  2377. next('f');
  2378. next('a');
  2379. next('l');
  2380. next('s');
  2381. next('e');
  2382. return false;
  2383. case 'n':
  2384. next('n');
  2385. next('u');
  2386. next('l');
  2387. next('l');
  2388. return null;
  2389. }
  2390. error("Unexpected '" + ch + "'");
  2391. },
  2392. value, // Place holder for the value function.
  2393. array = function () {
  2394. // Parse an array value.
  2395. var array = [];
  2396. if (ch === '[') {
  2397. next('[');
  2398. white();
  2399. if (ch === ']') {
  2400. next(']');
  2401. return array; // empty array
  2402. }
  2403. while (ch) {
  2404. array.push(value());
  2405. white();
  2406. if (ch === ']') {
  2407. next(']');
  2408. return array;
  2409. }
  2410. next(',');
  2411. white();
  2412. }
  2413. }
  2414. error("Bad array");
  2415. },
  2416. object = function () {
  2417. // Parse an object value.
  2418. var key,
  2419. object = {};
  2420. if (ch === '{') {
  2421. next('{');
  2422. white();
  2423. if (ch === '}') {
  2424. next('}');
  2425. return object; // empty object
  2426. }
  2427. while (ch) {
  2428. key = string();
  2429. white();
  2430. next(':');
  2431. if (Object.hasOwnProperty.call(object, key)) {
  2432. error('Duplicate key "' + key + '"');
  2433. }
  2434. object[key] = value();
  2435. white();
  2436. if (ch === '}') {
  2437. next('}');
  2438. return object;
  2439. }
  2440. next(',');
  2441. white();
  2442. }
  2443. }
  2444. error("Bad object");
  2445. };
  2446. value = function () {
  2447. // Parse a JSON value. It could be an object, an array, a string, a number,
  2448. // or a word.
  2449. white();
  2450. switch (ch) {
  2451. case '{':
  2452. return object();
  2453. case '[':
  2454. return array();
  2455. case '"':
  2456. return string();
  2457. case '-':
  2458. return number();
  2459. default:
  2460. return ch >= '0' && ch <= '9' ? number() : word();
  2461. }
  2462. };
  2463. // Return the json_parse function. It will have access to all of the above
  2464. // functions and variables.
  2465. return function (source, reviver) {
  2466. var result;
  2467. text = source;
  2468. at = 0;
  2469. ch = ' ';
  2470. result = value();
  2471. white();
  2472. if (ch) {
  2473. error("Syntax error");
  2474. }
  2475. // If there is a reviver function, we recursively walk the new structure,
  2476. // passing each name/value pair to the reviver function for possible
  2477. // transformation, starting with a temporary root object that holds the result
  2478. // in an empty key. If there is not a reviver function, we simply return the
  2479. // result.
  2480. return typeof reviver === 'function' ? function walk(holder, key) {
  2481. var k, v, value = holder[key];
  2482. if (value && typeof value === 'object') {
  2483. for (k in value) {
  2484. if (Object.hasOwnProperty.call(value, k)) {
  2485. v = walk(value, k);
  2486. if (v !== undefined) {
  2487. value[k] = v;
  2488. } else {
  2489. delete value[k];
  2490. }
  2491. }
  2492. }
  2493. }
  2494. return reviver.call(holder, key, value);
  2495. }({'': result}, '') : result;
  2496. };
  2497. });