-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhandlebars.runtime.js
464 lines (451 loc) · 16.1 KB
/
handlebars.runtime.js
1
2
3
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
define("{{id}}", ["handlebars-runtime/1.3.0/dist/cjs/handlebars/base-debug", "handlebars-runtime/1.3.0/dist/cjs/handlebars/safe-string-debug", "handlebars-runtime/1.3.0/dist/cjs/handlebars/exception-debug", "handlebars-runtime/1.3.0/dist/cjs/handlebars/utils-debug", "handlebars-runtime/1.3.0/dist/cjs/handlebars/runtime-debug"], function(require, exports, module) {
"use strict";
/*globals Handlebars: true */
var base = require("handlebars-runtime/1.3.0/dist/cjs/handlebars/base-debug");
// Each of these augment the Handlebars object. No need to setup here.
// (This is done to easily share code between commonjs and browse envs)
var SafeString = require("handlebars-runtime/1.3.0/dist/cjs/handlebars/safe-string-debug")["default"];
var Exception = require("handlebars-runtime/1.3.0/dist/cjs/handlebars/exception-debug")["default"];
var Utils = require("handlebars-runtime/1.3.0/dist/cjs/handlebars/utils-debug");
var runtime = require("handlebars-runtime/1.3.0/dist/cjs/handlebars/runtime-debug");
// For compatibility and usage outside of module systems, make the Handlebars object a namespace
var create = function() {
var hb = new base.HandlebarsEnvironment();
Utils.extend(hb, base);
hb.SafeString = SafeString;
hb.Exception = Exception;
hb.Utils = Utils;
hb.VM = runtime;
hb.template = function(spec) {
return runtime.template(spec, hb);
};
return hb;
};
var Handlebars = create();
Handlebars.create = create;
exports["default"] = Handlebars;
});
define("handlebars-runtime/1.3.0/dist/cjs/handlebars/base-debug", ["handlebars-runtime/1.3.0/dist/cjs/handlebars/utils-debug", "handlebars-runtime/1.3.0/dist/cjs/handlebars/exception-debug", "handlebars-runtime/1.3.0/dist/cjs/handlebars/safe-string-debug"], function(require, exports, module) {
"use strict";
var Utils = require("handlebars-runtime/1.3.0/dist/cjs/handlebars/utils-debug");
var Exception = require("handlebars-runtime/1.3.0/dist/cjs/handlebars/exception-debug")["default"];
var VERSION = "1.3.0";
exports.VERSION = VERSION;
var COMPILER_REVISION = 4;
exports.COMPILER_REVISION = COMPILER_REVISION;
var REVISION_CHANGES = {
1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it
2: '== 1.0.0-rc.3',
3: '== 1.0.0-rc.4',
4: '>= 1.0.0'
};
exports.REVISION_CHANGES = REVISION_CHANGES;
var isArray = Utils.isArray,
isFunction = Utils.isFunction,
toString = Utils.toString,
objectType = '[object Object]';
function HandlebarsEnvironment(helpers, partials) {
this.helpers = helpers || {};
this.partials = partials || {};
registerDefaultHelpers(this);
}
exports.HandlebarsEnvironment = HandlebarsEnvironment;
HandlebarsEnvironment.prototype = {
constructor: HandlebarsEnvironment,
logger: logger,
log: log,
registerHelper: function(name, fn, inverse) {
if (toString.call(name) === objectType) {
if (inverse || fn) {
throw new Exception('Arg not supported with multiple helpers');
}
Utils.extend(this.helpers, name);
} else {
if (inverse) {
fn.not = inverse;
}
this.helpers[name] = fn;
}
},
registerPartial: function(name, str) {
if (toString.call(name) === objectType) {
Utils.extend(this.partials, name);
} else {
this.partials[name] = str;
}
}
};
function registerDefaultHelpers(instance) {
instance.registerHelper('helperMissing', function(arg) {
if (arguments.length === 2) {
return undefined;
} else {
throw new Exception("Missing helper: '" + arg + "'");
}
});
instance.registerHelper('blockHelperMissing', function(context, options) {
var inverse = options.inverse || function() {},
fn = options.fn;
if (isFunction(context)) {
context = context.call(this);
}
if (context === true) {
return fn(this);
} else if (context === false || context == null) {
return inverse(this);
} else if (isArray(context)) {
if (context.length > 0) {
return instance.helpers.each(context, options);
} else {
return inverse(this);
}
} else {
return fn(context);
}
});
instance.registerHelper('each', function(context, options) {
var fn = options.fn,
inverse = options.inverse;
var i = 0,
ret = "",
data;
if (isFunction(context)) {
context = context.call(this);
}
if (options.data) {
data = createFrame(options.data);
}
if (context && typeof context === 'object') {
if (isArray(context)) {
for (var j = context.length; i < j; i++) {
if (data) {
data.index = i;
data.first = (i === 0);
data.last = (i === (context.length - 1));
}
ret = ret + fn(context[i], {
data: data
});
}
} else {
for (var key in context) {
if (context.hasOwnProperty(key)) {
if (data) {
data.key = key;
data.index = i;
data.first = (i === 0);
}
ret = ret + fn(context[key], {
data: data
});
i++;
}
}
}
}
if (i === 0) {
ret = inverse(this);
}
return ret;
});
instance.registerHelper('if', function(conditional, options) {
if (isFunction(conditional)) {
conditional = conditional.call(this);
}
// Default behavior is to render the positive path if the value is truthy and not empty.
// The `includeZero` option may be set to treat the condtional as purely not empty based on the
// behavior of isEmpty. Effectively this determines if 0 is handled by the positive path or negative.
if ((!options.hash.includeZero && !conditional) || Utils.isEmpty(conditional)) {
return options.inverse(this);
} else {
return options.fn(this);
}
});
instance.registerHelper('unless', function(conditional, options) {
return instance.helpers['if'].call(this, conditional, {
fn: options.inverse,
inverse: options.fn,
hash: options.hash
});
});
instance.registerHelper('with', function(context, options) {
if (isFunction(context)) {
context = context.call(this);
}
if (!Utils.isEmpty(context)) return options.fn(context);
});
instance.registerHelper('log', function(context, options) {
var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1;
instance.log(level, context);
});
}
var logger = {
methodMap: {
0: 'debug',
1: 'info',
2: 'warn',
3: 'error'
},
// State enum
DEBUG: 0,
INFO: 1,
WARN: 2,
ERROR: 3,
level: 3,
// can be overridden in the host environment
log: function(level, obj) {
if (logger.level <= level) {
var method = logger.methodMap[level];
if (typeof console !== 'undefined' && console[method]) {
console[method].call(console, obj);
}
}
}
};
exports.logger = logger;
function log(level, obj) {
logger.log(level, obj);
}
exports.log = log;
var createFrame = function(object) {
var obj = {};
Utils.extend(obj, object);
return obj;
};
exports.createFrame = createFrame;
});
define("handlebars-runtime/1.3.0/dist/cjs/handlebars/safe-string-debug", [], function(require, exports, module) {
"use strict";
// Build out our basic SafeString type
function SafeString(string) {
this.string = string;
}
SafeString.prototype.toString = function() {
return "" + this.string;
};
exports["default"] = SafeString;
});
define("handlebars-runtime/1.3.0/dist/cjs/handlebars/exception-debug", [], function(require, exports, module) {
"use strict";
var errorProps = ['description', 'fileName', 'lineNumber', 'message', 'name', 'number', 'stack'];
function Exception(message, node) {
var line;
if (node && node.firstLine) {
line = node.firstLine;
message += ' - ' + line + ':' + node.firstColumn;
}
var tmp = Error.prototype.constructor.call(this, message);
// Unfortunately errors are not enumerable in Chrome (at least), so `for prop in tmp` doesn't work.
for (var idx = 0; idx < errorProps.length; idx++) {
this[errorProps[idx]] = tmp[errorProps[idx]];
}
if (line) {
this.lineNumber = line;
this.column = node.firstColumn;
}
}
Exception.prototype = new Error();
exports["default"] = Exception;
});
define("handlebars-runtime/1.3.0/dist/cjs/handlebars/utils-debug", ["handlebars-runtime/1.3.0/dist/cjs/handlebars/safe-string-debug"], function(require, exports, module) {
"use strict";
/*jshint -W004 */
var SafeString = require("handlebars-runtime/1.3.0/dist/cjs/handlebars/safe-string-debug")["default"];
var escape = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
"`": "`"
};
var badChars = /[&<>"'`]/g;
var possible = /[&<>"'`]/;
function escapeChar(chr) {
return escape[chr] || "&";
}
function extend(obj, value) {
for (var key in value) {
if (Object.prototype.hasOwnProperty.call(value, key)) {
obj[key] = value[key];
}
}
}
exports.extend = extend;
var toString = Object.prototype.toString;
exports.toString = toString;
// Sourced from lodash
// https://github.com/bestiejs/lodash/blob/master/LICENSE.txt
var isFunction = function(value) {
return typeof value === 'function';
};
// fallback for older versions of Chrome and Safari
if (isFunction(/x/)) {
isFunction = function(value) {
return typeof value === 'function' && toString.call(value) === '[object Function]';
};
}
var isFunction;
exports.isFunction = isFunction;
var isArray = Array.isArray || function(value) {
return (value && typeof value === 'object') ? toString.call(value) === '[object Array]' : false;
};
exports.isArray = isArray;
function escapeExpression(string) {
// don't escape SafeStrings, since they're already safe
if (string instanceof SafeString) {
return string.toString();
} else if (!string && string !== 0) {
return "";
}
// Force a string conversion as this will be done by the append regardless and
// the regex test will do this transparently behind the scenes, causing issues if
// an object's to string has escaped characters in it.
string = "" + string;
if (!possible.test(string)) {
return string;
}
return string.replace(badChars, escapeChar);
}
exports.escapeExpression = escapeExpression;
function isEmpty(value) {
if (!value && value !== 0) {
return true;
} else if (isArray(value) && value.length === 0) {
return true;
} else {
return false;
}
}
exports.isEmpty = isEmpty;
});
define("handlebars-runtime/1.3.0/dist/cjs/handlebars/runtime-debug", ["handlebars-runtime/1.3.0/dist/cjs/handlebars/utils-debug", "handlebars-runtime/1.3.0/dist/cjs/handlebars/exception-debug", "handlebars-runtime/1.3.0/dist/cjs/handlebars/base-debug", "handlebars-runtime/1.3.0/dist/cjs/handlebars/safe-string-debug"], function(require, exports, module) {
"use strict";
var Utils = require("handlebars-runtime/1.3.0/dist/cjs/handlebars/utils-debug");
var Exception = require("handlebars-runtime/1.3.0/dist/cjs/handlebars/exception-debug")["default"];
var COMPILER_REVISION = require("handlebars-runtime/1.3.0/dist/cjs/handlebars/base-debug").COMPILER_REVISION;
var REVISION_CHANGES = require("handlebars-runtime/1.3.0/dist/cjs/handlebars/base-debug").REVISION_CHANGES;
function checkRevision(compilerInfo) {
var compilerRevision = compilerInfo && compilerInfo[0] || 1,
currentRevision = COMPILER_REVISION;
if (compilerRevision !== currentRevision) {
if (compilerRevision < currentRevision) {
var runtimeVersions = REVISION_CHANGES[currentRevision],
compilerVersions = REVISION_CHANGES[compilerRevision];
throw new Exception("Template was precompiled with an older version of Handlebars than the current runtime. " + "Please update your precompiler to a newer version (" + runtimeVersions + ") or downgrade your runtime to an older version (" + compilerVersions + ").");
} else {
// Use the embedded version info since the runtime doesn't know about this revision yet
throw new Exception("Template was precompiled with a newer version of Handlebars than the current runtime. " + "Please update your runtime to a newer version (" + compilerInfo[1] + ").");
}
}
}
exports.checkRevision = checkRevision; // TODO: Remove this line and break up compilePartial
function template(templateSpec, env) {
if (!env) {
throw new Exception("No environment passed to template");
}
// Note: Using env.VM references rather than local var references throughout this section to allow
// for external users to override these as psuedo-supported APIs.
var invokePartialWrapper = function(partial, name, context, helpers, partials, data) {
var result = env.VM.invokePartial.apply(this, arguments);
if (result != null) {
return result;
}
if (env.compile) {
var options = {
helpers: helpers,
partials: partials,
data: data
};
partials[name] = env.compile(partial, {
data: data !== undefined
}, env);
return partials[name](context, options);
} else {
throw new Exception("The partial " + name + " could not be compiled when running in runtime-only mode");
}
};
// Just add water
var container = {
escapeExpression: Utils.escapeExpression,
invokePartial: invokePartialWrapper,
programs: [],
program: function(i, fn, data) {
var programWrapper = this.programs[i];
if (data) {
programWrapper = program(i, fn, data);
} else if (!programWrapper) {
programWrapper = this.programs[i] = program(i, fn);
}
return programWrapper;
},
merge: function(param, common) {
var ret = param || common;
if (param && common && (param !== common)) {
ret = {};
Utils.extend(ret, common);
Utils.extend(ret, param);
}
return ret;
},
programWithDepth: env.VM.programWithDepth,
noop: env.VM.noop,
compilerInfo: null
};
return function(context, options) {
options = options || {};
var namespace = options.partial ? options : env,
helpers,
partials;
if (!options.partial) {
helpers = options.helpers;
partials = options.partials;
}
var result = templateSpec.call(container, namespace, context, helpers, partials, options.data);
if (!options.partial) {
env.VM.checkRevision(container.compilerInfo);
}
return result;
};
}
exports.template = template;
function programWithDepth(i, fn, data /*, $depth */ ) {
var args = Array.prototype.slice.call(arguments, 3);
var prog = function(context, options) {
options = options || {};
return fn.apply(this, [context, options.data || data].concat(args));
};
prog.program = i;
prog.depth = args.length;
return prog;
}
exports.programWithDepth = programWithDepth;
function program(i, fn, data) {
var prog = function(context, options) {
options = options || {};
return fn(context, options.data || data);
};
prog.program = i;
prog.depth = 0;
return prog;
}
exports.program = program;
function invokePartial(partial, name, context, helpers, partials, data) {
var options = {
partial: true,
helpers: helpers,
partials: partials,
data: data
};
if (partial === undefined) {
throw new Exception("The partial " + name + " could not be found");
} else if (partial instanceof Function) {
return partial(context, options);
}
}
exports.invokePartial = invokePartial;
function noop() {
return "";
}
exports.noop = noop;
});