handlebars-amd-rails 1.0.0.rc.3

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