Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / handlebars / lib / handlebars / compiler / helpers.js
1 import Exception from '../exception';
2
3 function validateClose(open, close) {
4   close = close.path ? close.path.original : close;
5
6   if (open.path.original !== close) {
7     let errorNode = {loc: open.path.loc};
8
9     throw new Exception(open.path.original + " doesn't match " + close, errorNode);
10   }
11 }
12
13 export function SourceLocation(source, locInfo) {
14   this.source = source;
15   this.start = {
16     line: locInfo.first_line,
17     column: locInfo.first_column
18   };
19   this.end = {
20     line: locInfo.last_line,
21     column: locInfo.last_column
22   };
23 }
24
25 export function id(token) {
26   if (/^\[.*\]$/.test(token)) {
27     return token.substr(1, token.length - 2);
28   } else {
29     return token;
30   }
31 }
32
33 export function stripFlags(open, close) {
34   return {
35     open: open.charAt(2) === '~',
36     close: close.charAt(close.length - 3) === '~'
37   };
38 }
39
40 export function stripComment(comment) {
41   return comment.replace(/^\{\{~?\!-?-?/, '')
42                 .replace(/-?-?~?\}\}$/, '');
43 }
44
45 export function preparePath(data, parts, loc) {
46   loc = this.locInfo(loc);
47
48   let original = data ? '@' : '',
49       dig = [],
50       depth = 0,
51       depthString = '';
52
53   for (let i = 0, l = parts.length; i < l; i++) {
54     let part = parts[i].part,
55         // If we have [] syntax then we do not treat path references as operators,
56         // i.e. foo.[this] resolves to approximately context.foo['this']
57         isLiteral = parts[i].original !== part;
58     original += (parts[i].separator || '') + part;
59
60     if (!isLiteral && (part === '..' || part === '.' || part === 'this')) {
61       if (dig.length > 0) {
62         throw new Exception('Invalid path: ' + original, {loc});
63       } else if (part === '..') {
64         depth++;
65         depthString += '../';
66       }
67     } else {
68       dig.push(part);
69     }
70   }
71
72   return {
73     type: 'PathExpression',
74     data,
75     depth,
76     parts: dig,
77     original,
78     loc
79   };
80 }
81
82 export function prepareMustache(path, params, hash, open, strip, locInfo) {
83   // Must use charAt to support IE pre-10
84   let escapeFlag = open.charAt(3) || open.charAt(2),
85       escaped = escapeFlag !== '{' && escapeFlag !== '&';
86
87   let decorator = (/\*/.test(open));
88   return {
89     type: decorator ? 'Decorator' : 'MustacheStatement',
90     path,
91     params,
92     hash,
93     escaped,
94     strip,
95     loc: this.locInfo(locInfo)
96   };
97 }
98
99 export function prepareRawBlock(openRawBlock, contents, close, locInfo) {
100   validateClose(openRawBlock, close);
101
102   locInfo = this.locInfo(locInfo);
103   let program = {
104     type: 'Program',
105     body: contents,
106     strip: {},
107     loc: locInfo
108   };
109
110   return {
111     type: 'BlockStatement',
112     path: openRawBlock.path,
113     params: openRawBlock.params,
114     hash: openRawBlock.hash,
115     program,
116     openStrip: {},
117     inverseStrip: {},
118     closeStrip: {},
119     loc: locInfo
120   };
121 }
122
123 export function prepareBlock(openBlock, program, inverseAndProgram, close, inverted, locInfo) {
124   if (close && close.path) {
125     validateClose(openBlock, close);
126   }
127
128   let decorator = (/\*/.test(openBlock.open));
129
130   program.blockParams = openBlock.blockParams;
131
132   let inverse,
133       inverseStrip;
134
135   if (inverseAndProgram) {
136     if (decorator) {
137       throw new Exception('Unexpected inverse block on decorator', inverseAndProgram);
138     }
139
140     if (inverseAndProgram.chain) {
141       inverseAndProgram.program.body[0].closeStrip = close.strip;
142     }
143
144     inverseStrip = inverseAndProgram.strip;
145     inverse = inverseAndProgram.program;
146   }
147
148   if (inverted) {
149     inverted = inverse;
150     inverse = program;
151     program = inverted;
152   }
153
154   return {
155     type: decorator ? 'DecoratorBlock' : 'BlockStatement',
156     path: openBlock.path,
157     params: openBlock.params,
158     hash: openBlock.hash,
159     program,
160     inverse,
161     openStrip: openBlock.strip,
162     inverseStrip,
163     closeStrip: close && close.strip,
164     loc: this.locInfo(locInfo)
165   };
166 }
167
168 export function prepareProgram(statements, loc) {
169   if (!loc && statements.length) {
170     const firstLoc = statements[0].loc,
171           lastLoc = statements[statements.length - 1].loc;
172
173     /* istanbul ignore else */
174     if (firstLoc && lastLoc) {
175       loc = {
176         source: firstLoc.source,
177         start: {
178           line: firstLoc.start.line,
179           column: firstLoc.start.column
180         },
181         end: {
182           line: lastLoc.end.line,
183           column: lastLoc.end.column
184         }
185       };
186     }
187   }
188
189   return {
190     type: 'Program',
191     body: statements,
192     strip: {},
193     loc: loc
194   };
195 }
196
197
198 export function preparePartialBlock(open, program, close, locInfo) {
199   validateClose(open, close);
200
201   return {
202     type: 'PartialBlockStatement',
203     name: open.path,
204     params: open.params,
205     hash: open.hash,
206     program,
207     openStrip: open.strip,
208     closeStrip: close && close.strip,
209     loc: this.locInfo(locInfo)
210   };
211 }
212