-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspacebars-compiler.js
1256 lines (1111 loc) · 42.4 KB
/
spacebars-compiler.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
module.exports = function(Meteor) {
var _ = Meteor.underscore;
var HTML = Meteor.HTML;
var HTMLTools = Meteor.HTMLTools;
var BlazeTools = Meteor.BlazeTools;
var Package = {};
var SpacebarsCompiler;
var TemplateTag;
var ReactComponentSiblingForbidder;
SpacebarsCompiler = {};
// A TemplateTag is the result of parsing a single `{{...}}` tag.
//
// The `.type` of a TemplateTag is one of:
//
// - `"DOUBLE"` - `{{foo}}`
// - `"TRIPLE"` - `{{{foo}}}`
// - `"EXPR"` - `(foo)`
// - `"COMMENT"` - `{{! foo}}`
// - `"BLOCKCOMMENT" - `{{!-- foo--}}`
// - `"INCLUSION"` - `{{> foo}}`
// - `"BLOCKOPEN"` - `{{#foo}}`
// - `"BLOCKCLOSE"` - `{{/foo}}`
// - `"ELSE"` - `{{else}}`
// - `"ESCAPE"` - `{{|`, `{{{|`, `{{{{|` and so on
//
// Besides `type`, the mandatory properties of a TemplateTag are:
//
// - `path` - An array of one or more strings. The path of `{{foo.bar}}`
// is `["foo", "bar"]`. Applies to DOUBLE, TRIPLE, INCLUSION, BLOCKOPEN,
// and BLOCKCLOSE.
//
// - `args` - An array of zero or more argument specs. An argument spec
// is a two or three element array, consisting of a type, value, and
// optional keyword name. For example, the `args` of `{{foo "bar" x=3}}`
// are `[["STRING", "bar"], ["NUMBER", 3, "x"]]`. Applies to DOUBLE,
// TRIPLE, INCLUSION, and BLOCKOPEN.
//
// - `value` - A string of the comment's text. Applies to COMMENT and
// BLOCKCOMMENT.
//
// These additional are typically set during parsing:
//
// - `position` - The HTMLTools.TEMPLATE_TAG_POSITION specifying at what sort
// of site the TemplateTag was encountered (e.g. at element level or as
// part of an attribute value). Its absence implies
// TEMPLATE_TAG_POSITION.ELEMENT.
//
// - `content` and `elseContent` - When a BLOCKOPEN tag's contents are
// parsed, they are put here. `elseContent` will only be present if
// an `{{else}}` was found.
var TEMPLATE_TAG_POSITION = HTMLTools.TEMPLATE_TAG_POSITION;
TemplateTag = SpacebarsCompiler.TemplateTag = function () {
HTMLTools.TemplateTag.apply(this, arguments);
};
TemplateTag.prototype = new HTMLTools.TemplateTag;
TemplateTag.prototype.constructorName = 'SpacebarsCompiler.TemplateTag';
var makeStacheTagStartRegex = function (r) {
return new RegExp(r.source + /(?![{>!#/])/.source,
r.ignoreCase ? 'i' : '');
};
// "starts" regexes are used to see what type of template
// tag the parser is looking at. They must match a non-empty
// result, but not the interesting part of the tag.
var starts = {
ESCAPE: /^\{\{(?=\{*\|)/,
ELSE: makeStacheTagStartRegex(/^\{\{\s*else(?=[\s}])/i),
DOUBLE: makeStacheTagStartRegex(/^\{\{\s*(?!\s)/),
TRIPLE: makeStacheTagStartRegex(/^\{\{\{\s*(?!\s)/),
BLOCKCOMMENT: makeStacheTagStartRegex(/^\{\{\s*!--/),
COMMENT: makeStacheTagStartRegex(/^\{\{\s*!/),
INCLUSION: makeStacheTagStartRegex(/^\{\{\s*>\s*(?!\s)/),
BLOCKOPEN: makeStacheTagStartRegex(/^\{\{\s*#\s*(?!\s)/),
BLOCKCLOSE: makeStacheTagStartRegex(/^\{\{\s*\/\s*(?!\s)/)
};
var ends = {
DOUBLE: /^\s*\}\}/,
TRIPLE: /^\s*\}\}\}/,
EXPR: /^\s*\)/
};
var endsString = {
DOUBLE: '}}',
TRIPLE: '}}}',
EXPR: ')'
};
// Parse a tag from the provided scanner or string. If the input
// doesn't start with `{{`, returns null. Otherwise, either succeeds
// and returns a SpacebarsCompiler.TemplateTag, or throws an error (using
// `scanner.fatal` if a scanner is provided).
TemplateTag.parse = function (scannerOrString) {
var scanner = scannerOrString;
if (typeof scanner === 'string')
scanner = new HTMLTools.Scanner(scannerOrString);
if (! (scanner.peek() === '{' &&
(scanner.rest()).slice(0, 2) === '{{'))
return null;
var run = function (regex) {
// regex is assumed to start with `^`
var result = regex.exec(scanner.rest());
if (! result)
return null;
var ret = result[0];
scanner.pos += ret.length;
return ret;
};
var advance = function (amount) {
scanner.pos += amount;
};
var scanIdentifier = function (isFirstInPath) {
var id = BlazeTools.parseExtendedIdentifierName(scanner);
if (! id) {
expected('IDENTIFIER');
}
if (isFirstInPath &&
(id === 'null' || id === 'true' || id === 'false'))
scanner.fatal("Can't use null, true, or false, as an identifier at start of path");
return id;
};
var scanPath = function () {
var segments = [];
// handle initial `.`, `..`, `./`, `../`, `../..`, `../../`, etc
var dots;
if ((dots = run(/^[\.\/]+/))) {
var ancestorStr = '.'; // eg `../../..` maps to `....`
var endsWithSlash = /\/$/.test(dots);
if (endsWithSlash)
dots = dots.slice(0, -1);
_.each(dots.split('/'), function(dotClause, index) {
if (index === 0) {
if (dotClause !== '.' && dotClause !== '..')
expected("`.`, `..`, `./` or `../`");
} else {
if (dotClause !== '..')
expected("`..` or `../`");
}
if (dotClause === '..')
ancestorStr += '.';
});
segments.push(ancestorStr);
if (!endsWithSlash)
return segments;
}
while (true) {
// scan a path segment
if (run(/^\[/)) {
var seg = run(/^[\s\S]*?\]/);
if (! seg)
error("Unterminated path segment");
seg = seg.slice(0, -1);
if (! seg && ! segments.length)
error("Path can't start with empty string");
segments.push(seg);
} else {
var id = scanIdentifier(! segments.length);
if (id === 'this') {
if (! segments.length) {
// initial `this`
segments.push('.');
} else {
error("Can only use `this` at the beginning of a path.\nInstead of `foo.this` or `../this`, just write `foo` or `..`.");
}
} else {
segments.push(id);
}
}
var sep = run(/^[\.\/]/);
if (! sep)
break;
}
return segments;
};
// scan the keyword portion of a keyword argument
// (the "foo" portion in "foo=bar").
// Result is either the keyword matched, or null
// if we're not at a keyword argument position.
var scanArgKeyword = function () {
var match = /^([^\{\}\(\)\>#=\s"'\[\]]+)\s*=\s*/.exec(scanner.rest());
if (match) {
scanner.pos += match[0].length;
return match[1];
} else {
return null;
}
};
// scan an argument; succeeds or errors.
// Result is an array of two or three items:
// type , value, and (indicating a keyword argument)
// keyword name.
var scanArg = function () {
var keyword = scanArgKeyword(); // null if not parsing a kwarg
var value = scanArgValue();
return keyword ? value.concat(keyword) : value;
};
// scan an argument value (for keyword or positional arguments);
// succeeds or errors. Result is an array of type, value.
var scanArgValue = function () {
var startPos = scanner.pos;
var result;
if ((result = BlazeTools.parseNumber(scanner))) {
return ['NUMBER', result.value];
} else if ((result = BlazeTools.parseStringLiteral(scanner))) {
return ['STRING', result.value];
} else if (/^[\.\[]/.test(scanner.peek())) {
return ['PATH', scanPath()];
} else if (run(/^\(/)) {
return ['EXPR', scanExpr('EXPR')];
} else if ((result = BlazeTools.parseExtendedIdentifierName(scanner))) {
var id = result;
if (id === 'null') {
return ['NULL', null];
} else if (id === 'true' || id === 'false') {
return ['BOOLEAN', id === 'true'];
} else {
scanner.pos = startPos; // unconsume `id`
return ['PATH', scanPath()];
}
} else {
expected('identifier, number, string, boolean, null, or a sub expression enclosed in "(", ")"');
}
};
var scanExpr = function (type) {
var endType = type;
if (type === 'INCLUSION' || type === 'BLOCKOPEN')
endType = 'DOUBLE';
var tag = new TemplateTag;
tag.type = type;
tag.path = scanPath();
tag.args = [];
var foundKwArg = false;
while (true) {
run(/^\s*/);
if (run(ends[endType]))
break;
else if (/^[})]/.test(scanner.peek())) {
expected('`' + endsString[endType] + '`');
}
var newArg = scanArg();
if (newArg.length === 3) {
foundKwArg = true;
} else {
if (foundKwArg)
error("Can't have a non-keyword argument after a keyword argument");
}
tag.args.push(newArg);
// expect a whitespace or a closing ')' or '}'
if (run(/^(?=[\s})])/) !== '')
expected('space');
}
return tag;
};
var type;
var error = function (msg) {
scanner.fatal(msg);
};
var expected = function (what) {
error('Expected ' + what);
};
// must do ESCAPE first, immediately followed by ELSE
// order of others doesn't matter
if (run(starts.ESCAPE)) type = 'ESCAPE';
else if (run(starts.ELSE)) type = 'ELSE';
else if (run(starts.DOUBLE)) type = 'DOUBLE';
else if (run(starts.TRIPLE)) type = 'TRIPLE';
else if (run(starts.BLOCKCOMMENT)) type = 'BLOCKCOMMENT';
else if (run(starts.COMMENT)) type = 'COMMENT';
else if (run(starts.INCLUSION)) type = 'INCLUSION';
else if (run(starts.BLOCKOPEN)) type = 'BLOCKOPEN';
else if (run(starts.BLOCKCLOSE)) type = 'BLOCKCLOSE';
else
error('Unknown stache tag');
var tag = new TemplateTag;
tag.type = type;
if (type === 'BLOCKCOMMENT') {
var result = run(/^[\s\S]*?--\s*?\}\}/);
if (! result)
error("Unclosed block comment");
tag.value = result.slice(0, result.lastIndexOf('--'));
} else if (type === 'COMMENT') {
var result = run(/^[\s\S]*?\}\}/);
if (! result)
error("Unclosed comment");
tag.value = result.slice(0, -2);
} else if (type === 'BLOCKCLOSE') {
tag.path = scanPath();
if (! run(ends.DOUBLE))
expected('`}}`');
} else if (type === 'ELSE') {
if (! run(ends.DOUBLE))
expected('`}}`');
} else if (type === 'ESCAPE') {
var result = run(/^\{*\|/);
tag.value = '{{' + result.slice(0, -1);
} else {
// DOUBLE, TRIPLE, BLOCKOPEN, INCLUSION
tag = scanExpr(type);
}
return tag;
};
// Returns a SpacebarsCompiler.TemplateTag parsed from `scanner`, leaving scanner
// at its original position.
//
// An error will still be thrown if there is not a valid template tag at
// the current position.
TemplateTag.peek = function (scanner) {
var startPos = scanner.pos;
var result = TemplateTag.parse(scanner);
scanner.pos = startPos;
return result;
};
// Like `TemplateTag.parse`, but in the case of blocks, parse the complete
// `{{#foo}}...{{/foo}}` with `content` and possible `elseContent`, rather
// than just the BLOCKOPEN tag.
//
// In addition:
//
// - Throws an error if `{{else}}` or `{{/foo}}` tag is encountered.
//
// - Returns `null` for a COMMENT. (This case is distinguishable from
// parsing no tag by the fact that the scanner is advanced.)
//
// - Takes an HTMLTools.TEMPLATE_TAG_POSITION `position` and sets it as the
// TemplateTag's `.position` property.
//
// - Validates the tag's well-formedness and legality at in its position.
TemplateTag.parseCompleteTag = function (scannerOrString, position) {
var scanner = scannerOrString;
if (typeof scanner === 'string')
scanner = new HTMLTools.Scanner(scannerOrString);
var startPos = scanner.pos; // for error messages
var result = TemplateTag.parse(scannerOrString);
if (! result)
return result;
if (result.type === 'BLOCKCOMMENT')
return null;
if (result.type === 'COMMENT')
return null;
if (result.type === 'ELSE')
scanner.fatal("Unexpected {{else}}");
if (result.type === 'BLOCKCLOSE')
scanner.fatal("Unexpected closing template tag");
position = (position || TEMPLATE_TAG_POSITION.ELEMENT);
if (position !== TEMPLATE_TAG_POSITION.ELEMENT)
result.position = position;
if (result.type === 'BLOCKOPEN') {
// parse block contents
// Construct a string version of `.path` for comparing start and
// end tags. For example, `foo/[0]` was parsed into `["foo", "0"]`
// and now becomes `foo,0`. This form may also show up in error
// messages.
var blockName = result.path.join(',');
var textMode = null;
if (blockName === 'markdown' ||
position === TEMPLATE_TAG_POSITION.IN_RAWTEXT) {
textMode = HTML.TEXTMODE.STRING;
} else if (position === TEMPLATE_TAG_POSITION.IN_RCDATA ||
position === TEMPLATE_TAG_POSITION.IN_ATTRIBUTE) {
textMode = HTML.TEXTMODE.RCDATA;
}
var parserOptions = {
getTemplateTag: TemplateTag.parseCompleteTag,
shouldStop: isAtBlockCloseOrElse,
textMode: textMode
};
result.content = HTMLTools.parseFragment(scanner, parserOptions);
if (scanner.rest().slice(0, 2) !== '{{')
scanner.fatal("Expected {{else}} or block close for " + blockName);
var lastPos = scanner.pos; // save for error messages
var tmplTag = TemplateTag.parse(scanner); // {{else}} or {{/foo}}
if (tmplTag.type === 'ELSE') {
// parse {{else}} and content up to close tag
result.elseContent = HTMLTools.parseFragment(scanner, parserOptions);
if (scanner.rest().slice(0, 2) !== '{{')
scanner.fatal("Expected block close for " + blockName);
lastPos = scanner.pos;
tmplTag = TemplateTag.parse(scanner);
}
if (tmplTag.type === 'BLOCKCLOSE') {
var blockName2 = tmplTag.path.join(',');
if (blockName !== blockName2) {
scanner.pos = lastPos;
scanner.fatal('Expected tag to close ' + blockName + ', found ' +
blockName2);
}
} else {
scanner.pos = lastPos;
scanner.fatal('Expected tag to close ' + blockName + ', found ' +
tmplTag.type);
}
}
var finalPos = scanner.pos;
scanner.pos = startPos;
validateTag(result, scanner);
scanner.pos = finalPos;
return result;
};
var isAtBlockCloseOrElse = function (scanner) {
// Detect `{{else}}` or `{{/foo}}`.
//
// We do as much work ourselves before deferring to `TemplateTag.peek`,
// for efficiency (we're called for every input token) and to be
// less obtrusive, because `TemplateTag.peek` will throw an error if it
// sees `{{` followed by a malformed tag.
var rest, type;
return (scanner.peek() === '{' &&
(rest = scanner.rest()).slice(0, 2) === '{{' &&
/^\{\{\s*(\/|else\b)/.test(rest) &&
(type = TemplateTag.peek(scanner).type) &&
(type === 'BLOCKCLOSE' || type === 'ELSE'));
};
// Validate that `templateTag` is correctly formed and legal for its
// HTML position. Use `scanner` to report errors. On success, does
// nothing.
var validateTag = function (ttag, scanner) {
if (ttag.type === 'INCLUSION' || ttag.type === 'BLOCKOPEN') {
var args = ttag.args;
if (ttag.path[0] === 'each' && args[1] && args[1][0] === 'PATH' &&
args[1][1][0] === 'in') {
// For slightly better error messages, we detect the each-in case
// here in order not to complain if the user writes `{{#each 3 in x}}`
// that "3 is not a function"
} else {
if (args.length > 1 && args[0].length === 2 && args[0][0] !== 'PATH') {
// we have a positional argument that is not a PATH followed by
// other arguments
scanner.fatal("First argument must be a function, to be called on " +
"the rest of the arguments; found " + args[0][0]);
}
}
}
var position = ttag.position || TEMPLATE_TAG_POSITION.ELEMENT;
if (position === TEMPLATE_TAG_POSITION.IN_ATTRIBUTE) {
if (ttag.type === 'DOUBLE' || ttag.type === 'ESCAPE') {
return;
} else if (ttag.type === 'BLOCKOPEN') {
var path = ttag.path;
var path0 = path[0];
if (! (path.length === 1 && (path0 === 'if' ||
path0 === 'unless' ||
path0 === 'with' ||
path0 === 'each'))) {
scanner.fatal("Custom block helpers are not allowed in an HTML attribute, only built-in ones like #each and #if");
}
} else {
scanner.fatal(ttag.type + " template tag is not allowed in an HTML attribute");
}
} else if (position === TEMPLATE_TAG_POSITION.IN_START_TAG) {
if (! (ttag.type === 'DOUBLE')) {
scanner.fatal("Reactive HTML attributes must either have a constant name or consist of a single {{helper}} providing a dictionary of names and values. A template tag of type " + ttag.type + " is not allowed here.");
}
if (scanner.peek() === '=') {
scanner.fatal("Template tags are not allowed in attribute names, only in attribute values or in the form of a single {{helper}} that evaluates to a dictionary of name=value pairs.");
}
}
};
// Optimize parts of an HTMLjs tree into raw HTML strings when they don't
// contain template tags.
var constant = function (value) {
return function () { return value; };
};
var OPTIMIZABLE = {
NONE: 0,
PARTS: 1,
FULL: 2
};
// We can only turn content into an HTML string if it contains no template
// tags and no "tricky" HTML tags. If we can optimize the entire content
// into a string, we return OPTIMIZABLE.FULL. If the we are given an
// unoptimizable node, we return OPTIMIZABLE.NONE. If we are given a tree
// that contains an unoptimizable node somewhere, we return OPTIMIZABLE.PARTS.
//
// For example, we always create SVG elements programmatically, since SVG
// doesn't have innerHTML. If we are given an SVG element, we return NONE.
// However, if we are given a big tree that contains SVG somewhere, we
// return PARTS so that the optimizer can descend into the tree and optimize
// other parts of it.
var CanOptimizeVisitor = HTML.Visitor.extend();
CanOptimizeVisitor.def({
visitNull: constant(OPTIMIZABLE.FULL),
visitPrimitive: constant(OPTIMIZABLE.FULL),
visitComment: constant(OPTIMIZABLE.FULL),
visitCharRef: constant(OPTIMIZABLE.FULL),
visitRaw: constant(OPTIMIZABLE.FULL),
visitObject: constant(OPTIMIZABLE.NONE),
visitFunction: constant(OPTIMIZABLE.NONE),
visitArray: function (x) {
for (var i = 0; i < x.length; i++)
if (this.visit(x[i]) !== OPTIMIZABLE.FULL)
return OPTIMIZABLE.PARTS;
return OPTIMIZABLE.FULL;
},
visitTag: function (tag) {
var tagName = tag.tagName;
if (tagName === 'textarea') {
// optimizing into a TEXTAREA's RCDATA would require being a little
// more clever.
return OPTIMIZABLE.NONE;
} else if (tagName === 'script') {
// script tags don't work when rendered from strings
return OPTIMIZABLE.NONE;
} else if (! (HTML.isKnownElement(tagName) &&
! HTML.isKnownSVGElement(tagName))) {
// foreign elements like SVG can't be stringified for innerHTML.
return OPTIMIZABLE.NONE;
} else if (tagName === 'table') {
// Avoid ever producing HTML containing `<table><tr>...`, because the
// browser will insert a TBODY. If we just `createElement("table")` and
// `createElement("tr")`, on the other hand, no TBODY is necessary
// (assuming IE 8+).
return OPTIMIZABLE.NONE;
}
var children = tag.children;
for (var i = 0; i < children.length; i++)
if (this.visit(children[i]) !== OPTIMIZABLE.FULL)
return OPTIMIZABLE.PARTS;
if (this.visitAttributes(tag.attrs) !== OPTIMIZABLE.FULL)
return OPTIMIZABLE.PARTS;
return OPTIMIZABLE.FULL;
},
visitAttributes: function (attrs) {
if (attrs) {
var isArray = HTML.isArray(attrs);
for (var i = 0; i < (isArray ? attrs.length : 1); i++) {
var a = (isArray ? attrs[i] : attrs);
if ((typeof a !== 'object') || (a instanceof HTMLTools.TemplateTag))
return OPTIMIZABLE.PARTS;
for (var k in a)
if (this.visit(a[k]) !== OPTIMIZABLE.FULL)
return OPTIMIZABLE.PARTS;
}
}
return OPTIMIZABLE.FULL;
}
});
var getOptimizability = function (content) {
return (new CanOptimizeVisitor).visit(content);
};
var toRaw = function (x) {
return HTML.Raw(HTML.toHTML(x));
};
var TreeTransformer = HTML.TransformingVisitor.extend();
TreeTransformer.def({
visitAttributes: function (attrs/*, ...*/) {
// pass template tags through by default
if (attrs instanceof HTMLTools.TemplateTag)
return attrs;
return HTML.TransformingVisitor.prototype.visitAttributes.apply(
this, arguments);
}
});
// Replace parts of the HTMLjs tree that have no template tags (or
// tricky HTML tags) with HTML.Raw objects containing raw HTML.
var OptimizingVisitor = TreeTransformer.extend();
OptimizingVisitor.def({
visitNull: toRaw,
visitPrimitive: toRaw,
visitComment: toRaw,
visitCharRef: toRaw,
visitArray: function (array) {
var optimizability = getOptimizability(array);
if (optimizability === OPTIMIZABLE.FULL) {
return toRaw(array);
} else if (optimizability === OPTIMIZABLE.PARTS) {
return TreeTransformer.prototype.visitArray.call(this, array);
} else {
return array;
}
},
visitTag: function (tag) {
var optimizability = getOptimizability(tag);
if (optimizability === OPTIMIZABLE.FULL) {
return toRaw(tag);
} else if (optimizability === OPTIMIZABLE.PARTS) {
return TreeTransformer.prototype.visitTag.call(this, tag);
} else {
return tag;
}
},
visitChildren: function (children) {
// don't optimize the children array into a Raw object!
return TreeTransformer.prototype.visitArray.call(this, children);
},
visitAttributes: function (attrs) {
return attrs;
}
});
// Combine consecutive HTML.Raws. Remove empty ones.
var RawCompactingVisitor = TreeTransformer.extend();
RawCompactingVisitor.def({
visitArray: function (array) {
var result = [];
for (var i = 0; i < array.length; i++) {
var item = array[i];
if ((item instanceof HTML.Raw) &&
((! item.value) ||
(result.length &&
(result[result.length - 1] instanceof HTML.Raw)))) {
// two cases: item is an empty Raw, or previous item is
// a Raw as well. In the latter case, replace the previous
// Raw with a longer one that includes the new Raw.
if (item.value) {
result[result.length - 1] = HTML.Raw(
result[result.length - 1].value + item.value);
}
} else {
result.push(item);
}
}
return result;
}
});
// Replace pointless Raws like `HTMl.Raw('foo')` that contain no special
// characters with simple strings.
var RawReplacingVisitor = TreeTransformer.extend();
RawReplacingVisitor.def({
visitRaw: function (raw) {
var html = raw.value;
if (html.indexOf('&') < 0 && html.indexOf('<') < 0) {
return html;
} else {
return raw;
}
}
});
SpacebarsCompiler.optimize = function (tree) {
tree = (new OptimizingVisitor).visit(tree);
tree = (new RawCompactingVisitor).visit(tree);
tree = (new RawReplacingVisitor).visit(tree);
return tree;
};
// A visitor to ensure that React components included via the `{{>
// React}}` template defined in the react-template-helper package are
// the only child in their parent component. Otherwise `React.render`
// would eliminate all of their sibling nodes.
//
// It's a little strange that this logic is in spacebars-compiler if
// it's only relevant to a specific package but there's no way to have
// a package hook into a build plugin.
ReactComponentSiblingForbidder = HTML.Visitor.extend();
ReactComponentSiblingForbidder.def({
visitArray: function (array, parentTag) {
for (var i = 0; i < array.length; i++) {
this.visit(array[i], parentTag);
}
},
visitObject: function (obj, parentTag) {
if (obj.type === "INCLUSION" && obj.path.length === 1 && obj.path[0] === "React") {
if (!parentTag) {
throw new Error(
"{{> React}} must be used in a container element"
+ (this.sourceName ? (" in " + this.sourceName) : "")
+ ". Learn more at https://github.com/meteor/meteor/wiki/React-components-must-be-the-only-thing-in-their-wrapper-element");
}
var numSiblings = 0;
for (var i = 0; i < parentTag.children.length; i++) {
var child = parentTag.children[i];
if (child !== obj && !(typeof child === "string" && child.match(/^\s*$/))) {
numSiblings++;
}
}
if (numSiblings > 0) {
throw new Error(
"{{> React}} must be used as the only child in a container element"
+ (this.sourceName ? (" in " + this.sourceName) : "")
+ ". Learn more at https://github.com/meteor/meteor/wiki/React-components-must-be-the-only-thing-in-their-wrapper-element");
}
}
},
visitTag: function (tag) {
this.visitArray(tag.children, tag /*parentTag*/);
}
});
// ============================================================
// Code-generation of template tags
// The `CodeGen` class currently has no instance state, but in theory
// it could be useful to track per-function state, like whether we
// need to emit `var self = this` or not.
var CodeGen = SpacebarsCompiler.CodeGen = function () {};
var builtInBlockHelpers = SpacebarsCompiler._builtInBlockHelpers = {
'if': 'Blaze.If',
'unless': 'Blaze.Unless',
'with': 'Spacebars.With',
'each': 'Blaze.Each',
'let': 'Blaze.Let'
};
// Mapping of "macros" which, when preceded by `Template.`, expand
// to special code rather than following the lookup rules for dotted
// symbols.
var builtInTemplateMacros = {
// `view` is a local variable defined in the generated render
// function for the template in which `Template.contentBlock` or
// `Template.elseBlock` is invoked.
'contentBlock': 'view.templateContentBlock',
'elseBlock': 'view.templateElseBlock',
// Confusingly, this makes `{{> Template.dynamic}}` an alias
// for `{{> __dynamic}}`, where "__dynamic" is the template that
// implements the dynamic template feature.
'dynamic': 'Template.__dynamic',
'subscriptionsReady': 'view.templateInstance().subscriptionsReady()'
};
var additionalReservedNames = ["body", "toString", "instance", "constructor",
"toString", "toLocaleString", "valueOf", "hasOwnProperty", "isPrototypeOf",
"propertyIsEnumerable", "__defineGetter__", "__lookupGetter__",
"__defineSetter__", "__lookupSetter__", "__proto__", "dynamic",
"registerHelper", "currentData", "parentData"];
// A "reserved name" can't be used as a <template> name. This
// function is used by the template file scanner.
//
// Note that the runtime imposes additional restrictions, for example
// banning the name "body" and names of built-in object properties
// like "toString".
SpacebarsCompiler.isReservedName = function (name) {
return builtInBlockHelpers.hasOwnProperty(name) ||
builtInTemplateMacros.hasOwnProperty(name) ||
_.indexOf(additionalReservedNames, name) > -1;
};
var makeObjectLiteral = function (obj) {
var parts = [];
for (var k in obj)
parts.push(BlazeTools.toObjectLiteralKey(k) + ': ' + obj[k]);
return '{' + parts.join(', ') + '}';
};
_.extend(CodeGen.prototype, {
codeGenTemplateTag: function (tag) {
var self = this;
if (tag.position === HTMLTools.TEMPLATE_TAG_POSITION.IN_START_TAG) {
// Special dynamic attributes: `<div {{attrs}}>...`
// only `tag.type === 'DOUBLE'` allowed (by earlier validation)
return BlazeTools.EmitCode('function () { return ' +
self.codeGenMustache(tag.path, tag.args, 'attrMustache')
+ '; }');
} else {
if (tag.type === 'DOUBLE' || tag.type === 'TRIPLE') {
var code = self.codeGenMustache(tag.path, tag.args);
if (tag.type === 'TRIPLE') {
code = 'Spacebars.makeRaw(' + code + ')';
}
if (tag.position !== HTMLTools.TEMPLATE_TAG_POSITION.IN_ATTRIBUTE) {
// Reactive attributes are already wrapped in a function,
// and there's no fine-grained reactivity.
// Anywhere else, we need to create a View.
code = 'Blaze.View(' +
BlazeTools.toJSLiteral('lookup:' + tag.path.join('.')) + ', ' +
'function () { return ' + code + '; })';
}
return BlazeTools.EmitCode(code);
} else if (tag.type === 'INCLUSION' || tag.type === 'BLOCKOPEN') {
var path = tag.path;
var args = tag.args;
if (tag.type === 'BLOCKOPEN' &&
builtInBlockHelpers.hasOwnProperty(path[0])) {
// if, unless, with, each.
//
// If someone tries to do `{{> if}}`, we don't
// get here, but an error is thrown when we try to codegen the path.
// Note: If we caught these errors earlier, while scanning, we'd be able to
// provide nice line numbers.
if (path.length > 1)
throw new Error("Unexpected dotted path beginning with " + path[0]);
if (! args.length)
throw new Error("#" + path[0] + " requires an argument");
var dataCode = null;
// #each has a special treatment as it features two different forms:
// - {{#each people}}
// - {{#each person in people}}
if (path[0] === 'each' && args.length >= 2 && args[1][0] === 'PATH' &&
args[1][1].length && args[1][1][0] === 'in') {
// minimum conditions are met for each-in. now validate this
// isn't some weird case.
var eachUsage = "Use either {{#each items}} or " +
"{{#each item in items}} form of #each.";
var inArg = args[1];
if (! (args.length >= 3 && inArg[1].length === 1)) {
// we don't have at least 3 space-separated parts after #each, or
// inArg doesn't look like ['PATH',['in']]
throw new Error("Malformed #each. " + eachUsage);
}
// split out the variable name and sequence arguments
var variableArg = args[0];
if (! (variableArg[0] === "PATH" && variableArg[1].length === 1 &&
variableArg[1][0].replace(/\./g, ''))) {
throw new Error("Bad variable name in #each");
}
var variable = variableArg[1][0];
dataCode = 'function () { return { _sequence: ' +
self.codeGenInclusionData(args.slice(2)) +
', _variable: ' + BlazeTools.toJSLiteral(variable) + ' }; }';
} else if (path[0] === 'let') {
var dataProps = {};
_.each(args, function (arg) {
if (arg.length !== 3) {
// not a keyword arg (x=y)
throw new Error("Incorrect form of #let");
}
var argKey = arg[2];
dataProps[argKey] =
'function () { return Spacebars.call(' +
self.codeGenArgValue(arg) + '); }';
});
dataCode = makeObjectLiteral(dataProps);
}
if (! dataCode) {
// `args` must exist (tag.args.length > 0)
dataCode = self.codeGenInclusionDataFunc(args) || 'null';
}
// `content` must exist
var contentBlock = (('content' in tag) ?
self.codeGenBlock(tag.content) : null);
// `elseContent` may not exist
var elseContentBlock = (('elseContent' in tag) ?
self.codeGenBlock(tag.elseContent) : null);
var callArgs = [dataCode, contentBlock];
if (elseContentBlock)
callArgs.push(elseContentBlock);
return BlazeTools.EmitCode(
builtInBlockHelpers[path[0]] + '(' + callArgs.join(', ') + ')');
} else {
var compCode = self.codeGenPath(path, {lookupTemplate: true});
if (path.length > 1) {
// capture reactivity
compCode = 'function () { return Spacebars.call(' + compCode +
'); }';
}
var dataCode = self.codeGenInclusionDataFunc(tag.args);
var content = (('content' in tag) ?
self.codeGenBlock(tag.content) : null);
var elseContent = (('elseContent' in tag) ?
self.codeGenBlock(tag.elseContent) : null);
var includeArgs = [compCode];
if (content) {
includeArgs.push(content);
if (elseContent)
includeArgs.push(elseContent);
}
var includeCode =
'Spacebars.include(' + includeArgs.join(', ') + ')';
// calling convention compat -- set the data context around the
// entire inclusion, so that if the name of the inclusion is
// a helper function, it gets the data context in `this`.
// This makes for a pretty confusing calling convention --
// In `{{#foo bar}}`, `foo` is evaluated in the context of `bar`
// -- but it's what we shipped for 0.8.0. The rationale is that
// `{{#foo bar}}` is sugar for `{{#with bar}}{{#foo}}...`.
if (dataCode) {
includeCode =
'Blaze._TemplateWith(' + dataCode + ', function () { return ' +
includeCode + '; })';
}
// XXX BACK COMPAT - UI is the old name, Template is the new
if ((path[0] === 'UI' || path[0] === 'Template') &&
(path[1] === 'contentBlock' || path[1] === 'elseBlock')) {
// Call contentBlock and elseBlock in the appropriate scope
includeCode = 'Blaze._InOuterTemplateScope(view, function () { return '
+ includeCode + '; })';
}
return BlazeTools.EmitCode(includeCode);
}
} else if (tag.type === 'ESCAPE') {
return tag.value;
} else {
// Can't get here; TemplateTag validation should catch any
// inappropriate tag types that might come out of the parser.
throw new Error("Unexpected template tag type: " + tag.type);
}
}
},
// `path` is an array of at least one string.
//
// If `path.length > 1`, the generated code may be reactive
// (i.e. it may invalidate the current computation).
//
// No code is generated to call the result if it's a function.
//
// Options:
//
// - lookupTemplate {Boolean} If true, generated code also looks in
// the list of templates. (After helpers, before data context).
// Used when generating code for `{{> foo}}` or `{{#foo}}`. Only
// used for non-dotted paths.
codeGenPath: function (path, opts) {
if (builtInBlockHelpers.hasOwnProperty(path[0]))
throw new Error("Can't use the built-in '" + path[0] + "' here");
// Let `{{#if Template.contentBlock}}` check whether this template was
// invoked via inclusion or as a block helper, in addition to supporting
// `{{> Template.contentBlock}}`.
// XXX BACK COMPAT - UI is the old name, Template is the new
if (path.length >= 2 &&
(path[0] === 'UI' || path[0] === 'Template')
&& builtInTemplateMacros.hasOwnProperty(path[1])) {
if (path.length > 2)
throw new Error("Unexpected dotted path beginning with " +
path[0] + '.' + path[1]);
return builtInTemplateMacros[path[1]];
}
var firstPathItem = BlazeTools.toJSLiteral(path[0]);
var lookupMethod = 'lookup';
if (opts && opts.lookupTemplate && path.length === 1)