handlebars 0.4.0 → 0.5.0

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