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