react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / handlebars / lib / handlebars / runtime.js
80698 viewsmodule Utils from "./utils";1import Exception from "./exception";2import { COMPILER_REVISION, REVISION_CHANGES, createFrame } from "./base";34export function checkRevision(compilerInfo) {5var compilerRevision = compilerInfo && compilerInfo[0] || 1,6currentRevision = COMPILER_REVISION;78if (compilerRevision !== currentRevision) {9if (compilerRevision < currentRevision) {10var runtimeVersions = REVISION_CHANGES[currentRevision],11compilerVersions = REVISION_CHANGES[compilerRevision];12throw new Exception("Template was precompiled with an older version of Handlebars than the current runtime. "+13"Please update your precompiler to a newer version ("+runtimeVersions+") or downgrade your runtime to an older version ("+compilerVersions+").");14} else {15// Use the embedded version info since the runtime doesn't know about this revision yet16throw new Exception("Template was precompiled with a newer version of Handlebars than the current runtime. "+17"Please update your runtime to a newer version ("+compilerInfo[1]+").");18}19}20}2122// TODO: Remove this line and break up compilePartial2324export function template(templateSpec, env) {25/* istanbul ignore next */26if (!env) {27throw new Exception("No environment passed to template");28}29if (!templateSpec || !templateSpec.main) {30throw new Exception('Unknown template object: ' + typeof templateSpec);31}3233// Note: Using env.VM references rather than local var references throughout this section to allow34// for external users to override these as psuedo-supported APIs.35env.VM.checkRevision(templateSpec.compiler);3637var invokePartialWrapper = function(partial, context, options) {38if (options.hash) {39context = Utils.extend({}, context, options.hash);40}4142partial = env.VM.resolvePartial.call(this, partial, context, options);43var result = env.VM.invokePartial.call(this, partial, context, options);4445if (result == null && env.compile) {46options.partials[options.name] = env.compile(partial, templateSpec.compilerOptions, env);47result = options.partials[options.name](context, options);48}49if (result != null) {50if (options.indent) {51var lines = result.split('\n');52for (var i = 0, l = lines.length; i < l; i++) {53if (!lines[i] && i + 1 === l) {54break;55}5657lines[i] = options.indent + lines[i];58}59result = lines.join('\n');60}61return result;62} else {63throw new Exception("The partial " + options.name + " could not be compiled when running in runtime-only mode");64}65};6667// Just add water68var container = {69strict: function(obj, name) {70if (!(name in obj)) {71throw new Exception('"' + name + '" not defined in ' + obj);72}73return obj[name];74},75lookup: function(depths, name) {76var len = depths.length;77for (var i = 0; i < len; i++) {78if (depths[i] && depths[i][name] != null) {79return depths[i][name];80}81}82},83lambda: function(current, context) {84return typeof current === 'function' ? current.call(context) : current;85},8687escapeExpression: Utils.escapeExpression,88invokePartial: invokePartialWrapper,8990fn: function(i) {91return templateSpec[i];92},9394programs: [],95program: function(i, data, declaredBlockParams, blockParams, depths) {96var programWrapper = this.programs[i],97fn = this.fn(i);98if (data || depths || blockParams || declaredBlockParams) {99programWrapper = program(this, i, fn, data, declaredBlockParams, blockParams, depths);100} else if (!programWrapper) {101programWrapper = this.programs[i] = program(this, i, fn);102}103return programWrapper;104},105106data: function(data, depth) {107while (data && depth--) {108data = data._parent;109}110return data;111},112merge: function(param, common) {113var ret = param || common;114115if (param && common && (param !== common)) {116ret = Utils.extend({}, common, param);117}118119return ret;120},121122noop: env.VM.noop,123compilerInfo: templateSpec.compiler124};125126var ret = function(context, options) {127options = options || {};128var data = options.data;129130ret._setup(options);131if (!options.partial && templateSpec.useData) {132data = initData(context, data);133}134var depths,135blockParams = templateSpec.useBlockParams ? [] : undefined;136if (templateSpec.useDepths) {137depths = options.depths ? [context].concat(options.depths) : [context];138}139140return templateSpec.main.call(container, context, container.helpers, container.partials, data, blockParams, depths);141};142ret.isTop = true;143144ret._setup = function(options) {145if (!options.partial) {146container.helpers = container.merge(options.helpers, env.helpers);147148if (templateSpec.usePartial) {149container.partials = container.merge(options.partials, env.partials);150}151} else {152container.helpers = options.helpers;153container.partials = options.partials;154}155};156157ret._child = function(i, data, blockParams, depths) {158if (templateSpec.useBlockParams && !blockParams) {159throw new Exception('must pass block params');160}161if (templateSpec.useDepths && !depths) {162throw new Exception('must pass parent depths');163}164165return program(container, i, templateSpec[i], data, 0, blockParams, depths);166};167return ret;168}169170export function program(container, i, fn, data, declaredBlockParams, blockParams, depths) {171var prog = function(context, options) {172options = options || {};173174return fn.call(container,175context,176container.helpers, container.partials,177options.data || data,178blockParams && [options.blockParams].concat(blockParams),179depths && [context].concat(depths));180};181prog.program = i;182prog.depth = depths ? depths.length : 0;183prog.blockParams = declaredBlockParams || 0;184return prog;185}186187export function resolvePartial(partial, context, options) {188if (!partial) {189partial = options.partials[options.name];190} else if (!partial.call && !options.name) {191// This is a dynamic partial that returned a string192options.name = partial;193partial = options.partials[partial];194}195return partial;196}197198export function invokePartial(partial, context, options) {199options.partial = true;200201if(partial === undefined) {202throw new Exception("The partial " + options.name + " could not be found");203} else if(partial instanceof Function) {204return partial(context, options);205}206}207208export function noop() { return ""; }209210function initData(context, data) {211if (!data || !('root' in data)) {212data = data ? createFrame(data) : {};213data.root = context;214}215return data;216}217218219