Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / handlebars / lib / handlebars / runtime.js
1 import * as Utils from './utils';
2 import Exception from './exception';
3 import { COMPILER_REVISION, REVISION_CHANGES, createFrame } from './base';
4
5 export function checkRevision(compilerInfo) {
6   const compilerRevision = compilerInfo && compilerInfo[0] || 1,
7         currentRevision = COMPILER_REVISION;
8
9   if (compilerRevision !== currentRevision) {
10     if (compilerRevision < currentRevision) {
11       const runtimeVersions = REVISION_CHANGES[currentRevision],
12             compilerVersions = REVISION_CHANGES[compilerRevision];
13       throw new Exception('Template was precompiled with an older version of Handlebars than the current runtime. ' +
14             'Please update your precompiler to a newer version (' + runtimeVersions + ') or downgrade your runtime to an older version (' + compilerVersions + ').');
15     } else {
16       // Use the embedded version info since the runtime doesn't know about this revision yet
17       throw new Exception('Template was precompiled with a newer version of Handlebars than the current runtime. ' +
18             'Please update your runtime to a newer version (' + compilerInfo[1] + ').');
19     }
20   }
21 }
22
23 export function template(templateSpec, env) {
24   /* istanbul ignore next */
25   if (!env) {
26     throw new Exception('No environment passed to template');
27   }
28   if (!templateSpec || !templateSpec.main) {
29     throw new Exception('Unknown template object: ' + typeof templateSpec);
30   }
31
32   templateSpec.main.decorator = templateSpec.main_d;
33
34   // Note: Using env.VM references rather than local var references throughout this section to allow
35   // for external users to override these as psuedo-supported APIs.
36   env.VM.checkRevision(templateSpec.compiler);
37
38   function invokePartialWrapper(partial, context, options) {
39     if (options.hash) {
40       context = Utils.extend({}, context, options.hash);
41       if (options.ids) {
42         options.ids[0] = true;
43       }
44     }
45
46     partial = env.VM.resolvePartial.call(this, partial, context, options);
47     let result = env.VM.invokePartial.call(this, partial, context, options);
48
49     if (result == null && env.compile) {
50       options.partials[options.name] = env.compile(partial, templateSpec.compilerOptions, env);
51       result = options.partials[options.name](context, options);
52     }
53     if (result != null) {
54       if (options.indent) {
55         let lines = result.split('\n');
56         for (let i = 0, l = lines.length; i < l; i++) {
57           if (!lines[i] && i + 1 === l) {
58             break;
59           }
60
61           lines[i] = options.indent + lines[i];
62         }
63         result = lines.join('\n');
64       }
65       return result;
66     } else {
67       throw new Exception('The partial ' + options.name + ' could not be compiled when running in runtime-only mode');
68     }
69   }
70
71   // Just add water
72   let container = {
73     strict: function(obj, name) {
74       if (!(name in obj)) {
75         throw new Exception('"' + name + '" not defined in ' + obj);
76       }
77       return obj[name];
78     },
79     lookup: function(depths, name) {
80       const len = depths.length;
81       for (let i = 0; i < len; i++) {
82         if (depths[i] && depths[i][name] != null) {
83           return depths[i][name];
84         }
85       }
86     },
87     lambda: function(current, context) {
88       return typeof current === 'function' ? current.call(context) : current;
89     },
90
91     escapeExpression: Utils.escapeExpression,
92     invokePartial: invokePartialWrapper,
93
94     fn: function(i) {
95       let ret = templateSpec[i];
96       ret.decorator = templateSpec[i + '_d'];
97       return ret;
98     },
99
100     programs: [],
101     program: function(i, data, declaredBlockParams, blockParams, depths) {
102       let programWrapper = this.programs[i],
103           fn = this.fn(i);
104       if (data || depths || blockParams || declaredBlockParams) {
105         programWrapper = wrapProgram(this, i, fn, data, declaredBlockParams, blockParams, depths);
106       } else if (!programWrapper) {
107         programWrapper = this.programs[i] = wrapProgram(this, i, fn);
108       }
109       return programWrapper;
110     },
111
112     data: function(value, depth) {
113       while (value && depth--) {
114         value = value._parent;
115       }
116       return value;
117     },
118     merge: function(param, common) {
119       let obj = param || common;
120
121       if (param && common && (param !== common)) {
122         obj = Utils.extend({}, common, param);
123       }
124
125       return obj;
126     },
127
128     noop: env.VM.noop,
129     compilerInfo: templateSpec.compiler
130   };
131
132   function ret(context, options = {}) {
133     let data = options.data;
134
135     ret._setup(options);
136     if (!options.partial && templateSpec.useData) {
137       data = initData(context, data);
138     }
139     let depths,
140         blockParams = templateSpec.useBlockParams ? [] : undefined;
141     if (templateSpec.useDepths) {
142       if (options.depths) {
143         depths = context != options.depths[0] ? [context].concat(options.depths) : options.depths;
144       } else {
145         depths = [context];
146       }
147     }
148
149     function main(context/*, options*/) {
150       return '' + templateSpec.main(container, context, container.helpers, container.partials, data, blockParams, depths);
151     }
152     main = executeDecorators(templateSpec.main, main, container, options.depths || [], data, blockParams);
153     return main(context, options);
154   }
155   ret.isTop = true;
156
157   ret._setup = function(options) {
158     if (!options.partial) {
159       container.helpers = container.merge(options.helpers, env.helpers);
160
161       if (templateSpec.usePartial) {
162         container.partials = container.merge(options.partials, env.partials);
163       }
164       if (templateSpec.usePartial || templateSpec.useDecorators) {
165         container.decorators = container.merge(options.decorators, env.decorators);
166       }
167     } else {
168       container.helpers = options.helpers;
169       container.partials = options.partials;
170       container.decorators = options.decorators;
171     }
172   };
173
174   ret._child = function(i, data, blockParams, depths) {
175     if (templateSpec.useBlockParams && !blockParams) {
176       throw new Exception('must pass block params');
177     }
178     if (templateSpec.useDepths && !depths) {
179       throw new Exception('must pass parent depths');
180     }
181
182     return wrapProgram(container, i, templateSpec[i], data, 0, blockParams, depths);
183   };
184   return ret;
185 }
186
187 export function wrapProgram(container, i, fn, data, declaredBlockParams, blockParams, depths) {
188   function prog(context, options = {}) {
189     let currentDepths = depths;
190     if (depths && context != depths[0]) {
191       currentDepths = [context].concat(depths);
192     }
193
194     return fn(container,
195         context,
196         container.helpers, container.partials,
197         options.data || data,
198         blockParams && [options.blockParams].concat(blockParams),
199         currentDepths);
200   }
201
202   prog = executeDecorators(fn, prog, container, depths, data, blockParams);
203
204   prog.program = i;
205   prog.depth = depths ? depths.length : 0;
206   prog.blockParams = declaredBlockParams || 0;
207   return prog;
208 }
209
210 export function resolvePartial(partial, context, options) {
211   if (!partial) {
212     if (options.name === '@partial-block') {
213       let data = options.data;
214       while (data['partial-block'] === noop) {
215         data = data._parent;
216       }
217       partial = data['partial-block'];
218       data['partial-block'] = noop;
219     } else {
220       partial = options.partials[options.name];
221     }
222   } else if (!partial.call && !options.name) {
223     // This is a dynamic partial that returned a string
224     options.name = partial;
225     partial = options.partials[partial];
226   }
227   return partial;
228 }
229
230 export function invokePartial(partial, context, options) {
231   options.partial = true;
232   if (options.ids) {
233     options.data.contextPath = options.ids[0] || options.data.contextPath;
234   }
235
236   let partialBlock;
237   if (options.fn && options.fn !== noop) {
238     options.data = createFrame(options.data);
239     partialBlock = options.data['partial-block'] = options.fn;
240
241     if (partialBlock.partials) {
242       options.partials = Utils.extend({}, options.partials, partialBlock.partials);
243     }
244   }
245
246   if (partial === undefined && partialBlock) {
247     partial = partialBlock;
248   }
249
250   if (partial === undefined) {
251     throw new Exception('The partial ' + options.name + ' could not be found');
252   } else if (partial instanceof Function) {
253     return partial(context, options);
254   }
255 }
256
257 export function noop() { return ''; }
258
259 function initData(context, data) {
260   if (!data || !('root' in data)) {
261     data = data ? createFrame(data) : {};
262     data.root = context;
263   }
264   return data;
265 }
266
267 function executeDecorators(fn, prog, container, depths, data, blockParams) {
268   if (fn.decorator) {
269     let props = {};
270     prog = fn.decorator(prog, props, container, depths && depths[0], data, blockParams, depths);
271     Utils.extend(prog, props);
272   }
273   return prog;
274 }