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,133 +0,0 @@
1
- var Handlebars = require('./base');
2
-
3
- // BEGIN(BROWSER)
4
- (function() {
5
-
6
- Handlebars.AST = {};
7
-
8
- Handlebars.AST.ProgramNode = function(statements, inverse) {
9
- this.type = "program";
10
- this.statements = statements;
11
- if(inverse) { this.inverse = new Handlebars.AST.ProgramNode(inverse); }
12
- };
13
-
14
- Handlebars.AST.MustacheNode = function(rawParams, hash, unescaped) {
15
- this.type = "mustache";
16
- this.escaped = !unescaped;
17
- this.hash = hash;
18
-
19
- var id = this.id = rawParams[0];
20
- var params = this.params = rawParams.slice(1);
21
-
22
- // a mustache is an eligible helper if:
23
- // * its id is simple (a single part, not `this` or `..`)
24
- var eligibleHelper = this.eligibleHelper = id.isSimple;
25
-
26
- // a mustache is definitely a helper if:
27
- // * it is an eligible helper, and
28
- // * it has at least one parameter or hash segment
29
- this.isHelper = eligibleHelper && (params.length || hash);
30
-
31
- // if a mustache is an eligible helper but not a definite
32
- // helper, it is ambiguous, and will be resolved in a later
33
- // pass or at runtime.
34
- };
35
-
36
- Handlebars.AST.PartialNode = function(partialName, context) {
37
- this.type = "partial";
38
- this.partialName = partialName;
39
- this.context = context;
40
- };
41
-
42
- var verifyMatch = function(open, close) {
43
- if(open.original !== close.original) {
44
- throw new Handlebars.Exception(open.original + " doesn't match " + close.original);
45
- }
46
- };
47
-
48
- Handlebars.AST.BlockNode = function(mustache, program, inverse, close) {
49
- verifyMatch(mustache.id, close);
50
- this.type = "block";
51
- this.mustache = mustache;
52
- this.program = program;
53
- this.inverse = inverse;
54
-
55
- if (this.inverse && !this.program) {
56
- this.isInverse = true;
57
- }
58
- };
59
-
60
- Handlebars.AST.ContentNode = function(string) {
61
- this.type = "content";
62
- this.string = string;
63
- };
64
-
65
- Handlebars.AST.HashNode = function(pairs) {
66
- this.type = "hash";
67
- this.pairs = pairs;
68
- };
69
-
70
- Handlebars.AST.IdNode = function(parts) {
71
- this.type = "ID";
72
- this.original = parts.join(".");
73
-
74
- var dig = [], depth = 0;
75
-
76
- for(var i=0,l=parts.length; i<l; i++) {
77
- var part = parts[i];
78
-
79
- if (part === ".." || part === "." || part === "this") {
80
- if (dig.length > 0) { throw new Handlebars.Exception("Invalid path: " + this.original); }
81
- else if (part === "..") { depth++; }
82
- else { this.isScoped = true; }
83
- }
84
- else { dig.push(part); }
85
- }
86
-
87
- this.parts = dig;
88
- this.string = dig.join('.');
89
- this.depth = depth;
90
-
91
- // an ID is simple if it only has one part, and that part is not
92
- // `..` or `this`.
93
- this.isSimple = parts.length === 1 && !this.isScoped && depth === 0;
94
-
95
- this.stringModeValue = this.string;
96
- };
97
-
98
- Handlebars.AST.PartialNameNode = function(name) {
99
- this.type = "PARTIAL_NAME";
100
- this.name = name;
101
- };
102
-
103
- Handlebars.AST.DataNode = function(id) {
104
- this.type = "DATA";
105
- this.id = id;
106
- };
107
-
108
- Handlebars.AST.StringNode = function(string) {
109
- this.type = "STRING";
110
- this.string = string;
111
- this.stringModeValue = string;
112
- };
113
-
114
- Handlebars.AST.IntegerNode = function(integer) {
115
- this.type = "INTEGER";
116
- this.integer = integer;
117
- this.stringModeValue = Number(integer);
118
- };
119
-
120
- Handlebars.AST.BooleanNode = function(bool) {
121
- this.type = "BOOLEAN";
122
- this.bool = bool;
123
- this.stringModeValue = bool === "true";
124
- };
125
-
126
- Handlebars.AST.CommentNode = function(comment) {
127
- this.type = "comment";
128
- this.comment = comment;
129
- };
130
-
131
- })();
132
- // END(BROWSER)
133
-
@@ -1,21 +0,0 @@
1
- var handlebars = require("./parser");
2
- var Handlebars = require("../base");
3
-
4
- // BEGIN(BROWSER)
5
- Handlebars.Parser = handlebars;
6
-
7
- Handlebars.parse = function(input) {
8
-
9
- // Just return if an already-compile AST was passed in.
10
- if(input.constructor === Handlebars.AST.ProgramNode) { return input; }
11
-
12
- Handlebars.Parser.yy = Handlebars.AST;
13
- return Handlebars.Parser.parse(input);
14
- };
15
-
16
- Handlebars.print = function(ast) {
17
- return new Handlebars.PrintVisitor().accept(ast);
18
- };
19
- // END(BROWSER)
20
-
21
- module.exports = Handlebars;
@@ -1,1267 +0,0 @@
1
- var Handlebars = require("./base");
2
-
3
- // BEGIN(BROWSER)
4
-
5
- /*jshint eqnull:true*/
6
- Handlebars.Compiler = function() {};
7
- Handlebars.JavaScriptCompiler = function() {};
8
-
9
- (function(Compiler, JavaScriptCompiler) {
10
- // the foundHelper register will disambiguate helper lookup from finding a
11
- // function in a context. This is necessary for mustache compatibility, which
12
- // requires that context functions in blocks are evaluated by blockHelperMissing,
13
- // and then proceed as if the resulting value was provided to blockHelperMissing.
14
-
15
- Compiler.prototype = {
16
- compiler: Compiler,
17
-
18
- disassemble: function() {
19
- var opcodes = this.opcodes, opcode, out = [], params, param;
20
-
21
- for (var i=0, l=opcodes.length; i<l; i++) {
22
- opcode = opcodes[i];
23
-
24
- if (opcode.opcode === 'DECLARE') {
25
- out.push("DECLARE " + opcode.name + "=" + opcode.value);
26
- } else {
27
- params = [];
28
- for (var j=0; j<opcode.args.length; j++) {
29
- param = opcode.args[j];
30
- if (typeof param === "string") {
31
- param = "\"" + param.replace("\n", "\\n") + "\"";
32
- }
33
- params.push(param);
34
- }
35
- out.push(opcode.opcode + " " + params.join(" "));
36
- }
37
- }
38
-
39
- return out.join("\n");
40
- },
41
- equals: function(other) {
42
- var len = this.opcodes.length;
43
- if (other.opcodes.length !== len) {
44
- return false;
45
- }
46
-
47
- for (var i = 0; i < len; i++) {
48
- var opcode = this.opcodes[i],
49
- otherOpcode = other.opcodes[i];
50
- if (opcode.opcode !== otherOpcode.opcode || opcode.args.length !== otherOpcode.args.length) {
51
- return false;
52
- }
53
- for (var j = 0; j < opcode.args.length; j++) {
54
- if (opcode.args[j] !== otherOpcode.args[j]) {
55
- return false;
56
- }
57
- }
58
- }
59
- return true;
60
- },
61
-
62
- guid: 0,
63
-
64
- compile: function(program, options) {
65
- this.children = [];
66
- this.depths = {list: []};
67
- this.options = options;
68
-
69
- // These changes will propagate to the other compiler components
70
- var knownHelpers = this.options.knownHelpers;
71
- this.options.knownHelpers = {
72
- 'helperMissing': true,
73
- 'blockHelperMissing': true,
74
- 'each': true,
75
- 'if': true,
76
- 'unless': true,
77
- 'with': true,
78
- 'log': true
79
- };
80
- if (knownHelpers) {
81
- for (var name in knownHelpers) {
82
- this.options.knownHelpers[name] = knownHelpers[name];
83
- }
84
- }
85
-
86
- return this.program(program);
87
- },
88
-
89
- accept: function(node) {
90
- return this[node.type](node);
91
- },
92
-
93
- program: function(program) {
94
- var statements = program.statements, statement;
95
- this.opcodes = [];
96
-
97
- for(var i=0, l=statements.length; i<l; i++) {
98
- statement = statements[i];
99
- this[statement.type](statement);
100
- }
101
- this.isSimple = l === 1;
102
-
103
- this.depths.list = this.depths.list.sort(function(a, b) {
104
- return a - b;
105
- });
106
-
107
- return this;
108
- },
109
-
110
- compileProgram: function(program) {
111
- var result = new this.compiler().compile(program, this.options);
112
- var guid = this.guid++, depth;
113
-
114
- this.usePartial = this.usePartial || result.usePartial;
115
-
116
- this.children[guid] = result;
117
-
118
- for(var i=0, l=result.depths.list.length; i<l; i++) {
119
- depth = result.depths.list[i];
120
-
121
- if(depth < 2) { continue; }
122
- else { this.addDepth(depth - 1); }
123
- }
124
-
125
- return guid;
126
- },
127
-
128
- block: function(block) {
129
- var mustache = block.mustache,
130
- program = block.program,
131
- inverse = block.inverse;
132
-
133
- if (program) {
134
- program = this.compileProgram(program);
135
- }
136
-
137
- if (inverse) {
138
- inverse = this.compileProgram(inverse);
139
- }
140
-
141
- var type = this.classifyMustache(mustache);
142
-
143
- if (type === "helper") {
144
- this.helperMustache(mustache, program, inverse);
145
- } else if (type === "simple") {
146
- this.simpleMustache(mustache);
147
-
148
- // now that the simple mustache is resolved, we need to
149
- // evaluate it by executing `blockHelperMissing`
150
- this.opcode('pushProgram', program);
151
- this.opcode('pushProgram', inverse);
152
- this.opcode('emptyHash');
153
- this.opcode('blockValue');
154
- } else {
155
- this.ambiguousMustache(mustache, program, inverse);
156
-
157
- // now that the simple mustache is resolved, we need to
158
- // evaluate it by executing `blockHelperMissing`
159
- this.opcode('pushProgram', program);
160
- this.opcode('pushProgram', inverse);
161
- this.opcode('emptyHash');
162
- this.opcode('ambiguousBlockValue');
163
- }
164
-
165
- this.opcode('append');
166
- },
167
-
168
- hash: function(hash) {
169
- var pairs = hash.pairs, pair, val;
170
-
171
- this.opcode('pushHash');
172
-
173
- for(var i=0, l=pairs.length; i<l; i++) {
174
- pair = pairs[i];
175
- val = pair[1];
176
-
177
- if (this.options.stringParams) {
178
- this.opcode('pushStringParam', val.stringModeValue, val.type);
179
- } else {
180
- this.accept(val);
181
- }
182
-
183
- this.opcode('assignToHash', pair[0]);
184
- }
185
- this.opcode('popHash');
186
- },
187
-
188
- partial: function(partial) {
189
- var partialName = partial.partialName;
190
- this.usePartial = true;
191
-
192
- if(partial.context) {
193
- this.ID(partial.context);
194
- } else {
195
- this.opcode('push', 'depth0');
196
- }
197
-
198
- this.opcode('invokePartial', partialName.name);
199
- this.opcode('append');
200
- },
201
-
202
- content: function(content) {
203
- this.opcode('appendContent', content.string);
204
- },
205
-
206
- mustache: function(mustache) {
207
- var options = this.options;
208
- var type = this.classifyMustache(mustache);
209
-
210
- if (type === "simple") {
211
- this.simpleMustache(mustache);
212
- } else if (type === "helper") {
213
- this.helperMustache(mustache);
214
- } else {
215
- this.ambiguousMustache(mustache);
216
- }
217
-
218
- if(mustache.escaped && !options.noEscape) {
219
- this.opcode('appendEscaped');
220
- } else {
221
- this.opcode('append');
222
- }
223
- },
224
-
225
- ambiguousMustache: function(mustache, program, inverse) {
226
- var id = mustache.id,
227
- name = id.parts[0],
228
- isBlock = program != null || inverse != null;
229
-
230
- this.opcode('getContext', id.depth);
231
-
232
- this.opcode('pushProgram', program);
233
- this.opcode('pushProgram', inverse);
234
-
235
- this.opcode('invokeAmbiguous', name, isBlock);
236
- },
237
-
238
- simpleMustache: function(mustache) {
239
- var id = mustache.id;
240
-
241
- if (id.type === 'DATA') {
242
- this.DATA(id);
243
- } else if (id.parts.length) {
244
- this.ID(id);
245
- } else {
246
- // Simplified ID for `this`
247
- this.addDepth(id.depth);
248
- this.opcode('getContext', id.depth);
249
- this.opcode('pushContext');
250
- }
251
-
252
- this.opcode('resolvePossibleLambda');
253
- },
254
-
255
- helperMustache: function(mustache, program, inverse) {
256
- var params = this.setupFullMustacheParams(mustache, program, inverse),
257
- name = mustache.id.parts[0];
258
-
259
- if (this.options.knownHelpers[name]) {
260
- this.opcode('invokeKnownHelper', params.length, name);
261
- } else if (this.knownHelpersOnly) {
262
- throw new Error("You specified knownHelpersOnly, but used the unknown helper " + name);
263
- } else {
264
- this.opcode('invokeHelper', params.length, name);
265
- }
266
- },
267
-
268
- ID: function(id) {
269
- this.addDepth(id.depth);
270
- this.opcode('getContext', id.depth);
271
-
272
- var name = id.parts[0];
273
- if (!name) {
274
- this.opcode('pushContext');
275
- } else {
276
- this.opcode('lookupOnContext', id.parts[0]);
277
- }
278
-
279
- for(var i=1, l=id.parts.length; i<l; i++) {
280
- this.opcode('lookup', id.parts[i]);
281
- }
282
- },
283
-
284
- DATA: function(data) {
285
- this.options.data = true;
286
- this.opcode('lookupData', data.id);
287
- },
288
-
289
- STRING: function(string) {
290
- this.opcode('pushString', string.string);
291
- },
292
-
293
- INTEGER: function(integer) {
294
- this.opcode('pushLiteral', integer.integer);
295
- },
296
-
297
- BOOLEAN: function(bool) {
298
- this.opcode('pushLiteral', bool.bool);
299
- },
300
-
301
- comment: function() {},
302
-
303
- // HELPERS
304
- opcode: function(name) {
305
- this.opcodes.push({ opcode: name, args: [].slice.call(arguments, 1) });
306
- },
307
-
308
- declare: function(name, value) {
309
- this.opcodes.push({ opcode: 'DECLARE', name: name, value: value });
310
- },
311
-
312
- addDepth: function(depth) {
313
- if(isNaN(depth)) { throw new Error("EWOT"); }
314
- if(depth === 0) { return; }
315
-
316
- if(!this.depths[depth]) {
317
- this.depths[depth] = true;
318
- this.depths.list.push(depth);
319
- }
320
- },
321
-
322
- classifyMustache: function(mustache) {
323
- var isHelper = mustache.isHelper;
324
- var isEligible = mustache.eligibleHelper;
325
- var options = this.options;
326
-
327
- // if ambiguous, we can possibly resolve the ambiguity now
328
- if (isEligible && !isHelper) {
329
- var name = mustache.id.parts[0];
330
-
331
- if (options.knownHelpers[name]) {
332
- isHelper = true;
333
- } else if (options.knownHelpersOnly) {
334
- isEligible = false;
335
- }
336
- }
337
-
338
- if (isHelper) { return "helper"; }
339
- else if (isEligible) { return "ambiguous"; }
340
- else { return "simple"; }
341
- },
342
-
343
- pushParams: function(params) {
344
- var i = params.length, param;
345
-
346
- while(i--) {
347
- param = params[i];
348
-
349
- if(this.options.stringParams) {
350
- if(param.depth) {
351
- this.addDepth(param.depth);
352
- }
353
-
354
- this.opcode('getContext', param.depth || 0);
355
- this.opcode('pushStringParam', param.stringModeValue, param.type);
356
- } else {
357
- this[param.type](param);
358
- }
359
- }
360
- },
361
-
362
- setupMustacheParams: function(mustache) {
363
- var params = mustache.params;
364
- this.pushParams(params);
365
-
366
- if(mustache.hash) {
367
- this.hash(mustache.hash);
368
- } else {
369
- this.opcode('emptyHash');
370
- }
371
-
372
- return params;
373
- },
374
-
375
- // this will replace setupMustacheParams when we're done
376
- setupFullMustacheParams: function(mustache, program, inverse) {
377
- var params = mustache.params;
378
- this.pushParams(params);
379
-
380
- this.opcode('pushProgram', program);
381
- this.opcode('pushProgram', inverse);
382
-
383
- if(mustache.hash) {
384
- this.hash(mustache.hash);
385
- } else {
386
- this.opcode('emptyHash');
387
- }
388
-
389
- return params;
390
- }
391
- };
392
-
393
- var Literal = function(value) {
394
- this.value = value;
395
- };
396
-
397
- JavaScriptCompiler.prototype = {
398
- // PUBLIC API: You can override these methods in a subclass to provide
399
- // alternative compiled forms for name lookup and buffering semantics
400
- nameLookup: function(parent, name /* , type*/) {
401
- if (/^[0-9]+$/.test(name)) {
402
- return parent + "[" + name + "]";
403
- } else if (JavaScriptCompiler.isValidJavaScriptVariableName(name)) {
404
- return parent + "." + name;
405
- }
406
- else {
407
- return parent + "['" + name + "']";
408
- }
409
- },
410
-
411
- appendToBuffer: function(string) {
412
- if (this.environment.isSimple) {
413
- return "return " + string + ";";
414
- } else {
415
- return {
416
- appendToBuffer: true,
417
- content: string,
418
- toString: function() { return "buffer += " + string + ";"; }
419
- };
420
- }
421
- },
422
-
423
- initializeBuffer: function() {
424
- return this.quotedString("");
425
- },
426
-
427
- namespace: "Handlebars",
428
- // END PUBLIC API
429
-
430
- compile: function(environment, options, context, asObject) {
431
- this.environment = environment;
432
- this.options = options || {};
433
-
434
- Handlebars.log(Handlebars.logger.DEBUG, this.environment.disassemble() + "\n\n");
435
-
436
- this.name = this.environment.name;
437
- this.isChild = !!context;
438
- this.context = context || {
439
- programs: [],
440
- environments: [],
441
- aliases: { }
442
- };
443
-
444
- this.preamble();
445
-
446
- this.stackSlot = 0;
447
- this.stackVars = [];
448
- this.registers = { list: [] };
449
- this.compileStack = [];
450
- this.inlineStack = [];
451
-
452
- this.compileChildren(environment, options);
453
-
454
- var opcodes = environment.opcodes, opcode;
455
-
456
- this.i = 0;
457
-
458
- for(l=opcodes.length; this.i<l; this.i++) {
459
- opcode = opcodes[this.i];
460
-
461
- if(opcode.opcode === 'DECLARE') {
462
- this[opcode.name] = opcode.value;
463
- } else {
464
- this[opcode.opcode].apply(this, opcode.args);
465
- }
466
- }
467
-
468
- return this.createFunctionContext(asObject);
469
- },
470
-
471
- nextOpcode: function() {
472
- var opcodes = this.environment.opcodes;
473
- return opcodes[this.i + 1];
474
- },
475
-
476
- eat: function() {
477
- this.i = this.i + 1;
478
- },
479
-
480
- preamble: function() {
481
- var out = [];
482
-
483
- if (!this.isChild) {
484
- var namespace = this.namespace;
485
- var copies = "helpers = helpers || " + namespace + ".helpers;";
486
- if (this.environment.usePartial) { copies = copies + " partials = partials || " + namespace + ".partials;"; }
487
- if (this.options.data) { copies = copies + " data = data || {};"; }
488
- out.push(copies);
489
- } else {
490
- out.push('');
491
- }
492
-
493
- if (!this.environment.isSimple) {
494
- out.push(", buffer = " + this.initializeBuffer());
495
- } else {
496
- out.push("");
497
- }
498
-
499
- // track the last context pushed into place to allow skipping the
500
- // getContext opcode when it would be a noop
501
- this.lastContext = 0;
502
- this.source = out;
503
- },
504
-
505
- createFunctionContext: function(asObject) {
506
- var locals = this.stackVars.concat(this.registers.list);
507
-
508
- if(locals.length > 0) {
509
- this.source[1] = this.source[1] + ", " + locals.join(", ");
510
- }
511
-
512
- // Generate minimizer alias mappings
513
- if (!this.isChild) {
514
- for (var alias in this.context.aliases) {
515
- this.source[1] = this.source[1] + ', ' + alias + '=' + this.context.aliases[alias];
516
- }
517
- }
518
-
519
- if (this.source[1]) {
520
- this.source[1] = "var " + this.source[1].substring(2) + ";";
521
- }
522
-
523
- // Merge children
524
- if (!this.isChild) {
525
- this.source[1] += '\n' + this.context.programs.join('\n') + '\n';
526
- }
527
-
528
- if (!this.environment.isSimple) {
529
- this.source.push("return buffer;");
530
- }
531
-
532
- var params = this.isChild ? ["depth0", "data"] : ["Handlebars", "depth0", "helpers", "partials", "data"];
533
-
534
- for(var i=0, l=this.environment.depths.list.length; i<l; i++) {
535
- params.push("depth" + this.environment.depths.list[i]);
536
- }
537
-
538
- // Perform a second pass over the output to merge content when possible
539
- var source = this.mergeSource();
540
-
541
- if (!this.isChild) {
542
- var revision = Handlebars.COMPILER_REVISION,
543
- versions = Handlebars.REVISION_CHANGES[revision];
544
- source = "this.compilerInfo = ["+revision+",'"+versions+"'];\n"+source;
545
- }
546
-
547
- if (asObject) {
548
- params.push(source);
549
-
550
- return Function.apply(this, params);
551
- } else {
552
- var functionSource = 'function ' + (this.name || '') + '(' + params.join(',') + ') {\n ' + source + '}';
553
- Handlebars.log(Handlebars.logger.DEBUG, functionSource + "\n\n");
554
- return functionSource;
555
- }
556
- },
557
- mergeSource: function() {
558
- // WARN: We are not handling the case where buffer is still populated as the source should
559
- // not have buffer append operations as their final action.
560
- var source = '',
561
- buffer;
562
- for (var i = 0, len = this.source.length; i < len; i++) {
563
- var line = this.source[i];
564
- if (line.appendToBuffer) {
565
- if (buffer) {
566
- buffer = buffer + '\n + ' + line.content;
567
- } else {
568
- buffer = line.content;
569
- }
570
- } else {
571
- if (buffer) {
572
- source += 'buffer += ' + buffer + ';\n ';
573
- buffer = undefined;
574
- }
575
- source += line + '\n ';
576
- }
577
- }
578
- return source;
579
- },
580
-
581
- // [blockValue]
582
- //
583
- // On stack, before: hash, inverse, program, value
584
- // On stack, after: return value of blockHelperMissing
585
- //
586
- // The purpose of this opcode is to take a block of the form
587
- // `{{#foo}}...{{/foo}}`, resolve the value of `foo`, and
588
- // replace it on the stack with the result of properly
589
- // invoking blockHelperMissing.
590
- blockValue: function() {
591
- this.context.aliases.blockHelperMissing = 'helpers.blockHelperMissing';
592
-
593
- var params = ["depth0"];
594
- this.setupParams(0, params);
595
-
596
- this.replaceStack(function(current) {
597
- params.splice(1, 0, current);
598
- return "blockHelperMissing.call(" + params.join(", ") + ")";
599
- });
600
- },
601
-
602
- // [ambiguousBlockValue]
603
- //
604
- // On stack, before: hash, inverse, program, value
605
- // Compiler value, before: lastHelper=value of last found helper, if any
606
- // On stack, after, if no lastHelper: same as [blockValue]
607
- // On stack, after, if lastHelper: value
608
- ambiguousBlockValue: function() {
609
- this.context.aliases.blockHelperMissing = 'helpers.blockHelperMissing';
610
-
611
- var params = ["depth0"];
612
- this.setupParams(0, params);
613
-
614
- var current = this.topStack();
615
- params.splice(1, 0, current);
616
-
617
- // Use the options value generated from the invocation
618
- params[params.length-1] = 'options';
619
-
620
- this.source.push("if (!" + this.lastHelper + ") { " + current + " = blockHelperMissing.call(" + params.join(", ") + "); }");
621
- },
622
-
623
- // [appendContent]
624
- //
625
- // On stack, before: ...
626
- // On stack, after: ...
627
- //
628
- // Appends the string value of `content` to the current buffer
629
- appendContent: function(content) {
630
- this.source.push(this.appendToBuffer(this.quotedString(content)));
631
- },
632
-
633
- // [append]
634
- //
635
- // On stack, before: value, ...
636
- // On stack, after: ...
637
- //
638
- // Coerces `value` to a String and appends it to the current buffer.
639
- //
640
- // If `value` is truthy, or 0, it is coerced into a string and appended
641
- // Otherwise, the empty string is appended
642
- append: function() {
643
- // Force anything that is inlined onto the stack so we don't have duplication
644
- // when we examine local
645
- this.flushInline();
646
- var local = this.popStack();
647
- this.source.push("if(" + local + " || " + local + " === 0) { " + this.appendToBuffer(local) + " }");
648
- if (this.environment.isSimple) {
649
- this.source.push("else { " + this.appendToBuffer("''") + " }");
650
- }
651
- },
652
-
653
- // [appendEscaped]
654
- //
655
- // On stack, before: value, ...
656
- // On stack, after: ...
657
- //
658
- // Escape `value` and append it to the buffer
659
- appendEscaped: function() {
660
- this.context.aliases.escapeExpression = 'this.escapeExpression';
661
-
662
- this.source.push(this.appendToBuffer("escapeExpression(" + this.popStack() + ")"));
663
- },
664
-
665
- // [getContext]
666
- //
667
- // On stack, before: ...
668
- // On stack, after: ...
669
- // Compiler value, after: lastContext=depth
670
- //
671
- // Set the value of the `lastContext` compiler value to the depth
672
- getContext: function(depth) {
673
- if(this.lastContext !== depth) {
674
- this.lastContext = depth;
675
- }
676
- },
677
-
678
- // [lookupOnContext]
679
- //
680
- // On stack, before: ...
681
- // On stack, after: currentContext[name], ...
682
- //
683
- // Looks up the value of `name` on the current context and pushes
684
- // it onto the stack.
685
- lookupOnContext: function(name) {
686
- this.push(this.nameLookup('depth' + this.lastContext, name, 'context'));
687
- },
688
-
689
- // [pushContext]
690
- //
691
- // On stack, before: ...
692
- // On stack, after: currentContext, ...
693
- //
694
- // Pushes the value of the current context onto the stack.
695
- pushContext: function() {
696
- this.pushStackLiteral('depth' + this.lastContext);
697
- },
698
-
699
- // [resolvePossibleLambda]
700
- //
701
- // On stack, before: value, ...
702
- // On stack, after: resolved value, ...
703
- //
704
- // If the `value` is a lambda, replace it on the stack by
705
- // the return value of the lambda
706
- resolvePossibleLambda: function() {
707
- this.context.aliases.functionType = '"function"';
708
-
709
- this.replaceStack(function(current) {
710
- return "typeof " + current + " === functionType ? " + current + ".apply(depth0) : " + current;
711
- });
712
- },
713
-
714
- // [lookup]
715
- //
716
- // On stack, before: value, ...
717
- // On stack, after: value[name], ...
718
- //
719
- // Replace the value on the stack with the result of looking
720
- // up `name` on `value`
721
- lookup: function(name) {
722
- this.replaceStack(function(current) {
723
- return current + " == null || " + current + " === false ? " + current + " : " + this.nameLookup(current, name, 'context');
724
- });
725
- },
726
-
727
- // [lookupData]
728
- //
729
- // On stack, before: ...
730
- // On stack, after: data[id], ...
731
- //
732
- // Push the result of looking up `id` on the current data
733
- lookupData: function(id) {
734
- this.push(this.nameLookup('data', id, 'data'));
735
- },
736
-
737
- // [pushStringParam]
738
- //
739
- // On stack, before: ...
740
- // On stack, after: string, currentContext, ...
741
- //
742
- // This opcode is designed for use in string mode, which
743
- // provides the string value of a parameter along with its
744
- // depth rather than resolving it immediately.
745
- pushStringParam: function(string, type) {
746
- this.pushStackLiteral('depth' + this.lastContext);
747
-
748
- this.pushString(type);
749
-
750
- if (typeof string === 'string') {
751
- this.pushString(string);
752
- } else {
753
- this.pushStackLiteral(string);
754
- }
755
- },
756
-
757
- emptyHash: function() {
758
- this.pushStackLiteral('{}');
759
-
760
- if (this.options.stringParams) {
761
- this.register('hashTypes', '{}');
762
- }
763
- },
764
- pushHash: function() {
765
- this.hash = {values: [], types: []};
766
- },
767
- popHash: function() {
768
- var hash = this.hash;
769
- this.hash = undefined;
770
-
771
- if (this.options.stringParams) {
772
- this.register('hashTypes', '{' + hash.types.join(',') + '}');
773
- }
774
- this.push('{\n ' + hash.values.join(',\n ') + '\n }');
775
- },
776
-
777
- // [pushString]
778
- //
779
- // On stack, before: ...
780
- // On stack, after: quotedString(string), ...
781
- //
782
- // Push a quoted version of `string` onto the stack
783
- pushString: function(string) {
784
- this.pushStackLiteral(this.quotedString(string));
785
- },
786
-
787
- // [push]
788
- //
789
- // On stack, before: ...
790
- // On stack, after: expr, ...
791
- //
792
- // Push an expression onto the stack
793
- push: function(expr) {
794
- this.inlineStack.push(expr);
795
- return expr;
796
- },
797
-
798
- // [pushLiteral]
799
- //
800
- // On stack, before: ...
801
- // On stack, after: value, ...
802
- //
803
- // Pushes a value onto the stack. This operation prevents
804
- // the compiler from creating a temporary variable to hold
805
- // it.
806
- pushLiteral: function(value) {
807
- this.pushStackLiteral(value);
808
- },
809
-
810
- // [pushProgram]
811
- //
812
- // On stack, before: ...
813
- // On stack, after: program(guid), ...
814
- //
815
- // Push a program expression onto the stack. This takes
816
- // a compile-time guid and converts it into a runtime-accessible
817
- // expression.
818
- pushProgram: function(guid) {
819
- if (guid != null) {
820
- this.pushStackLiteral(this.programExpression(guid));
821
- } else {
822
- this.pushStackLiteral(null);
823
- }
824
- },
825
-
826
- // [invokeHelper]
827
- //
828
- // On stack, before: hash, inverse, program, params..., ...
829
- // On stack, after: result of helper invocation
830
- //
831
- // Pops off the helper's parameters, invokes the helper,
832
- // and pushes the helper's return value onto the stack.
833
- //
834
- // If the helper is not found, `helperMissing` is called.
835
- invokeHelper: function(paramSize, name) {
836
- this.context.aliases.helperMissing = 'helpers.helperMissing';
837
-
838
- var helper = this.lastHelper = this.setupHelper(paramSize, name, true);
839
-
840
- this.push(helper.name);
841
- this.replaceStack(function(name) {
842
- return name + ' ? ' + name + '.call(' +
843
- helper.callParams + ") " + ": helperMissing.call(" +
844
- helper.helperMissingParams + ")";
845
- });
846
- },
847
-
848
- // [invokeKnownHelper]
849
- //
850
- // On stack, before: hash, inverse, program, params..., ...
851
- // On stack, after: result of helper invocation
852
- //
853
- // This operation is used when the helper is known to exist,
854
- // so a `helperMissing` fallback is not required.
855
- invokeKnownHelper: function(paramSize, name) {
856
- var helper = this.setupHelper(paramSize, name);
857
- this.push(helper.name + ".call(" + helper.callParams + ")");
858
- },
859
-
860
- // [invokeAmbiguous]
861
- //
862
- // On stack, before: hash, inverse, program, params..., ...
863
- // On stack, after: result of disambiguation
864
- //
865
- // This operation is used when an expression like `{{foo}}`
866
- // is provided, but we don't know at compile-time whether it
867
- // is a helper or a path.
868
- //
869
- // This operation emits more code than the other options,
870
- // and can be avoided by passing the `knownHelpers` and
871
- // `knownHelpersOnly` flags at compile-time.
872
- invokeAmbiguous: function(name, helperCall) {
873
- this.context.aliases.functionType = '"function"';
874
-
875
- this.pushStackLiteral('{}'); // Hash value
876
- var helper = this.setupHelper(0, name, helperCall);
877
-
878
- var helperName = this.lastHelper = this.nameLookup('helpers', name, 'helper');
879
-
880
- var nonHelper = this.nameLookup('depth' + this.lastContext, name, 'context');
881
- var nextStack = this.nextStack();
882
-
883
- this.source.push('if (' + nextStack + ' = ' + helperName + ') { ' + nextStack + ' = ' + nextStack + '.call(' + helper.callParams + '); }');
884
- this.source.push('else { ' + nextStack + ' = ' + nonHelper + '; ' + nextStack + ' = typeof ' + nextStack + ' === functionType ? ' + nextStack + '.apply(depth0) : ' + nextStack + '; }');
885
- },
886
-
887
- // [invokePartial]
888
- //
889
- // On stack, before: context, ...
890
- // On stack after: result of partial invocation
891
- //
892
- // This operation pops off a context, invokes a partial with that context,
893
- // and pushes the result of the invocation back.
894
- invokePartial: function(name) {
895
- var params = [this.nameLookup('partials', name, 'partial'), "'" + name + "'", this.popStack(), "helpers", "partials"];
896
-
897
- if (this.options.data) {
898
- params.push("data");
899
- }
900
-
901
- this.context.aliases.self = "this";
902
- this.push("self.invokePartial(" + params.join(", ") + ")");
903
- },
904
-
905
- // [assignToHash]
906
- //
907
- // On stack, before: value, hash, ...
908
- // On stack, after: hash, ...
909
- //
910
- // Pops a value and hash off the stack, assigns `hash[key] = value`
911
- // and pushes the hash back onto the stack.
912
- assignToHash: function(key) {
913
- var value = this.popStack(),
914
- type;
915
-
916
- if (this.options.stringParams) {
917
- type = this.popStack();
918
- this.popStack();
919
- }
920
-
921
- var hash = this.hash;
922
- if (type) {
923
- hash.types.push("'" + key + "': " + type);
924
- }
925
- hash.values.push("'" + key + "': (" + value + ")");
926
- },
927
-
928
- // HELPERS
929
-
930
- compiler: JavaScriptCompiler,
931
-
932
- compileChildren: function(environment, options) {
933
- var children = environment.children, child, compiler;
934
-
935
- for(var i=0, l=children.length; i<l; i++) {
936
- child = children[i];
937
- compiler = new this.compiler();
938
-
939
- var index = this.matchExistingProgram(child);
940
-
941
- if (index == null) {
942
- this.context.programs.push(''); // Placeholder to prevent name conflicts for nested children
943
- index = this.context.programs.length;
944
- child.index = index;
945
- child.name = 'program' + index;
946
- this.context.programs[index] = compiler.compile(child, options, this.context);
947
- this.context.environments[index] = child;
948
- } else {
949
- child.index = index;
950
- child.name = 'program' + index;
951
- }
952
- }
953
- },
954
- matchExistingProgram: function(child) {
955
- for (var i = 0, len = this.context.environments.length; i < len; i++) {
956
- var environment = this.context.environments[i];
957
- if (environment && environment.equals(child)) {
958
- return i;
959
- }
960
- }
961
- },
962
-
963
- programExpression: function(guid) {
964
- this.context.aliases.self = "this";
965
-
966
- if(guid == null) {
967
- return "self.noop";
968
- }
969
-
970
- var child = this.environment.children[guid],
971
- depths = child.depths.list, depth;
972
-
973
- var programParams = [child.index, child.name, "data"];
974
-
975
- for(var i=0, l = depths.length; i<l; i++) {
976
- depth = depths[i];
977
-
978
- if(depth === 1) { programParams.push("depth0"); }
979
- else { programParams.push("depth" + (depth - 1)); }
980
- }
981
-
982
- if(depths.length === 0) {
983
- return "self.program(" + programParams.join(", ") + ")";
984
- } else {
985
- programParams.shift();
986
- return "self.programWithDepth(" + programParams.join(", ") + ")";
987
- }
988
- },
989
-
990
- register: function(name, val) {
991
- this.useRegister(name);
992
- this.source.push(name + " = " + val + ";");
993
- },
994
-
995
- useRegister: function(name) {
996
- if(!this.registers[name]) {
997
- this.registers[name] = true;
998
- this.registers.list.push(name);
999
- }
1000
- },
1001
-
1002
- pushStackLiteral: function(item) {
1003
- return this.push(new Literal(item));
1004
- },
1005
-
1006
- pushStack: function(item) {
1007
- this.flushInline();
1008
-
1009
- var stack = this.incrStack();
1010
- if (item) {
1011
- this.source.push(stack + " = " + item + ";");
1012
- }
1013
- this.compileStack.push(stack);
1014
- return stack;
1015
- },
1016
-
1017
- replaceStack: function(callback) {
1018
- var prefix = '',
1019
- inline = this.isInline(),
1020
- stack;
1021
-
1022
- // If we are currently inline then we want to merge the inline statement into the
1023
- // replacement statement via ','
1024
- if (inline) {
1025
- var top = this.popStack(true);
1026
-
1027
- if (top instanceof Literal) {
1028
- // Literals do not need to be inlined
1029
- stack = top.value;
1030
- } else {
1031
- // Get or create the current stack name for use by the inline
1032
- var name = this.stackSlot ? this.topStackName() : this.incrStack();
1033
-
1034
- prefix = '(' + this.push(name) + ' = ' + top + '),';
1035
- stack = this.topStack();
1036
- }
1037
- } else {
1038
- stack = this.topStack();
1039
- }
1040
-
1041
- var item = callback.call(this, stack);
1042
-
1043
- if (inline) {
1044
- if (this.inlineStack.length || this.compileStack.length) {
1045
- this.popStack();
1046
- }
1047
- this.push('(' + prefix + item + ')');
1048
- } else {
1049
- // Prevent modification of the context depth variable. Through replaceStack
1050
- if (!/^stack/.test(stack)) {
1051
- stack = this.nextStack();
1052
- }
1053
-
1054
- this.source.push(stack + " = (" + prefix + item + ");");
1055
- }
1056
- return stack;
1057
- },
1058
-
1059
- nextStack: function() {
1060
- return this.pushStack();
1061
- },
1062
-
1063
- incrStack: function() {
1064
- this.stackSlot++;
1065
- if(this.stackSlot > this.stackVars.length) { this.stackVars.push("stack" + this.stackSlot); }
1066
- return this.topStackName();
1067
- },
1068
- topStackName: function() {
1069
- return "stack" + this.stackSlot;
1070
- },
1071
- flushInline: function() {
1072
- var inlineStack = this.inlineStack;
1073
- if (inlineStack.length) {
1074
- this.inlineStack = [];
1075
- for (var i = 0, len = inlineStack.length; i < len; i++) {
1076
- var entry = inlineStack[i];
1077
- if (entry instanceof Literal) {
1078
- this.compileStack.push(entry);
1079
- } else {
1080
- this.pushStack(entry);
1081
- }
1082
- }
1083
- }
1084
- },
1085
- isInline: function() {
1086
- return this.inlineStack.length;
1087
- },
1088
-
1089
- popStack: function(wrapped) {
1090
- var inline = this.isInline(),
1091
- item = (inline ? this.inlineStack : this.compileStack).pop();
1092
-
1093
- if (!wrapped && (item instanceof Literal)) {
1094
- return item.value;
1095
- } else {
1096
- if (!inline) {
1097
- this.stackSlot--;
1098
- }
1099
- return item;
1100
- }
1101
- },
1102
-
1103
- topStack: function(wrapped) {
1104
- var stack = (this.isInline() ? this.inlineStack : this.compileStack),
1105
- item = stack[stack.length - 1];
1106
-
1107
- if (!wrapped && (item instanceof Literal)) {
1108
- return item.value;
1109
- } else {
1110
- return item;
1111
- }
1112
- },
1113
-
1114
- quotedString: function(str) {
1115
- return '"' + str
1116
- .replace(/\\/g, '\\\\')
1117
- .replace(/"/g, '\\"')
1118
- .replace(/\n/g, '\\n')
1119
- .replace(/\r/g, '\\r') + '"';
1120
- },
1121
-
1122
- setupHelper: function(paramSize, name, missingParams) {
1123
- var params = [];
1124
- this.setupParams(paramSize, params, missingParams);
1125
- var foundHelper = this.nameLookup('helpers', name, 'helper');
1126
-
1127
- return {
1128
- params: params,
1129
- name: foundHelper,
1130
- callParams: ["depth0"].concat(params).join(", "),
1131
- helperMissingParams: missingParams && ["depth0", this.quotedString(name)].concat(params).join(", ")
1132
- };
1133
- },
1134
-
1135
- // the params and contexts arguments are passed in arrays
1136
- // to fill in
1137
- setupParams: function(paramSize, params, useRegister) {
1138
- var options = [], contexts = [], types = [], param, inverse, program;
1139
-
1140
- options.push("hash:" + this.popStack());
1141
-
1142
- inverse = this.popStack();
1143
- program = this.popStack();
1144
-
1145
- // Avoid setting fn and inverse if neither are set. This allows
1146
- // helpers to do a check for `if (options.fn)`
1147
- if (program || inverse) {
1148
- if (!program) {
1149
- this.context.aliases.self = "this";
1150
- program = "self.noop";
1151
- }
1152
-
1153
- if (!inverse) {
1154
- this.context.aliases.self = "this";
1155
- inverse = "self.noop";
1156
- }
1157
-
1158
- options.push("inverse:" + inverse);
1159
- options.push("fn:" + program);
1160
- }
1161
-
1162
- for(var i=0; i<paramSize; i++) {
1163
- param = this.popStack();
1164
- params.push(param);
1165
-
1166
- if(this.options.stringParams) {
1167
- types.push(this.popStack());
1168
- contexts.push(this.popStack());
1169
- }
1170
- }
1171
-
1172
- if (this.options.stringParams) {
1173
- options.push("contexts:[" + contexts.join(",") + "]");
1174
- options.push("types:[" + types.join(",") + "]");
1175
- options.push("hashTypes:hashTypes");
1176
- }
1177
-
1178
- if(this.options.data) {
1179
- options.push("data:data");
1180
- }
1181
-
1182
- options = "{" + options.join(",") + "}";
1183
- if (useRegister) {
1184
- this.register('options', options);
1185
- params.push('options');
1186
- } else {
1187
- params.push(options);
1188
- }
1189
- return params.join(", ");
1190
- }
1191
- };
1192
-
1193
- var reservedWords = (
1194
- "break else new var" +
1195
- " case finally return void" +
1196
- " catch for switch while" +
1197
- " continue function this with" +
1198
- " default if throw" +
1199
- " delete in try" +
1200
- " do instanceof typeof" +
1201
- " abstract enum int short" +
1202
- " boolean export interface static" +
1203
- " byte extends long super" +
1204
- " char final native synchronized" +
1205
- " class float package throws" +
1206
- " const goto private transient" +
1207
- " debugger implements protected volatile" +
1208
- " double import public let yield"
1209
- ).split(" ");
1210
-
1211
- var compilerWords = JavaScriptCompiler.RESERVED_WORDS = {};
1212
-
1213
- for(var i=0, l=reservedWords.length; i<l; i++) {
1214
- compilerWords[reservedWords[i]] = true;
1215
- }
1216
-
1217
- JavaScriptCompiler.isValidJavaScriptVariableName = function(name) {
1218
- if(!JavaScriptCompiler.RESERVED_WORDS[name] && /^[a-zA-Z_$][0-9a-zA-Z_$]+$/.test(name)) {
1219
- return true;
1220
- }
1221
- return false;
1222
- };
1223
-
1224
- })(Handlebars.Compiler, Handlebars.JavaScriptCompiler);
1225
-
1226
- Handlebars.precompile = function(input, options) {
1227
- if (!input || (typeof input !== 'string' && input.constructor !== Handlebars.AST.ProgramNode)) {
1228
- throw new Handlebars.Exception("You must pass a string or Handlebars AST to Handlebars.compile. You passed " + input);
1229
- }
1230
-
1231
- options = options || {};
1232
- if (!('data' in options)) {
1233
- options.data = true;
1234
- }
1235
- var ast = Handlebars.parse(input);
1236
- var environment = new Handlebars.Compiler().compile(ast, options);
1237
- return new Handlebars.JavaScriptCompiler().compile(environment, options);
1238
- };
1239
-
1240
- Handlebars.compile = function(input, options) {
1241
- if (!input || (typeof input !== 'string' && input.constructor !== Handlebars.AST.ProgramNode)) {
1242
- throw new Handlebars.Exception("You must pass a string or Handlebars AST to Handlebars.compile. You passed " + input);
1243
- }
1244
-
1245
- options = options || {};
1246
- if (!('data' in options)) {
1247
- options.data = true;
1248
- }
1249
- var compiled;
1250
- function compile() {
1251
- var ast = Handlebars.parse(input);
1252
- var environment = new Handlebars.Compiler().compile(ast, options);
1253
- var templateSpec = new Handlebars.JavaScriptCompiler().compile(environment, options, undefined, true);
1254
- return Handlebars.template(templateSpec);
1255
- }
1256
-
1257
- // Template is only compiled on first use and cached after that point.
1258
- return function(context, options) {
1259
- if (!compiled) {
1260
- compiled = compile();
1261
- }
1262
- return compiled.call(this, context, options);
1263
- };
1264
- };
1265
-
1266
- // END(BROWSER)
1267
-