1 // lib/handlebars/base.js 2 var Handlebars = {}; 3 4 Handlebars.VERSION = "1.0.beta.2"; 5 6 Handlebars.helpers = {}; 7 Handlebars.partials = {}; 8 9 Handlebars.registerHelper = function(name, fn, inverse) { 10 if(inverse) { fn.not = inverse; } 11 this.helpers[name] = fn; 12 }; 13 14 Handlebars.registerPartial = function(name, str) { 15 this.partials[name] = str; 16 }; 17 18 Handlebars.registerHelper('helperMissing', function(arg) { 19 if(arguments.length === 2) { 20 return undefined; 21 } else { 22 throw new Error("Could not find property '" + arg + "'"); 23 } 24 }); 25 26 Handlebars.registerHelper('blockHelperMissing', function(context, options) { 27 var inverse = options.inverse || function() {}, fn = options.fn; 28 29 30 var ret = ""; 31 var type = Object.prototype.toString.call(context); 32 33 if(type === "[object Function]") { 34 context = context(); 35 } 36 37 if(context === true) { 38 return fn(this); 39 } else if(context === false || context == null) { 40 return inverse(this); 41 } else if(type === "[object Array]") { 42 if(context.length > 0) { 43 for(var i=0, j=context.length; i<j; i++) { 44 ret = ret + fn(context[i]); 45 } 46 } else { 47 ret = inverse(this); 48 } 49 return ret; 50 } else { 51 return fn(context); 52 } 53 }); 54 55 Handlebars.registerHelper('each', function(context, options) { 56 var fn = options.fn, inverse = options.inverse; 57 var ret = ""; 58 59 if(context && context.length > 0) { 60 for(var i=0, j=context.length; i<j; i++) { 61 ret = ret + fn(context[i]); 62 } 63 } else { 64 ret = inverse(this); 65 } 66 return ret; 67 }); 68 69 Handlebars.registerHelper('if', function(context, options) { 70 if(!context || Handlebars.Utils.isEmpty(context)) { 71 return options.inverse(this); 72 } else { 73 return options.fn(this); 74 } 75 }); 76 77 Handlebars.registerHelper('unless', function(context, options) { 78 var fn = options.fn, inverse = options.inverse; 79 options.fn = inverse; 80 options.inverse = fn; 81 82 return Handlebars.helpers['if'].call(this, context, options); 83 }); 84 85 Handlebars.registerHelper('with', function(context, options) { 86 return options.fn(context); 87 }); 88 89 Handlebars.registerHelper('log', function(context) { 90 Handlebars.log(context); 91 }); 92 ; 93 // lib/handlebars/compiler/parser.js 94 /* Jison generated parser */ 95 var handlebars = (function(){ 96 97 var parser = {trace: function trace() { }, 98 yy: {}, 99 symbols_: {"error":2,"root":3,"program":4,"EOF":5,"statements":6,"simpleInverse":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,"params":25,"hash":26,"param":27,"STRING":28,"INTEGER":29,"BOOLEAN":30,"hashSegments":31,"hashSegment":32,"ID":33,"EQUALS":34,"pathSegments":35,"SEP":36,"$accept":0,"$end":1}, 100 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:"STRING",29:"INTEGER",30:"BOOLEAN",33:"ID",34:"EQUALS",36:"SEP"}, 101 productions_: [0,[3,2],[4,3],[4,1],[4,0],[6,1],[6,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],[7,2],[17,3],[17,2],[17,2],[17,1],[25,2],[25,1],[27,1],[27,1],[27,1],[27,1],[26,1],[31,2],[31,1],[32,3],[32,3],[32,3],[32,3],[21,1],[35,3],[35,1]], 102 performAction: function anonymous(yytext,yyleng,yylineno,yy,yystate,$$,_$) { 103 104 var $0 = $$.length - 1; 105 switch (yystate) { 106 case 1: return $$[$0-1] 107 break; 108 case 2: this.$ = new yy.ProgramNode($$[$0-2], $$[$0]) 109 break; 110 case 3: this.$ = new yy.ProgramNode($$[$0]) 111 break; 112 case 4: this.$ = new yy.ProgramNode([]) 113 break; 114 case 5: this.$ = [$$[$0]] 115 break; 116 case 6: $$[$0-1].push($$[$0]); this.$ = $$[$0-1] 117 break; 118 case 7: this.$ = new yy.InverseNode($$[$0-2], $$[$0-1], $$[$0]) 119 break; 120 case 8: this.$ = new yy.BlockNode($$[$0-2], $$[$0-1], $$[$0]) 121 break; 122 case 9: this.$ = $$[$0] 123 break; 124 case 10: this.$ = $$[$0] 125 break; 126 case 11: this.$ = new yy.ContentNode($$[$0]) 127 break; 128 case 12: this.$ = new yy.CommentNode($$[$0]) 129 break; 130 case 13: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1]) 131 break; 132 case 14: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1]) 133 break; 134 case 15: this.$ = $$[$0-1] 135 break; 136 case 16: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1]) 137 break; 138 case 17: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1], true) 139 break; 140 case 18: this.$ = new yy.PartialNode($$[$0-1]) 141 break; 142 case 19: this.$ = new yy.PartialNode($$[$0-2], $$[$0-1]) 143 break; 144 case 20: 145 break; 146 case 21: this.$ = [[$$[$0-2]].concat($$[$0-1]), $$[$0]] 147 break; 148 case 22: this.$ = [[$$[$0-1]].concat($$[$0]), null] 149 break; 150 case 23: this.$ = [[$$[$0-1]], $$[$0]] 151 break; 152 case 24: this.$ = [[$$[$0]], null] 153 break; 154 case 25: $$[$0-1].push($$[$0]); this.$ = $$[$0-1]; 155 break; 156 case 26: this.$ = [$$[$0]] 157 break; 158 case 27: this.$ = $$[$0] 159 break; 160 case 28: this.$ = new yy.StringNode($$[$0]) 161 break; 162 case 29: this.$ = new yy.IntegerNode($$[$0]) 163 break; 164 case 30: this.$ = new yy.BooleanNode($$[$0]) 165 break; 166 case 31: this.$ = new yy.HashNode($$[$0]) 167 break; 168 case 32: $$[$0-1].push($$[$0]); this.$ = $$[$0-1] 169 break; 170 case 33: this.$ = [$$[$0]] 171 break; 172 case 34: this.$ = [$$[$0-2], $$[$0]] 173 break; 174 case 35: this.$ = [$$[$0-2], new yy.StringNode($$[$0])] 175 break; 176 case 36: this.$ = [$$[$0-2], new yy.IntegerNode($$[$0])] 177 break; 178 case 37: this.$ = [$$[$0-2], new yy.BooleanNode($$[$0])] 179 break; 180 case 38: this.$ = new yy.IdNode($$[$0]) 181 break; 182 case 39: $$[$0-2].push($$[$0]); this.$ = $$[$0-2]; 183 break; 184 case 40: this.$ = [$$[$0]] 185 break; 186 } 187 }, 188 table: [{3:1,4:2,5:[2,4],6:3,8:4,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],22:[1,13],23:[1,14],24:[1,15]},{1:[3]},{5:[1,16]},{5:[2,3],7:17,8:18,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,19],20:[2,3],22:[1,13],23:[1,14],24:[1,15]},{5:[2,5],14:[2,5],15:[2,5],16:[2,5],19:[2,5],20:[2,5],22:[2,5],23:[2,5],24:[2,5]},{4:20,6:3,8:4,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],20:[2,4],22:[1,13],23:[1,14],24:[1,15]},{4:21,6:3,8:4,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],20:[2,4],22:[1,13],23:[1,14],24:[1,15]},{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,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]},{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]},{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]},{17:22,21:23,33:[1,25],35:24},{17:26,21:23,33:[1,25],35:24},{17:27,21:23,33:[1,25],35:24},{17:28,21:23,33:[1,25],35:24},{21:29,33:[1,25],35:24},{1:[2,1]},{6:30,8:4,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],22:[1,13],23:[1,14],24:[1,15]},{5:[2,6],14:[2,6],15:[2,6],16:[2,6],19:[2,6],20:[2,6],22:[2,6],23:[2,6],24:[2,6]},{17:22,18:[1,31],21:23,33:[1,25],35:24},{10:32,20:[1,33]},{10:34,20:[1,33]},{18:[1,35]},{18:[2,24],21:40,25:36,26:37,27:38,28:[1,41],29:[1,42],30:[1,43],31:39,32:44,33:[1,45],35:24},{18:[2,38],28:[2,38],29:[2,38],30:[2,38],33:[2,38],36:[1,46]},{18:[2,40],28:[2,40],29:[2,40],30:[2,40],33:[2,40],36:[2,40]},{18:[1,47]},{18:[1,48]},{18:[1,49]},{18:[1,50],21:51,33:[1,25],35:24},{5:[2,2],8:18,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],20:[2,2],22:[1,13],23:[1,14],24:[1,15]},{14:[2,20],15:[2,20],16:[2,20],19:[2,20],22:[2,20],23:[2,20],24:[2,20]},{5:[2,7],14:[2,7],15:[2,7],16:[2,7],19:[2,7],20:[2,7],22:[2,7],23:[2,7],24:[2,7]},{21:52,33:[1,25],35:24},{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]},{14:[2,14],15:[2,14],16:[2,14],19:[2,14],20:[2,14],22:[2,14],23:[2,14],24:[2,14]},{18:[2,22],21:40,26:53,27:54,28:[1,41],29:[1,42],30:[1,43],31:39,32:44,33:[1,45],35:24},{18:[2,23]},{18:[2,26],28:[2,26],29:[2,26],30:[2,26],33:[2,26]},{18:[2,31],32:55,33:[1,56]},{18:[2,27],28:[2,27],29:[2,27],30:[2,27],33:[2,27]},{18:[2,28],28:[2,28],29:[2,28],30:[2,28],33:[2,28]},{18:[2,29],28:[2,29],29:[2,29],30:[2,29],33:[2,29]},{18:[2,30],28:[2,30],29:[2,30],30:[2,30],33:[2,30]},{18:[2,33],33:[2,33]},{18:[2,40],28:[2,40],29:[2,40],30:[2,40],33:[2,40],34:[1,57],36:[2,40]},{33:[1,58]},{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,16],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,17],14:[2,17],15:[2,17],16:[2,17],19:[2,17],20:[2,17],22:[2,17],23:[2,17],24:[2,17]},{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]},{18:[1,59]},{18:[1,60]},{18:[2,21]},{18:[2,25],28:[2,25],29:[2,25],30:[2,25],33:[2,25]},{18:[2,32],33:[2,32]},{34:[1,57]},{21:61,28:[1,62],29:[1,63],30:[1,64],33:[1,25],35:24},{18:[2,39],28:[2,39],29:[2,39],30:[2,39],33:[2,39],36:[2,39]},{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,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]},{18:[2,34],33:[2,34]},{18:[2,35],33:[2,35]},{18:[2,36],33:[2,36]},{18:[2,37],33:[2,37]}], 189 defaultActions: {16:[2,1],37:[2,23],53:[2,21]}, 190 parseError: function parseError(str, hash) { 191 throw new Error(str); 192 }, 193 parse: function parse(input) { 194 var self = this, 195 stack = [0], 196 vstack = [null], // semantic value stack 197 lstack = [], // location stack 198 table = this.table, 199 yytext = '', 200 yylineno = 0, 201 yyleng = 0, 202 recovering = 0, 203 TERROR = 2, 204 EOF = 1; 205 206 //this.reductionCount = this.shiftCount = 0; 207 208 this.lexer.setInput(input); 209 this.lexer.yy = this.yy; 210 this.yy.lexer = this.lexer; 211 if (typeof this.lexer.yylloc == 'undefined') 212 this.lexer.yylloc = {}; 213 var yyloc = this.lexer.yylloc; 214 lstack.push(yyloc); 215 216 if (typeof this.yy.parseError === 'function') 217 this.parseError = this.yy.parseError; 218 219 function popStack (n) { 220 stack.length = stack.length - 2*n; 221 vstack.length = vstack.length - n; 222 lstack.length = lstack.length - n; 223 } 224 225 function lex() { 226 var token; 227 token = self.lexer.lex() || 1; // $end = 1 228 // if token isn't its numeric value, convert 229 if (typeof token !== 'number') { 230 token = self.symbols_[token] || token; 231 } 232 return token; 233 }; 234 235 var symbol, preErrorSymbol, state, action, a, r, yyval={},p,len,newState, expected; 236 while (true) { 237 // retreive state number from top of stack 238 state = stack[stack.length-1]; 239 240 // use default actions if available 241 if (this.defaultActions[state]) { 242 action = this.defaultActions[state]; 243 } else { 244 if (symbol == null) 245 symbol = lex(); 246 // read action for current state and first input 247 action = table[state] && table[state][symbol]; 248 } 249 250 // handle parse error 251 if (typeof action === 'undefined' || !action.length || !action[0]) { 252 253 if (!recovering) { 254 // Report error 255 expected = []; 256 for (p in table[state]) if (this.terminals_[p] && p > 2) { 257 expected.push("'"+this.terminals_[p]+"'"); 258 } 259 var errStr = ''; 260 if (this.lexer.showPosition) { 261 errStr = 'Parse error on line '+(yylineno+1)+":\n"+this.lexer.showPosition()+'\nExpecting '+expected.join(', '); 262 } else { 263 errStr = 'Parse error on line '+(yylineno+1)+": Unexpected " + 264 (symbol == 1 /*EOF*/ ? "end of input" : 265 ("'"+(this.terminals_[symbol] || symbol)+"'")); 266 } 267 this.parseError(errStr, 268 {text: this.lexer.match, token: this.terminals_[symbol] || symbol, line: this.lexer.yylineno, loc: yyloc, expected: expected}); 269 } 270 271 // just recovered from another error 272 if (recovering == 3) { 273 if (symbol == EOF) { 274 throw new Error(errStr || 'Parsing halted.'); 275 } 276 277 // discard current lookahead and grab another 278 yyleng = this.lexer.yyleng; 279 yytext = this.lexer.yytext; 280 yylineno = this.lexer.yylineno; 281 yyloc = this.lexer.yylloc; 282 symbol = lex(); 283 } 284 285 // try to recover from error 286 while (1) { 287 // check for error recovery rule in this state 288 if ((TERROR.toString()) in table[state]) { 289 break; 290 } 291 if (state == 0) { 292 throw new Error(errStr || 'Parsing halted.'); 293 } 294 popStack(1); 295 state = stack[stack.length-1]; 296 } 297 298 preErrorSymbol = symbol; // save the lookahead token 299 symbol = TERROR; // insert generic error symbol as new lookahead 300 state = stack[stack.length-1]; 301 action = table[state] && table[state][TERROR]; 302 recovering = 3; // allow 3 real symbols to be shifted before reporting a new error 303 } 304 305 // this shouldn't happen, unless resolve defaults are off 306 if (action[0] instanceof Array && action.length > 1) { 307 throw new Error('Parse Error: multiple actions possible at state: '+state+', token: '+symbol); 308 } 309 310 switch (action[0]) { 311 312 case 1: // shift 313 //this.shiftCount++; 314 315 stack.push(symbol); 316 vstack.push(this.lexer.yytext); 317 lstack.push(this.lexer.yylloc); 318 stack.push(action[1]); // push state 319 symbol = null; 320 if (!preErrorSymbol) { // normal execution/no error 321 yyleng = this.lexer.yyleng; 322 yytext = this.lexer.yytext; 323 yylineno = this.lexer.yylineno; 324 yyloc = this.lexer.yylloc; 325 if (recovering > 0) 326 recovering--; 327 } else { // error just occurred, resume old lookahead f/ before error 328 symbol = preErrorSymbol; 329 preErrorSymbol = null; 330 } 331 break; 332 333 case 2: // reduce 334 //this.reductionCount++; 335 336 len = this.productions_[action[1]][1]; 337 338 // perform semantic action 339 yyval.$ = vstack[vstack.length-len]; // default to $$ = $1 340 // default location, uses first token for firsts, last for lasts 341 yyval._$ = { 342 first_line: lstack[lstack.length-(len||1)].first_line, 343 last_line: lstack[lstack.length-1].last_line, 344 first_column: lstack[lstack.length-(len||1)].first_column, 345 last_column: lstack[lstack.length-1].last_column 346 }; 347 r = this.performAction.call(yyval, yytext, yyleng, yylineno, this.yy, action[1], vstack, lstack); 348 349 if (typeof r !== 'undefined') { 350 return r; 351 } 352 353 // pop off stack 354 if (len) { 355 stack = stack.slice(0,-1*len*2); 356 vstack = vstack.slice(0, -1*len); 357 lstack = lstack.slice(0, -1*len); 358 } 359 360 stack.push(this.productions_[action[1]][0]); // push nonterminal (reduce) 361 vstack.push(yyval.$); 362 lstack.push(yyval._$); 363 // goto new state = table[STATE][NONTERMINAL] 364 newState = table[stack[stack.length-2]][stack[stack.length-1]]; 365 stack.push(newState); 366 break; 367 368 case 3: // accept 369 return true; 370 } 371 372 } 373 374 return true; 375 }};/* Jison generated lexer */ 376 var lexer = (function(){ 377 378 var lexer = ({EOF:1, 379 parseError:function parseError(str, hash) { 380 if (this.yy.parseError) { 381 this.yy.parseError(str, hash); 382 } else { 383 throw new Error(str); 384 } 385 }, 386 setInput:function (input) { 387 this._input = input; 388 this._more = this._less = this.done = false; 389 this.yylineno = this.yyleng = 0; 390 this.yytext = this.matched = this.match = ''; 391 this.conditionStack = ['INITIAL']; 392 this.yylloc = {first_line:1,first_column:0,last_line:1,last_column:0}; 393 return this; 394 }, 395 input:function () { 396 var ch = this._input[0]; 397 this.yytext+=ch; 398 this.yyleng++; 399 this.match+=ch; 400 this.matched+=ch; 401 var lines = ch.match(/\n/); 402 if (lines) this.yylineno++; 403 this._input = this._input.slice(1); 404 return ch; 405 }, 406 unput:function (ch) { 407 this._input = ch + this._input; 408 return this; 409 }, 410 more:function () { 411 this._more = true; 412 return this; 413 }, 414 pastInput:function () { 415 var past = this.matched.substr(0, this.matched.length - this.match.length); 416 return (past.length > 20 ? '...':'') + past.substr(-20).replace(/\n/g, ""); 417 }, 418 upcomingInput:function () { 419 var next = this.match; 420 if (next.length < 20) { 421 next += this._input.substr(0, 20-next.length); 422 } 423 return (next.substr(0,20)+(next.length > 20 ? '...':'')).replace(/\n/g, ""); 424 }, 425 showPosition:function () { 426 var pre = this.pastInput(); 427 var c = new Array(pre.length + 1).join("-"); 428 return pre + this.upcomingInput() + "\n" + c+"^"; 429 }, 430 next:function () { 431 if (this.done) { 432 return this.EOF; 433 } 434 if (!this._input) this.done = true; 435 436 var token, 437 match, 438 col, 439 lines; 440 if (!this._more) { 441 this.yytext = ''; 442 this.match = ''; 443 } 444 var rules = this._currentRules(); 445 for (var i=0;i < rules.length; i++) { 446 match = this._input.match(this.rules[rules[i]]); 447 if (match) { 448 lines = match[0].match(/\n.*/g); 449 if (lines) this.yylineno += lines.length; 450 this.yylloc = {first_line: this.yylloc.last_line, 451 last_line: this.yylineno+1, 452 first_column: this.yylloc.last_column, 453 last_column: lines ? lines[lines.length-1].length-1 : this.yylloc.last_column + match[0].length} 454 this.yytext += match[0]; 455 this.match += match[0]; 456 this.matches = match; 457 this.yyleng = this.yytext.length; 458 this._more = false; 459 this._input = this._input.slice(match[0].length); 460 this.matched += match[0]; 461 token = this.performAction.call(this, this.yy, this, rules[i],this.conditionStack[this.conditionStack.length-1]); 462 if (token) return token; 463 else return; 464 } 465 } 466 if (this._input === "") { 467 return this.EOF; 468 } else { 469 this.parseError('Lexical error on line '+(this.yylineno+1)+'. Unrecognized text.\n'+this.showPosition(), 470 {text: "", token: null, line: this.yylineno}); 471 } 472 }, 473 lex:function lex() { 474 var r = this.next(); 475 if (typeof r !== 'undefined') { 476 return r; 477 } else { 478 return this.lex(); 479 } 480 }, 481 begin:function begin(condition) { 482 this.conditionStack.push(condition); 483 }, 484 popState:function popState() { 485 return this.conditionStack.pop(); 486 }, 487 _currentRules:function _currentRules() { 488 return this.conditions[this.conditionStack[this.conditionStack.length-1]].rules; 489 }}); 490 lexer.performAction = function anonymous(yy,yy_,$avoiding_name_collisions,YY_START) { 491 492 var YYSTATE=YY_START 493 switch($avoiding_name_collisions) { 494 case 0: this.begin("mu"); if (yy_.yytext) return 14; 495 break; 496 case 1: return 14; 497 break; 498 case 2: return 24; 499 break; 500 case 3: return 16; 501 break; 502 case 4: return 20; 503 break; 504 case 5: return 19; 505 break; 506 case 6: return 19; 507 break; 508 case 7: return 23; 509 break; 510 case 8: return 23; 511 break; 512 case 9: yy_.yytext = yy_.yytext.substr(3,yy_.yyleng-5); this.begin("INITIAL"); return 15; 513 break; 514 case 10: return 22; 515 break; 516 case 11: return 34; 517 break; 518 case 12: return 33; 519 break; 520 case 13: return 33; 521 break; 522 case 14: return 36; 523 break; 524 case 15: /*ignore whitespace*/ 525 break; 526 case 16: this.begin("INITIAL"); return 18; 527 break; 528 case 17: this.begin("INITIAL"); return 18; 529 break; 530 case 18: yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2).replace(/\\"/g,'"'); return 28; 531 break; 532 case 19: return 30; 533 break; 534 case 20: return 30; 535 break; 536 case 21: return 29; 537 break; 538 case 22: return 33; 539 break; 540 case 23: yy_.yytext = yy_.yytext.substr(1, yy_.yyleng-2); return 33; 541 break; 542 case 24: return 'INVALID'; 543 break; 544 case 25: return 5; 545 break; 546 } 547 }; 548 lexer.rules = [/^[^\x00]*?(?=(\{\{))/,/^[^\x00]+/,/^\{\{>/,/^\{\{#/,/^\{\{\//,/^\{\{\^/,/^\{\{\s*else\b/,/^\{\{\{/,/^\{\{&/,/^\{\{![\s\S]*?\}\}/,/^\{\{/,/^=/,/^\.(?=[} ])/,/^\.\./,/^[/.]/,/^\s+/,/^\}\}\}/,/^\}\}/,/^"(\\["]|[^"])*"/,/^true(?=[}\s])/,/^false(?=[}\s])/,/^[0-9]+(?=[}\s])/,/^[a-zA-Z0-9_$-]+(?=[=}\s/.])/,/^\[.*\]/,/^./,/^$/]; 549 lexer.conditions = {"mu":{"rules":[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25],"inclusive":false},"INITIAL":{"rules":[0,1,25],"inclusive":true}};return lexer;})() 550 parser.lexer = lexer; 551 return parser; 552 })();; 553 // lib/handlebars/compiler/base.js 554 Handlebars.Parser = handlebars; 555 556 Handlebars.parse = function(string) { 557 Handlebars.Parser.yy = Handlebars.AST; 558 return Handlebars.Parser.parse(string); 559 }; 560 561 Handlebars.print = function(ast) { 562 return new Handlebars.PrintVisitor().accept(ast); 563 }; 564 565 Handlebars.logger = { 566 DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3, 567 568 // override in the host environment 569 log: function(level, str) {} 570 }; 571 572 Handlebars.log = function(level, str) { Handlebars.logger.log(level, str); }; 573 ; 574 // lib/handlebars/compiler/ast.js 575 (function() { 576 577 Handlebars.AST = {}; 578 579 Handlebars.AST.ProgramNode = function(statements, inverse) { 580 this.type = "program"; 581 this.statements = statements; 582 if(inverse) { this.inverse = new Handlebars.AST.ProgramNode(inverse); } 583 }; 584 585 Handlebars.AST.MustacheNode = function(params, hash, unescaped) { 586 this.type = "mustache"; 587 this.id = params[0]; 588 this.params = params.slice(1); 589 this.hash = hash; 590 this.escaped = !unescaped; 591 }; 592 593 Handlebars.AST.PartialNode = function(id, context) { 594 this.type = "partial"; 595 596 // TODO: disallow complex IDs 597 598 this.id = id; 599 this.context = context; 600 }; 601 602 var verifyMatch = function(open, close) { 603 if(open.original !== close.original) { 604 throw new Handlebars.Exception(open.original + " doesn't match " + close.original); 605 } 606 }; 607 608 Handlebars.AST.BlockNode = function(mustache, program, close) { 609 verifyMatch(mustache.id, close); 610 this.type = "block"; 611 this.mustache = mustache; 612 this.program = program; 613 }; 614 615 Handlebars.AST.InverseNode = function(mustache, program, close) { 616 verifyMatch(mustache.id, close); 617 this.type = "inverse"; 618 this.mustache = mustache; 619 this.program = program; 620 }; 621 622 Handlebars.AST.ContentNode = function(string) { 623 this.type = "content"; 624 this.string = string; 625 }; 626 627 Handlebars.AST.HashNode = function(pairs) { 628 this.type = "hash"; 629 this.pairs = pairs; 630 }; 631 632 Handlebars.AST.IdNode = function(parts) { 633 this.type = "ID"; 634 this.original = parts.join("."); 635 636 var dig = [], depth = 0; 637 638 for(var i=0,l=parts.length; i<l; i++) { 639 var part = parts[i]; 640 641 if(part === "..") { depth++; } 642 else if(part === "." || part === "this") { this.isScoped = true; } 643 else { dig.push(part); } 644 } 645 646 this.parts = dig; 647 this.string = dig.join('.'); 648 this.depth = depth; 649 this.isSimple = (dig.length === 1) && (depth === 0); 650 }; 651 652 Handlebars.AST.StringNode = function(string) { 653 this.type = "STRING"; 654 this.string = string; 655 }; 656 657 Handlebars.AST.IntegerNode = function(integer) { 658 this.type = "INTEGER"; 659 this.integer = integer; 660 }; 661 662 Handlebars.AST.BooleanNode = function(bool) { 663 this.type = "BOOLEAN"; 664 this.bool = bool; 665 }; 666 667 Handlebars.AST.CommentNode = function(comment) { 668 this.type = "comment"; 669 this.comment = comment; 670 }; 671 672 })();; 673 // lib/handlebars/utils.js 674 Handlebars.Exception = function(message) { 675 var tmp = Error.prototype.constructor.apply(this, arguments); 676 677 for (var p in tmp) { 678 if (tmp.hasOwnProperty(p)) { this[p] = tmp[p]; } 679 } 680 }; 681 Handlebars.Exception.prototype = new Error; 682 683 // Build out our basic SafeString type 684 Handlebars.SafeString = function(string) { 685 this.string = string; 686 }; 687 Handlebars.SafeString.prototype.toString = function() { 688 return this.string.toString(); 689 }; 690 691 (function() { 692 var escape = { 693 "&": "&", 694 "<": "<", 695 ">": ">", 696 '"': """, 697 "'": "'", 698 "`": "`" 699 }; 700 701 var badChars = /[&<>"'`]/g; 702 var possible = /[&<>"'`]/; 703 704 var escapeChar = function(chr) { 705 return escape[chr] || "&"; 706 }; 707 708 Handlebars.Utils = { 709 escapeExpression: function(string) { 710 // don't escape SafeStrings, since they're already safe 711 if (string instanceof Handlebars.SafeString) { 712 return string.toString(); 713 } else if (string == null || string === false) { 714 return ""; 715 } 716 717 if(!possible.test(string)) { return string; } 718 return string.replace(badChars, escapeChar); 719 }, 720 721 isEmpty: function(value) { 722 if (typeof value === "undefined") { 723 return true; 724 } else if (value === null) { 725 return true; 726 } else if (value === false) { 727 return true; 728 } else if(Object.prototype.toString.call(value) === "[object Array]" && value.length === 0) { 729 return true; 730 } else { 731 return false; 732 } 733 } 734 }; 735 })();; 736 // lib/handlebars/compiler/compiler.js 737 Handlebars.Compiler = function() {}; 738 Handlebars.JavaScriptCompiler = function() {}; 739 740 (function(Compiler, JavaScriptCompiler) { 741 Compiler.OPCODE_MAP = { 742 appendContent: 1, 743 getContext: 2, 744 lookupWithHelpers: 3, 745 lookup: 4, 746 append: 5, 747 invokeMustache: 6, 748 appendEscaped: 7, 749 pushString: 8, 750 truthyOrFallback: 9, 751 functionOrFallback: 10, 752 invokeProgram: 11, 753 invokePartial: 12, 754 push: 13, 755 assignToHash: 15, 756 pushStringParam: 16 757 }; 758 759 Compiler.MULTI_PARAM_OPCODES = { 760 appendContent: 1, 761 getContext: 1, 762 lookupWithHelpers: 2, 763 lookup: 1, 764 invokeMustache: 3, 765 pushString: 1, 766 truthyOrFallback: 1, 767 functionOrFallback: 1, 768 invokeProgram: 3, 769 invokePartial: 1, 770 push: 1, 771 assignToHash: 1, 772 pushStringParam: 1 773 }; 774 775 Compiler.DISASSEMBLE_MAP = {}; 776 777 for(var prop in Compiler.OPCODE_MAP) { 778 var value = Compiler.OPCODE_MAP[prop]; 779 Compiler.DISASSEMBLE_MAP[value] = prop; 780 } 781 782 Compiler.multiParamSize = function(code) { 783 return Compiler.MULTI_PARAM_OPCODES[Compiler.DISASSEMBLE_MAP[code]]; 784 }; 785 786 Compiler.prototype = { 787 compiler: Compiler, 788 789 disassemble: function() { 790 var opcodes = this.opcodes, opcode, nextCode; 791 var out = [], str, name, value; 792 793 for(var i=0, l=opcodes.length; i<l; i++) { 794 opcode = opcodes[i]; 795 796 if(opcode === 'DECLARE') { 797 name = opcodes[++i]; 798 value = opcodes[++i]; 799 out.push("DECLARE " + name + " = " + value); 800 } else { 801 str = Compiler.DISASSEMBLE_MAP[opcode]; 802 803 var extraParams = Compiler.multiParamSize(opcode); 804 var codes = []; 805 806 for(var j=0; j<extraParams; j++) { 807 nextCode = opcodes[++i]; 808 809 if(typeof nextCode === "string") { 810 nextCode = "\"" + nextCode.replace("\n", "\\n") + "\""; 811 } 812 813 codes.push(nextCode); 814 } 815 816 str = str + " " + codes.join(" "); 817 818 out.push(str); 819 } 820 } 821 822 return out.join("\n"); 823 }, 824 825 guid: 0, 826 827 compile: function(program, options) { 828 this.children = []; 829 this.depths = {list: []}; 830 this.options = options; 831 832 // These changes will propagate to the other compiler components 833 var knownHelpers = this.options.knownHelpers; 834 this.options.knownHelpers = { 835 'helperMissing': true, 836 'blockHelperMissing': true, 837 'each': true, 838 'if': true, 839 'unless': true, 840 'with': true, 841 'log': true 842 }; 843 if (knownHelpers) { 844 for (var name in knownHelpers) { 845 this.options.knownHelpers[name] = knownHelpers[name]; 846 } 847 } 848 849 return this.program(program); 850 }, 851 852 accept: function(node) { 853 return this[node.type](node); 854 }, 855 856 program: function(program) { 857 var statements = program.statements, statement; 858 this.opcodes = []; 859 860 for(var i=0, l=statements.length; i<l; i++) { 861 statement = statements[i]; 862 this[statement.type](statement); 863 } 864 this.isSimple = l === 1; 865 866 this.depths.list = this.depths.list.sort(function(a, b) { 867 return a - b; 868 }); 869 870 return this; 871 }, 872 873 compileProgram: function(program) { 874 var result = new this.compiler().compile(program, this.options); 875 var guid = this.guid++; 876 877 this.usePartial = this.usePartial || result.usePartial; 878 879 this.children[guid] = result; 880 881 for(var i=0, l=result.depths.list.length; i<l; i++) { 882 depth = result.depths.list[i]; 883 884 if(depth < 2) { continue; } 885 else { this.addDepth(depth - 1); } 886 } 887 888 return guid; 889 }, 890 891 block: function(block) { 892 var mustache = block.mustache; 893 var depth, child, inverse, inverseGuid; 894 895 var params = this.setupStackForMustache(mustache); 896 897 var programGuid = this.compileProgram(block.program); 898 899 if(block.program.inverse) { 900 inverseGuid = this.compileProgram(block.program.inverse); 901 this.declare('inverse', inverseGuid); 902 } 903 904 this.opcode('invokeProgram', programGuid, params.length, !!mustache.hash); 905 this.declare('inverse', null); 906 this.opcode('append'); 907 }, 908 909 inverse: function(block) { 910 var params = this.setupStackForMustache(block.mustache); 911 912 var programGuid = this.compileProgram(block.program); 913 914 this.declare('inverse', programGuid); 915 916 this.opcode('invokeProgram', null, params.length, !!block.mustache.hash); 917 this.opcode('append'); 918 }, 919 920 hash: function(hash) { 921 var pairs = hash.pairs, pair, val; 922 923 this.opcode('push', '{}'); 924 925 for(var i=0, l=pairs.length; i<l; i++) { 926 pair = pairs[i]; 927 val = pair[1]; 928 929 this.accept(val); 930 this.opcode('assignToHash', pair[0]); 931 } 932 }, 933 934 partial: function(partial) { 935 var id = partial.id; 936 this.usePartial = true; 937 938 if(partial.context) { 939 this.ID(partial.context); 940 } else { 941 this.opcode('push', 'depth0'); 942 } 943 944 this.opcode('invokePartial', id.original); 945 this.opcode('append'); 946 }, 947 948 content: function(content) { 949 this.opcode('appendContent', content.string); 950 }, 951 952 mustache: function(mustache) { 953 var params = this.setupStackForMustache(mustache); 954 955 this.opcode('invokeMustache', params.length, mustache.id.original, !!mustache.hash); 956 957 if(mustache.escaped) { 958 this.opcode('appendEscaped'); 959 } else { 960 this.opcode('append'); 961 } 962 }, 963 964 ID: function(id) { 965 this.addDepth(id.depth); 966 967 this.opcode('getContext', id.depth); 968 969 this.opcode('lookupWithHelpers', id.parts[0] || null, id.isScoped || false); 970 971 for(var i=1, l=id.parts.length; i<l; i++) { 972 this.opcode('lookup', id.parts[i]); 973 } 974 }, 975 976 STRING: function(string) { 977 this.opcode('pushString', string.string); 978 }, 979 980 INTEGER: function(integer) { 981 this.opcode('push', integer.integer); 982 }, 983 984 BOOLEAN: function(bool) { 985 this.opcode('push', bool.bool); 986 }, 987 988 comment: function() {}, 989 990 // HELPERS 991 pushParams: function(params) { 992 var i = params.length, param; 993 994 while(i--) { 995 param = params[i]; 996 997 if(this.options.stringParams) { 998 if(param.depth) { 999 this.addDepth(param.depth); 1000 } 1001 1002 this.opcode('getContext', param.depth || 0); 1003 this.opcode('pushStringParam', param.string); 1004 } else { 1005 this[param.type](param); 1006 } 1007 } 1008 }, 1009 1010 opcode: function(name, val1, val2, val3) { 1011 this.opcodes.push(Compiler.OPCODE_MAP[name]); 1012 if(val1 !== undefined) { this.opcodes.push(val1); } 1013 if(val2 !== undefined) { this.opcodes.push(val2); } 1014 if(val3 !== undefined) { this.opcodes.push(val3); } 1015 }, 1016 1017 declare: function(name, value) { 1018 this.opcodes.push('DECLARE'); 1019 this.opcodes.push(name); 1020 this.opcodes.push(value); 1021 }, 1022 1023 addDepth: function(depth) { 1024 if(depth === 0) { return; } 1025 1026 if(!this.depths[depth]) { 1027 this.depths[depth] = true; 1028 this.depths.list.push(depth); 1029 } 1030 }, 1031 1032 setupStackForMustache: function(mustache) { 1033 var params = mustache.params; 1034 1035 this.pushParams(params); 1036 1037 if(mustache.hash) { 1038 this.hash(mustache.hash); 1039 } 1040 1041 this.ID(mustache.id); 1042 1043 return params; 1044 } 1045 }; 1046 1047 JavaScriptCompiler.prototype = { 1048 // PUBLIC API: You can override these methods in a subclass to provide 1049 // alternative compiled forms for name lookup and buffering semantics 1050 nameLookup: function(parent, name, type) { 1051 if (/^[0-9]+$/.test(name)) { 1052 return parent + "[" + name + "]"; 1053 } else if (JavaScriptCompiler.isValidJavaScriptVariableName(name)) { 1054 return parent + "." + name; 1055 } 1056 else { 1057 return parent + "['" + name + "']"; 1058 } 1059 }, 1060 1061 appendToBuffer: function(string) { 1062 if (this.environment.isSimple) { 1063 return "return " + string + ";"; 1064 } else { 1065 return "buffer += " + string + ";"; 1066 } 1067 }, 1068 1069 initializeBuffer: function() { 1070 return this.quotedString(""); 1071 }, 1072 1073 namespace: "Handlebars", 1074 // END PUBLIC API 1075 1076 compile: function(environment, options, context, asObject) { 1077 this.environment = environment; 1078 this.options = options || {}; 1079 1080 this.name = this.environment.name; 1081 this.isChild = !!context; 1082 this.context = context || { 1083 programs: [], 1084 aliases: { self: 'this' }, 1085 registers: {list: []} 1086 }; 1087 1088 this.preamble(); 1089 1090 this.stackSlot = 0; 1091 this.stackVars = []; 1092 1093 this.compileChildren(environment, options); 1094 1095 var opcodes = environment.opcodes, opcode; 1096 1097 this.i = 0; 1098 1099 for(l=opcodes.length; this.i<l; this.i++) { 1100 opcode = this.nextOpcode(0); 1101 1102 if(opcode[0] === 'DECLARE') { 1103 this.i = this.i + 2; 1104 this[opcode[1]] = opcode[2]; 1105 } else { 1106 this.i = this.i + opcode[1].length; 1107 this[opcode[0]].apply(this, opcode[1]); 1108 } 1109 } 1110 1111 return this.createFunctionContext(asObject); 1112 }, 1113 1114 nextOpcode: function(n) { 1115 var opcodes = this.environment.opcodes, opcode = opcodes[this.i + n], name, val; 1116 var extraParams, codes; 1117 1118 if(opcode === 'DECLARE') { 1119 name = opcodes[this.i + 1]; 1120 val = opcodes[this.i + 2]; 1121 return ['DECLARE', name, val]; 1122 } else { 1123 name = Compiler.DISASSEMBLE_MAP[opcode]; 1124 1125 extraParams = Compiler.multiParamSize(opcode); 1126 codes = []; 1127 1128 for(var j=0; j<extraParams; j++) { 1129 codes.push(opcodes[this.i + j + 1 + n]); 1130 } 1131 1132 return [name, codes]; 1133 } 1134 }, 1135 1136 eat: function(opcode) { 1137 this.i = this.i + opcode.length; 1138 }, 1139 1140 preamble: function() { 1141 var out = []; 1142 1143 if (!this.isChild) { 1144 var namespace = this.namespace; 1145 var copies = "helpers = helpers || " + namespace + ".helpers;"; 1146 if(this.environment.usePartial) { copies = copies + " partials = partials || " + namespace + ".partials;"; } 1147 out.push(copies); 1148 } else { 1149 out.push(''); 1150 } 1151 1152 if (!this.environment.isSimple) { 1153 out.push(", buffer = " + this.initializeBuffer()); 1154 } else { 1155 out.push(""); 1156 } 1157 1158 // track the last context pushed into place to allow skipping the 1159 // getContext opcode when it would be a noop 1160 this.lastContext = 0; 1161 this.source = out; 1162 }, 1163 1164 createFunctionContext: function(asObject) { 1165 var locals = this.stackVars; 1166 if (!this.isChild) { 1167 locals = locals.concat(this.context.registers.list); 1168 } 1169 1170 if(locals.length > 0) { 1171 this.source[1] = this.source[1] + ", " + locals.join(", "); 1172 } 1173 1174 // Generate minimizer alias mappings 1175 if (!this.isChild) { 1176 var aliases = [] 1177 for (var alias in this.context.aliases) { 1178 this.source[1] = this.source[1] + ', ' + alias + '=' + this.context.aliases[alias]; 1179 } 1180 } 1181 1182 if (this.source[1]) { 1183 this.source[1] = "var " + this.source[1].substring(2) + ";"; 1184 } 1185 1186 // Merge children 1187 if (!this.isChild) { 1188 this.source[1] += '\n' + this.context.programs.join('\n') + '\n'; 1189 } 1190 1191 if (!this.environment.isSimple) { 1192 this.source.push("return buffer;"); 1193 } 1194 1195 var params = this.isChild ? ["depth0", "data"] : ["Handlebars", "depth0", "helpers", "partials", "data"]; 1196 1197 for(var i=0, l=this.environment.depths.list.length; i<l; i++) { 1198 params.push("depth" + this.environment.depths.list[i]); 1199 } 1200 1201 if (asObject) { 1202 params.push(this.source.join("\n ")); 1203 1204 return Function.apply(this, params); 1205 } else { 1206 var functionSource = 'function ' + (this.name || '') + '(' + params.join(',') + ') {\n ' + this.source.join("\n ") + '}'; 1207 Handlebars.log(Handlebars.logger.DEBUG, functionSource + "\n\n"); 1208 return functionSource; 1209 } 1210 }, 1211 1212 appendContent: function(content) { 1213 this.source.push(this.appendToBuffer(this.quotedString(content))); 1214 }, 1215 1216 append: function() { 1217 var local = this.popStack(); 1218 this.source.push("if(" + local + " || " + local + " === 0) { " + this.appendToBuffer(local) + " }"); 1219 if (this.environment.isSimple) { 1220 this.source.push("else { " + this.appendToBuffer("''") + " }"); 1221 } 1222 }, 1223 1224 appendEscaped: function() { 1225 var opcode = this.nextOpcode(1), extra = ""; 1226 this.context.aliases.escapeExpression = 'this.escapeExpression'; 1227 1228 if(opcode[0] === 'appendContent') { 1229 extra = " + " + this.quotedString(opcode[1][0]); 1230 this.eat(opcode); 1231 } 1232 1233 this.source.push(this.appendToBuffer("escapeExpression(" + this.popStack() + ")" + extra)); 1234 }, 1235 1236 getContext: function(depth) { 1237 if(this.lastContext !== depth) { 1238 this.lastContext = depth; 1239 } 1240 }, 1241 1242 lookupWithHelpers: function(name, isScoped) { 1243 if(name) { 1244 var topStack = this.nextStack(); 1245 1246 this.usingKnownHelper = false; 1247 1248 var toPush; 1249 if (!isScoped && this.options.knownHelpers[name]) { 1250 toPush = topStack + " = " + this.nameLookup('helpers', name, 'helper'); 1251 this.usingKnownHelper = true; 1252 } else if (isScoped || this.options.knownHelpersOnly) { 1253 toPush = topStack + " = " + this.nameLookup('depth' + this.lastContext, name, 'context'); 1254 } else { 1255 toPush = topStack + " = " 1256 + this.nameLookup('helpers', name, 'helper') 1257 + " || " 1258 + this.nameLookup('depth' + this.lastContext, name, 'context'); 1259 } 1260 1261 toPush += ';'; 1262 this.source.push(toPush); 1263 } else { 1264 this.pushStack('depth' + this.lastContext); 1265 } 1266 }, 1267 1268 lookup: function(name) { 1269 var topStack = this.topStack(); 1270 this.source.push(topStack + " = (" + topStack + " === null || " + topStack + " === undefined || " + topStack + " === false ? " + 1271 topStack + " : " + this.nameLookup(topStack, name, 'context') + ");"); 1272 }, 1273 1274 pushStringParam: function(string) { 1275 this.pushStack('depth' + this.lastContext); 1276 this.pushString(string); 1277 }, 1278 1279 pushString: function(string) { 1280 this.pushStack(this.quotedString(string)); 1281 }, 1282 1283 push: function(name) { 1284 this.pushStack(name); 1285 }, 1286 1287 invokeMustache: function(paramSize, original, hasHash) { 1288 this.populateParams(paramSize, this.quotedString(original), "{}", null, hasHash, function(nextStack, helperMissingString, id) { 1289 if (!this.usingKnownHelper) { 1290 this.context.aliases.helperMissing = 'helpers.helperMissing'; 1291 this.context.aliases.undef = 'void 0'; 1292 this.source.push("else if(" + id + "=== undef) { " + nextStack + " = helperMissing.call(" + helperMissingString + "); }"); 1293 if (nextStack !== id) { 1294 this.source.push("else { " + nextStack + " = " + id + "; }"); 1295 } 1296 } 1297 }); 1298 }, 1299 1300 invokeProgram: function(guid, paramSize, hasHash) { 1301 var inverse = this.programExpression(this.inverse); 1302 var mainProgram = this.programExpression(guid); 1303 1304 this.populateParams(paramSize, null, mainProgram, inverse, hasHash, function(nextStack, helperMissingString, id) { 1305 if (!this.usingKnownHelper) { 1306 this.context.aliases.blockHelperMissing = 'helpers.blockHelperMissing'; 1307 this.source.push("else { " + nextStack + " = blockHelperMissing.call(" + helperMissingString + "); }"); 1308 } 1309 }); 1310 }, 1311 1312 populateParams: function(paramSize, helperId, program, inverse, hasHash, fn) { 1313 var needsRegister = hasHash || this.options.stringParams || inverse || this.options.data; 1314 var id = this.popStack(), nextStack; 1315 var params = [], param, stringParam, stringOptions; 1316 1317 if (needsRegister) { 1318 this.register('tmp1', program); 1319 stringOptions = 'tmp1'; 1320 } else { 1321 stringOptions = '{ hash: {} }'; 1322 } 1323 1324 if (needsRegister) { 1325 var hash = (hasHash ? this.popStack() : '{}'); 1326 this.source.push('tmp1.hash = ' + hash + ';'); 1327 } 1328 1329 if(this.options.stringParams) { 1330 this.source.push('tmp1.contexts = [];'); 1331 } 1332 1333 for(var i=0; i<paramSize; i++) { 1334 param = this.popStack(); 1335 params.push(param); 1336 1337 if(this.options.stringParams) { 1338 this.source.push('tmp1.contexts.push(' + this.popStack() + ');'); 1339 } 1340 } 1341 1342 if(inverse) { 1343 this.source.push('tmp1.fn = tmp1;'); 1344 this.source.push('tmp1.inverse = ' + inverse + ';'); 1345 } 1346 1347 if(this.options.data) { 1348 this.source.push('tmp1.data = data;'); 1349 } 1350 1351 params.push(stringOptions); 1352 1353 this.populateCall(params, id, helperId || id, fn); 1354 }, 1355 1356 populateCall: function(params, id, helperId, fn) { 1357 var paramString = ["depth0"].concat(params).join(", "); 1358 var helperMissingString = ["depth0"].concat(helperId).concat(params).join(", "); 1359 1360 var nextStack = this.nextStack(); 1361 1362 if (this.usingKnownHelper) { 1363 this.source.push(nextStack + " = " + id + ".call(" + paramString + ");"); 1364 } else { 1365 this.context.aliases.functionType = '"function"'; 1366 this.source.push("if(typeof " + id + " === functionType) { " + nextStack + " = " + id + ".call(" + paramString + "); }"); 1367 } 1368 fn.call(this, nextStack, helperMissingString, id); 1369 this.usingKnownHelper = false; 1370 }, 1371 1372 invokePartial: function(context) { 1373 this.pushStack("self.invokePartial(" + this.nameLookup('partials', context, 'partial') + ", '" + context + "', " + this.popStack() + ", helpers, partials);"); 1374 }, 1375 1376 assignToHash: function(key) { 1377 var value = this.popStack(); 1378 var hash = this.topStack(); 1379 1380 this.source.push(hash + "['" + key + "'] = " + value + ";"); 1381 }, 1382 1383 // HELPERS 1384 1385 compiler: JavaScriptCompiler, 1386 1387 compileChildren: function(environment, options) { 1388 var children = environment.children, child, compiler; 1389 1390 for(var i=0, l=children.length; i<l; i++) { 1391 child = children[i]; 1392 compiler = new this.compiler(); 1393 1394 this.context.programs.push(''); // Placeholder to prevent name conflicts for nested children 1395 var index = this.context.programs.length; 1396 child.index = index; 1397 child.name = 'program' + index; 1398 this.context.programs[index] = compiler.compile(child, options, this.context); 1399 } 1400 }, 1401 1402 programExpression: function(guid) { 1403 if(guid == null) { return "self.noop"; } 1404 1405 var child = this.environment.children[guid], 1406 depths = child.depths.list; 1407 var programParams = [child.index, child.name, "data"]; 1408 1409 for(var i=0, l = depths.length; i<l; i++) { 1410 depth = depths[i]; 1411 1412 if(depth === 1) { programParams.push("depth0"); } 1413 else { programParams.push("depth" + (depth - 1)); } 1414 } 1415 1416 if(depths.length === 0) { 1417 return "self.program(" + programParams.join(", ") + ")"; 1418 } else { 1419 programParams.shift(); 1420 return "self.programWithDepth(" + programParams.join(", ") + ")"; 1421 } 1422 }, 1423 1424 register: function(name, val) { 1425 this.useRegister(name); 1426 this.source.push(name + " = " + val + ";"); 1427 }, 1428 1429 useRegister: function(name) { 1430 if(!this.context.registers[name]) { 1431 this.context.registers[name] = true; 1432 this.context.registers.list.push(name); 1433 } 1434 }, 1435 1436 pushStack: function(item) { 1437 this.source.push(this.nextStack() + " = " + item + ";"); 1438 return "stack" + this.stackSlot; 1439 }, 1440 1441 nextStack: function() { 1442 this.stackSlot++; 1443 if(this.stackSlot > this.stackVars.length) { this.stackVars.push("stack" + this.stackSlot); } 1444 return "stack" + this.stackSlot; 1445 }, 1446 1447 popStack: function() { 1448 return "stack" + this.stackSlot--; 1449 }, 1450 1451 topStack: function() { 1452 return "stack" + this.stackSlot; 1453 }, 1454 1455 quotedString: function(str) { 1456 return '"' + str 1457 .replace(/\\/g, '\\\\') 1458 .replace(/"/g, '\\"') 1459 .replace(/\n/g, '\\n') 1460 .replace(/\r/g, '\\r') + '"'; 1461 } 1462 }; 1463 1464 var reservedWords = ("break case catch continue default delete do else finally " + 1465 "for function if in instanceof new return switch this throw " + 1466 "try typeof var void while with null true false").split(" "); 1467 1468 var compilerWords = JavaScriptCompiler.RESERVED_WORDS = {}; 1469 1470 for(var i=0, l=reservedWords.length; i<l; i++) { 1471 compilerWords[reservedWords[i]] = true; 1472 } 1473 1474 JavaScriptCompiler.isValidJavaScriptVariableName = function(name) { 1475 if(!JavaScriptCompiler.RESERVED_WORDS[name] && /^[a-zA-Z_$][0-9a-zA-Z_$]+$/.test(name)) { 1476 return true; 1477 } 1478 return false; 1479 } 1480 1481 })(Handlebars.Compiler, Handlebars.JavaScriptCompiler); 1482 1483 Handlebars.precompile = function(string, options) { 1484 options = options || {}; 1485 1486 var ast = Handlebars.parse(string); 1487 var environment = new Handlebars.Compiler().compile(ast, options); 1488 return new Handlebars.JavaScriptCompiler().compile(environment, options); 1489 }; 1490 1491 Handlebars.compile = function(string, options) { 1492 options = options || {}; 1493 1494 var compiled; 1495 function compile() { 1496 var ast = Handlebars.parse(string); 1497 var environment = new Handlebars.Compiler().compile(ast, options); 1498 var templateSpec = new Handlebars.JavaScriptCompiler().compile(environment, options, undefined, true); 1499 return Handlebars.template(templateSpec); 1500 } 1501 1502 // Template is only compiled on first use and cached after that point. 1503 return function(context, options) { 1504 if (!compiled) { 1505 compiled = compile(); 1506 } 1507 return compiled.call(this, context, options); 1508 }; 1509 }; 1510 ; 1511 // lib/handlebars/vm.js 1512 Handlebars.VM = { 1513 template: function(templateSpec) { 1514 // Just add water 1515 var container = { 1516 escapeExpression: Handlebars.Utils.escapeExpression, 1517 invokePartial: Handlebars.VM.invokePartial, 1518 programs: [], 1519 program: function(i, fn, data) { 1520 var programWrapper = this.programs[i]; 1521 if(data) { 1522 return Handlebars.VM.program(fn, data); 1523 } else if(programWrapper) { 1524 return programWrapper; 1525 } else { 1526 programWrapper = this.programs[i] = Handlebars.VM.program(fn); 1527 return programWrapper; 1528 } 1529 }, 1530 programWithDepth: Handlebars.VM.programWithDepth, 1531 noop: Handlebars.VM.noop 1532 }; 1533 1534 return function(context, options) { 1535 options = options || {}; 1536 return templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data); 1537 }; 1538 }, 1539 1540 programWithDepth: function(fn, data, $depth) { 1541 var args = Array.prototype.slice.call(arguments, 2); 1542 1543 return function(context, options) { 1544 options = options || {}; 1545 1546 return fn.apply(this, [context, options.data || data].concat(args)); 1547 }; 1548 }, 1549 program: function(fn, data) { 1550 return function(context, options) { 1551 options = options || {}; 1552 1553 return fn(context, options.data || data); 1554 }; 1555 }, 1556 noop: function() { return ""; }, 1557 invokePartial: function(partial, name, context, helpers, partials) { 1558 if(partial === undefined) { 1559 throw new Handlebars.Exception("The partial " + name + " could not be found"); 1560 } else if(partial instanceof Function) { 1561 return partial(context, {helpers: helpers, partials: partials}); 1562 } else if (!Handlebars.compile) { 1563 throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in vm mode"); 1564 } else { 1565 partials[name] = Handlebars.compile(partial); 1566 return partials[name](context, {helpers: helpers, partials: partials}); 1567 } 1568 } 1569 }; 1570 1571 Handlebars.template = Handlebars.VM.template; 1572 ; 1573