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