1 | 'use strict'
|
2 |
|
3 | const vm = require('vm')
|
4 |
|
5 | const getNextTag = require('./tags')
|
6 | const parseLoopStatement = require('./loops')
|
7 | const escapeRegexpString = require('./escape')
|
8 | const makeLocalsBackup = require('./backup').make
|
9 | const revertBackupedLocals = require('./backup').revert
|
10 | const placeholders = require('./placeholders')
|
11 |
|
12 | const delimitersSettings = []
|
13 | let conditionals, switches, loops, scopes, ignored, delimitersReplace, unescapeDelimitersReplace
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function executeLoop (params, p1, p2, locals, tree) {
|
29 |
|
30 |
|
31 |
|
32 | const scopes = locals
|
33 |
|
34 | scopes[params[0]] = p1
|
35 |
|
36 | if (params[1]) scopes[params[1]] = p2
|
37 |
|
38 | return walk({ locals: scopes }, JSON.parse(tree))
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | function executeScope (scope, locals, node) {
|
53 | scope = Object.assign(locals, scope)
|
54 |
|
55 | return walk({ locals: scope }, node.content)
|
56 | }
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | function getLoopMeta (index, target) {
|
69 | index = Array.isArray(target) ? index : Object.keys(target).indexOf(index)
|
70 | const arr = Array.isArray(target) ? target : Object.keys(target)
|
71 |
|
72 | return {
|
73 | index: index,
|
74 | remaining: arr.length - index - 1,
|
75 | first: arr.indexOf(arr[index]) === 0,
|
76 | last: index + 1 === arr.length,
|
77 | length: arr.length
|
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 | module.exports = function postHTMLExpressions (options) {
|
104 |
|
105 | options = Object.assign({
|
106 | locals: {},
|
107 | delimiters: ['{{', '}}'],
|
108 | unescapeDelimiters: ['{{{', '}}}'],
|
109 | conditionalTags: ['if', 'elseif', 'else'],
|
110 | switchTags: ['switch', 'case', 'default'],
|
111 | loopTags: ['each'],
|
112 | scopeTags: ['scope'],
|
113 | ignoredTag: 'raw'
|
114 | }, options)
|
115 |
|
116 |
|
117 | loops = options.loopTags
|
118 | scopes = options.scopeTags
|
119 | conditionals = options.conditionalTags
|
120 | switches = options.switchTags
|
121 | ignored = options.ignoredTag
|
122 |
|
123 |
|
124 | let before = escapeRegexpString(options.delimiters[0])
|
125 | let after = escapeRegexpString(options.delimiters[1])
|
126 |
|
127 | const delimitersRegexp = new RegExp(`(?<!@)${before}(.+?)${after}`, 'g')
|
128 |
|
129 | before = escapeRegexpString(options.unescapeDelimiters[0])
|
130 | after = escapeRegexpString(options.unescapeDelimiters[1])
|
131 |
|
132 | const unescapeDelimitersRegexp = new RegExp(`(?<!@)${before}(.+?)${after}`, 'g')
|
133 |
|
134 |
|
135 | const delimiters = [
|
136 | { text: options.delimiters, regexp: delimitersRegexp, escape: true },
|
137 | { text: options.unescapeDelimiters, regexp: unescapeDelimitersRegexp, escape: false }
|
138 | ]
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | if (options.delimiters.join().length > options.unescapeDelimiters.join().length) {
|
144 | delimitersSettings[0] = delimiters[0]
|
145 | delimitersSettings[1] = delimiters[1]
|
146 | } else {
|
147 | delimitersSettings[0] = delimiters[1]
|
148 | delimitersSettings[1] = delimiters[0]
|
149 | }
|
150 |
|
151 | delimitersReplace = new RegExp(`@${escapeRegexpString(delimitersSettings[1].text[0])}`, 'g')
|
152 | unescapeDelimitersReplace = new RegExp(`@${escapeRegexpString(delimitersSettings[0].text[0])}`, 'g')
|
153 |
|
154 |
|
155 | return function (tree) {
|
156 | return clearRawTag(walk({ locals: options.locals }, tree))
|
157 | }
|
158 | }
|
159 |
|
160 | function walk (opts, nodes) {
|
161 |
|
162 | const ctx = vm.createContext(opts.locals)
|
163 |
|
164 |
|
165 |
|
166 | let skip
|
167 |
|
168 |
|
169 | return [].concat(nodes).reduce((m, node, i) => {
|
170 |
|
171 | if (skip) { skip--; return m }
|
172 |
|
173 |
|
174 | if (node.tag === ignored) {
|
175 | m.push(node)
|
176 |
|
177 | return m
|
178 | }
|
179 |
|
180 |
|
181 | if (typeof node === 'string') {
|
182 | node = placeholders(node, ctx, delimitersSettings)
|
183 | node = node
|
184 | .replace(unescapeDelimitersReplace, delimitersSettings[0].text[0])
|
185 | .replace(delimitersReplace, delimitersSettings[1].text[0])
|
186 |
|
187 | m.push(node)
|
188 |
|
189 | return m
|
190 | }
|
191 |
|
192 |
|
193 | if (node.attrs) {
|
194 | for (const key in node.attrs) {
|
195 | if (typeof node.attrs[key] === 'string') {
|
196 | node.attrs[key] = placeholders(node.attrs[key], ctx, delimitersSettings)
|
197 | node.attrs[key] = node.attrs[key]
|
198 | .replace(unescapeDelimitersReplace, delimitersSettings[0].text[0])
|
199 | .replace(delimitersReplace, delimitersSettings[1].text[0])
|
200 | }
|
201 | }
|
202 | }
|
203 |
|
204 |
|
205 | if (node.content && loops.includes(node.tag) === false && node.tag !== scopes[0]) {
|
206 | node.content = walk(opts, node.content)
|
207 | }
|
208 |
|
209 |
|
210 |
|
211 | if (node.tag === conditionals[0]) {
|
212 |
|
213 | if (!(node.attrs && node.attrs.condition)) {
|
214 | throw new Error(`the "${conditionals[0]}" tag must have a "condition" attribute`)
|
215 | }
|
216 |
|
217 |
|
218 | let expressionIndex = 1
|
219 | let expression = `if (${node.attrs.condition}) { 0 } `
|
220 |
|
221 | const branches = [node.content]
|
222 |
|
223 |
|
224 |
|
225 | let computedNextTag = getNextTag(nodes, ++i)
|
226 |
|
227 | let current = computedNextTag[0]
|
228 | let nextTag = computedNextTag[1]
|
229 |
|
230 | while (conditionals.slice(1).indexOf(nextTag.tag) > -1) {
|
231 | let statement = nextTag.tag
|
232 | let condition = ''
|
233 |
|
234 |
|
235 |
|
236 | if (nextTag.tag === conditionals[2]) statement = 'else'
|
237 |
|
238 |
|
239 | if (nextTag.tag === conditionals[1]) {
|
240 |
|
241 | if (!(nextTag.attrs && nextTag.attrs.condition)) {
|
242 | throw new Error(`the "${conditionals[1]}" tag must have a "condition" attribute`)
|
243 | }
|
244 | condition = nextTag.attrs.condition
|
245 |
|
246 |
|
247 | statement = 'else if'
|
248 | }
|
249 | branches.push(nextTag.content)
|
250 |
|
251 |
|
252 | expression += statement + (condition ? ` (${condition})` : '') + ` { ${expressionIndex++} } `
|
253 |
|
254 | computedNextTag = getNextTag(nodes, ++current)
|
255 |
|
256 | current = computedNextTag[0]
|
257 | nextTag = computedNextTag[1]
|
258 | }
|
259 |
|
260 |
|
261 | const branch = branches[vm.runInContext(expression, ctx)]
|
262 |
|
263 |
|
264 |
|
265 |
|
266 | skip = current - i
|
267 |
|
268 |
|
269 | if (branch) Array.prototype.push.apply(m, walk(opts, branch))
|
270 |
|
271 | return m
|
272 | }
|
273 |
|
274 |
|
275 | if (node.tag === switches[0]) {
|
276 |
|
277 | if (!(node.attrs && node.attrs.expression)) {
|
278 | throw new Error(`the "${switches[0]}" tag must have a "expression" attribute`)
|
279 | }
|
280 |
|
281 |
|
282 | let expressionIndex = 0
|
283 | let expression = `switch(${node.attrs.expression}) {`
|
284 |
|
285 | const branches = []
|
286 |
|
287 | for (let i = 0; i < node.content.length; i++) {
|
288 | const currentNode = node.content[i]
|
289 | if (typeof currentNode === 'string') {
|
290 | continue
|
291 | }
|
292 |
|
293 | if (currentNode.tag === switches[1]) {
|
294 |
|
295 | if (!(currentNode.attrs && currentNode.attrs.n)) {
|
296 | throw new Error(`the "${switches[1]}" tag must have a "n" attribute`)
|
297 | }
|
298 | expression += `case ${currentNode.attrs.n}: {${expressionIndex++}}; break; `
|
299 | } else if (currentNode.tag === switches[2]) {
|
300 | expression += `default: {${expressionIndex++}}`
|
301 | } else {
|
302 | throw new Error(`the "${switches[0]}" tag can contain only "${switches[1]}" tags and one "${switches[2]}" tag`)
|
303 | }
|
304 | branches.push(currentNode)
|
305 | }
|
306 |
|
307 | expression += '}'
|
308 |
|
309 |
|
310 | const branch = branches[vm.runInContext(expression, ctx)]
|
311 |
|
312 |
|
313 | Array.prototype.push.apply(m, walk(opts, branch.content))
|
314 |
|
315 | return m
|
316 | }
|
317 |
|
318 |
|
319 | if (loops.includes(node.tag)) {
|
320 |
|
321 | if (!(node.attrs && node.attrs.loop)) {
|
322 | throw new Error(`the "${node.tag}" tag must have a "loop" attribute`)
|
323 | }
|
324 |
|
325 |
|
326 | const loopParams = parseLoopStatement(node.attrs.loop)
|
327 | const target = vm.runInContext(loopParams.expression, ctx)
|
328 |
|
329 |
|
330 | if (typeof target !== 'object') {
|
331 | throw new Error('You must provide an array or object to loop through')
|
332 | }
|
333 |
|
334 | if (loopParams.keys.length < 1 || loopParams.keys[0] === '') {
|
335 | throw new Error('You must provide at least one loop argument')
|
336 | }
|
337 |
|
338 |
|
339 | const treeString = JSON.stringify(node.content)
|
340 | const keys = loopParams.keys
|
341 |
|
342 |
|
343 | const localsBackup = makeLocalsBackup(keys, opts.locals)
|
344 |
|
345 |
|
346 | if (Array.isArray(target)) {
|
347 | for (let index = 0; index < target.length; index++) {
|
348 | opts.locals.loop = getLoopMeta(index, target)
|
349 | m.push(executeLoop(keys, target[index], index, opts.locals, treeString))
|
350 | }
|
351 | } else {
|
352 | for (const key in target) {
|
353 | opts.locals.loop = getLoopMeta(key, target)
|
354 | m.push(executeLoop(keys, target[key], key, opts.locals, treeString))
|
355 | }
|
356 | }
|
357 |
|
358 |
|
359 | opts.locals = revertBackupedLocals(keys, opts.locals, localsBackup)
|
360 |
|
361 |
|
362 | return m
|
363 | }
|
364 |
|
365 |
|
366 | if (node.tag === scopes[0]) {
|
367 |
|
368 | if (!node.attrs || !node.attrs.with) {
|
369 | throw new Error(`the "${scopes[0]}" tag must have a "with" attribute`)
|
370 | }
|
371 |
|
372 | const target = vm.runInContext(node.attrs.with, ctx)
|
373 |
|
374 |
|
375 | if (typeof target !== 'object' || Array.isArray(target)) {
|
376 | throw new Error('You must provide an object to make scope')
|
377 | }
|
378 |
|
379 | const keys = Object.keys(target)
|
380 |
|
381 |
|
382 | const localsBackup = makeLocalsBackup(keys, opts.locals)
|
383 |
|
384 | m.push(executeScope(target, opts.locals, node))
|
385 |
|
386 |
|
387 | opts.locals = revertBackupedLocals(keys, opts.locals, localsBackup)
|
388 |
|
389 |
|
390 | return m
|
391 | }
|
392 |
|
393 |
|
394 | m.push(node)
|
395 |
|
396 | return m
|
397 | }, [])
|
398 | }
|
399 |
|
400 | function clearRawTag (tree) {
|
401 | return tree.reduce((m, node) => {
|
402 | if (node.content) {
|
403 | node.content = clearRawTag(node.content)
|
404 | }
|
405 |
|
406 | if (node.tag === ignored) {
|
407 | node.tag = false
|
408 | }
|
409 |
|
410 | m.push(node)
|
411 |
|
412 | return m
|
413 | }, [])
|
414 | }
|