-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelper.js
574 lines (572 loc) · 18.8 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*global td: false */
let suite = {
name: 'Helpers',
tests: [
{
description: 'Helper returns a document fragment',
setup() {
td.registerHelper('createDiv', () => {
let frag = document.createDocumentFragment();
frag.appendChild(document.createElement('div'));
return frag;
});
},
template: '{@createDiv/}',
context: {},
expectedHTML: '<div></div>'
},
{
description: 'Helper with a parameter',
setup() {
td.registerHelper('createEl', (context, params) => {
let frag = document.createDocumentFragment();
frag.appendChild(document.createElement(params.name));
return frag;
});
},
template: '{@createEl name="div"/}',
context: {},
expectedHTML: '<div></div>'
},
{
description: 'Helper with a parameter that is a Tornado reference',
setup() {
td.registerHelper('createEl', (context, params) => {
let frag = document.createDocumentFragment();
frag.appendChild(document.createElement(params.name));
return frag;
});
},
template: '{@createEl name=elName/}',
context: {elName: 'div'},
expectedHTML: '<div></div>'
},
{
description: 'Helper with a parameter that is a Tornado reference with a promise',
setup() {
td.registerHelper('createEl', (context, params) => {
let frag = document.createDocumentFragment();
frag.appendChild(document.createElement(params.name));
return frag;
});
},
template: '{@createEl name=elName/}',
context: {elName: new Promise(resolve=> {
resolve('div');
})},
expectedHTML: '<div></div>'
},
{
description: 'Helper with a body (tests @eq with strings)',
template: '{@eq key="hello" val="hello"}<div>Hello, helpers!</div>{/eq}',
context: {},
expectedHTML: '<div>Hello, helpers!</div>'
},
{
description: 'Helper with an else body (tests @eq where strings are not equal)',
template: '{@eq key="hello" val="hi"}<div>Hello, helpers!</div>{:else}<div>Not equal :(</div>{/eq}',
context: {},
expectedHTML: '<div>Not equal :(</div>'
},
{
description: 'Helper with a named body',
setup() {
td.registerHelper('waffles', (context, params, bodies) => {
if (params.key === params.val) {
return bodies.main(context);
} else if (bodies.waffles) {
return bodies.waffles(context);
}
});
},
template: '{@waffles key="hello" val="hi"}<div>Hello, helpers!</div>{:waffles}<div>I love waffles!</div>{/waffles}',
context: {},
expectedHTML: '<div>I love waffles!</div>'
},
{
description: '@eq with equal numbers',
template: '{@eq key=5 val=5}<div>2 + 2</div>{:else}<div>Not equal :(</div>{/eq}',
context: {},
expectedHTML: '<div>2 + 2</div>'
},
{
description: '@eq with non-equal numbers',
template: '{@eq key=5 val=6}<div>2 + 2</div>{:else}<div>Not equal :(</div>{/eq}',
context: {},
expectedHTML: '<div>Not equal :(</div>'
},
{
description: '@eq with dynamic values',
template: '{@eq key=5 val=numFriends}<div>Just the right amount of friends</div>{:else}<div>Not equal :(</div>{/eq}',
context: {
friends: ['Prash', 'Jimmy', 'Kate', 'Seth', 'Tonya'],
numFriends: function() {
return this.friends.length;
}
},
expectedHTML: '<div>Just the right amount of friends</div>'
},
{
description: '@ne with non-equal numbers',
template: '{@ne key=5 val=6}<div>2 + 2</div>{:else}<div>These are equal :(</div>{/ne}',
context: {},
expectedHTML: '<div>2 + 2</div>'
},
{
description: '@ne with equal numbers',
template: '{@ne key=5 val=5}<div>2 + 2</div>{:else}<div>These are equal :(</div>{/ne}',
context: {},
expectedHTML: '<div>These are equal :(</div>'
},
{
description: '@ne with key and val of 0',
template: '{@ne key=0 val=0}<div>2 + 2</div>{:else}<div>These are equal :(</div>{/ne}',
context: {},
expectedHTML: '<div>These are equal :(</div>'
},
{
description: '@ne with non-equal strings',
template: '{@ne key="hello" val="bonjour"}<div>Bonjour, ça va?</div>{:else}<div>Huh?</div>{/ne}',
context: {},
expectedHTML: '<div>Bonjour, ça va?</div>'
},
{
description: '@ne with equal strings',
template: '{@ne key="Bom dia" val="Bom dia"}<div>Tudo bem, filho?</div>{:else}<div>Huh?</div>{/ne}',
context: {},
expectedHTML: '<div>Huh?</div>'
},
{
description: '@gt where key is greater than val',
template: '{@gt key=42 val=2}<div>This is the answer?</div>{/gt}',
context: {},
expectedHTML: '<div>This is the answer?</div>'
},
{
description: '@gt where key is less than val',
template: '{@gt key=2 val=42}<div>This is the answer?</div>{:else}<div>What was the question?</div>{/gt}',
context: {},
expectedHTML: '<div>What was the question?</div>'
},
{
description: '@gt where key and val are equal',
template: '{@gt key=2 val=2}<div>This is the answer?</div>{:else}<div>They are the same</div>{/gt}',
context: {},
expectedHTML: '<div>They are the same</div>'
},
{
description: '@gte where key is greater than val',
template: '{@gte key=42 val=2}<div>This is the answer?</div>{/gte}',
context: {},
expectedHTML: '<div>This is the answer?</div>'
},
{
description: '@gte where key is less than val',
template: '{@gte key=2 val=42}<div>This is the answer?</div>{:else}<div>What was the question?</div>{/gte}',
context: {},
expectedHTML: '<div>What was the question?</div>'
},
{
description: '@gte where key and val are equal',
template: '{@gte key=2 val=2}<div>This is the answer?</div>{:else}<div>They are the same</div>{/gte}',
context: {},
expectedHTML: '<div>This is the answer?</div>'
},
{
description: '@lt where key is greater than val',
template: '{@lt key=42 val=2}<div>This is the answer?</div>{/lt}',
context: {},
expectedHTML: ''
},
{
description: '@lt where key is less than val',
template: '{@lt key=2 val=42}<div>This is the answer?</div>{:else}<div>What was the question?</div>{/lt}',
context: {},
expectedHTML: '<div>This is the answer?</div>'
},
{
description: '@lt where key and val are equal',
template: '{@lt key=2 val=2}<div>This is the answer?</div>{:else}<div>They are the same</div>{/lt}',
context: {},
expectedHTML: '<div>They are the same</div>'
},
{
description: '@lte where key is greater than val',
template: '{@lte key=42 val=2}<div>This is the answer?</div>{/lte}',
context: {},
expectedHTML: ''
},
{
description: '@lte where key is less than val',
template: '{@lte key=2 val=42}<div>This is the answer?</div>{:else}<div>What was the question?</div>{/lte}',
context: {},
expectedHTML: '<div>This is the answer?</div>'
},
{
description: '@lte where key and val are equal',
template: '{@lte key=2 val=2}<div>This is the answer?</div>{:else}<div>They are the same</div>{/lte}',
context: {},
expectedHTML: '<div>This is the answer?</div>'
},
{
description: '@sep in a section-loop with just one item',
template: '{#names}{.}{@sep}; {/sep}{/names}',
context: {
names: ['Steven']
},
expectedHTML: 'Steven'
},
{
description: '@sep in a section-loop with multiple items',
template: '{#names}{.}{@sep}; {/sep}{/names}',
context: {
names: ['Steven', 'Jimmy', 'Prash']
},
expectedHTML: 'Steven; Jimmy; Prash'
},
{
description: '@sep in a nested section-loop with multiple items',
template: '{#names}{first} {#last}{.}{@sep},{/sep}{/last}{@sep}; {/sep}{/names}',
context: {
names: [
{first: 'Steven', last: ['F', 'o', 'o', 't', 'e']},
{first: 'Prash', last: ['J', 'a', 'i', 'n']}
]
},
expectedHTML: 'Steven F,o,o,t,e; Prash J,a,i,n'
},
{
description: '@sep in a section-loop with no items',
template: '{#names}{.}{@sep}; {/sep}{/names}',
context: {
names: []
},
expectedHTML: ''
},
{
description: '@sep outside of a loop',
template: '<span>{@sep}artificial separation{/sep}</span>',
context: {},
expectedHTML: '<span></span>'
},
{
description: '@first in a loop with one item',
template: '{#names}<span class="{@first}first{/first}">{.}</span>{/names}',
context: {
names: ['Steven']
},
expectedHTML: '<span class="first">Steven</span>'
},
{
description: '@first in a loop with multiple items',
template: '{#names}<span class="{@first}first{/first}">{.}</span>{/names}',
context: {
names: ['Steven', 'Jimmy', 'Prash']
},
expectedHTML: '<span class="first">Steven</span><span class>Jimmy</span><span class>Prash</span>'
},
{
description: '@first in a nested loop with multiple items',
template: '{#names}<div class="container{@first} outer-first{/first}">{#last}<span class="{@first}first{/first}">{.}</span>{/last}</div>{/names}',
context: {
names: [
{first: 'Steven', last: ['F', 'o', 'o', 't', 'e']},
{first: 'Prash', last: ['J', 'a', 'i', 'n']}
]
},
expectedHTML: '<div class="container outer-first"><span class="first">F</span><span class>o</span><span class>o</span><span class>t</span><span class>e</span></div><div class="container"><span class="first">J</span><span class>a</span><span class>i</span><span class>n</span></div>'
},
{
description: '@first in a loop with no items',
template: '{#names}<span class="{@first}first{/first}">{.}</span>{/names}',
context: {
names: []
},
expectedHTML: ''
},
{
description: '@first outside of a loop',
template: '<span class="{@first}first{/first}"></span>',
context: {
names: []
},
expectedHTML: '<span class></span>'
},
{
description: '@last in a loop with one item',
template: '{#names}<span class="{@last}last{/last}">{.}</span>{/names}',
context: {
names: ['Steven']
},
expectedHTML: '<span class="last">Steven</span>'
},
{
description: '@last in a loop with multiple items',
template: '{#names}<span class="{@last}last{/last}">{.}</span>{/names}',
context: {
names: ['Steven', 'Jimmy', 'Prash']
},
expectedHTML: '<span class>Steven</span><span class>Jimmy</span><span class="last">Prash</span>'
},
{
description: '@last in a nested loop with multiple items',
template: '{#names}<div class="container{@last} outer-last{/last}">{#last}<span class="{@last}last{/last}">{.}</span>{/last}</div>{/names}',
context: {
names: [
{first: 'Steven', last: ['F', 'o', 'o', 't', 'e']},
{first: 'Prash', last: ['J', 'a', 'i', 'n']}
]
},
expectedHTML: '<div class="container"><span class>F</span><span class>o</span><span class>o</span><span class>t</span><span class="last">e</span></div><div class="container outer-last"><span class>J</span><span class>a</span><span class>i</span><span class="last">n</span></div>'
},
{
description: '@last in a loop with no items',
template: '{#names}<span class="{@last}last{/last}">{.}</span>{/names}',
context: {
names: []
},
expectedHTML: ''
},
{
description: '@last outside of a loop',
template: '<span class="{@last}last{/last}"></span>',
context: {
names: []
},
expectedHTML: '<span class></span>'
},
{
description: '@select with only @default',
template: '{@select key=type}{@default}Default{/default}{/select}',
context: {
type: 'green'
},
expectedHTML: 'Default'
},
{
description: '@select with @eq that matches',
template: '{@select key=type}{@eq val="green"}GREEN!{/eq}{/select}',
context: {
type: 'green'
},
expectedHTML: 'GREEN!'
},
{
description: '@select with multiple @eq, one that matches',
template: '{@select key=type}{@eq val="blue"}BLUE!{/eq}{@eq val="green"}GREEN!{/eq}{/select}',
context: {
type: 'green'
},
expectedHTML: 'GREEN!'
},
{
description: '@select with multiple @eq, none match',
template: '{@select key=type}{@eq val="blue"}BLUE!{/eq}{@eq val="green"}GREEN!{/eq}{/select}',
context: {
type: 'yellow'
},
expectedHTML: ''
},
{
description: '@select with multiple @eq, none match, and a @default',
template: '{@select key=type}{@eq val="blue"}BLUE!{/eq}{@eq val="green"}GREEN!{/eq}{@default}A lack of color here{/default}{/select}',
context: {
type: 'yellow'
},
expectedHTML: 'A lack of color here'
},
{
description: '@select with multiple @eq, one match, and a @default',
template: '{@select key=type}{@eq val="blue"}BLUE!{/eq}{@eq val="green"}GREEN!{/eq}{@default}A lack of color here{/default}{/select}',
context: {
type: 'green'
},
expectedHTML: 'GREEN!'
},
{
description: 'Nested @select',
template: '{@select key=type}{@eq val="blue"}BLUE!{/eq}{@default}A lack of {@select key=category}{@eq val="color"}color{/eq}{/select} here{/default}{/select}',
context: {
type: 'green',
category: 'color'
},
expectedHTML: 'A lack of color here'
},
{
description: 'Nested @select with nested @default',
template: '{@select key=type}{@eq val="blue"}BLUE!{/eq}{@default}A lack of {@select key=category}{@eq val="color"}color{/eq}{@default}category{/default}{/select} here{/default}{/select}',
context: {
type: 'green',
category: 'stuff'
},
expectedHTML: 'A lack of category here'
},
{
description: '@math with no body -- add',
template: '{@math a=1 b=2 operator="add"/}',
context: {},
expectedHTML: '3'
},
{
description: '@math with no body -- (+)',
template: '{@math a=1 b=2 operator="+"/}',
context: {},
expectedHTML: '3'
},
{
description: '@math with no body -- subtract',
template: '{@math a=1 b=2 operator="subtract"/}',
context: {},
expectedHTML: '-1'
},
{
description: '@math with no body -- (-))',
template: '{@math a=1 b=2 operator="-"/}',
context: {},
expectedHTML: '-1'
},
{
description: '@math with no body -- multiply',
template: '{@math a=7 b=6 operator="multiply"/}',
context: {},
expectedHTML: '42'
},
{
description: '@math with no body -- (*)',
template: '{@math a=7 b=6 operator="*"/}',
context: {},
expectedHTML: '42'
},
{
description: '@math with no body -- divide',
template: '{@math a=144 b=12 operator="divide"/}',
context: {},
expectedHTML: '12'
},
{
description: '@math with no body -- (/)',
template: '{@math a=144 b=12 operator="/"/}',
context: {},
expectedHTML: '12'
},
{
description: '@math with no body -- mod',
template: '{@math a=15 b=12 operator="mod"/}',
context: {},
expectedHTML: '3'
},
{
description: '@math with no body -- (%)',
template: '{@math a=15 b=12 operator="%"/}',
context: {},
expectedHTML: '3'
},
{
description: '@math with no body -- ceil',
template: '{@math a=1.5 operator="ceil"/}',
context: {},
expectedHTML: '2'
},
{
description: '@math with no body -- floor',
template: '{@math a=1.5 operator="floor"/}',
context: {},
expectedHTML: '1'
},
{
description: '@math with no body -- round',
template: '{@math a=1.5 operator="round"/}',
context: {},
expectedHTML: '2'
},
{
description: '@math with no body -- abs',
template: '{@math a=-15 operator="abs"/}',
context: {},
expectedHTML: '15'
},
{
description: '@math with no body -- toint',
template: '{@math a=age operator="toint"/}',
context: {
age: '15'
},
expectedHTML: '15'
},
{
description: '@math with body and @eq',
template: '{@math a=5 b=10 operator="add"}{@eq val=15}!Quinceanera!{/eq}{/math}',
context: {
age: '15'
},
expectedHTML: '!Quinceanera!'
},
{
description: '@math with body and @gt',
template: '{@math a=hour b=12 operator="mod"}{@gt val=3}It\'s past 3 o\'clock{/gt}{/math}',
context: {
hour: '17'
},
expectedHTML: 'It\'s past 3 o\'clock'
},
{
description: '@math with body and @default',
template: '{@math a=hour b=12 operator="mod"}{@gt val=3}It\'s past 3 o\'clock{/gt}{@default}It\'s not past 3 yet{/default}{/math}',
context: {
hour: '15'
},
expectedHTML: 'It\'s not past 3 yet'
},
{
description: '@repeat with no references in the body',
template: '{@repeat count=2}<p>Hello!</p>{/repeat}',
context: {},
expectedHTML: '<p>Hello!</p><p>Hello!</p>'
},
{
description: '@repeat with references in the body',
template: '{@repeat count=2}<p>Hello, {name}!</p>{/repeat}',
context: {
name: 'Mundo'
},
expectedHTML: '<p>Hello, Mundo!</p><p>Hello, Mundo!</p>'
},
{
description: '@repeat with references to $idx and $len',
template: '{@repeat count=2}<p>{$idx} of {$len}</p>{/repeat}',
context: {},
expectedHTML: '<p>0 of 2</p><p>1 of 2</p>'
},
{
description: '@repeat nested within section with references to $idx and $len',
template: '{#numbers}<div>{$idx}: {.} {@repeat count=2}<p>{$idx} of {$len}</p>{/repeat}</div>{/numbers}',
context: {
numbers: [1,2]
},
expectedHTML: '<div>0: 1 <p>0 of 2</p><p>1 of 2</p></div><div>1: 2 <p>0 of 2</p><p>1 of 2</p></div>'
},
{
description: 'Helper context dotted reference lookup',
template: '{#numbers root=root}{$root.symbol}{.}{/numbers}',
context: {
root: {
symbol: '#'
},
numbers: [1,2]
},
expectedHTML: '#1#2'
},
{
description: 'Helper context dotted reference, first step doesn\'t exist',
template: '{#numbers root=root}{$rot.symbol}{.}{/numbers}',
context: {
root: {
symbol: '#'
},
numbers: [1,2]
},
expectedHTML: '12'
}
]
};
export default suite;