Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80684 views
1
/*!
2
3
handlebars v3.0.0
4
5
Copyright (C) 2011-2014 by Yehuda Katz
6
7
Permission is hereby granted, free of charge, to any person obtaining a copy
8
of this software and associated documentation files (the "Software"), to deal
9
in the Software without restriction, including without limitation the rights
10
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
copies of the Software, and to permit persons to whom the Software is
12
furnished to do so, subject to the following conditions:
13
14
The above copyright notice and this permission notice shall be included in
15
all copies or substantial portions of the Software.
16
17
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
THE SOFTWARE.
24
25
@license
26
*/
27
define(
28
'handlebars/utils',["exports"],
29
function(__exports__) {
30
31
/*jshint -W004 */
32
var escape = {
33
"&": "&",
34
"<": "&lt;",
35
">": "&gt;",
36
'"': "&quot;",
37
"'": "&#x27;",
38
"`": "&#x60;"
39
};
40
41
var badChars = /[&<>"'`]/g;
42
var possible = /[&<>"'`]/;
43
44
function escapeChar(chr) {
45
return escape[chr];
46
}
47
48
function extend(obj /* , ...source */) {
49
for (var i = 1; i < arguments.length; i++) {
50
for (var key in arguments[i]) {
51
if (Object.prototype.hasOwnProperty.call(arguments[i], key)) {
52
obj[key] = arguments[i][key];
53
}
54
}
55
}
56
57
return obj;
58
}
59
60
__exports__.extend = extend;var toString = Object.prototype.toString;
61
__exports__.toString = toString;
62
// Sourced from lodash
63
// https://github.com/bestiejs/lodash/blob/master/LICENSE.txt
64
var isFunction = function(value) {
65
return typeof value === 'function';
66
};
67
// fallback for older versions of Chrome and Safari
68
/* istanbul ignore next */
69
if (isFunction(/x/)) {
70
isFunction = function(value) {
71
return typeof value === 'function' && toString.call(value) === '[object Function]';
72
};
73
}
74
var isFunction;
75
__exports__.isFunction = isFunction;
76
/* istanbul ignore next */
77
var isArray = Array.isArray || function(value) {
78
return (value && typeof value === 'object') ? toString.call(value) === '[object Array]' : false;
79
};
80
__exports__.isArray = isArray;
81
// Older IE versions do not directly support indexOf so we must implement our own, sadly.
82
function indexOf(array, value) {
83
for (var i = 0, len = array.length; i < len; i++) {
84
if (array[i] === value) {
85
return i;
86
}
87
}
88
return -1;
89
}
90
91
__exports__.indexOf = indexOf;
92
function escapeExpression(string) {
93
// don't escape SafeStrings, since they're already safe
94
if (string && string.toHTML) {
95
return string.toHTML();
96
} else if (string == null) {
97
return "";
98
} else if (!string) {
99
return string + '';
100
}
101
102
// Force a string conversion as this will be done by the append regardless and
103
// the regex test will do this transparently behind the scenes, causing issues if
104
// an object's to string has escaped characters in it.
105
string = "" + string;
106
107
if(!possible.test(string)) { return string; }
108
return string.replace(badChars, escapeChar);
109
}
110
111
__exports__.escapeExpression = escapeExpression;function isEmpty(value) {
112
if (!value && value !== 0) {
113
return true;
114
} else if (isArray(value) && value.length === 0) {
115
return true;
116
} else {
117
return false;
118
}
119
}
120
121
__exports__.isEmpty = isEmpty;function blockParams(params, ids) {
122
params.path = ids;
123
return params;
124
}
125
126
__exports__.blockParams = blockParams;function appendContextPath(contextPath, id) {
127
return (contextPath ? contextPath + '.' : '') + id;
128
}
129
130
__exports__.appendContextPath = appendContextPath;
131
});
132
define(
133
'handlebars/exception',["exports"],
134
function(__exports__) {
135
136
137
var errorProps = ['description', 'fileName', 'lineNumber', 'message', 'name', 'number', 'stack'];
138
139
function Exception(message, node) {
140
var loc = node && node.loc,
141
line,
142
column;
143
if (loc) {
144
line = loc.start.line;
145
column = loc.start.column;
146
147
message += ' - ' + line + ':' + column;
148
}
149
150
var tmp = Error.prototype.constructor.call(this, message);
151
152
// Unfortunately errors are not enumerable in Chrome (at least), so `for prop in tmp` doesn't work.
153
for (var idx = 0; idx < errorProps.length; idx++) {
154
this[errorProps[idx]] = tmp[errorProps[idx]];
155
}
156
157
if (loc) {
158
this.lineNumber = line;
159
this.column = column;
160
}
161
}
162
163
Exception.prototype = new Error();
164
165
__exports__["default"] = Exception;
166
});
167
define(
168
'handlebars/base',["./utils","./exception","exports"],
169
function(__dependency1__, __dependency2__, __exports__) {
170
171
var Utils = __dependency1__;
172
var Exception = __dependency2__["default"];
173
174
var VERSION = "3.0.0";
175
__exports__.VERSION = VERSION;var COMPILER_REVISION = 6;
176
__exports__.COMPILER_REVISION = COMPILER_REVISION;
177
var REVISION_CHANGES = {
178
1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it
179
2: '== 1.0.0-rc.3',
180
3: '== 1.0.0-rc.4',
181
4: '== 1.x.x',
182
5: '== 2.0.0-alpha.x',
183
6: '>= 2.0.0-beta.1'
184
};
185
__exports__.REVISION_CHANGES = REVISION_CHANGES;
186
var isArray = Utils.isArray,
187
isFunction = Utils.isFunction,
188
toString = Utils.toString,
189
objectType = '[object Object]';
190
191
function HandlebarsEnvironment(helpers, partials) {
192
this.helpers = helpers || {};
193
this.partials = partials || {};
194
195
registerDefaultHelpers(this);
196
}
197
198
__exports__.HandlebarsEnvironment = HandlebarsEnvironment;HandlebarsEnvironment.prototype = {
199
constructor: HandlebarsEnvironment,
200
201
logger: logger,
202
log: log,
203
204
registerHelper: function(name, fn) {
205
if (toString.call(name) === objectType) {
206
if (fn) { throw new Exception('Arg not supported with multiple helpers'); }
207
Utils.extend(this.helpers, name);
208
} else {
209
this.helpers[name] = fn;
210
}
211
},
212
unregisterHelper: function(name) {
213
delete this.helpers[name];
214
},
215
216
registerPartial: function(name, partial) {
217
if (toString.call(name) === objectType) {
218
Utils.extend(this.partials, name);
219
} else {
220
if (typeof partial === 'undefined') {
221
throw new Exception('Attempting to register a partial as undefined');
222
}
223
this.partials[name] = partial;
224
}
225
},
226
unregisterPartial: function(name) {
227
delete this.partials[name];
228
}
229
};
230
231
function registerDefaultHelpers(instance) {
232
instance.registerHelper('helperMissing', function(/* [args, ]options */) {
233
if(arguments.length === 1) {
234
// A missing field in a {{foo}} constuct.
235
return undefined;
236
} else {
237
// Someone is actually trying to call something, blow up.
238
throw new Exception("Missing helper: '" + arguments[arguments.length-1].name + "'");
239
}
240
});
241
242
instance.registerHelper('blockHelperMissing', function(context, options) {
243
var inverse = options.inverse,
244
fn = options.fn;
245
246
if(context === true) {
247
return fn(this);
248
} else if(context === false || context == null) {
249
return inverse(this);
250
} else if (isArray(context)) {
251
if(context.length > 0) {
252
if (options.ids) {
253
options.ids = [options.name];
254
}
255
256
return instance.helpers.each(context, options);
257
} else {
258
return inverse(this);
259
}
260
} else {
261
if (options.data && options.ids) {
262
var data = createFrame(options.data);
263
data.contextPath = Utils.appendContextPath(options.data.contextPath, options.name);
264
options = {data: data};
265
}
266
267
return fn(context, options);
268
}
269
});
270
271
instance.registerHelper('each', function(context, options) {
272
if (!options) {
273
throw new Exception('Must pass iterator to #each');
274
}
275
276
var fn = options.fn, inverse = options.inverse;
277
var i = 0, ret = "", data;
278
279
var contextPath;
280
if (options.data && options.ids) {
281
contextPath = Utils.appendContextPath(options.data.contextPath, options.ids[0]) + '.';
282
}
283
284
if (isFunction(context)) { context = context.call(this); }
285
286
if (options.data) {
287
data = createFrame(options.data);
288
}
289
290
function execIteration(key, i, last) {
291
if (data) {
292
data.key = key;
293
data.index = i;
294
data.first = i === 0;
295
data.last = !!last;
296
297
if (contextPath) {
298
data.contextPath = contextPath + key;
299
}
300
}
301
302
ret = ret + fn(context[key], {
303
data: data,
304
blockParams: Utils.blockParams([context[key], key], [contextPath + key, null])
305
});
306
}
307
308
if(context && typeof context === 'object') {
309
if (isArray(context)) {
310
for(var j = context.length; i<j; i++) {
311
execIteration(i, i, i === context.length-1);
312
}
313
} else {
314
var priorKey;
315
316
for(var key in context) {
317
if(context.hasOwnProperty(key)) {
318
// We're running the iterations one step out of sync so we can detect
319
// the last iteration without have to scan the object twice and create
320
// an itermediate keys array.
321
if (priorKey) {
322
execIteration(priorKey, i-1);
323
}
324
priorKey = key;
325
i++;
326
}
327
}
328
if (priorKey) {
329
execIteration(priorKey, i-1, true);
330
}
331
}
332
}
333
334
if(i === 0){
335
ret = inverse(this);
336
}
337
338
return ret;
339
});
340
341
instance.registerHelper('if', function(conditional, options) {
342
if (isFunction(conditional)) { conditional = conditional.call(this); }
343
344
// Default behavior is to render the positive path if the value is truthy and not empty.
345
// The `includeZero` option may be set to treat the condtional as purely not empty based on the
346
// behavior of isEmpty. Effectively this determines if 0 is handled by the positive path or negative.
347
if ((!options.hash.includeZero && !conditional) || Utils.isEmpty(conditional)) {
348
return options.inverse(this);
349
} else {
350
return options.fn(this);
351
}
352
});
353
354
instance.registerHelper('unless', function(conditional, options) {
355
return instance.helpers['if'].call(this, conditional, {fn: options.inverse, inverse: options.fn, hash: options.hash});
356
});
357
358
instance.registerHelper('with', function(context, options) {
359
if (isFunction(context)) { context = context.call(this); }
360
361
var fn = options.fn;
362
363
if (!Utils.isEmpty(context)) {
364
if (options.data && options.ids) {
365
var data = createFrame(options.data);
366
data.contextPath = Utils.appendContextPath(options.data.contextPath, options.ids[0]);
367
options = {data:data};
368
}
369
370
return fn(context, options);
371
} else {
372
return options.inverse(this);
373
}
374
});
375
376
instance.registerHelper('log', function(message, options) {
377
var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1;
378
instance.log(level, message);
379
});
380
381
instance.registerHelper('lookup', function(obj, field) {
382
return obj && obj[field];
383
});
384
}
385
386
var logger = {
387
methodMap: { 0: 'debug', 1: 'info', 2: 'warn', 3: 'error' },
388
389
// State enum
390
DEBUG: 0,
391
INFO: 1,
392
WARN: 2,
393
ERROR: 3,
394
level: 1,
395
396
// Can be overridden in the host environment
397
log: function(level, message) {
398
if (typeof console !== 'undefined' && logger.level <= level) {
399
var method = logger.methodMap[level];
400
(console[method] || console.log).call(console, message);
401
}
402
}
403
};
404
__exports__.logger = logger;
405
var log = logger.log;
406
__exports__.log = log;
407
var createFrame = function(object) {
408
var frame = Utils.extend({}, object);
409
frame._parent = object;
410
return frame;
411
};
412
__exports__.createFrame = createFrame;
413
});
414
define(
415
'handlebars/safe-string',["exports"],
416
function(__exports__) {
417
418
// Build out our basic SafeString type
419
function SafeString(string) {
420
this.string = string;
421
}
422
423
SafeString.prototype.toString = SafeString.prototype.toHTML = function() {
424
return "" + this.string;
425
};
426
427
__exports__["default"] = SafeString;
428
});
429
define(
430
'handlebars/runtime',["./utils","./exception","./base","exports"],
431
function(__dependency1__, __dependency2__, __dependency3__, __exports__) {
432
433
var Utils = __dependency1__;
434
var Exception = __dependency2__["default"];
435
var COMPILER_REVISION = __dependency3__.COMPILER_REVISION;
436
var REVISION_CHANGES = __dependency3__.REVISION_CHANGES;
437
var createFrame = __dependency3__.createFrame;
438
439
function checkRevision(compilerInfo) {
440
var compilerRevision = compilerInfo && compilerInfo[0] || 1,
441
currentRevision = COMPILER_REVISION;
442
443
if (compilerRevision !== currentRevision) {
444
if (compilerRevision < currentRevision) {
445
var runtimeVersions = REVISION_CHANGES[currentRevision],
446
compilerVersions = REVISION_CHANGES[compilerRevision];
447
throw new Exception("Template was precompiled with an older version of Handlebars than the current runtime. "+
448
"Please update your precompiler to a newer version ("+runtimeVersions+") or downgrade your runtime to an older version ("+compilerVersions+").");
449
} else {
450
// Use the embedded version info since the runtime doesn't know about this revision yet
451
throw new Exception("Template was precompiled with a newer version of Handlebars than the current runtime. "+
452
"Please update your runtime to a newer version ("+compilerInfo[1]+").");
453
}
454
}
455
}
456
457
__exports__.checkRevision = checkRevision;// TODO: Remove this line and break up compilePartial
458
459
function template(templateSpec, env) {
460
/* istanbul ignore next */
461
if (!env) {
462
throw new Exception("No environment passed to template");
463
}
464
if (!templateSpec || !templateSpec.main) {
465
throw new Exception('Unknown template object: ' + typeof templateSpec);
466
}
467
468
// Note: Using env.VM references rather than local var references throughout this section to allow
469
// for external users to override these as psuedo-supported APIs.
470
env.VM.checkRevision(templateSpec.compiler);
471
472
var invokePartialWrapper = function(partial, context, options) {
473
if (options.hash) {
474
context = Utils.extend({}, context, options.hash);
475
}
476
477
partial = env.VM.resolvePartial.call(this, partial, context, options);
478
var result = env.VM.invokePartial.call(this, partial, context, options);
479
480
if (result == null && env.compile) {
481
options.partials[options.name] = env.compile(partial, templateSpec.compilerOptions, env);
482
result = options.partials[options.name](context, options);
483
}
484
if (result != null) {
485
if (options.indent) {
486
var lines = result.split('\n');
487
for (var i = 0, l = lines.length; i < l; i++) {
488
if (!lines[i] && i + 1 === l) {
489
break;
490
}
491
492
lines[i] = options.indent + lines[i];
493
}
494
result = lines.join('\n');
495
}
496
return result;
497
} else {
498
throw new Exception("The partial " + options.name + " could not be compiled when running in runtime-only mode");
499
}
500
};
501
502
// Just add water
503
var container = {
504
strict: function(obj, name) {
505
if (!(name in obj)) {
506
throw new Exception('"' + name + '" not defined in ' + obj);
507
}
508
return obj[name];
509
},
510
lookup: function(depths, name) {
511
var len = depths.length;
512
for (var i = 0; i < len; i++) {
513
if (depths[i] && depths[i][name] != null) {
514
return depths[i][name];
515
}
516
}
517
},
518
lambda: function(current, context) {
519
return typeof current === 'function' ? current.call(context) : current;
520
},
521
522
escapeExpression: Utils.escapeExpression,
523
invokePartial: invokePartialWrapper,
524
525
fn: function(i) {
526
return templateSpec[i];
527
},
528
529
programs: [],
530
program: function(i, data, declaredBlockParams, blockParams, depths) {
531
var programWrapper = this.programs[i],
532
fn = this.fn(i);
533
if (data || depths || blockParams || declaredBlockParams) {
534
programWrapper = program(this, i, fn, data, declaredBlockParams, blockParams, depths);
535
} else if (!programWrapper) {
536
programWrapper = this.programs[i] = program(this, i, fn);
537
}
538
return programWrapper;
539
},
540
541
data: function(data, depth) {
542
while (data && depth--) {
543
data = data._parent;
544
}
545
return data;
546
},
547
merge: function(param, common) {
548
var ret = param || common;
549
550
if (param && common && (param !== common)) {
551
ret = Utils.extend({}, common, param);
552
}
553
554
return ret;
555
},
556
557
noop: env.VM.noop,
558
compilerInfo: templateSpec.compiler
559
};
560
561
var ret = function(context, options) {
562
options = options || {};
563
var data = options.data;
564
565
ret._setup(options);
566
if (!options.partial && templateSpec.useData) {
567
data = initData(context, data);
568
}
569
var depths,
570
blockParams = templateSpec.useBlockParams ? [] : undefined;
571
if (templateSpec.useDepths) {
572
depths = options.depths ? [context].concat(options.depths) : [context];
573
}
574
575
return templateSpec.main.call(container, context, container.helpers, container.partials, data, blockParams, depths);
576
};
577
ret.isTop = true;
578
579
ret._setup = function(options) {
580
if (!options.partial) {
581
container.helpers = container.merge(options.helpers, env.helpers);
582
583
if (templateSpec.usePartial) {
584
container.partials = container.merge(options.partials, env.partials);
585
}
586
} else {
587
container.helpers = options.helpers;
588
container.partials = options.partials;
589
}
590
};
591
592
ret._child = function(i, data, blockParams, depths) {
593
if (templateSpec.useBlockParams && !blockParams) {
594
throw new Exception('must pass block params');
595
}
596
if (templateSpec.useDepths && !depths) {
597
throw new Exception('must pass parent depths');
598
}
599
600
return program(container, i, templateSpec[i], data, 0, blockParams, depths);
601
};
602
return ret;
603
}
604
605
__exports__.template = template;function program(container, i, fn, data, declaredBlockParams, blockParams, depths) {
606
var prog = function(context, options) {
607
options = options || {};
608
609
return fn.call(container,
610
context,
611
container.helpers, container.partials,
612
options.data || data,
613
blockParams && [options.blockParams].concat(blockParams),
614
depths && [context].concat(depths));
615
};
616
prog.program = i;
617
prog.depth = depths ? depths.length : 0;
618
prog.blockParams = declaredBlockParams || 0;
619
return prog;
620
}
621
622
__exports__.program = program;function resolvePartial(partial, context, options) {
623
if (!partial) {
624
partial = options.partials[options.name];
625
} else if (!partial.call && !options.name) {
626
// This is a dynamic partial that returned a string
627
options.name = partial;
628
partial = options.partials[partial];
629
}
630
return partial;
631
}
632
633
__exports__.resolvePartial = resolvePartial;function invokePartial(partial, context, options) {
634
options.partial = true;
635
636
if(partial === undefined) {
637
throw new Exception("The partial " + options.name + " could not be found");
638
} else if(partial instanceof Function) {
639
return partial(context, options);
640
}
641
}
642
643
__exports__.invokePartial = invokePartial;function noop() { return ""; }
644
645
__exports__.noop = noop;function initData(context, data) {
646
if (!data || !('root' in data)) {
647
data = data ? createFrame(data) : {};
648
data.root = context;
649
}
650
return data;
651
}
652
});
653
define(
654
'handlebars.runtime',["./handlebars/base","./handlebars/safe-string","./handlebars/exception","./handlebars/utils","./handlebars/runtime","exports"],
655
function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __exports__) {
656
657
/*globals Handlebars: true */
658
var base = __dependency1__;
659
660
// Each of these augment the Handlebars object. No need to setup here.
661
// (This is done to easily share code between commonjs and browse envs)
662
var SafeString = __dependency2__["default"];
663
var Exception = __dependency3__["default"];
664
var Utils = __dependency4__;
665
var runtime = __dependency5__;
666
667
// For compatibility and usage outside of module systems, make the Handlebars object a namespace
668
var create = function() {
669
var hb = new base.HandlebarsEnvironment();
670
671
Utils.extend(hb, base);
672
hb.SafeString = SafeString;
673
hb.Exception = Exception;
674
hb.Utils = Utils;
675
hb.escapeExpression = Utils.escapeExpression;
676
677
hb.VM = runtime;
678
hb.template = function(spec) {
679
return runtime.template(spec, hb);
680
};
681
682
return hb;
683
};
684
685
var Handlebars = create();
686
Handlebars.create = create;
687
688
/*jshint -W040 */
689
/* istanbul ignore next */
690
var root = typeof global !== 'undefined' ? global : window,
691
$Handlebars = root.Handlebars;
692
/* istanbul ignore next */
693
Handlebars.noConflict = function() {
694
if (root.Handlebars === Handlebars) {
695
root.Handlebars = $Handlebars;
696
}
697
};
698
699
Handlebars['default'] = Handlebars;
700
701
__exports__["default"] = Handlebars;
702
});
703
704