-
Notifications
You must be signed in to change notification settings - Fork 427
/
Copy pathindex.js
1336 lines (1158 loc) · 46.7 KB
/
index.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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2011 Twitter, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var is = strictEqual;
test("Scan Text No Tags", function() {
var text = "<h2>hi</h2>";
var tokens = Hogan.scan(text);
is(tokens.length, 1, "One token");
is(tokens[0].text + '', text, "text is equal to first token");
});
test("Scan One Tag", function() {
var text = "{{hmm}}";
var tokens = Hogan.scan(text);
is(tokens.length, 1, "One token");
is(tokens[0].n, "hmm", "First token content is variable name.");
});
test("Scan Multiple Tags", function() {
var text = "asdf{{hmm}}asdf2{{hmm2}}asdf3";
var tokens = Hogan.scan(text);
is(tokens.length, 5, "3 text tokens, 2 tag tokens.");
is(tokens[0].text+'', "asdf", "first token is text");
is(tokens[1].n, "hmm", "second token is tag");
is(tokens[1].tag, "_v", "second token is a variable");
is(tokens[2].text+'', "asdf2", "third token is text");
is(tokens[3].n, "hmm2", "fourth token is tag");
is(tokens[3].tag, "_v", "fourth token is a variable");
is(tokens[4].text+'', "asdf3", "Fifth token is text");
});
test("Scan Section Open", function() {
var text = "{{#hmm}}";
var tokens = Hogan.scan(text);
is(tokens.length, 1, "One token");
is(tokens[0].n, "hmm", "First token content is variable name.");
is(tokens[0].tag, "#", "First token is a section.");
});
test("Scan Section Close", function() {
var text = "{{/hmm}}";
var tokens = Hogan.scan(text);
is(tokens.length, 1, "One token");
is(tokens[0].n, "hmm", "First token content is variable name.");
is(tokens[0].tag, "/", "First token is a section.");
});
test("Scan Section", function() {
var text = "{{#hmm}}{{/hmm}}";
var tokens = Hogan.scan(text);
is(tokens.length, 2, "One token");
is(tokens[0].n, "hmm", "First token content is variable name.");
is(tokens[0].tag, "#", "First token is a section.");
is(tokens[1].n, "hmm", "Second token content is variable name.");
is(tokens[1].tag, "/", "Second token is a section.");
});
test("Scan Section In Content", function() {
var text = "abc{{#hmm}}def{{/hmm}}ghi";
var tokens = Hogan.scan(text);
is(tokens.length, 5, "3 text tokens, 2 tag tokens.");
is(tokens[0].text + '', "abc", "first token is text");
is(tokens[1].n, "hmm", "second token is tag");
is(tokens[1].tag, "#", "second token is a variable");
is(tokens[2].text+'', "def", "third token is text");
is(tokens[3].n, "hmm", "fourth token is tag");
is(tokens[3].tag, "/", "fourth token is a variable");
is(tokens[4].text+'', "ghi", "Fifth token is text");
});
test("Scan Negative Section", function() {
var text = "{{^hmm}}{{/hmm}}";
var tokens = Hogan.scan(text);
is(tokens.length, 2, "One token");
is(tokens[0].n, "hmm", "First token content is variable name.");
is(tokens[0].tag, "^", "First token is a negative section.");
is(tokens[1].n, "hmm", "First token content is variable name.");
is(tokens[1].tag, "/", "Second token is a section.");
});
test("Scan Partial", function() {
var text = "{{>hmm}}";
var tokens = Hogan.scan(text);
is(tokens.length, 1, "One token");
is(tokens[0].n, "hmm", "First token content is variable name.");
is(tokens[0].tag, ">", "First token is a partial.");
});
test("Scan Backward Partial", function() {
var text = "{{<hmm}}";
var tokens = Hogan.scan(text);
is(tokens.length, 1, "One token");
is(tokens[0].n, "hmm", "First token content is variable name.");
is(tokens[0].tag, "<", "First token is a backward partial.");
});
test("Scan Ampersand No Escape Tag", function() {
var text = "{{&hmm}}";
var tokens = Hogan.scan(text);
is(tokens.length, 1, "One token");
is(tokens[0].n, "hmm", "First token content is variable name.");
is(tokens[0].tag, "&", "First token is an ampersand no-escape.");
});
test("Scan Triple Stache", function() {
var text = "{{{hmm}}}";
var tokens = Hogan.scan(text);
is(tokens.length, 1, "One token");
is(tokens[0].n, "hmm", "First token content is variable name.");
is(tokens[0].tag, "{", "First token is a triple-stache.");
});
test("Scan Section With Triple Stache Inside", function() {
var text = "a{{#yo}}b{{{hmm}}}c{{/yo}}d";
var tokens = Hogan.scan(text);
is(tokens.length, 7, "One token");
is(tokens[0].text+'', "a", "First token content is correct text.");
is(tokens[1].n, "yo", "Second token content is correct text.");
is(tokens[1].tag, "#", "Second token is a section.");
is(tokens[2].text+'', "b", "Third token content is correct text.");
is(tokens[3].n, "hmm", "Fourth token content is correct text.");
is(tokens[3].tag, "{", "Fourth token is a triple stache.");
is(tokens[4].text+'', "c", "Fifth token content is correct text.");
is(tokens[5].n, "yo", "Sixth token content is correct text.");
is(tokens[5].tag, "/", "Sixth token is a close.");
is(tokens[6].text+'', "d", "Seventh token content is correct text.");
});
test("Scan Set Delimiter", function() {
var text = "a{{=<% %>=}}b";
var tokens = Hogan.scan(text);
is(tokens.length, 2, "change delimiter doesn't appear as token.");
is(tokens[0].text+'', "a", "text before change delimiter is processed.");
is(tokens[1].text+'', "b", "text after change delimiter is processed.");
});
test("Scan Reset Delimiter", function() {
var text = "a{{=<% %>=}}b<%hmm%>c<%={{ }}=%>d{{hmm}}";
var tokens = Hogan.scan(text);
is(tokens.length, 6, "8 tokens, delimiter changes don't count.");
is(tokens[0].text+'', "a", "first token is correct.");
is(tokens[1].text+'', "b", "third token is correct.");
is(tokens[2].tag, "_v", "third token is correct tag.");
is(tokens[2].n, "hmm", "third token is correct name.");
is(tokens[3].text+'', "c", "fifth token is correct.");
is(tokens[4].text+'', "d", "seventh token is correct.");
is(tokens[5].tag, "_v", "eighth token is correct tag.");
is(tokens[5].n, "hmm", "eighth token is correct name.");
});
test("Single Char Delimiter", function() {
var text = '({{foo}} {{=[ ]=}}[text])';
var tokens = Hogan.scan(text);
var t = Hogan.compile(text);
var s = t.render({foo: "bar", text: 'It worked!'});
is(s, '(bar It worked!)', "Hogan substitution worked after custom delimiters.");
});
test("Set Delimiter With Whitespace", function() {
var text = "{{= | | =}}|foo|";
var t = Hogan.compile(text);
var s = t.render({foo: "bar"});
is(s, 'bar', "custom delimiters with whitespace works.")
});
test("Delimiters with extra whitespace", function() {
var text = "{{= | | =}}|foo|";
var t = Hogan.compile(text);
var s = t.render({foo: "bar"});
is(s, 'bar', "custom delimiters with extra whitespace works");
});
test("Delimiter cache busting", function() {
var text = "|foo|[foo]";
var t = Hogan.compile(text, {delimiters: "| |"});
var s = t.render({foo: "bar"});
is(s, "bar[foo]", "custom delimiters first pass works");
var t2 = Hogan.compile(text, {delimiters: "[ ]"});
is(t != t2, true, "Compiler returns new template with new delimiter options");
s = t2.render({foo: "bar"});
is(s, "|foo|bar", "custom delimiters first pass works");
var t3 = Hogan.compile(text, {delimiters: "| |"});
is(t, t3, "Compiler returns cached template with same delimiter options");
});
test("Parse Basic", function() {
var text = "test";
var tree = Hogan.parse(Hogan.scan(text));
is(tree.length, 1, "one parse node");
is(tree[0].text+'', "test", "text is correct");
});
test("Parse Variables", function() {
var text = "test{{foo}}test!{{bar}}test!!{{baz}}test!!!";
var tree = Hogan.parse(Hogan.scan(text));
is(tree.length, 7, "one parse node");
is(tree[0].text+'', "test", "first text is correct");
is(tree[2].text+'', "test!", "second text is correct")
is(tree[4].text+'', "test!!", "third text is correct")
is(tree[6].text+'', "test!!!", "last text is correct")
is(tree[1].n, "foo", "first var is correct");
is(tree[3].n, "bar", "second var is correct");
is(tree[5].n, "baz", "third var is correct");
});
test("Parse Section", function() {
var text = "a{{#foo}}b{{/foo}}c";
var tree = Hogan.parse(Hogan.scan(text));
is(tree.length, 3, "three nodes at base");
is(tree[0].text+'', "a", "correct text in first node");
is(tree[1].hasOwnProperty('nodes'), true, "second node is a section");
is(tree[1].tag, '#', "second node is a section");
is(tree[1].n, "foo", "correct name for section");
is(tree[1].nodes[0].text+'', "b", "correct text in section");
is(tree[2].text+'', "c", "correct text in last node");
});
test("Parse Indexes", function() {
var text = "abc{{#foo}}asdf{{bar}}asdf{{/foo}}def";
var tree = Hogan.parse(Hogan.scan(text));
is(text.substring(tree[1].i, tree[1].end), "asdf{{bar}}asdf", "section text indexes are correct");
});
test("Parse Negative Section", function() {
var text = "a{{^foo}}b{{/foo}}c";
var tree = Hogan.parse(Hogan.scan(text));
is(tree.length, 3, "three nodes at base");
is(tree[0].text+'', "a", "correct text in first node");
is(tree[1].hasOwnProperty('nodes'), true, "second node is a section");
is(tree[1].tag, '^', "second node is a negative section");
is(tree[1].n, "foo", "correct name for section");
is(tree[1].nodes[0].text+'', "b", "correct text in section");
is(tree[2].text+'', "c", "correct text in last node");
});
test("Parse Nested Sections", function() {
var text = "{{#bar}}{{#foo}}c{{/foo}}{{/bar}}"
var tree = Hogan.parse(Hogan.scan(text));
is(tree.length, 1, "one node at base");
is(tree[0].tag, "#", "open section is first node");
is(tree[0].n, "bar", "first section name is 'bar'");
is(tree[0].nodes.length, 1, "first section contains one node.");
is(tree[0].nodes[0].n, "foo", "correct name for nested section");
is(tree[0].nodes[0].nodes[0].text+'', "c", "correct text in nested section");
});
test("Missing Closing Tag", function() {
var text = "a{{#foo}}bc";
raises(function() {
var tree = Hogan.parse(Hogan.scan(text));
}, "missing closing tag: foo", "Error is generated");
});
test("Bad Nesting", function() {
var text = "a{{#foo}}{{#bar}}b{{/foo}}{{/bar}}c";
raises(function() {
var tree = Hogan.parse(Hogan.scan(text));
}, "Nesting error: bar vs. foo", "Error is generated");
});
test("Basic Output", function() {
var text = "test";
var t = Hogan.compile(text);
is(t.render(), text, "template renders one text node");
});
test("One Variable", function() {
var text = "test {{foo}} test";
var t = Hogan.compile(text);
var s = t.render({foo:'bar'});
is(s, "test bar test", "basic variable substitution works.");
});
test("Render With Whitespace", function() {
var text = "{{ string }}";
var t = Hogan.compile(text);
is(t.render({string: "---" }), "---", "tags with whitespace render correctly.");
});
test("Render With Whitespace Around Triple Stache", function() {
var text = " {{{string}}}\n";
var t = Hogan.compile(text);
is(t.render({string: "---" }), " ---\n", "triple stache surrounded by whitespace render correctly.");
});
test("Render With Whitespace Around Ampersand", function() {
var text = " {{& string }}\n";
var t = Hogan.compile(text);
is(t.render({string: "---" }), " ---\n", "ampersand surrounded by whitespace render correctly.");
});
test("Multiple Variables", function() {
var text = "test {{foo}} test {{bar}} test {{baz}} test {{foo}} test";
var t = Hogan.compile(text);
var s = t.render({foo:'42', bar: '43', baz: '44'});
is(s, "test 42 test 43 test 44 test 42 test", "all variables render correctly.");
});
test("Number Values", function() {
var text = "integer: {{foo}} float: {{bar}} negative: {{baz}}";
var t = Hogan.compile(text);
var s = t.render({foo: 42, bar: 42.42, baz: -42});
is(s, "integer: 42 float: 42.42 negative: -42", "numbers render correctly");
});
test("Object Render", function() {
var text = "object: {{foo}}";
var t = Hogan.compile(text);
var s = t.render({foo: {}});
is(s, "object: [object Object]", "objects render default toString.");
});
test("Object To String Render", function() {
var text = "object: {{foo}}";
var t = Hogan.compile(text);
var s = t.render({foo: {toString: function(){ return "yo!"}}});
is(s, "object: yo!", "objects render supplied toString.");
});
test("Array Render", function() {
var text = "array: {{foo}}";
var t = Hogan.compile(text);
var s = t.render({foo: ["a","b","c"]});
is(s, "array: a,b,c", "arrays render default toString.");
});
test("Escaping", function() {
var text = "{{foo}}";
var t = Hogan.compile(text);
var s = t.render();
var s = t.render({foo: "< > <div> \' \" &"});
is(s, "< > <div> ' " &", "input correctly escaped.");
var ec ={ "'": "'", '"': """, "<": "<", ">": ">", "&": "&"}
for (var char in ec) {
var s = t.render({foo: char + " just me"});
is(s, ec[char] + " just me", "input correctly escaped.");
}
});
test("Escaping \\u2028", function() {
var text = "{{foo}}\u2028{{bar}}";
var t = Hogan.compile(text);
var s = t.render({foo: 'foo', bar: 'bar'});
is(s, "foo\u2028bar", "\\u2028 improperly escaped");
});
test("Escaping \\u2029", function() {
var text = "{{foo}}\u2029{{bar}}";
var t = Hogan.compile(text);
var s = t.render({foo: 'foo', bar: 'bar'});
is(s, "foo\u2029bar", "\\u2029 improperly escaped");
});
test("Mustache Injection", function() {
var text = "{{foo}}";
var t = Hogan.compile(text);
var s = t.render({foo:"{{{<42}}}"})
is(s, "{{{<42}}}", "Can't inject mustache");
});
test("Triple Stache", function() {
var text = "{{{foo}}}";
var t = Hogan.compile(text);
var s = t.render({foo: "< > <div> \' \" &"});
is(s, "< > <div> \' \" &", "input correctly not-escaped.");
});
test("Amp No Escaping", function() {
var text = "{{&foo}}";
var t = Hogan.compile(text);
var s = t.render({foo: "< > <div> \' \" &"});
is(s, "< > <div> \' \" &", "input correctly not-escaped.");
});
test("Partial Basic", function() {
var partialText = "this is text from the partial--the magic number {{foo}} is from a variable";
var p = Hogan.compile(partialText);
var text = "This template contains a partial ({{>testPartial}})."
var t = Hogan.compile(text);
var s = t.render({foo: 42}, {testPartial: p});
is(s, "This template contains a partial (this is text from the partial--the magic number 42 is from a variable).", "partials work");
});
test("Nested Partials", function() {
var partialText = "this is text from the partial--the magic number {{foo}} is from a variable";
var p = Hogan.compile(partialText);
var partialText2 = "This template contains a partial ({{>testPartial}})."
var p2 = Hogan.compile(partialText2);
var text = "This template contains a partial that contains a partial [{{>testPartial2}}]."
var t = Hogan.compile(text);
var s = t.render({foo: 42}, {testPartial: p, testPartial2: p2});
is(s, "This template contains a partial that contains a partial [This template contains a partial (this is text from the partial--the magic number 42 is from a variable).].", "nested partials work");
});
test("Negative Section", function() {
var text = "This template {{^foo}}BOO {{/foo}}contains an inverted section."
var t = Hogan.compile(text);
var s = t.render();
is(s, "This template BOO contains an inverted section.", "inverted sections with no context work");
s = t.render({foo:[]});
is(s, "This template BOO contains an inverted section.", "inverted sections with empty list context work");
s = t.render({ foo:false });
is(s, "This template BOO contains an inverted section.", "inverted sections with false context work");
s = t.render({foo:''});
is(s, "This template BOO contains an inverted section.", "inverted sections with empty string context do render");
s = t.render({foo:true});
is(s, "This template contains an inverted section.", "inverted sections with true context work");
s = t.render({foo: function() { return false; }});
is(s, "This template BOO contains an inverted section.", "inverted sections with false returning method in context work");
s = t.render({foo: 0});
is(s, "This template BOO contains an inverted section.", "inverted sections with 0 returning method in context work");
});
test("Section Elision", function() {
var text = "This template {{#foo}}BOO {{/foo}}contains a section."
var t = Hogan.compile(text);
var s = t.render();
is(s, "This template contains a section.", "sections with no context work");
s = t.render({foo:[]});
is(s, "This template contains a section.", "sections with empty list context work");
s = t.render({foo:false});
is(s, "This template contains a section.", "sections with false context work");
s = t.render({foo:''});
is(s, "This template contains a section.", "sections with empty string context do not render");
s = t.render({foo:true});
is(s, "This template BOO contains a section.", "sections with true context work");
s = t.render({foo: function() { return false; }});
is(s, "This template contains a section.", "sections with false returning method in context work");
s = t.render({foo: 0});
is(s, "This template contains a section.", "sections with 0 returning method in context work");
});
test("Section Object Context", function() {
var text = "This template {{#foo}}{{bar}} {{/foo}}contains a section."
var t = Hogan.compile(text);
var s = t.render({foo:{bar:42}});
is(s, "This template 42 contains a section.", "sections with object context work");
});
test("Section Array Context", function() {
var text = "This template {{#foo}}{{bar}} {{/foo}}contains a section."
var t = Hogan.compile(text);
var s = t.render({foo:[{bar:42}, {bar:43}, {bar:44}]});
is(s, "This template 42 43 44 contains a section.", "sections with object ctx and array values work");
});
test("Falsy Variable No Render", function() {
var text = "I ({{cannot}}) be seen!";
var t = Hogan.compile(text);
var s = t.render();
is(s, "I () be seen!", "missing value doesn't render.");
});
test("Undefined Return Value From Lambda", function() {
var text = "abc{{foo}}def";
var t = Hogan.compile(text);
var context = {
foo: function(s) {
return undefined;
}
}
var s = t.render(context);
is(s, "abcdef", "deal with undefined return values from lambdas.")
});
test("Sections with null values are treated as key hits", function() {
var text = "{{#obj}}{{#sub}}{{^test}}ok{{/test}}{{/sub}}{{/obj}}";
var t = Hogan.compile(text);
var context = {
obj: {
test: true,
sub: {
test: null
}
}
}
var s = t.render(context);
is(s, "ok");
});
test("Sections with undefined values are treated as key misses", function() {
var text = "{{#obj}}{{#sub}}{{#test}}ok{{/test}}{{/sub}}{{/obj}}";
var t = Hogan.compile(text);
var context = {
obj: {
test: true,
sub: {
test: undefined
}
}
}
var s = t.render(context);
is(s, "ok");
});
test("Section Extensions", function() {
var text = "Test {{_//|__foo}}bar{{/foo}}";
var options = {sectionTags:[{o:'_//|__foo', c:'foo'}]};
var tree = Hogan.parse(Hogan.scan(text), text, options);
is(tree[1].tag, "#", "_//|__foo node transformed to section");
is(tree[1].n, "_//|__foo", "_//|__foo node transformed to section");
var t = Hogan.compile(text, options);
var s = t.render({'_//|__foo':true});
is(s, "Test bar", "Custom sections work");
});
test("Misnested Section Extensions", function() {
var text = "Test {{__foo}}bar{{/bar}}";
var options = {sectionTags:[{o:'__foo', c:'foo'}, {o:'__bar', c:'bar'}]};
raises(function() {
var tree = Hogan.parse(Hogan.scan(text), text, options);
}, "Nesting error: __foo vs. bar", "Error is generated");
});
test("Section Extensions In Higher Order Sections", function() {
var text = "Test{{_foo}}bar{{/foo}}";
var options = {sectionTags:[{o:'_foo', c:'foo'}, {o:'_baz', c:'baz'}]};
var t = Hogan.compile(text, options);
var context = {
"_baz": true,
"_foo": function () {
return function(s) {
return "{{_baz}}" + s + "{{/baz}}qux"
};
}
}
var s = t.render(context);
is(s, "Testbarqux", "unprocessed test");
});
test("Section Extension With Higher Order Sections Access Outer Context ", function() {
var text = "{{#inner}}{{#extension}}{{outerValue}}{{/extension}}{{/inner}}"
var t = Hogan.compile(text);
var context = {
outerValue: "Outer value",
inner: {
innerValue: "Inner value"
},
extension: function () {
return function (tmpl, ctx) {
var key = /{{(.*)}}/.exec(tmpl)[1];
return ctx[0][key];
}
}
};
var s = t.render(context);
is(s, "Outer value", "unprocessed test");
});
test("Section Extensions In Lambda Replace Variable", function() {
var text = "Test{{foo}}";
var options = {sectionTags:[{o:'_baz', c:'baz'}]};
var t = Hogan.compile(text, options);
var context = {
"_baz": true,
"foo": function () {
return function() { return "{{_baz}}abcdef{{/baz}}"; };
}
}
var s = t.render(context);
is(s, "Testabcdef", "unprocessed test");
});
test("disableLambda option works on interpolation", function() {
var text = "Test{{foo}}";
var options = {disableLambda: true}
var t = Hogan.compile(text, options);
var context = {
"baz": true,
"foo": function () {
return function() { return "{{#baz}}abcdef{{/baz}}"; };
}
}
var msg = "";
try {
var s = t.render(context);
} catch (e) {
msg = e.message;
}
is(msg, "Lambda features disabled.", "unprocessed test");
});
test("disableLambda option works on sections", function() {
var text = "Test{{#foo}}{{/foo}}";
var options = {disableLambda: true}
var t = Hogan.compile(text, options);
var context = {
"baz": true,
"foo": function () {
return function() { return "{{#baz}}abcdef{{/baz}}"; };
}
}
var msg = "";
try {
var s = t.render(context);
} catch (e) {
msg = e.message;
}
is(msg, "Lambda features disabled.", "unprocessed test");
});
test("Mustache not reprocessed for method calls in interpolations", function() {
var text = "text with {{foo}} inside";
var t = Hogan.compile(text);
var context = {
foo: function() {
return "no processing of {{tags}}";
}
}
var s = t.render(context);
is(s, "text with no processing of {{tags}} inside", "method calls should not be processed as mustache.");
var text = "text with {{{foo}}} inside";
var t = Hogan.compile(text);
var s = t.render(context);
is(s, "text with no processing of {{tags}} inside", "method calls should not be processed as mustache in triple staches.");
});
test("Mustache is reprocessed for lambdas in interpolations", function() {
var text = "text with {{foo}} inside";
var t = Hogan.compile(text);
var context = {
bar: "42",
foo: function() {
return function() {
return "processing of {{bar}}";
};
}
};
var s = t.render(context);
is(s, "text with processing of 42 inside", "the return value of lambdas should be processed mustache.");
});
test("Nested Section", function() {
var text = "{{#foo}}{{#bar}}{{baz}}{{/bar}}{{/foo}}";
var t = Hogan.compile(text);
var s = t.render({foo: 42, bar: 42, baz:42});
is(s, "42", "can reach up context stack");
});
test("Dotted Names", function() {
var text = '"{{person.name}}" == "{{#person}}{{name}}{{/person}}"';
var t = Hogan.compile(text);
var s = t.render({person:{name:'Joe'}});
is(s, '"Joe" == "Joe"', "dotted names work");
});
test("Implicit Iterator", function() {
var text = '{{#stuff}} {{.}} {{/stuff}}';
var t = Hogan.compile(text);
var s = t.render({stuff:[42,43,44]});
is(s, " 42 43 44 ", "implicit iterators work");
});
test("Partials And Delimiters", function() {
var text = '{{>include}}*\n{{= | | =}}\n*|>include|';
var partialText = ' .{{value}}. ';
var partial = Hogan.compile(partialText);
var t = Hogan.compile(text);
var s = t.render({value:"yes"}, {'include':partial});
is(s, " .yes. *\n* .yes. ", "partials work around delimiters");
});
test("String Partials", function() {
var text = "foo{{>mypartial}}baz";
var partialText = " bar ";
var t = Hogan.compile(text);
var s = t.render({}, {'mypartial': partialText});
is(s, "foo bar baz", "string partial works.");
});
test("Missing Partials", function() {
var text = "foo{{>mypartial}} bar";
var t = Hogan.compile(text);
var s = t.render({});
is(s, "foo bar", "missing partial works.");
});
test("Indented Standalone Comment", function() {
var text = 'Begin.\n {{! Indented Comment Block! }}\nEnd.';
var t = Hogan.compile(text);
var s = t.render();
is(s, 'Begin.\nEnd.', "Standalone comment blocks are removed.");
});
test("New Line Between Delimiter Changes", function() {
var data = { section: true, data: 'I got interpolated.' };
var text = '\n{{#section}}\n {{data}}\n |data|\n{{/section}}x\n\n{{= | | =}}\n|#section|\n {{data}}\n |data|\n|/section|';
var t = Hogan.compile(text);
var s = t.render(data);
is(s, '\n I got interpolated.\n |data|\nx\n\n {{data}}\n I got interpolated.\n', 'render correct')
});
test("Mustache JS Apostrophe", function() {
var text = '{{apos}}{{control}}';
var t = Hogan.compile(text);
var s = t.render({'apos':"'", 'control':"X"});
is(s, ''X', 'Apostrophe is escaped.');
});
test("Mustache JS Array Of Implicit Partials", function() {
var text = 'Here is some stuff!\n{{#numbers}}\n{{>partial}}\n{{/numbers}}\n';
var partialText = '{{.}}\n';
var t = Hogan.compile(text);
var s = t.render({numbers:[1,2,3,4]}, {partial: partialText});
is(s, 'Here is some stuff!\n1\n2\n3\n4\n', 'Partials with implicit iterators work.');
});
test("Mustache JS Array Of Partials", function() {
var text = 'Here is some stuff!\n{{#numbers}}\n{{>partial}}\n{{/numbers}}\n';
var partialText = '{{i}}\n';
var t = Hogan.compile(text);
var s = t.render({numbers:[{i:1},{i:2},{i:3},{i:4}]}, {partial: partialText});
is(s, 'Here is some stuff!\n1\n2\n3\n4\n', 'Partials with arrays work.');
});
test("Mustache JS Array Of Strings", function() {
var text = '{{#strings}}{{.}} {{/strings}}';
var t = Hogan.compile(text);
var s = t.render({strings:['foo', 'bar', 'baz']});
is(s, 'foo bar baz ', 'array of strings works with implicit iterators.');
});
test("Mustache JS Undefined String", function() {
var text = 'foo{{bar}}baz';
var t = Hogan.compile(text);
var s = t.render({bar:undefined});
is(s, 'foobaz', 'undefined value does not render.');
});
test("Mustache JS Undefined Triple Stache", function() {
var text = 'foo{{{bar}}}baz';
var t = Hogan.compile(text);
var s = t.render({bar:undefined});
is(s, 'foobaz', 'undefined value does not render in triple stache.');
});
test("Mustache JS Null String", function() {
var text = 'foo{{bar}}baz';
var t = Hogan.compile(text);
var s = t.render({bar:null});
is(s, 'foobaz', 'undefined value does not render.');
});
test("Mustache JS Null Triple Stache", function() {
var text = 'foo{{{bar}}}baz';
var t = Hogan.compile(text);
var s = t.render({bar:null});
is(s, 'foobaz', 'undefined value does not render in triple stache.');
});
test("Mustache JS Triple Stache Alt Delimiter", function() {
var text = '{{=<% %>=}}<% foo %> {{foo}} <%{bar}%> {{{bar}}}';
var t = Hogan.compile(text);
var s = t.render({foo:'yeah', bar:'hmm'});
is(s, 'yeah {{foo}} hmm {{{bar}}}', 'triple stache inside alternate delimiter works.');
});
/* template inheritance */
test("Parse a $ tag", function() {
var text = '{{$title}}Default title{{/title}}';
var tree = Hogan.parse(Hogan.scan(text));
is(tree[0].tag, "$", '$ should have correct tag name');
is(tree.length, 1, 'there should be one node at the top level');
is(tree[0].nodes.length, 1, 'there should be one child text node');
});
test("Default content", function() {
var text = '{{$title}}Default title{{/title}}';
var t = Hogan.compile(text);
var s = t.render();
is(s, 'Default title');
});
test("Default content renders variables", function() {
var text = '{{$foo}}default {{bar}} content{{/foo}}';
var t = Hogan.compile(text);
var s = t.render({bar: 'baz'});
is(s, 'default baz content', 'default content renders variables');
});
test("Default content renders triple stache variables", function() {
var text = '{{$foo}}default {{{bar}}} content{{/foo}}';
var t = Hogan.compile(text);
var s = t.render({bar: '<baz>'});
is(s, 'default <baz> content', 'default content renders triple stache variables');
});
test("Default content renders sections", function() {
var text = '{{$foo}}default {{#bar}}{{baz}}{{/bar}} content{{/foo}}';
var t = Hogan.compile(text);
var s = t.render({bar: {baz: 'qux'}});
is(s, 'default qux content', 'sections work');
});
test("Default content renders negative sections", function() {
var text = '{{$foo}}default{{^bar}}{{baz}}{{/bar}} content{{/foo}}';
var t = Hogan.compile(text);
var s = t.render({foo: {baz: 'qux'}});
is(s, 'default content', 'negative sections work');
});
test("Mustache injection in default content", function() {
var text = '{{$foo}}default {{#bar}}{{baz}}{{/bar}} content{{/foo}}';
var t = Hogan.compile(text);
var s = t.render({bar: {baz: '{{qux}}'}});
is(s, 'default {{qux}} content', 'mustache tags are not injected.');
});
test("Default content rendered inside included templates", function(){
var include = Hogan.compile("{{$foo}}default content{{/foo}}");
var template = "{{<include}}{{/include}}";
var t = Hogan.compile(template);
var s = t.render({},{'include':include});
is(s, 'default content', 'default content from included template');
});
test("Overridden content", function() {
var text = '{{<super}}{{$title}}sub template title{{/title}}{{/super}}';
var super_template = '...{{$title}}Default title{{/title}}...';
var t = Hogan.compile(text);
var s = t.render({}, {"super": super_template});
is(s, '...sub template title...', 'renders overridden content');
});
test("Overridden partial", function() {
var partial = "{{$stuff}}...{{/stuff}}";
var template = "test {{<partial}}{{$stuff}}override{{/stuff}}{{/partial}}";
var t = Hogan.compile(template);
var s = t.render({}, {"partial": partial});
is(s, 'test override');
});
test("Two overridden partials with different content", function() {
var partial = "|{{$stuff}}...{{/stuff}}{{$default}} default{{/default}}|";
var template = "test {{<partial}}{{$stuff}}override1{{/stuff}}{{/partial}} " +
"{{<partial}}{{$stuff}}override2{{/stuff}}{{/partial}}";
var t = Hogan.compile(template);
var s = t.render({}, {"partial": partial});
is(s, 'test |override1 default| |override2 default|');
});
test("Override partial with newlines", function() {
var partial = "{{$ballmer}}peaking{{/ballmer}}";
var template = "{{<partial}}{{$ballmer}}\npeaked\n\n:(\n{{/ballmer}}{{/partial}}";
var t = Hogan.compile(template);
var s = t.render({}, {"partial": partial});
is(s, "peaked\n\n:(\n");
var compiledAsString = Hogan.compile(template, {asString: true});
eval('var fromString = new Hogan.Template(' + compiledAsString + ', template, Hogan);');
is(s, fromString.render({}, {"partial": partial}));
});
test("Inherit indentation when overriding a partial", function() {
var partial = "stop:\n {{$nineties}}collaborate and listen{{/nineties}}";
var template = "{{<partial}}{{$nineties}}hammer time{{/nineties}}{{/partial}}";
var t = Hogan.compile(template);
var s = t.render({}, {"partial": partial});
is(s, "stop:\n hammer time");
});
test("Override one substitution but not the other", function() {
var partial = Hogan.compile("{{$stuff}}default one{{/stuff}}, {{$stuff2}}default two{{/stuff2}}");
var template = "{{<partial}}{{$stuff2}}override two{{/stuff2}}{{/partial}}";
var t = Hogan.compile(template);
var s = t.render({}, {"partial": partial});
is(s, 'default one, override two', 'overrides only one substitution');
var partial2 = Hogan.compile("{{$stuff}}new default one{{/stuff}}, {{$stuff2}}new default two{{/stuff2}}");
var s = t.render({}, {"partial": partial2});
is(s, 'new default one, override two', 'picks up changes to the partial dictionary');
});
test("Super templates behave identically to partials when called with no parameters", function() {
var partial = Hogan.compile("{{$foo}}default content{{/foo}}");
var t = Hogan.compile("{{>include}}|{{<include}}{{/include}}");
var s = t.render({}, {include:partial});
is(s, "default content|default content", "should be the partial rendered twice");
});
test("Recursion in inherited templates", function() {
var include = Hogan.compile("{{$foo}}default content{{/foo}} {{$bar}}{{<include2}}{{/include2}}{{/bar}}");
var include2 = Hogan.compile("{{$foo}}include2 default content{{/foo}} {{<include}}{{$bar}}don't recurse{{/bar}}{{/include}}");
var t = Hogan.compile("{{<include}}{{$foo}}override{{/foo}}{{/include}}");
var s = t.render({}, {include: include, include2: include2});
is(s, "override override override don't recurse", "matches expected recursive output");
});
test("Cache contains old partials instances", function() {
var tests = [{
template: "{{<parent}}{{$a}}c{{/a}}{{/parent}}",
partials: {
parent: "{{<grandParent}}{{$a}}p{{/a}}{{/grandParent}}",
grandParent: "{{$a}}g{{/a}}"
},
expected: "c"
}, {
template: "{{<parent}}{{/parent}}",
partials:{
parent: "{{<grandParent}}{{$a}}p{{/a}}{{/grandParent}}",
grandParent: "{{$a}}g{{/a}}"
},
expected: "p"
}];
tests.forEach(function(test) {
var partials = {};
for (var i in test.partials) {
partials[i] = Hogan.compile(test.partials[i]);
}
var output = Hogan.compile(test.template).render({}, partials);
is(output, test.expected);
});
});
test("Doesn't parse templates that have non-$ tags inside super template tags", function() {
var msg = "";
try {
Hogan.compile("{{<foo}}{{busted}}{{/foo}}");
} catch (e) {
msg = e.message;
}
is(msg, "Illegal content in < super tag.");
})
test("Allows text inside a super tag, but ignores it", function() {
var partial = Hogan.compile("{{$foo}}default content{{/foo}}");
var t = Hogan.compile("{{<include}} asdfasd asdfasdfasdf {{/include}}");
var s = t.render({}, {include: partial});
is(s, "default content", "should render without the text");
});
test("Ignores text inside super templates, but does parse $ tags", function() {
var partial = Hogan.compile("{{$foo}}default content{{/foo}}");
var t = Hogan.compile("{{<include}} asdfasd {{$foo}}hmm{{/foo}} asdfasdfasdf {{/include}}");
var s = t.render({}, {include: partial});
is(s, "hmm", "should render without the text");
});
test("Issue #62: partial references inside substitutions should work", function () {
var parent = "This is a parent template. {{$content}}Child content goes here{{/content}} Ending the parent template.";
var main = "Main template start. {{< parent}}{{$content}}This content includes a partial: {{> include}}{{/content}}{{/ parent}} Main template end.";
var include = "INCLUDED CONTENT!";
var templates = {
parent: Hogan.compile(parent),
main: Hogan.compile(main),
include: Hogan.compile(include)
};
is(templates.main.partials.include0, undefined, "partial reference from subustitution is not defined.");
is(templates.main.render({}, templates), "Main template start. This is a parent template. This content includes a partial: INCLUDED CONTENT! Ending the parent template. Main template end.", "Included content works inside substitution.");
eval('var parentFromString = new Hogan.Template(' + Hogan.compile(parent, {asString: true}) + ');');
eval('var mainFromString = new Hogan.Template(' + Hogan.compile(main, {asString: true}) + ');');
eval('var includeFromString = new Hogan.Template(' + Hogan.compile(include, {asString: true}) + ');');
// now test compiling these as a string
var templatesAsString = {
parent: parentFromString,