2
0

handlebars.js 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201
  1. /*
  2. Copyright (C) 2011 by Yehuda Katz
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. // lib/handlebars/base.js
  20. /*jshint eqnull:true*/
  21. this.Handlebars = {};
  22. (function(Handlebars) {
  23. Handlebars.VERSION = "1.0.0-rc.3";
  24. Handlebars.COMPILER_REVISION = 2;
  25. Handlebars.REVISION_CHANGES = {
  26. 1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it
  27. 2: '>= 1.0.0-rc.3'
  28. };
  29. Handlebars.helpers = {};
  30. Handlebars.partials = {};
  31. Handlebars.registerHelper = function(name, fn, inverse) {
  32. if(inverse) { fn.not = inverse; }
  33. this.helpers[name] = fn;
  34. };
  35. Handlebars.registerPartial = function(name, str) {
  36. this.partials[name] = str;
  37. };
  38. Handlebars.registerHelper('helperMissing', function(arg) {
  39. if(arguments.length === 2) {
  40. return undefined;
  41. } else {
  42. throw new Error("Could not find property '" + arg + "'");
  43. }
  44. });
  45. var toString = Object.prototype.toString, functionType = "[object Function]";
  46. Handlebars.registerHelper('blockHelperMissing', function(context, options) {
  47. var inverse = options.inverse || function() {}, fn = options.fn;
  48. var ret = "";
  49. var type = toString.call(context);
  50. if(type === functionType) { context = context.call(this); }
  51. if(context === true) {
  52. return fn(this);
  53. } else if(context === false || context == null) {
  54. return inverse(this);
  55. } else if(type === "[object Array]") {
  56. if(context.length > 0) {
  57. return Handlebars.helpers.each(context, options);
  58. } else {
  59. return inverse(this);
  60. }
  61. } else {
  62. return fn(context);
  63. }
  64. });
  65. Handlebars.K = function() {};
  66. Handlebars.createFrame = Object.create || function(object) {
  67. Handlebars.K.prototype = object;
  68. var obj = new Handlebars.K();
  69. Handlebars.K.prototype = null;
  70. return obj;
  71. };
  72. Handlebars.logger = {
  73. DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3,
  74. methodMap: {0: 'debug', 1: 'info', 2: 'warn', 3: 'error'},
  75. // can be overridden in the host environment
  76. log: function(level, obj) {
  77. if (Handlebars.logger.level <= level) {
  78. var method = Handlebars.logger.methodMap[level];
  79. if (typeof console !== 'undefined' && console[method]) {
  80. console[method].call(console, obj);
  81. }
  82. }
  83. }
  84. };
  85. Handlebars.log = function(level, obj) { Handlebars.logger.log(level, obj); };
  86. Handlebars.registerHelper('each', function(context, options) {
  87. var fn = options.fn, inverse = options.inverse;
  88. var i = 0, ret = "", data;
  89. if (options.data) {
  90. data = Handlebars.createFrame(options.data);
  91. }
  92. if(context && typeof context === 'object') {
  93. if(context instanceof Array){
  94. for(var j = context.length; i<j; i++) {
  95. if (data) { data.index = i; }
  96. ret = ret + fn(context[i], { data: data });
  97. }
  98. } else {
  99. for(var key in context) {
  100. if(context.hasOwnProperty(key)) {
  101. if(data) { data.key = key; }
  102. ret = ret + fn(context[key], {data: data});
  103. i++;
  104. }
  105. }
  106. }
  107. }
  108. if(i === 0){
  109. ret = inverse(this);
  110. }
  111. return ret;
  112. });
  113. Handlebars.registerHelper('if', function(context, options) {
  114. var type = toString.call(context);
  115. if(type === functionType) { context = context.call(this); }
  116. if(!context || Handlebars.Utils.isEmpty(context)) {
  117. return options.inverse(this);
  118. } else {
  119. return options.fn(this);
  120. }
  121. });
  122. Handlebars.registerHelper('unless', function(context, options) {
  123. var fn = options.fn, inverse = options.inverse;
  124. options.fn = inverse;
  125. options.inverse = fn;
  126. return Handlebars.helpers['if'].call(this, context, options);
  127. });
  128. Handlebars.registerHelper('with', function(context, options) {
  129. return options.fn(context);
  130. });
  131. Handlebars.registerHelper('log', function(context, options) {
  132. var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1;
  133. Handlebars.log(level, context);
  134. });
  135. }(this.Handlebars));
  136. ;
  137. // lib/handlebars/compiler/parser.js
  138. /* Jison generated parser */
  139. var handlebars = (function(){
  140. var parser = {trace: function trace() { },
  141. yy: {},
  142. symbols_: {"error":2,"root":3,"program":4,"EOF":5,"simpleInverse":6,"statements":7,"statement":8,"openInverse":9,"closeBlock":10,"openBlock":11,"mustache":12,"partial":13,"CONTENT":14,"COMMENT":15,"OPEN_BLOCK":16,"inMustache":17,"CLOSE":18,"OPEN_INVERSE":19,"OPEN_ENDBLOCK":20,"path":21,"OPEN":22,"OPEN_UNESCAPED":23,"OPEN_PARTIAL":24,"partialName":25,"params":26,"hash":27,"DATA":28,"param":29,"STRING":30,"INTEGER":31,"BOOLEAN":32,"hashSegments":33,"hashSegment":34,"ID":35,"EQUALS":36,"PARTIAL_NAME":37,"pathSegments":38,"SEP":39,"$accept":0,"$end":1},
  143. terminals_: {2:"error",5:"EOF",14:"CONTENT",15:"COMMENT",16:"OPEN_BLOCK",18:"CLOSE",19:"OPEN_INVERSE",20:"OPEN_ENDBLOCK",22:"OPEN",23:"OPEN_UNESCAPED",24:"OPEN_PARTIAL",28:"DATA",30:"STRING",31:"INTEGER",32:"BOOLEAN",35:"ID",36:"EQUALS",37:"PARTIAL_NAME",39:"SEP"},
  144. productions_: [0,[3,2],[4,2],[4,3],[4,2],[4,1],[4,1],[4,0],[7,1],[7,2],[8,3],[8,3],[8,1],[8,1],[8,1],[8,1],[11,3],[9,3],[10,3],[12,3],[12,3],[13,3],[13,4],[6,2],[17,3],[17,2],[17,2],[17,1],[17,1],[26,2],[26,1],[29,1],[29,1],[29,1],[29,1],[29,1],[27,1],[33,2],[33,1],[34,3],[34,3],[34,3],[34,3],[34,3],[25,1],[21,1],[38,3],[38,1]],
  145. performAction: function anonymous(yytext,yyleng,yylineno,yy,yystate,$$,_$) {
  146. var $0 = $$.length - 1;
  147. switch (yystate) {
  148. case 1: return $$[$0-1];
  149. break;
  150. case 2: this.$ = new yy.ProgramNode([], $$[$0]);
  151. break;
  152. case 3: this.$ = new yy.ProgramNode($$[$0-2], $$[$0]);
  153. break;
  154. case 4: this.$ = new yy.ProgramNode($$[$0-1], []);
  155. break;
  156. case 5: this.$ = new yy.ProgramNode($$[$0]);
  157. break;
  158. case 6: this.$ = new yy.ProgramNode([], []);
  159. break;
  160. case 7: this.$ = new yy.ProgramNode([]);
  161. break;
  162. case 8: this.$ = [$$[$0]];
  163. break;
  164. case 9: $$[$0-1].push($$[$0]); this.$ = $$[$0-1];
  165. break;
  166. case 10: this.$ = new yy.BlockNode($$[$0-2], $$[$0-1].inverse, $$[$0-1], $$[$0]);
  167. break;
  168. case 11: this.$ = new yy.BlockNode($$[$0-2], $$[$0-1], $$[$0-1].inverse, $$[$0]);
  169. break;
  170. case 12: this.$ = $$[$0];
  171. break;
  172. case 13: this.$ = $$[$0];
  173. break;
  174. case 14: this.$ = new yy.ContentNode($$[$0]);
  175. break;
  176. case 15: this.$ = new yy.CommentNode($$[$0]);
  177. break;
  178. case 16: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1]);
  179. break;
  180. case 17: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1]);
  181. break;
  182. case 18: this.$ = $$[$0-1];
  183. break;
  184. case 19: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1]);
  185. break;
  186. case 20: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1], true);
  187. break;
  188. case 21: this.$ = new yy.PartialNode($$[$0-1]);
  189. break;
  190. case 22: this.$ = new yy.PartialNode($$[$0-2], $$[$0-1]);
  191. break;
  192. case 23:
  193. break;
  194. case 24: this.$ = [[$$[$0-2]].concat($$[$0-1]), $$[$0]];
  195. break;
  196. case 25: this.$ = [[$$[$0-1]].concat($$[$0]), null];
  197. break;
  198. case 26: this.$ = [[$$[$0-1]], $$[$0]];
  199. break;
  200. case 27: this.$ = [[$$[$0]], null];
  201. break;
  202. case 28: this.$ = [[new yy.DataNode($$[$0])], null];
  203. break;
  204. case 29: $$[$0-1].push($$[$0]); this.$ = $$[$0-1];
  205. break;
  206. case 30: this.$ = [$$[$0]];
  207. break;
  208. case 31: this.$ = $$[$0];
  209. break;
  210. case 32: this.$ = new yy.StringNode($$[$0]);
  211. break;
  212. case 33: this.$ = new yy.IntegerNode($$[$0]);
  213. break;
  214. case 34: this.$ = new yy.BooleanNode($$[$0]);
  215. break;
  216. case 35: this.$ = new yy.DataNode($$[$0]);
  217. break;
  218. case 36: this.$ = new yy.HashNode($$[$0]);
  219. break;
  220. case 37: $$[$0-1].push($$[$0]); this.$ = $$[$0-1];
  221. break;
  222. case 38: this.$ = [$$[$0]];
  223. break;
  224. case 39: this.$ = [$$[$0-2], $$[$0]];
  225. break;
  226. case 40: this.$ = [$$[$0-2], new yy.StringNode($$[$0])];
  227. break;
  228. case 41: this.$ = [$$[$0-2], new yy.IntegerNode($$[$0])];
  229. break;
  230. case 42: this.$ = [$$[$0-2], new yy.BooleanNode($$[$0])];
  231. break;
  232. case 43: this.$ = [$$[$0-2], new yy.DataNode($$[$0])];
  233. break;
  234. case 44: this.$ = new yy.PartialNameNode($$[$0]);
  235. break;
  236. case 45: this.$ = new yy.IdNode($$[$0]);
  237. break;
  238. case 46: $$[$0-2].push($$[$0]); this.$ = $$[$0-2];
  239. break;
  240. case 47: this.$ = [$$[$0]];
  241. break;
  242. }
  243. },
  244. table: [{3:1,4:2,5:[2,7],6:3,7:4,8:6,9:7,11:8,12:9,13:10,14:[1,11],15:[1,12],16:[1,13],19:[1,5],22:[1,14],23:[1,15],24:[1,16]},{1:[3]},{5:[1,17]},{5:[2,6],7:18,8:6,9:7,11:8,12:9,13:10,14:[1,11],15:[1,12],16:[1,13],19:[1,19],20:[2,6],22:[1,14],23:[1,15],24:[1,16]},{5:[2,5],6:20,8:21,9:7,11:8,12:9,13:10,14:[1,11],15:[1,12],16:[1,13],19:[1,5],20:[2,5],22:[1,14],23:[1,15],24:[1,16]},{17:23,18:[1,22],21:24,28:[1,25],35:[1,27],38:26},{5:[2,8],14:[2,8],15:[2,8],16:[2,8],19:[2,8],20:[2,8],22:[2,8],23:[2,8],24:[2,8]},{4:28,6:3,7:4,8:6,9:7,11:8,12:9,13:10,14:[1,11],15:[1,12],16:[1,13],19:[1,5],20:[2,7],22:[1,14],23:[1,15],24:[1,16]},{4:29,6:3,7:4,8:6,9:7,11:8,12:9,13:10,14:[1,11],15:[1,12],16:[1,13],19:[1,5],20:[2,7],22:[1,14],23:[1,15],24:[1,16]},{5:[2,12],14:[2,12],15:[2,12],16:[2,12],19:[2,12],20:[2,12],22:[2,12],23:[2,12],24:[2,12]},{5:[2,13],14:[2,13],15:[2,13],16:[2,13],19:[2,13],20:[2,13],22:[2,13],23:[2,13],24:[2,13]},{5:[2,14],14:[2,14],15:[2,14],16:[2,14],19:[2,14],20:[2,14],22:[2,14],23:[2,14],24:[2,14]},{5:[2,15],14:[2,15],15:[2,15],16:[2,15],19:[2,15],20:[2,15],22:[2,15],23:[2,15],24:[2,15]},{17:30,21:24,28:[1,25],35:[1,27],38:26},{17:31,21:24,28:[1,25],35:[1,27],38:26},{17:32,21:24,28:[1,25],35:[1,27],38:26},{25:33,37:[1,34]},{1:[2,1]},{5:[2,2],8:21,9:7,11:8,12:9,13:10,14:[1,11],15:[1,12],16:[1,13],19:[1,19],20:[2,2],22:[1,14],23:[1,15],24:[1,16]},{17:23,21:24,28:[1,25],35:[1,27],38:26},{5:[2,4],7:35,8:6,9:7,11:8,12:9,13:10,14:[1,11],15:[1,12],16:[1,13],19:[1,19],20:[2,4],22:[1,14],23:[1,15],24:[1,16]},{5:[2,9],14:[2,9],15:[2,9],16:[2,9],19:[2,9],20:[2,9],22:[2,9],23:[2,9],24:[2,9]},{5:[2,23],14:[2,23],15:[2,23],16:[2,23],19:[2,23],20:[2,23],22:[2,23],23:[2,23],24:[2,23]},{18:[1,36]},{18:[2,27],21:41,26:37,27:38,28:[1,45],29:39,30:[1,42],31:[1,43],32:[1,44],33:40,34:46,35:[1,47],38:26},{18:[2,28]},{18:[2,45],28:[2,45],30:[2,45],31:[2,45],32:[2,45],35:[2,45],39:[1,48]},{18:[2,47],28:[2,47],30:[2,47],31:[2,47],32:[2,47],35:[2,47],39:[2,47]},{10:49,20:[1,50]},{10:51,20:[1,50]},{18:[1,52]},{18:[1,53]},{18:[1,54]},{18:[1,55],21:56,35:[1,27],38:26},{18:[2,44],35:[2,44]},{5:[2,3],8:21,9:7,11:8,12:9,13:10,14:[1,11],15:[1,12],16:[1,13],19:[1,19],20:[2,3],22:[1,14],23:[1,15],24:[1,16]},{14:[2,17],15:[2,17],16:[2,17],19:[2,17],20:[2,17],22:[2,17],23:[2,17],24:[2,17]},{18:[2,25],21:41,27:57,28:[1,45],29:58,30:[1,42],31:[1,43],32:[1,44],33:40,34:46,35:[1,47],38:26},{18:[2,26]},{18:[2,30],28:[2,30],30:[2,30],31:[2,30],32:[2,30],35:[2,30]},{18:[2,36],34:59,35:[1,60]},{18:[2,31],28:[2,31],30:[2,31],31:[2,31],32:[2,31],35:[2,31]},{18:[2,32],28:[2,32],30:[2,32],31:[2,32],32:[2,32],35:[2,32]},{18:[2,33],28:[2,33],30:[2,33],31:[2,33],32:[2,33],35:[2,33]},{18:[2,34],28:[2,34],30:[2,34],31:[2,34],32:[2,34],35:[2,34]},{18:[2,35],28:[2,35],30:[2,35],31:[2,35],32:[2,35],35:[2,35]},{18:[2,38],35:[2,38]},{18:[2,47],28:[2,47],30:[2,47],31:[2,47],32:[2,47],35:[2,47],36:[1,61],39:[2,47]},{35:[1,62]},{5:[2,10],14:[2,10],15:[2,10],16:[2,10],19:[2,10],20:[2,10],22:[2,10],23:[2,10],24:[2,10]},{21:63,35:[1,27],38:26},{5:[2,11],14:[2,11],15:[2,11],16:[2,11],19:[2,11],20:[2,11],22:[2,11],23:[2,11],24:[2,11]},{14:[2,16],15:[2,16],16:[2,16],19:[2,16],20:[2,16],22:[2,16],23:[2,16],24:[2,16]},{5:[2,19],14:[2,19],15:[2,19],16:[2,19],19:[2,19],20:[2,19],22:[2,19],23:[2,19],24:[2,19]},{5:[2,20],14:[2,20],15:[2,20],16:[2,20],19:[2,20],20:[2,20],22:[2,20],23:[2,20],24:[2,20]},{5:[2,21],14:[2,21],15:[2,21],16:[2,21],19:[2,21],20:[2,21],22:[2,21],23:[2,21],24:[2,21]},{18:[1,64]},{18:[2,24]},{18:[2,29],28:[2,29],30:[2,29],31:[2,29],32:[2,29],35:[2,29]},{18:[2,37],35:[2,37]},{36:[1,61]},{21:65,28:[1,69],30:[1,66],31:[1,67],32:[1,68],35:[1,27],38:26},{18:[2,46],28:[2,46],30:[2,46],31:[2,46],32:[2,46],35:[2,46],39:[2,46]},{18:[1,70]},{5:[2,22],14:[2,22],15:[2,22],16:[2,22],19:[2,22],20:[2,22],22:[2,22],23:[2,22],24:[2,22]},{18:[2,39],35:[2,39]},{18:[2,40],35:[2,40]},{18:[2,41],35:[2,41]},{18:[2,42],35:[2,42]},{18:[2,43],35:[2,43]},{5:[2,18],14:[2,18],15:[2,18],16:[2,18],19:[2,18],20:[2,18],22:[2,18],23:[2,18],24:[2,18]}],
  245. defaultActions: {17:[2,1],25:[2,28],38:[2,26],57:[2,24]},
  246. parseError: function parseError(str, hash) {
  247. throw new Error(str);
  248. },
  249. parse: function parse(input) {
  250. var self = this, stack = [0], vstack = [null], lstack = [], table = this.table, yytext = "", yylineno = 0, yyleng = 0, recovering = 0, TERROR = 2, EOF = 1;
  251. this.lexer.setInput(input);
  252. this.lexer.yy = this.yy;
  253. this.yy.lexer = this.lexer;
  254. this.yy.parser = this;
  255. if (typeof this.lexer.yylloc == "undefined")
  256. this.lexer.yylloc = {};
  257. var yyloc = this.lexer.yylloc;
  258. lstack.push(yyloc);
  259. var ranges = this.lexer.options && this.lexer.options.ranges;
  260. if (typeof this.yy.parseError === "function")
  261. this.parseError = this.yy.parseError;
  262. function popStack(n) {
  263. stack.length = stack.length - 2 * n;
  264. vstack.length = vstack.length - n;
  265. lstack.length = lstack.length - n;
  266. }
  267. function lex() {
  268. var token;
  269. token = self.lexer.lex() || 1;
  270. if (typeof token !== "number") {
  271. token = self.symbols_[token] || token;
  272. }
  273. return token;
  274. }
  275. var symbol, preErrorSymbol, state, action, a, r, yyval = {}, p, len, newState, expected;
  276. while (true) {
  277. state = stack[stack.length - 1];
  278. if (this.defaultActions[state]) {
  279. action = this.defaultActions[state];
  280. } else {
  281. if (symbol === null || typeof symbol == "undefined") {
  282. symbol = lex();
  283. }
  284. action = table[state] && table[state][symbol];
  285. }
  286. if (typeof action === "undefined" || !action.length || !action[0]) {
  287. var errStr = "";
  288. if (!recovering) {
  289. expected = [];
  290. for (p in table[state])
  291. if (this.terminals_[p] && p > 2) {
  292. expected.push("'" + this.terminals_[p] + "'");
  293. }
  294. if (this.lexer.showPosition) {
  295. errStr = "Parse error on line " + (yylineno + 1) + ":\n" + this.lexer.showPosition() + "\nExpecting " + expected.join(", ") + ", got '" + (this.terminals_[symbol] || symbol) + "'";
  296. } else {
  297. errStr = "Parse error on line " + (yylineno + 1) + ": Unexpected " + (symbol == 1?"end of input":"'" + (this.terminals_[symbol] || symbol) + "'");
  298. }
  299. this.parseError(errStr, {text: this.lexer.match, token: this.terminals_[symbol] || symbol, line: this.lexer.yylineno, loc: yyloc, expected: expected});
  300. }
  301. }
  302. if (action[0] instanceof Array && action.length > 1) {
  303. throw new Error("Parse Error: multiple actions possible at state: " + state + ", token: " + symbol);
  304. }
  305. switch (action[0]) {
  306. case 1:
  307. stack.push(symbol);
  308. vstack.push(this.lexer.yytext);
  309. lstack.push(this.lexer.yylloc);
  310. stack.push(action[1]);
  311. symbol = null;
  312. if (!preErrorSymbol) {
  313. yyleng = this.lexer.yyleng;
  314. yytext = this.lexer.yytext;
  315. yylineno = this.lexer.yylineno;
  316. yyloc = this.lexer.yylloc;
  317. if (recovering > 0)
  318. recovering--;
  319. } else {
  320. symbol = preErrorSymbol;
  321. preErrorSymbol = null;
  322. }
  323. break;
  324. case 2:
  325. len = this.productions_[action[1]][1];
  326. yyval.$ = vstack[vstack.length - len];
  327. yyval._$ = {first_line: lstack[lstack.length - (len || 1)].first_line, last_line: lstack[lstack.length - 1].last_line, first_column: lstack[lstack.length - (len || 1)].first_column, last_column: lstack[lstack.length - 1].last_column};
  328. if (ranges) {
  329. yyval._$.range = [lstack[lstack.length - (len || 1)].range[0], lstack[lstack.length - 1].range[1]];
  330. }
  331. r = this.performAction.call(yyval, yytext, yyleng, yylineno, this.yy, action[1], vstack, lstack);
  332. if (typeof r !== "undefined") {
  333. return r;
  334. }
  335. if (len) {
  336. stack = stack.slice(0, -1 * len * 2);
  337. vstack = vstack.slice(0, -1 * len);
  338. lstack = lstack.slice(0, -1 * len);
  339. }
  340. stack.push(this.productions_[action[1]][0]);
  341. vstack.push(yyval.$);
  342. lstack.push(yyval._$);
  343. newState = table[stack[stack.length - 2]][stack[stack.length - 1]];
  344. stack.push(newState);
  345. break;
  346. case 3:
  347. return true;
  348. }
  349. }
  350. return true;
  351. }
  352. };
  353. /* Jison generated lexer */
  354. var lexer = (function(){
  355. var lexer = ({EOF:1,
  356. parseError:function parseError(str, hash) {
  357. if (this.yy.parser) {
  358. this.yy.parser.parseError(str, hash);
  359. } else {
  360. throw new Error(str);
  361. }
  362. },
  363. setInput:function (input) {
  364. this._input = input;
  365. this._more = this._less = this.done = false;
  366. this.yylineno = this.yyleng = 0;
  367. this.yytext = this.matched = this.match = '';
  368. this.conditionStack = ['INITIAL'];
  369. this.yylloc = {first_line:1,first_column:0,last_line:1,last_column:0};
  370. if (this.options.ranges) this.yylloc.range = [0,0];
  371. this.offset = 0;
  372. return this;
  373. },
  374. input:function () {
  375. var ch = this._input[0];
  376. this.yytext += ch;
  377. this.yyleng++;
  378. this.offset++;
  379. this.match += ch;
  380. this.matched += ch;
  381. var lines = ch.match(/(?:\r\n?|\n).*/g);
  382. if (lines) {
  383. this.yylineno++;
  384. this.yylloc.last_line++;
  385. } else {
  386. this.yylloc.last_column++;
  387. }
  388. if (this.options.ranges) this.yylloc.range[1]++;
  389. this._input = this._input.slice(1);
  390. return ch;
  391. },
  392. unput:function (ch) {
  393. var len = ch.length;
  394. var lines = ch.split(/(?:\r\n?|\n)/g);
  395. this._input = ch + this._input;
  396. this.yytext = this.yytext.substr(0, this.yytext.length-len-1);
  397. //this.yyleng -= len;
  398. this.offset -= len;
  399. var oldLines = this.match.split(/(?:\r\n?|\n)/g);
  400. this.match = this.match.substr(0, this.match.length-1);
  401. this.matched = this.matched.substr(0, this.matched.length-1);
  402. if (lines.length-1) this.yylineno -= lines.length-1;
  403. var r = this.yylloc.range;
  404. this.yylloc = {first_line: this.yylloc.first_line,
  405. last_line: this.yylineno+1,
  406. first_column: this.yylloc.first_column,
  407. last_column: lines ?
  408. (lines.length === oldLines.length ? this.yylloc.first_column : 0) + oldLines[oldLines.length - lines.length].length - lines[0].length:
  409. this.yylloc.first_column - len
  410. };
  411. if (this.options.ranges) {
  412. this.yylloc.range = [r[0], r[0] + this.yyleng - len];
  413. }
  414. return this;
  415. },
  416. more:function () {
  417. this._more = true;
  418. return this;
  419. },
  420. less:function (n) {
  421. this.unput(this.match.slice(n));
  422. },
  423. pastInput:function () {
  424. var past = this.matched.substr(0, this.matched.length - this.match.length);
  425. return (past.length > 20 ? '...':'') + past.substr(-20).replace(/\n/g, "");
  426. },
  427. upcomingInput:function () {
  428. var next = this.match;
  429. if (next.length < 20) {
  430. next += this._input.substr(0, 20-next.length);
  431. }
  432. return (next.substr(0,20)+(next.length > 20 ? '...':'')).replace(/\n/g, "");
  433. },
  434. showPosition:function () {
  435. var pre = this.pastInput();
  436. var c = new Array(pre.length + 1).join("-");
  437. return pre + this.upcomingInput() + "\n" + c+"^";
  438. },
  439. next:function () {
  440. if (this.done) {
  441. return this.EOF;
  442. }
  443. if (!this._input) this.done = true;
  444. var token,
  445. match,
  446. tempMatch,
  447. index,
  448. col,
  449. lines;
  450. if (!this._more) {
  451. this.yytext = '';
  452. this.match = '';
  453. }
  454. var rules = this._currentRules();
  455. for (var i=0;i < rules.length; i++) {
  456. tempMatch = this._input.match(this.rules[rules[i]]);
  457. if (tempMatch && (!match || tempMatch[0].length > match[0].length)) {
  458. match = tempMatch;
  459. index = i;
  460. if (!this.options.flex) break;
  461. }
  462. }
  463. if (match) {
  464. lines = match[0].match(/(?:\r\n?|\n).*/g);
  465. if (lines) this.yylineno += lines.length;
  466. this.yylloc = {first_line: this.yylloc.last_line,
  467. last_line: this.yylineno+1,
  468. first_column: this.yylloc.last_column,
  469. last_column: lines ? lines[lines.length-1].length-lines[lines.length-1].match(/\r?\n?/)[0].length : this.yylloc.last_column + match[0].length};
  470. this.yytext += match[0];
  471. this.match += match[0];
  472. this.matches = match;
  473. this.yyleng = this.yytext.length;
  474. if (this.options.ranges) {
  475. this.yylloc.range = [this.offset, this.offset += this.yyleng];
  476. }
  477. this._more = false;
  478. this._input = this._input.slice(match[0].length);
  479. this.matched += match[0];
  480. token = this.performAction.call(this, this.yy, this, rules[index],this.conditionStack[this.conditionStack.length-1]);
  481. if (this.done && this._input) this.done = false;
  482. if (token) return token;
  483. else return;
  484. }
  485. if (this._input === "") {
  486. return this.EOF;
  487. } else {
  488. return this.parseError('Lexical error on line '+(this.yylineno+1)+'. Unrecognized text.\n'+this.showPosition(),
  489. {text: "", token: null, line: this.yylineno});
  490. }
  491. },
  492. lex:function lex() {
  493. var r = this.next();
  494. if (typeof r !== 'undefined') {
  495. return r;
  496. } else {
  497. return this.lex();
  498. }
  499. },
  500. begin:function begin(condition) {
  501. this.conditionStack.push(condition);
  502. },
  503. popState:function popState() {
  504. return this.conditionStack.pop();
  505. },
  506. _currentRules:function _currentRules() {
  507. return this.conditions[this.conditionStack[this.conditionStack.length-1]].rules;
  508. },
  509. topState:function () {
  510. return this.conditionStack[this.conditionStack.length-2];
  511. },
  512. pushState:function begin(condition) {
  513. this.begin(condition);
  514. }});
  515. lexer.options = {};
  516. lexer.performAction = function anonymous(yy,yy_,$avoiding_name_collisions,YY_START) {
  517. var YYSTATE=YY_START
  518. switch($avoiding_name_collisions) {
  519. case 0:
  520. if(yy_.yytext.slice(-1) !== "\\") this.begin("mu");
  521. if(yy_.yytext.slice(-1) === "\\") yy_.yytext = yy_.yytext.substr(0,yy_.yyleng-1), this.begin("emu");
  522. if(yy_.yytext) return 14;
  523. break;
  524. case 1: return 14;
  525. break;
  526. case 2:
  527. if(yy_.yytext.slice(-1) !== "\\") this.popState();
  528. if(yy_.yytext.slice(-1) === "\\") yy_.yytext = yy_.yytext.substr(0,yy_.yyleng-1);
  529. return 14;
  530. break;
  531. case 3: yy_.yytext = yy_.yytext.substr(0, yy_.yyleng-4); this.popState(); return 15;
  532. break;
  533. case 4: this.begin("par"); return 24;
  534. break;
  535. case 5: return 16;
  536. break;
  537. case 6: return 20;
  538. break;
  539. case 7: return 19;
  540. break;
  541. case 8: return 19;
  542. break;
  543. case 9: return 23;
  544. break;
  545. case 10: return 23;
  546. break;
  547. case 11: this.popState(); this.begin('com');
  548. break;
  549. case 12: yy_.yytext = yy_.yytext.substr(3,yy_.yyleng-5); this.popState(); return 15;
  550. break;
  551. case 13: return 22;
  552. break;
  553. case 14: return 36;
  554. break;
  555. case 15: return 35;
  556. break;
  557. case 16: return 35;
  558. break;
  559. case 17: return 39;
  560. break;
  561. case 18: /*ignore whitespace*/
  562. break;
  563. case 19: this.popState(); return 18;
  564. break;
  565. case 20: this.popState(); return 18;
  566. break;
  567. case 21: yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2).replace(/\\"/g,'"'); return 30;
  568. break;
  569. case 22: yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2).replace(/\\'/g,"'"); return 30;
  570. break;
  571. case 23: yy_.yytext = yy_.yytext.substr(1); return 28;
  572. break;
  573. case 24: return 32;
  574. break;
  575. case 25: return 32;
  576. break;
  577. case 26: return 31;
  578. break;
  579. case 27: return 35;
  580. break;
  581. case 28: yy_.yytext = yy_.yytext.substr(1, yy_.yyleng-2); return 35;
  582. break;
  583. case 29: return 'INVALID';
  584. break;
  585. case 30: /*ignore whitespace*/
  586. break;
  587. case 31: this.popState(); return 37;
  588. break;
  589. case 32: return 5;
  590. break;
  591. }
  592. };
  593. lexer.rules = [/^(?:[^\x00]*?(?=(\{\{)))/,/^(?:[^\x00]+)/,/^(?:[^\x00]{2,}?(?=(\{\{|$)))/,/^(?:[\s\S]*?--\}\})/,/^(?:\{\{>)/,/^(?:\{\{#)/,/^(?:\{\{\/)/,/^(?:\{\{\^)/,/^(?:\{\{\s*else\b)/,/^(?:\{\{\{)/,/^(?:\{\{&)/,/^(?:\{\{!--)/,/^(?:\{\{![\s\S]*?\}\})/,/^(?:\{\{)/,/^(?:=)/,/^(?:\.(?=[} ]))/,/^(?:\.\.)/,/^(?:[\/.])/,/^(?:\s+)/,/^(?:\}\}\})/,/^(?:\}\})/,/^(?:"(\\["]|[^"])*")/,/^(?:'(\\[']|[^'])*')/,/^(?:@[a-zA-Z]+)/,/^(?:true(?=[}\s]))/,/^(?:false(?=[}\s]))/,/^(?:[0-9]+(?=[}\s]))/,/^(?:[a-zA-Z0-9_$-]+(?=[=}\s\/.]))/,/^(?:\[[^\]]*\])/,/^(?:.)/,/^(?:\s+)/,/^(?:[a-zA-Z0-9_$-/]+)/,/^(?:$)/];
  594. lexer.conditions = {"mu":{"rules":[4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,32],"inclusive":false},"emu":{"rules":[2],"inclusive":false},"com":{"rules":[3],"inclusive":false},"par":{"rules":[30,31],"inclusive":false},"INITIAL":{"rules":[0,1,32],"inclusive":true}};
  595. return lexer;})()
  596. parser.lexer = lexer;
  597. function Parser () { this.yy = {}; }Parser.prototype = parser;parser.Parser = Parser;
  598. return new Parser;
  599. })();;
  600. // lib/handlebars/compiler/base.js
  601. Handlebars.Parser = handlebars;
  602. Handlebars.parse = function(input) {
  603. // Just return if an already-compile AST was passed in.
  604. if(input.constructor === Handlebars.AST.ProgramNode) { return input; }
  605. Handlebars.Parser.yy = Handlebars.AST;
  606. return Handlebars.Parser.parse(input);
  607. };
  608. Handlebars.print = function(ast) {
  609. return new Handlebars.PrintVisitor().accept(ast);
  610. };;
  611. // lib/handlebars/compiler/ast.js
  612. (function() {
  613. Handlebars.AST = {};
  614. Handlebars.AST.ProgramNode = function(statements, inverse) {
  615. this.type = "program";
  616. this.statements = statements;
  617. if(inverse) { this.inverse = new Handlebars.AST.ProgramNode(inverse); }
  618. };
  619. Handlebars.AST.MustacheNode = function(rawParams, hash, unescaped) {
  620. this.type = "mustache";
  621. this.escaped = !unescaped;
  622. this.hash = hash;
  623. var id = this.id = rawParams[0];
  624. var params = this.params = rawParams.slice(1);
  625. // a mustache is an eligible helper if:
  626. // * its id is simple (a single part, not `this` or `..`)
  627. var eligibleHelper = this.eligibleHelper = id.isSimple;
  628. // a mustache is definitely a helper if:
  629. // * it is an eligible helper, and
  630. // * it has at least one parameter or hash segment
  631. this.isHelper = eligibleHelper && (params.length || hash);
  632. // if a mustache is an eligible helper but not a definite
  633. // helper, it is ambiguous, and will be resolved in a later
  634. // pass or at runtime.
  635. };
  636. Handlebars.AST.PartialNode = function(partialName, context) {
  637. this.type = "partial";
  638. this.partialName = partialName;
  639. this.context = context;
  640. };
  641. var verifyMatch = function(open, close) {
  642. if(open.original !== close.original) {
  643. throw new Handlebars.Exception(open.original + " doesn't match " + close.original);
  644. }
  645. };
  646. Handlebars.AST.BlockNode = function(mustache, program, inverse, close) {
  647. verifyMatch(mustache.id, close);
  648. this.type = "block";
  649. this.mustache = mustache;
  650. this.program = program;
  651. this.inverse = inverse;
  652. if (this.inverse && !this.program) {
  653. this.isInverse = true;
  654. }
  655. };
  656. Handlebars.AST.ContentNode = function(string) {
  657. this.type = "content";
  658. this.string = string;
  659. };
  660. Handlebars.AST.HashNode = function(pairs) {
  661. this.type = "hash";
  662. this.pairs = pairs;
  663. };
  664. Handlebars.AST.IdNode = function(parts) {
  665. this.type = "ID";
  666. this.original = parts.join(".");
  667. var dig = [], depth = 0;
  668. for(var i=0,l=parts.length; i<l; i++) {
  669. var part = parts[i];
  670. if (part === ".." || part === "." || part === "this") {
  671. if (dig.length > 0) { throw new Handlebars.Exception("Invalid path: " + this.original); }
  672. else if (part === "..") { depth++; }
  673. else { this.isScoped = true; }
  674. }
  675. else { dig.push(part); }
  676. }
  677. this.parts = dig;
  678. this.string = dig.join('.');
  679. this.depth = depth;
  680. // an ID is simple if it only has one part, and that part is not
  681. // `..` or `this`.
  682. this.isSimple = parts.length === 1 && !this.isScoped && depth === 0;
  683. this.stringModeValue = this.string;
  684. };
  685. Handlebars.AST.PartialNameNode = function(name) {
  686. this.type = "PARTIAL_NAME";
  687. this.name = name;
  688. };
  689. Handlebars.AST.DataNode = function(id) {
  690. this.type = "DATA";
  691. this.id = id;
  692. };
  693. Handlebars.AST.StringNode = function(string) {
  694. this.type = "STRING";
  695. this.string = string;
  696. this.stringModeValue = string;
  697. };
  698. Handlebars.AST.IntegerNode = function(integer) {
  699. this.type = "INTEGER";
  700. this.integer = integer;
  701. this.stringModeValue = Number(integer);
  702. };
  703. Handlebars.AST.BooleanNode = function(bool) {
  704. this.type = "BOOLEAN";
  705. this.bool = bool;
  706. this.stringModeValue = bool === "true";
  707. };
  708. Handlebars.AST.CommentNode = function(comment) {
  709. this.type = "comment";
  710. this.comment = comment;
  711. };
  712. })();;
  713. // lib/handlebars/utils.js
  714. var errorProps = ['description', 'fileName', 'lineNumber', 'message', 'name', 'number', 'stack'];
  715. Handlebars.Exception = function(message) {
  716. var tmp = Error.prototype.constructor.apply(this, arguments);
  717. // Unfortunately errors are not enumerable in Chrome (at least), so `for prop in tmp` doesn't work.
  718. for (var idx = 0; idx < errorProps.length; idx++) {
  719. this[errorProps[idx]] = tmp[errorProps[idx]];
  720. }
  721. };
  722. Handlebars.Exception.prototype = new Error();
  723. // Build out our basic SafeString type
  724. Handlebars.SafeString = function(string) {
  725. this.string = string;
  726. };
  727. Handlebars.SafeString.prototype.toString = function() {
  728. return this.string.toString();
  729. };
  730. (function() {
  731. var escape = {
  732. "&": "&amp;",
  733. "<": "&lt;",
  734. ">": "&gt;",
  735. '"': "&quot;",
  736. "'": "&#x27;",
  737. "`": "&#x60;"
  738. };
  739. var badChars = /[&<>"'`]/g;
  740. var possible = /[&<>"'`]/;
  741. var escapeChar = function(chr) {
  742. return escape[chr] || "&amp;";
  743. };
  744. Handlebars.Utils = {
  745. escapeExpression: function(string) {
  746. // don't escape SafeStrings, since they're already safe
  747. if (string instanceof Handlebars.SafeString) {
  748. return string.toString();
  749. } else if (string == null || string === false) {
  750. return "";
  751. }
  752. if(!possible.test(string)) { return string; }
  753. return string.replace(badChars, escapeChar);
  754. },
  755. isEmpty: function(value) {
  756. if (!value && value !== 0) {
  757. return true;
  758. } else if(Object.prototype.toString.call(value) === "[object Array]" && value.length === 0) {
  759. return true;
  760. } else {
  761. return false;
  762. }
  763. }
  764. };
  765. })();;
  766. // lib/handlebars/compiler/compiler.js
  767. /*jshint eqnull:true*/
  768. Handlebars.Compiler = function() {};
  769. Handlebars.JavaScriptCompiler = function() {};
  770. (function(Compiler, JavaScriptCompiler) {
  771. // the foundHelper register will disambiguate helper lookup from finding a
  772. // function in a context. This is necessary for mustache compatibility, which
  773. // requires that context functions in blocks are evaluated by blockHelperMissing,
  774. // and then proceed as if the resulting value was provided to blockHelperMissing.
  775. Compiler.prototype = {
  776. compiler: Compiler,
  777. disassemble: function() {
  778. var opcodes = this.opcodes, opcode, out = [], params, param;
  779. for (var i=0, l=opcodes.length; i<l; i++) {
  780. opcode = opcodes[i];
  781. if (opcode.opcode === 'DECLARE') {
  782. out.push("DECLARE " + opcode.name + "=" + opcode.value);
  783. } else {
  784. params = [];
  785. for (var j=0; j<opcode.args.length; j++) {
  786. param = opcode.args[j];
  787. if (typeof param === "string") {
  788. param = "\"" + param.replace("\n", "\\n") + "\"";
  789. }
  790. params.push(param);
  791. }
  792. out.push(opcode.opcode + " " + params.join(" "));
  793. }
  794. }
  795. return out.join("\n");
  796. },
  797. equals: function(other) {
  798. var len = this.opcodes.length;
  799. if (other.opcodes.length !== len) {
  800. return false;
  801. }
  802. for (var i = 0; i < len; i++) {
  803. var opcode = this.opcodes[i],
  804. otherOpcode = other.opcodes[i];
  805. if (opcode.opcode !== otherOpcode.opcode || opcode.args.length !== otherOpcode.args.length) {
  806. return false;
  807. }
  808. for (var j = 0; j < opcode.args.length; j++) {
  809. if (opcode.args[j] !== otherOpcode.args[j]) {
  810. return false;
  811. }
  812. }
  813. }
  814. return true;
  815. },
  816. guid: 0,
  817. compile: function(program, options) {
  818. this.children = [];
  819. this.depths = {list: []};
  820. this.options = options;
  821. // These changes will propagate to the other compiler components
  822. var knownHelpers = this.options.knownHelpers;
  823. this.options.knownHelpers = {
  824. 'helperMissing': true,
  825. 'blockHelperMissing': true,
  826. 'each': true,
  827. 'if': true,
  828. 'unless': true,
  829. 'with': true,
  830. 'log': true
  831. };
  832. if (knownHelpers) {
  833. for (var name in knownHelpers) {
  834. this.options.knownHelpers[name] = knownHelpers[name];
  835. }
  836. }
  837. return this.program(program);
  838. },
  839. accept: function(node) {
  840. return this[node.type](node);
  841. },
  842. program: function(program) {
  843. var statements = program.statements, statement;
  844. this.opcodes = [];
  845. for(var i=0, l=statements.length; i<l; i++) {
  846. statement = statements[i];
  847. this[statement.type](statement);
  848. }
  849. this.isSimple = l === 1;
  850. this.depths.list = this.depths.list.sort(function(a, b) {
  851. return a - b;
  852. });
  853. return this;
  854. },
  855. compileProgram: function(program) {
  856. var result = new this.compiler().compile(program, this.options);
  857. var guid = this.guid++, depth;
  858. this.usePartial = this.usePartial || result.usePartial;
  859. this.children[guid] = result;
  860. for(var i=0, l=result.depths.list.length; i<l; i++) {
  861. depth = result.depths.list[i];
  862. if(depth < 2) { continue; }
  863. else { this.addDepth(depth - 1); }
  864. }
  865. return guid;
  866. },
  867. block: function(block) {
  868. var mustache = block.mustache,
  869. program = block.program,
  870. inverse = block.inverse;
  871. if (program) {
  872. program = this.compileProgram(program);
  873. }
  874. if (inverse) {
  875. inverse = this.compileProgram(inverse);
  876. }
  877. var type = this.classifyMustache(mustache);
  878. if (type === "helper") {
  879. this.helperMustache(mustache, program, inverse);
  880. } else if (type === "simple") {
  881. this.simpleMustache(mustache);
  882. // now that the simple mustache is resolved, we need to
  883. // evaluate it by executing `blockHelperMissing`
  884. this.opcode('pushProgram', program);
  885. this.opcode('pushProgram', inverse);
  886. this.opcode('emptyHash');
  887. this.opcode('blockValue');
  888. } else {
  889. this.ambiguousMustache(mustache, program, inverse);
  890. // now that the simple mustache is resolved, we need to
  891. // evaluate it by executing `blockHelperMissing`
  892. this.opcode('pushProgram', program);
  893. this.opcode('pushProgram', inverse);
  894. this.opcode('emptyHash');
  895. this.opcode('ambiguousBlockValue');
  896. }
  897. this.opcode('append');
  898. },
  899. hash: function(hash) {
  900. var pairs = hash.pairs, pair, val;
  901. this.opcode('pushHash');
  902. for(var i=0, l=pairs.length; i<l; i++) {
  903. pair = pairs[i];
  904. val = pair[1];
  905. if (this.options.stringParams) {
  906. this.opcode('pushStringParam', val.stringModeValue, val.type);
  907. } else {
  908. this.accept(val);
  909. }
  910. this.opcode('assignToHash', pair[0]);
  911. }
  912. this.opcode('popHash');
  913. },
  914. partial: function(partial) {
  915. var partialName = partial.partialName;
  916. this.usePartial = true;
  917. if(partial.context) {
  918. this.ID(partial.context);
  919. } else {
  920. this.opcode('push', 'depth0');
  921. }
  922. this.opcode('invokePartial', partialName.name);
  923. this.opcode('append');
  924. },
  925. content: function(content) {
  926. this.opcode('appendContent', content.string);
  927. },
  928. mustache: function(mustache) {
  929. var options = this.options;
  930. var type = this.classifyMustache(mustache);
  931. if (type === "simple") {
  932. this.simpleMustache(mustache);
  933. } else if (type === "helper") {
  934. this.helperMustache(mustache);
  935. } else {
  936. this.ambiguousMustache(mustache);
  937. }
  938. if(mustache.escaped && !options.noEscape) {
  939. this.opcode('appendEscaped');
  940. } else {
  941. this.opcode('append');
  942. }
  943. },
  944. ambiguousMustache: function(mustache, program, inverse) {
  945. var id = mustache.id,
  946. name = id.parts[0],
  947. isBlock = program != null || inverse != null;
  948. this.opcode('getContext', id.depth);
  949. this.opcode('pushProgram', program);
  950. this.opcode('pushProgram', inverse);
  951. this.opcode('invokeAmbiguous', name, isBlock);
  952. },
  953. simpleMustache: function(mustache) {
  954. var id = mustache.id;
  955. if (id.type === 'DATA') {
  956. this.DATA(id);
  957. } else if (id.parts.length) {
  958. this.ID(id);
  959. } else {
  960. // Simplified ID for `this`
  961. this.addDepth(id.depth);
  962. this.opcode('getContext', id.depth);
  963. this.opcode('pushContext');
  964. }
  965. this.opcode('resolvePossibleLambda');
  966. },
  967. helperMustache: function(mustache, program, inverse) {
  968. var params = this.setupFullMustacheParams(mustache, program, inverse),
  969. name = mustache.id.parts[0];
  970. if (this.options.knownHelpers[name]) {
  971. this.opcode('invokeKnownHelper', params.length, name);
  972. } else if (this.knownHelpersOnly) {
  973. throw new Error("You specified knownHelpersOnly, but used the unknown helper " + name);
  974. } else {
  975. this.opcode('invokeHelper', params.length, name);
  976. }
  977. },
  978. ID: function(id) {
  979. this.addDepth(id.depth);
  980. this.opcode('getContext', id.depth);
  981. var name = id.parts[0];
  982. if (!name) {
  983. this.opcode('pushContext');
  984. } else {
  985. this.opcode('lookupOnContext', id.parts[0]);
  986. }
  987. for(var i=1, l=id.parts.length; i<l; i++) {
  988. this.opcode('lookup', id.parts[i]);
  989. }
  990. },
  991. DATA: function(data) {
  992. this.options.data = true;
  993. this.opcode('lookupData', data.id);
  994. },
  995. STRING: function(string) {
  996. this.opcode('pushString', string.string);
  997. },
  998. INTEGER: function(integer) {
  999. this.opcode('pushLiteral', integer.integer);
  1000. },
  1001. BOOLEAN: function(bool) {
  1002. this.opcode('pushLiteral', bool.bool);
  1003. },
  1004. comment: function() {},
  1005. // HELPERS
  1006. opcode: function(name) {
  1007. this.opcodes.push({ opcode: name, args: [].slice.call(arguments, 1) });
  1008. },
  1009. declare: function(name, value) {
  1010. this.opcodes.push({ opcode: 'DECLARE', name: name, value: value });
  1011. },
  1012. addDepth: function(depth) {
  1013. if(isNaN(depth)) { throw new Error("EWOT"); }
  1014. if(depth === 0) { return; }
  1015. if(!this.depths[depth]) {
  1016. this.depths[depth] = true;
  1017. this.depths.list.push(depth);
  1018. }
  1019. },
  1020. classifyMustache: function(mustache) {
  1021. var isHelper = mustache.isHelper;
  1022. var isEligible = mustache.eligibleHelper;
  1023. var options = this.options;
  1024. // if ambiguous, we can possibly resolve the ambiguity now
  1025. if (isEligible && !isHelper) {
  1026. var name = mustache.id.parts[0];
  1027. if (options.knownHelpers[name]) {
  1028. isHelper = true;
  1029. } else if (options.knownHelpersOnly) {
  1030. isEligible = false;
  1031. }
  1032. }
  1033. if (isHelper) { return "helper"; }
  1034. else if (isEligible) { return "ambiguous"; }
  1035. else { return "simple"; }
  1036. },
  1037. pushParams: function(params) {
  1038. var i = params.length, param;
  1039. while(i--) {
  1040. param = params[i];
  1041. if(this.options.stringParams) {
  1042. if(param.depth) {
  1043. this.addDepth(param.depth);
  1044. }
  1045. this.opcode('getContext', param.depth || 0);
  1046. this.opcode('pushStringParam', param.stringModeValue, param.type);
  1047. } else {
  1048. this[param.type](param);
  1049. }
  1050. }
  1051. },
  1052. setupMustacheParams: function(mustache) {
  1053. var params = mustache.params;
  1054. this.pushParams(params);
  1055. if(mustache.hash) {
  1056. this.hash(mustache.hash);
  1057. } else {
  1058. this.opcode('emptyHash');
  1059. }
  1060. return params;
  1061. },
  1062. // this will replace setupMustacheParams when we're done
  1063. setupFullMustacheParams: function(mustache, program, inverse) {
  1064. var params = mustache.params;
  1065. this.pushParams(params);
  1066. this.opcode('pushProgram', program);
  1067. this.opcode('pushProgram', inverse);
  1068. if(mustache.hash) {
  1069. this.hash(mustache.hash);
  1070. } else {
  1071. this.opcode('emptyHash');
  1072. }
  1073. return params;
  1074. }
  1075. };
  1076. var Literal = function(value) {
  1077. this.value = value;
  1078. };
  1079. JavaScriptCompiler.prototype = {
  1080. // PUBLIC API: You can override these methods in a subclass to provide
  1081. // alternative compiled forms for name lookup and buffering semantics
  1082. nameLookup: function(parent, name /* , type*/) {
  1083. if (/^[0-9]+$/.test(name)) {
  1084. return parent + "[" + name + "]";
  1085. } else if (JavaScriptCompiler.isValidJavaScriptVariableName(name)) {
  1086. return parent + "." + name;
  1087. }
  1088. else {
  1089. return parent + "['" + name + "']";
  1090. }
  1091. },
  1092. appendToBuffer: function(string) {
  1093. if (this.environment.isSimple) {
  1094. return "return " + string + ";";
  1095. } else {
  1096. return {
  1097. appendToBuffer: true,
  1098. content: string,
  1099. toString: function() { return "buffer += " + string + ";"; }
  1100. };
  1101. }
  1102. },
  1103. initializeBuffer: function() {
  1104. return this.quotedString("");
  1105. },
  1106. namespace: "Handlebars",
  1107. // END PUBLIC API
  1108. compile: function(environment, options, context, asObject) {
  1109. this.environment = environment;
  1110. this.options = options || {};
  1111. Handlebars.log(Handlebars.logger.DEBUG, this.environment.disassemble() + "\n\n");
  1112. this.name = this.environment.name;
  1113. this.isChild = !!context;
  1114. this.context = context || {
  1115. programs: [],
  1116. environments: [],
  1117. aliases: { }
  1118. };
  1119. this.preamble();
  1120. this.stackSlot = 0;
  1121. this.stackVars = [];
  1122. this.registers = { list: [] };
  1123. this.compileStack = [];
  1124. this.inlineStack = [];
  1125. this.compileChildren(environment, options);
  1126. var opcodes = environment.opcodes, opcode;
  1127. this.i = 0;
  1128. for(l=opcodes.length; this.i<l; this.i++) {
  1129. opcode = opcodes[this.i];
  1130. if(opcode.opcode === 'DECLARE') {
  1131. this[opcode.name] = opcode.value;
  1132. } else {
  1133. this[opcode.opcode].apply(this, opcode.args);
  1134. }
  1135. }
  1136. return this.createFunctionContext(asObject);
  1137. },
  1138. nextOpcode: function() {
  1139. var opcodes = this.environment.opcodes;
  1140. return opcodes[this.i + 1];
  1141. },
  1142. eat: function() {
  1143. this.i = this.i + 1;
  1144. },
  1145. preamble: function() {
  1146. var out = [];
  1147. if (!this.isChild) {
  1148. var namespace = this.namespace;
  1149. var copies = "helpers = helpers || " + namespace + ".helpers;";
  1150. if (this.environment.usePartial) { copies = copies + " partials = partials || " + namespace + ".partials;"; }
  1151. if (this.options.data) { copies = copies + " data = data || {};"; }
  1152. out.push(copies);
  1153. } else {
  1154. out.push('');
  1155. }
  1156. if (!this.environment.isSimple) {
  1157. out.push(", buffer = " + this.initializeBuffer());
  1158. } else {
  1159. out.push("");
  1160. }
  1161. // track the last context pushed into place to allow skipping the
  1162. // getContext opcode when it would be a noop
  1163. this.lastContext = 0;
  1164. this.source = out;
  1165. },
  1166. createFunctionContext: function(asObject) {
  1167. var locals = this.stackVars.concat(this.registers.list);
  1168. if(locals.length > 0) {
  1169. this.source[1] = this.source[1] + ", " + locals.join(", ");
  1170. }
  1171. // Generate minimizer alias mappings
  1172. if (!this.isChild) {
  1173. for (var alias in this.context.aliases) {
  1174. this.source[1] = this.source[1] + ', ' + alias + '=' + this.context.aliases[alias];
  1175. }
  1176. }
  1177. if (this.source[1]) {
  1178. this.source[1] = "var " + this.source[1].substring(2) + ";";
  1179. }
  1180. // Merge children
  1181. if (!this.isChild) {
  1182. this.source[1] += '\n' + this.context.programs.join('\n') + '\n';
  1183. }
  1184. if (!this.environment.isSimple) {
  1185. this.source.push("return buffer;");
  1186. }
  1187. var params = this.isChild ? ["depth0", "data"] : ["Handlebars", "depth0", "helpers", "partials", "data"];
  1188. for(var i=0, l=this.environment.depths.list.length; i<l; i++) {
  1189. params.push("depth" + this.environment.depths.list[i]);
  1190. }
  1191. // Perform a second pass over the output to merge content when possible
  1192. var source = this.mergeSource();
  1193. if (!this.isChild) {
  1194. var revision = Handlebars.COMPILER_REVISION,
  1195. versions = Handlebars.REVISION_CHANGES[revision];
  1196. source = "this.compilerInfo = ["+revision+",'"+versions+"'];\n"+source;
  1197. }
  1198. if (asObject) {
  1199. params.push(source);
  1200. return Function.apply(this, params);
  1201. } else {
  1202. var functionSource = 'function ' + (this.name || '') + '(' + params.join(',') + ') {\n ' + source + '}';
  1203. Handlebars.log(Handlebars.logger.DEBUG, functionSource + "\n\n");
  1204. return functionSource;
  1205. }
  1206. },
  1207. mergeSource: function() {
  1208. // WARN: We are not handling the case where buffer is still populated as the source should
  1209. // not have buffer append operations as their final action.
  1210. var source = '',
  1211. buffer;
  1212. for (var i = 0, len = this.source.length; i < len; i++) {
  1213. var line = this.source[i];
  1214. if (line.appendToBuffer) {
  1215. if (buffer) {
  1216. buffer = buffer + '\n + ' + line.content;
  1217. } else {
  1218. buffer = line.content;
  1219. }
  1220. } else {
  1221. if (buffer) {
  1222. source += 'buffer += ' + buffer + ';\n ';
  1223. buffer = undefined;
  1224. }
  1225. source += line + '\n ';
  1226. }
  1227. }
  1228. return source;
  1229. },
  1230. // [blockValue]
  1231. //
  1232. // On stack, before: hash, inverse, program, value
  1233. // On stack, after: return value of blockHelperMissing
  1234. //
  1235. // The purpose of this opcode is to take a block of the form
  1236. // `{{#foo}}...{{/foo}}`, resolve the value of `foo`, and
  1237. // replace it on the stack with the result of properly
  1238. // invoking blockHelperMissing.
  1239. blockValue: function() {
  1240. this.context.aliases.blockHelperMissing = 'helpers.blockHelperMissing';
  1241. var params = ["depth0"];
  1242. this.setupParams(0, params);
  1243. this.replaceStack(function(current) {
  1244. params.splice(1, 0, current);
  1245. return "blockHelperMissing.call(" + params.join(", ") + ")";
  1246. });
  1247. },
  1248. // [ambiguousBlockValue]
  1249. //
  1250. // On stack, before: hash, inverse, program, value
  1251. // Compiler value, before: lastHelper=value of last found helper, if any
  1252. // On stack, after, if no lastHelper: same as [blockValue]
  1253. // On stack, after, if lastHelper: value
  1254. ambiguousBlockValue: function() {
  1255. this.context.aliases.blockHelperMissing = 'helpers.blockHelperMissing';
  1256. var params = ["depth0"];
  1257. this.setupParams(0, params);
  1258. var current = this.topStack();
  1259. params.splice(1, 0, current);
  1260. // Use the options value generated from the invocation
  1261. params[params.length-1] = 'options';
  1262. this.source.push("if (!" + this.lastHelper + ") { " + current + " = blockHelperMissing.call(" + params.join(", ") + "); }");
  1263. },
  1264. // [appendContent]
  1265. //
  1266. // On stack, before: ...
  1267. // On stack, after: ...
  1268. //
  1269. // Appends the string value of `content` to the current buffer
  1270. appendContent: function(content) {
  1271. this.source.push(this.appendToBuffer(this.quotedString(content)));
  1272. },
  1273. // [append]
  1274. //
  1275. // On stack, before: value, ...
  1276. // On stack, after: ...
  1277. //
  1278. // Coerces `value` to a String and appends it to the current buffer.
  1279. //
  1280. // If `value` is truthy, or 0, it is coerced into a string and appended
  1281. // Otherwise, the empty string is appended
  1282. append: function() {
  1283. // Force anything that is inlined onto the stack so we don't have duplication
  1284. // when we examine local
  1285. this.flushInline();
  1286. var local = this.popStack();
  1287. this.source.push("if(" + local + " || " + local + " === 0) { " + this.appendToBuffer(local) + " }");
  1288. if (this.environment.isSimple) {
  1289. this.source.push("else { " + this.appendToBuffer("''") + " }");
  1290. }
  1291. },
  1292. // [appendEscaped]
  1293. //
  1294. // On stack, before: value, ...
  1295. // On stack, after: ...
  1296. //
  1297. // Escape `value` and append it to the buffer
  1298. appendEscaped: function() {
  1299. this.context.aliases.escapeExpression = 'this.escapeExpression';
  1300. this.source.push(this.appendToBuffer("escapeExpression(" + this.popStack() + ")"));
  1301. },
  1302. // [getContext]
  1303. //
  1304. // On stack, before: ...
  1305. // On stack, after: ...
  1306. // Compiler value, after: lastContext=depth
  1307. //
  1308. // Set the value of the `lastContext` compiler value to the depth
  1309. getContext: function(depth) {
  1310. if(this.lastContext !== depth) {
  1311. this.lastContext = depth;
  1312. }
  1313. },
  1314. // [lookupOnContext]
  1315. //
  1316. // On stack, before: ...
  1317. // On stack, after: currentContext[name], ...
  1318. //
  1319. // Looks up the value of `name` on the current context and pushes
  1320. // it onto the stack.
  1321. lookupOnContext: function(name) {
  1322. this.push(this.nameLookup('depth' + this.lastContext, name, 'context'));
  1323. },
  1324. // [pushContext]
  1325. //
  1326. // On stack, before: ...
  1327. // On stack, after: currentContext, ...
  1328. //
  1329. // Pushes the value of the current context onto the stack.
  1330. pushContext: function() {
  1331. this.pushStackLiteral('depth' + this.lastContext);
  1332. },
  1333. // [resolvePossibleLambda]
  1334. //
  1335. // On stack, before: value, ...
  1336. // On stack, after: resolved value, ...
  1337. //
  1338. // If the `value` is a lambda, replace it on the stack by
  1339. // the return value of the lambda
  1340. resolvePossibleLambda: function() {
  1341. this.context.aliases.functionType = '"function"';
  1342. this.replaceStack(function(current) {
  1343. return "typeof " + current + " === functionType ? " + current + ".apply(depth0) : " + current;
  1344. });
  1345. },
  1346. // [lookup]
  1347. //
  1348. // On stack, before: value, ...
  1349. // On stack, after: value[name], ...
  1350. //
  1351. // Replace the value on the stack with the result of looking
  1352. // up `name` on `value`
  1353. lookup: function(name) {
  1354. this.replaceStack(function(current) {
  1355. return current + " == null || " + current + " === false ? " + current + " : " + this.nameLookup(current, name, 'context');
  1356. });
  1357. },
  1358. // [lookupData]
  1359. //
  1360. // On stack, before: ...
  1361. // On stack, after: data[id], ...
  1362. //
  1363. // Push the result of looking up `id` on the current data
  1364. lookupData: function(id) {
  1365. this.push(this.nameLookup('data', id, 'data'));
  1366. },
  1367. // [pushStringParam]
  1368. //
  1369. // On stack, before: ...
  1370. // On stack, after: string, currentContext, ...
  1371. //
  1372. // This opcode is designed for use in string mode, which
  1373. // provides the string value of a parameter along with its
  1374. // depth rather than resolving it immediately.
  1375. pushStringParam: function(string, type) {
  1376. this.pushStackLiteral('depth' + this.lastContext);
  1377. this.pushString(type);
  1378. if (typeof string === 'string') {
  1379. this.pushString(string);
  1380. } else {
  1381. this.pushStackLiteral(string);
  1382. }
  1383. },
  1384. emptyHash: function() {
  1385. this.pushStackLiteral('{}');
  1386. if (this.options.stringParams) {
  1387. this.register('hashTypes', '{}');
  1388. }
  1389. },
  1390. pushHash: function() {
  1391. this.hash = {values: [], types: []};
  1392. },
  1393. popHash: function() {
  1394. var hash = this.hash;
  1395. this.hash = undefined;
  1396. if (this.options.stringParams) {
  1397. this.register('hashTypes', '{' + hash.types.join(',') + '}');
  1398. }
  1399. this.push('{\n ' + hash.values.join(',\n ') + '\n }');
  1400. },
  1401. // [pushString]
  1402. //
  1403. // On stack, before: ...
  1404. // On stack, after: quotedString(string), ...
  1405. //
  1406. // Push a quoted version of `string` onto the stack
  1407. pushString: function(string) {
  1408. this.pushStackLiteral(this.quotedString(string));
  1409. },
  1410. // [push]
  1411. //
  1412. // On stack, before: ...
  1413. // On stack, after: expr, ...
  1414. //
  1415. // Push an expression onto the stack
  1416. push: function(expr) {
  1417. this.inlineStack.push(expr);
  1418. return expr;
  1419. },
  1420. // [pushLiteral]
  1421. //
  1422. // On stack, before: ...
  1423. // On stack, after: value, ...
  1424. //
  1425. // Pushes a value onto the stack. This operation prevents
  1426. // the compiler from creating a temporary variable to hold
  1427. // it.
  1428. pushLiteral: function(value) {
  1429. this.pushStackLiteral(value);
  1430. },
  1431. // [pushProgram]
  1432. //
  1433. // On stack, before: ...
  1434. // On stack, after: program(guid), ...
  1435. //
  1436. // Push a program expression onto the stack. This takes
  1437. // a compile-time guid and converts it into a runtime-accessible
  1438. // expression.
  1439. pushProgram: function(guid) {
  1440. if (guid != null) {
  1441. this.pushStackLiteral(this.programExpression(guid));
  1442. } else {
  1443. this.pushStackLiteral(null);
  1444. }
  1445. },
  1446. // [invokeHelper]
  1447. //
  1448. // On stack, before: hash, inverse, program, params..., ...
  1449. // On stack, after: result of helper invocation
  1450. //
  1451. // Pops off the helper's parameters, invokes the helper,
  1452. // and pushes the helper's return value onto the stack.
  1453. //
  1454. // If the helper is not found, `helperMissing` is called.
  1455. invokeHelper: function(paramSize, name) {
  1456. this.context.aliases.helperMissing = 'helpers.helperMissing';
  1457. var helper = this.lastHelper = this.setupHelper(paramSize, name, true);
  1458. this.push(helper.name);
  1459. this.replaceStack(function(name) {
  1460. return name + ' ? ' + name + '.call(' +
  1461. helper.callParams + ") " + ": helperMissing.call(" +
  1462. helper.helperMissingParams + ")";
  1463. });
  1464. },
  1465. // [invokeKnownHelper]
  1466. //
  1467. // On stack, before: hash, inverse, program, params..., ...
  1468. // On stack, after: result of helper invocation
  1469. //
  1470. // This operation is used when the helper is known to exist,
  1471. // so a `helperMissing` fallback is not required.
  1472. invokeKnownHelper: function(paramSize, name) {
  1473. var helper = this.setupHelper(paramSize, name);
  1474. this.push(helper.name + ".call(" + helper.callParams + ")");
  1475. },
  1476. // [invokeAmbiguous]
  1477. //
  1478. // On stack, before: hash, inverse, program, params..., ...
  1479. // On stack, after: result of disambiguation
  1480. //
  1481. // This operation is used when an expression like `{{foo}}`
  1482. // is provided, but we don't know at compile-time whether it
  1483. // is a helper or a path.
  1484. //
  1485. // This operation emits more code than the other options,
  1486. // and can be avoided by passing the `knownHelpers` and
  1487. // `knownHelpersOnly` flags at compile-time.
  1488. invokeAmbiguous: function(name, helperCall) {
  1489. this.context.aliases.functionType = '"function"';
  1490. this.pushStackLiteral('{}'); // Hash value
  1491. var helper = this.setupHelper(0, name, helperCall);
  1492. var helperName = this.lastHelper = this.nameLookup('helpers', name, 'helper');
  1493. var nonHelper = this.nameLookup('depth' + this.lastContext, name, 'context');
  1494. var nextStack = this.nextStack();
  1495. this.source.push('if (' + nextStack + ' = ' + helperName + ') { ' + nextStack + ' = ' + nextStack + '.call(' + helper.callParams + '); }');
  1496. this.source.push('else { ' + nextStack + ' = ' + nonHelper + '; ' + nextStack + ' = typeof ' + nextStack + ' === functionType ? ' + nextStack + '.apply(depth0) : ' + nextStack + '; }');
  1497. },
  1498. // [invokePartial]
  1499. //
  1500. // On stack, before: context, ...
  1501. // On stack after: result of partial invocation
  1502. //
  1503. // This operation pops off a context, invokes a partial with that context,
  1504. // and pushes the result of the invocation back.
  1505. invokePartial: function(name) {
  1506. var params = [this.nameLookup('partials', name, 'partial'), "'" + name + "'", this.popStack(), "helpers", "partials"];
  1507. if (this.options.data) {
  1508. params.push("data");
  1509. }
  1510. this.context.aliases.self = "this";
  1511. this.push("self.invokePartial(" + params.join(", ") + ")");
  1512. },
  1513. // [assignToHash]
  1514. //
  1515. // On stack, before: value, hash, ...
  1516. // On stack, after: hash, ...
  1517. //
  1518. // Pops a value and hash off the stack, assigns `hash[key] = value`
  1519. // and pushes the hash back onto the stack.
  1520. assignToHash: function(key) {
  1521. var value = this.popStack(),
  1522. type;
  1523. if (this.options.stringParams) {
  1524. type = this.popStack();
  1525. this.popStack();
  1526. }
  1527. var hash = this.hash;
  1528. if (type) {
  1529. hash.types.push("'" + key + "': " + type);
  1530. }
  1531. hash.values.push("'" + key + "': (" + value + ")");
  1532. },
  1533. // HELPERS
  1534. compiler: JavaScriptCompiler,
  1535. compileChildren: function(environment, options) {
  1536. var children = environment.children, child, compiler;
  1537. for(var i=0, l=children.length; i<l; i++) {
  1538. child = children[i];
  1539. compiler = new this.compiler();
  1540. var index = this.matchExistingProgram(child);
  1541. if (index == null) {
  1542. this.context.programs.push(''); // Placeholder to prevent name conflicts for nested children
  1543. index = this.context.programs.length;
  1544. child.index = index;
  1545. child.name = 'program' + index;
  1546. this.context.programs[index] = compiler.compile(child, options, this.context);
  1547. this.context.environments[index] = child;
  1548. } else {
  1549. child.index = index;
  1550. child.name = 'program' + index;
  1551. }
  1552. }
  1553. },
  1554. matchExistingProgram: function(child) {
  1555. for (var i = 0, len = this.context.environments.length; i < len; i++) {
  1556. var environment = this.context.environments[i];
  1557. if (environment && environment.equals(child)) {
  1558. return i;
  1559. }
  1560. }
  1561. },
  1562. programExpression: function(guid) {
  1563. this.context.aliases.self = "this";
  1564. if(guid == null) {
  1565. return "self.noop";
  1566. }
  1567. var child = this.environment.children[guid],
  1568. depths = child.depths.list, depth;
  1569. var programParams = [child.index, child.name, "data"];
  1570. for(var i=0, l = depths.length; i<l; i++) {
  1571. depth = depths[i];
  1572. if(depth === 1) { programParams.push("depth0"); }
  1573. else { programParams.push("depth" + (depth - 1)); }
  1574. }
  1575. if(depths.length === 0) {
  1576. return "self.program(" + programParams.join(", ") + ")";
  1577. } else {
  1578. programParams.shift();
  1579. return "self.programWithDepth(" + programParams.join(", ") + ")";
  1580. }
  1581. },
  1582. register: function(name, val) {
  1583. this.useRegister(name);
  1584. this.source.push(name + " = " + val + ";");
  1585. },
  1586. useRegister: function(name) {
  1587. if(!this.registers[name]) {
  1588. this.registers[name] = true;
  1589. this.registers.list.push(name);
  1590. }
  1591. },
  1592. pushStackLiteral: function(item) {
  1593. return this.push(new Literal(item));
  1594. },
  1595. pushStack: function(item) {
  1596. this.flushInline();
  1597. var stack = this.incrStack();
  1598. if (item) {
  1599. this.source.push(stack + " = " + item + ";");
  1600. }
  1601. this.compileStack.push(stack);
  1602. return stack;
  1603. },
  1604. replaceStack: function(callback) {
  1605. var prefix = '',
  1606. inline = this.isInline(),
  1607. stack;
  1608. // If we are currently inline then we want to merge the inline statement into the
  1609. // replacement statement via ','
  1610. if (inline) {
  1611. var top = this.popStack(true);
  1612. if (top instanceof Literal) {
  1613. // Literals do not need to be inlined
  1614. stack = top.value;
  1615. } else {
  1616. // Get or create the current stack name for use by the inline
  1617. var name = this.stackSlot ? this.topStackName() : this.incrStack();
  1618. prefix = '(' + this.push(name) + ' = ' + top + '),';
  1619. stack = this.topStack();
  1620. }
  1621. } else {
  1622. stack = this.topStack();
  1623. }
  1624. var item = callback.call(this, stack);
  1625. if (inline) {
  1626. if (this.inlineStack.length || this.compileStack.length) {
  1627. this.popStack();
  1628. }
  1629. this.push('(' + prefix + item + ')');
  1630. } else {
  1631. // Prevent modification of the context depth variable. Through replaceStack
  1632. if (!/^stack/.test(stack)) {
  1633. stack = this.nextStack();
  1634. }
  1635. this.source.push(stack + " = (" + prefix + item + ");");
  1636. }
  1637. return stack;
  1638. },
  1639. nextStack: function() {
  1640. return this.pushStack();
  1641. },
  1642. incrStack: function() {
  1643. this.stackSlot++;
  1644. if(this.stackSlot > this.stackVars.length) { this.stackVars.push("stack" + this.stackSlot); }
  1645. return this.topStackName();
  1646. },
  1647. topStackName: function() {
  1648. return "stack" + this.stackSlot;
  1649. },
  1650. flushInline: function() {
  1651. var inlineStack = this.inlineStack;
  1652. if (inlineStack.length) {
  1653. this.inlineStack = [];
  1654. for (var i = 0, len = inlineStack.length; i < len; i++) {
  1655. var entry = inlineStack[i];
  1656. if (entry instanceof Literal) {
  1657. this.compileStack.push(entry);
  1658. } else {
  1659. this.pushStack(entry);
  1660. }
  1661. }
  1662. }
  1663. },
  1664. isInline: function() {
  1665. return this.inlineStack.length;
  1666. },
  1667. popStack: function(wrapped) {
  1668. var inline = this.isInline(),
  1669. item = (inline ? this.inlineStack : this.compileStack).pop();
  1670. if (!wrapped && (item instanceof Literal)) {
  1671. return item.value;
  1672. } else {
  1673. if (!inline) {
  1674. this.stackSlot--;
  1675. }
  1676. return item;
  1677. }
  1678. },
  1679. topStack: function(wrapped) {
  1680. var stack = (this.isInline() ? this.inlineStack : this.compileStack),
  1681. item = stack[stack.length - 1];
  1682. if (!wrapped && (item instanceof Literal)) {
  1683. return item.value;
  1684. } else {
  1685. return item;
  1686. }
  1687. },
  1688. quotedString: function(str) {
  1689. return '"' + str
  1690. .replace(/\\/g, '\\\\')
  1691. .replace(/"/g, '\\"')
  1692. .replace(/\n/g, '\\n')
  1693. .replace(/\r/g, '\\r') + '"';
  1694. },
  1695. setupHelper: function(paramSize, name, missingParams) {
  1696. var params = [];
  1697. this.setupParams(paramSize, params, missingParams);
  1698. var foundHelper = this.nameLookup('helpers', name, 'helper');
  1699. return {
  1700. params: params,
  1701. name: foundHelper,
  1702. callParams: ["depth0"].concat(params).join(", "),
  1703. helperMissingParams: missingParams && ["depth0", this.quotedString(name)].concat(params).join(", ")
  1704. };
  1705. },
  1706. // the params and contexts arguments are passed in arrays
  1707. // to fill in
  1708. setupParams: function(paramSize, params, useRegister) {
  1709. var options = [], contexts = [], types = [], param, inverse, program;
  1710. options.push("hash:" + this.popStack());
  1711. inverse = this.popStack();
  1712. program = this.popStack();
  1713. // Avoid setting fn and inverse if neither are set. This allows
  1714. // helpers to do a check for `if (options.fn)`
  1715. if (program || inverse) {
  1716. if (!program) {
  1717. this.context.aliases.self = "this";
  1718. program = "self.noop";
  1719. }
  1720. if (!inverse) {
  1721. this.context.aliases.self = "this";
  1722. inverse = "self.noop";
  1723. }
  1724. options.push("inverse:" + inverse);
  1725. options.push("fn:" + program);
  1726. }
  1727. for(var i=0; i<paramSize; i++) {
  1728. param = this.popStack();
  1729. params.push(param);
  1730. if(this.options.stringParams) {
  1731. types.push(this.popStack());
  1732. contexts.push(this.popStack());
  1733. }
  1734. }
  1735. if (this.options.stringParams) {
  1736. options.push("contexts:[" + contexts.join(",") + "]");
  1737. options.push("types:[" + types.join(",") + "]");
  1738. options.push("hashTypes:hashTypes");
  1739. }
  1740. if(this.options.data) {
  1741. options.push("data:data");
  1742. }
  1743. options = "{" + options.join(",") + "}";
  1744. if (useRegister) {
  1745. this.register('options', options);
  1746. params.push('options');
  1747. } else {
  1748. params.push(options);
  1749. }
  1750. return params.join(", ");
  1751. }
  1752. };
  1753. var reservedWords = (
  1754. "break else new var" +
  1755. " case finally return void" +
  1756. " catch for switch while" +
  1757. " continue function this with" +
  1758. " default if throw" +
  1759. " delete in try" +
  1760. " do instanceof typeof" +
  1761. " abstract enum int short" +
  1762. " boolean export interface static" +
  1763. " byte extends long super" +
  1764. " char final native synchronized" +
  1765. " class float package throws" +
  1766. " const goto private transient" +
  1767. " debugger implements protected volatile" +
  1768. " double import public let yield"
  1769. ).split(" ");
  1770. var compilerWords = JavaScriptCompiler.RESERVED_WORDS = {};
  1771. for(var i=0, l=reservedWords.length; i<l; i++) {
  1772. compilerWords[reservedWords[i]] = true;
  1773. }
  1774. JavaScriptCompiler.isValidJavaScriptVariableName = function(name) {
  1775. if(!JavaScriptCompiler.RESERVED_WORDS[name] && /^[a-zA-Z_$][0-9a-zA-Z_$]+$/.test(name)) {
  1776. return true;
  1777. }
  1778. return false;
  1779. };
  1780. })(Handlebars.Compiler, Handlebars.JavaScriptCompiler);
  1781. Handlebars.precompile = function(input, options) {
  1782. if (!input || (typeof input !== 'string' && input.constructor !== Handlebars.AST.ProgramNode)) {
  1783. throw new Handlebars.Exception("You must pass a string or Handlebars AST to Handlebars.compile. You passed " + input);
  1784. }
  1785. options = options || {};
  1786. if (!('data' in options)) {
  1787. options.data = true;
  1788. }
  1789. var ast = Handlebars.parse(input);
  1790. var environment = new Handlebars.Compiler().compile(ast, options);
  1791. return new Handlebars.JavaScriptCompiler().compile(environment, options);
  1792. };
  1793. Handlebars.compile = function(input, options) {
  1794. if (!input || (typeof input !== 'string' && input.constructor !== Handlebars.AST.ProgramNode)) {
  1795. throw new Handlebars.Exception("You must pass a string or Handlebars AST to Handlebars.compile. You passed " + input);
  1796. }
  1797. options = options || {};
  1798. if (!('data' in options)) {
  1799. options.data = true;
  1800. }
  1801. var compiled;
  1802. function compile() {
  1803. var ast = Handlebars.parse(input);
  1804. var environment = new Handlebars.Compiler().compile(ast, options);
  1805. var templateSpec = new Handlebars.JavaScriptCompiler().compile(environment, options, undefined, true);
  1806. return Handlebars.template(templateSpec);
  1807. }
  1808. // Template is only compiled on first use and cached after that point.
  1809. return function(context, options) {
  1810. if (!compiled) {
  1811. compiled = compile();
  1812. }
  1813. return compiled.call(this, context, options);
  1814. };
  1815. };
  1816. ;
  1817. // lib/handlebars/runtime.js
  1818. Handlebars.VM = {
  1819. template: function(templateSpec) {
  1820. // Just add water
  1821. var container = {
  1822. escapeExpression: Handlebars.Utils.escapeExpression,
  1823. invokePartial: Handlebars.VM.invokePartial,
  1824. programs: [],
  1825. program: function(i, fn, data) {
  1826. var programWrapper = this.programs[i];
  1827. if(data) {
  1828. return Handlebars.VM.program(fn, data);
  1829. } else if(programWrapper) {
  1830. return programWrapper;
  1831. } else {
  1832. programWrapper = this.programs[i] = Handlebars.VM.program(fn);
  1833. return programWrapper;
  1834. }
  1835. },
  1836. programWithDepth: Handlebars.VM.programWithDepth,
  1837. noop: Handlebars.VM.noop,
  1838. compilerInfo: null
  1839. };
  1840. return function(context, options) {
  1841. options = options || {};
  1842. var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
  1843. var compilerInfo = container.compilerInfo || [],
  1844. compilerRevision = compilerInfo[0] || 1,
  1845. currentRevision = Handlebars.COMPILER_REVISION;
  1846. if (compilerRevision !== currentRevision) {
  1847. if (compilerRevision < currentRevision) {
  1848. var runtimeVersions = Handlebars.REVISION_CHANGES[currentRevision],
  1849. compilerVersions = Handlebars.REVISION_CHANGES[compilerRevision];
  1850. throw "Template was precompiled with an older version of Handlebars than the current runtime. "+
  1851. "Please update your precompiler to a newer version ("+runtimeVersions+") or downgrade your runtime to an older version ("+compilerVersions+").";
  1852. } else {
  1853. // Use the embedded version info since the runtime doesn't know about this revision yet
  1854. throw "Template was precompiled with a newer version of Handlebars than the current runtime. "+
  1855. "Please update your runtime to a newer version ("+compilerInfo[1]+").";
  1856. }
  1857. }
  1858. return result;
  1859. };
  1860. },
  1861. programWithDepth: function(fn, data, $depth) {
  1862. var args = Array.prototype.slice.call(arguments, 2);
  1863. return function(context, options) {
  1864. options = options || {};
  1865. return fn.apply(this, [context, options.data || data].concat(args));
  1866. };
  1867. },
  1868. program: function(fn, data) {
  1869. return function(context, options) {
  1870. options = options || {};
  1871. return fn(context, options.data || data);
  1872. };
  1873. },
  1874. noop: function() { return ""; },
  1875. invokePartial: function(partial, name, context, helpers, partials, data) {
  1876. var options = { helpers: helpers, partials: partials, data: data };
  1877. if(partial === undefined) {
  1878. throw new Handlebars.Exception("The partial " + name + " could not be found");
  1879. } else if(partial instanceof Function) {
  1880. return partial(context, options);
  1881. } else if (!Handlebars.compile) {
  1882. throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in runtime-only mode");
  1883. } else {
  1884. partials[name] = Handlebars.compile(partial, {data: data !== undefined});
  1885. return partials[name](context, options);
  1886. }
  1887. }
  1888. };
  1889. Handlebars.template = Handlebars.VM.template;
  1890. ;