index.js
22 KB
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
'use strict';
var doctypes = require('doctypes');
var makeError = require('pug-error');
var buildRuntime = require('pug-runtime/build');
var runtime = require('pug-runtime');
var compileAttrs = require('pug-attrs');
var selfClosing = require('void-elements');
var constantinople = require('constantinople');
var stringify = require('js-stringify');
var addWith = require('with');
// This is used to prevent pretty printing inside certain tags
var WHITE_SPACE_SENSITIVE_TAGS = {
pre: true,
textarea: true
};
var INTERNAL_VARIABLES = [
'pug',
'pug_mixins',
'pug_interp',
'pug_debug_filename',
'pug_debug_line',
'pug_debug_sources',
'pug_html'
];
module.exports = generateCode;
module.exports.CodeGenerator = Compiler;
function generateCode(ast, options) {
return (new Compiler(ast, options)).compile();
}
function isConstant(src) {
return constantinople(src, {pug: runtime, 'pug_interp': undefined});
}
function toConstant(src) {
return constantinople.toConstant(src, {pug: runtime, 'pug_interp': undefined});
}
/**
* Initialize `Compiler` with the given `node`.
*
* @param {Node} node
* @param {Object} options
* @api public
*/
function Compiler(node, options) {
this.options = options = options || {};
this.node = node;
this.bufferedConcatenationCount = 0;
this.hasCompiledDoctype = false;
this.hasCompiledTag = false;
this.pp = options.pretty || false;
if (this.pp && typeof this.pp !== 'string') {
this.pp = ' ';
}
this.debug = false !== options.compileDebug;
this.indents = 0;
this.parentIndents = 0;
this.terse = false;
this.mixins = {};
this.dynamicMixins = false;
this.eachCount = 0;
if (options.doctype) this.setDoctype(options.doctype);
this.runtimeFunctionsUsed = [];
this.inlineRuntimeFunctions = options.inlineRuntimeFunctions || false;
if (this.debug && this.inlineRuntimeFunctions) {
this.runtimeFunctionsUsed.push('rethrow');
}
};
/**
* Compiler prototype.
*/
Compiler.prototype = {
runtime: function (name) {
if (this.inlineRuntimeFunctions) {
this.runtimeFunctionsUsed.push(name);
return 'pug_' + name;
} else {
return 'pug.' + name;
}
},
error: function (message, code, node) {
var err = makeError(code, message, {
line: node.line,
column: node.column,
filename: node.filename,
});
throw err;
},
/**
* Compile parse tree to JavaScript.
*
* @api public
*/
compile: function(){
this.buf = [];
if (this.pp) this.buf.push("var pug_indent = [];");
this.lastBufferedIdx = -1;
this.visit(this.node);
if (!this.dynamicMixins) {
// if there are no dynamic mixins we can remove any un-used mixins
var mixinNames = Object.keys(this.mixins);
for (var i = 0; i < mixinNames.length; i++) {
var mixin = this.mixins[mixinNames[i]];
if (!mixin.used) {
for (var x = 0; x < mixin.instances.length; x++) {
for (var y = mixin.instances[x].start; y < mixin.instances[x].end; y++) {
this.buf[y] = '';
}
}
}
}
}
var js = this.buf.join('\n');
var globals = this.options.globals ? this.options.globals.concat(INTERNAL_VARIABLES) : INTERNAL_VARIABLES;
if (this.options.self) {
js = 'var self = locals || {};' + js;
} else {
js = addWith('locals || {}', js, globals.concat(this.runtimeFunctionsUsed.map(function (name) { return 'pug_' + name; })));
}
if (this.debug) {
if (this.options.includeSources) {
js = 'var pug_debug_sources = ' + stringify(this.options.includeSources) + ';\n' + js;
}
js = 'var pug_debug_filename, pug_debug_line;' +
'try {' +
js +
'} catch (err) {' +
(this.inlineRuntimeFunctions ? 'pug_rethrow' : 'pug.rethrow') +
'(err, pug_debug_filename, pug_debug_line' +
(
this.options.includeSources
? ', pug_debug_sources[pug_debug_filename]'
: ''
) +
');' +
'}';
}
return buildRuntime(this.runtimeFunctionsUsed) + 'function ' + (this.options.templateName || 'template') + '(locals) {var pug_html = "", pug_mixins = {}, pug_interp;' + js + ';return pug_html;}';
},
/**
* Sets the default doctype `name`. Sets terse mode to `true` when
* html 5 is used, causing self-closing tags to end with ">" vs "/>",
* and boolean attributes are not mirrored.
*
* @param {string} name
* @api public
*/
setDoctype: function(name){
this.doctype = doctypes[name.toLowerCase()] || '<!DOCTYPE ' + name + '>';
this.terse = this.doctype.toLowerCase() == '<!doctype html>';
this.xml = 0 == this.doctype.indexOf('<?xml');
},
/**
* Buffer the given `str` exactly as is or with interpolation
*
* @param {String} str
* @param {Boolean} interpolate
* @api public
*/
buffer: function (str) {
var self = this;
str = stringify(str);
str = str.substr(1, str.length - 2);
if (this.lastBufferedIdx == this.buf.length && this.bufferedConcatenationCount < 100) {
if (this.lastBufferedType === 'code') {
this.lastBuffered += ' + "';
this.bufferedConcatenationCount++;
}
this.lastBufferedType = 'text';
this.lastBuffered += str;
this.buf[this.lastBufferedIdx - 1] = 'pug_html = pug_html + ' + this.bufferStartChar + this.lastBuffered + '";';
} else {
this.bufferedConcatenationCount = 0;
this.buf.push('pug_html = pug_html + "' + str + '";');
this.lastBufferedType = 'text';
this.bufferStartChar = '"';
this.lastBuffered = str;
this.lastBufferedIdx = this.buf.length;
}
},
/**
* Buffer the given `src` so it is evaluated at run time
*
* @param {String} src
* @api public
*/
bufferExpression: function (src) {
if (isConstant(src)) {
return this.buffer(toConstant(src) + '')
}
if (this.lastBufferedIdx == this.buf.length && this.bufferedConcatenationCount < 100) {
this.bufferedConcatenationCount++;
if (this.lastBufferedType === 'text') this.lastBuffered += '"';
this.lastBufferedType = 'code';
this.lastBuffered += ' + (' + src + ')';
this.buf[this.lastBufferedIdx - 1] = 'pug_html = pug_html + (' + this.bufferStartChar + this.lastBuffered + ');';
} else {
this.bufferedConcatenationCount = 0;
this.buf.push('pug_html = pug_html + (' + src + ');');
this.lastBufferedType = 'code';
this.bufferStartChar = '';
this.lastBuffered = '(' + src + ')';
this.lastBufferedIdx = this.buf.length;
}
},
/**
* Buffer an indent based on the current `indent`
* property and an additional `offset`.
*
* @param {Number} offset
* @param {Boolean} newline
* @api public
*/
prettyIndent: function(offset, newline){
offset = offset || 0;
newline = newline ? '\n' : '';
this.buffer(newline + Array(this.indents + offset).join(this.pp));
if (this.parentIndents)
this.buf.push('pug_html = pug_html + pug_indent.join("");');
},
/**
* Visit `node`.
*
* @param {Node} node
* @api public
*/
visit: function(node, parent){
var debug = this.debug;
if (!node) {
var msg;
if (parent) {
msg = 'A child of ' + parent.type + ' (' + (parent.filename || 'Pug') + ':' + parent.line + ')';
} else {
msg = 'A top-level node';
}
msg += ' is ' + node + ', expected a Pug AST Node.';
throw new TypeError(msg);
}
if (debug && node.debug !== false && node.type !== 'Block') {
if (node.line) {
var js = ';pug_debug_line = ' + node.line;
if (node.filename) js += ';pug_debug_filename = ' + stringify(node.filename);
this.buf.push(js + ';');
}
}
if (!this['visit' + node.type]) {
var msg;
if (parent) {
msg = 'A child of ' + parent.type
} else {
msg = 'A top-level node';
}
msg += ' (' + (node.filename || 'Pug') + ':' + node.line + ')'
+ ' is of type ' + node.type + ','
+ ' which is not supported by pug-code-gen.'
switch (node.type) {
case 'Filter':
msg += ' Please use pug-filters to preprocess this AST.'
break;
case 'Extends':
case 'Include':
case 'NamedBlock':
case 'FileReference': // unlikely but for the sake of completeness
msg += ' Please use pug-linker to preprocess this AST.'
break;
}
throw new TypeError(msg);
}
this.visitNode(node);
},
/**
* Visit `node`.
*
* @param {Node} node
* @api public
*/
visitNode: function(node){
return this['visit' + node.type](node);
},
/**
* Visit case `node`.
*
* @param {Literal} node
* @api public
*/
visitCase: function(node){
this.buf.push('switch (' + node.expr + '){');
this.visit(node.block, node);
this.buf.push('}');
},
/**
* Visit when `node`.
*
* @param {Literal} node
* @api public
*/
visitWhen: function(node){
if ('default' == node.expr) {
this.buf.push('default:');
} else {
this.buf.push('case ' + node.expr + ':');
}
if (node.block) {
this.visit(node.block, node);
this.buf.push(' break;');
}
},
/**
* Visit literal `node`.
*
* @param {Literal} node
* @api public
*/
visitLiteral: function(node){
this.buffer(node.str);
},
visitNamedBlock: function(block){
return this.visitBlock(block);
},
/**
* Visit all nodes in `block`.
*
* @param {Block} block
* @api public
*/
visitBlock: function(block){
var escapePrettyMode = this.escapePrettyMode;
var pp = this.pp;
// Pretty print multi-line text
if (pp && block.nodes.length > 1 && !escapePrettyMode &&
block.nodes[0].type === 'Text' && block.nodes[1].type === 'Text' ) {
this.prettyIndent(1, true);
}
for (var i = 0; i < block.nodes.length; ++i) {
// Pretty print text
if (pp && i > 0 && !escapePrettyMode &&
block.nodes[i].type === 'Text' && block.nodes[i-1].type === 'Text' &&
/\n$/.test(block.nodes[i - 1].val)) {
this.prettyIndent(1, false);
}
this.visit(block.nodes[i], block);
}
},
/**
* Visit a mixin's `block` keyword.
*
* @param {MixinBlock} block
* @api public
*/
visitMixinBlock: function(block){
if (this.pp) this.buf.push("pug_indent.push('" + Array(this.indents + 1).join(this.pp) + "');");
this.buf.push('block && block();');
if (this.pp) this.buf.push("pug_indent.pop();");
},
/**
* Visit `doctype`. Sets terse mode to `true` when html 5
* is used, causing self-closing tags to end with ">" vs "/>",
* and boolean attributes are not mirrored.
*
* @param {Doctype} doctype
* @api public
*/
visitDoctype: function(doctype){
if (doctype && (doctype.val || !this.doctype)) {
this.setDoctype(doctype.val || 'html');
}
if (this.doctype) this.buffer(this.doctype);
this.hasCompiledDoctype = true;
},
/**
* Visit `mixin`, generating a function that
* may be called within the template.
*
* @param {Mixin} mixin
* @api public
*/
visitMixin: function(mixin){
var name = 'pug_mixins[';
var args = mixin.args || '';
var block = mixin.block;
var attrs = mixin.attrs;
var attrsBlocks = this.attributeBlocks(mixin.attributeBlocks);
var pp = this.pp;
var dynamic = mixin.name[0]==='#';
var key = mixin.name;
if (dynamic) this.dynamicMixins = true;
name += (dynamic ? mixin.name.substr(2,mixin.name.length-3):'"'+mixin.name+'"')+']';
this.mixins[key] = this.mixins[key] || {used: false, instances: []};
if (mixin.call) {
this.mixins[key].used = true;
if (pp) this.buf.push("pug_indent.push('" + Array(this.indents + 1).join(pp) + "');")
if (block || attrs.length || attrsBlocks.length) {
this.buf.push(name + '.call({');
if (block) {
this.buf.push('block: function(){');
// Render block with no indents, dynamically added when rendered
this.parentIndents++;
var _indents = this.indents;
this.indents = 0;
this.visit(mixin.block, mixin);
this.indents = _indents;
this.parentIndents--;
if (attrs.length || attrsBlocks.length) {
this.buf.push('},');
} else {
this.buf.push('}');
}
}
if (attrsBlocks.length) {
if (attrs.length) {
var val = this.attrs(attrs);
attrsBlocks.unshift(val);
}
if (attrsBlocks.length > 1) {
this.buf.push('attributes: ' + this.runtime('merge') + '([' + attrsBlocks.join(',') + '])');
} else {
this.buf.push('attributes: ' + attrsBlocks[0]);
}
} else if (attrs.length) {
var val = this.attrs(attrs);
this.buf.push('attributes: ' + val);
}
if (args) {
this.buf.push('}, ' + args + ');');
} else {
this.buf.push('});');
}
} else {
this.buf.push(name + '(' + args + ');');
}
if (pp) this.buf.push("pug_indent.pop();")
} else {
var mixin_start = this.buf.length;
args = args ? args.split(',') : [];
var rest;
if (args.length && /^\.\.\./.test(args[args.length - 1].trim())) {
rest = args.pop().trim().replace(/^\.\.\./, '');
}
// we need use pug_interp here for v8: https://code.google.com/p/v8/issues/detail?id=4165
// once fixed, use this: this.buf.push(name + ' = function(' + args.join(',') + '){');
this.buf.push(name + ' = pug_interp = function(' + args.join(',') + '){');
this.buf.push('var block = (this && this.block), attributes = (this && this.attributes) || {};');
if (rest) {
this.buf.push('var ' + rest + ' = [];');
this.buf.push('for (pug_interp = ' + args.length + '; pug_interp < arguments.length; pug_interp++) {');
this.buf.push(' ' + rest + '.push(arguments[pug_interp]);');
this.buf.push('}');
}
this.parentIndents++;
this.visit(block, mixin);
this.parentIndents--;
this.buf.push('};');
var mixin_end = this.buf.length;
this.mixins[key].instances.push({start: mixin_start, end: mixin_end});
}
},
/**
* Visit `tag` buffering tag markup, generating
* attributes, visiting the `tag`'s code and block.
*
* @param {Tag} tag
* @param {boolean} interpolated
* @api public
*/
visitTag: function(tag, interpolated){
this.indents++;
var name = tag.name
, pp = this.pp
, self = this;
function bufferName() {
if (interpolated) self.bufferExpression(tag.expr);
else self.buffer(name);
}
if (WHITE_SPACE_SENSITIVE_TAGS[tag.name] === true) this.escapePrettyMode = true;
if (!this.hasCompiledTag) {
if (!this.hasCompiledDoctype && 'html' == name) {
this.visitDoctype();
}
this.hasCompiledTag = true;
}
// pretty print
if (pp && !tag.isInline)
this.prettyIndent(0, true);
if (tag.selfClosing || (!this.xml && selfClosing[tag.name])) {
this.buffer('<');
bufferName();
this.visitAttributes(tag.attrs, this.attributeBlocks(tag.attributeBlocks));
if (this.terse && !tag.selfClosing) {
this.buffer('>');
} else {
this.buffer('/>');
}
// if it is non-empty throw an error
if (tag.code ||
tag.block &&
!(tag.block.type === 'Block' && tag.block.nodes.length === 0) &&
tag.block.nodes.some(function (tag) {
return tag.type !== 'Text' || !/^\s*$/.test(tag.val)
})) {
this.error(name + ' is a self closing element: <'+name+'/> but contains nested content.', 'SELF_CLOSING_CONTENT', tag);
}
} else {
// Optimize attributes buffering
this.buffer('<');
bufferName();
this.visitAttributes(tag.attrs, this.attributeBlocks(tag.attributeBlocks));
this.buffer('>');
if (tag.code) this.visitCode(tag.code);
this.visit(tag.block, tag);
// pretty print
if (pp && !tag.isInline && WHITE_SPACE_SENSITIVE_TAGS[tag.name] !== true && !tagCanInline(tag))
this.prettyIndent(0, true);
this.buffer('</');
bufferName();
this.buffer('>');
}
if (WHITE_SPACE_SENSITIVE_TAGS[tag.name] === true) this.escapePrettyMode = false;
this.indents--;
},
/**
* Visit InterpolatedTag.
*
* @param {InterpolatedTag} tag
* @api public
*/
visitInterpolatedTag: function(tag) {
return this.visitTag(tag, true);
},
/**
* Visit `text` node.
*
* @param {Text} text
* @api public
*/
visitText: function(text){
this.buffer(text.val);
},
/**
* Visit a `comment`, only buffering when the buffer flag is set.
*
* @param {Comment} comment
* @api public
*/
visitComment: function(comment){
if (!comment.buffer) return;
if (this.pp) this.prettyIndent(1, true);
this.buffer('<!--' + comment.val + '-->');
},
/**
* Visit a `YieldBlock`.
*
* This is necessary since we allow compiling a file with `yield`.
*
* @param {YieldBlock} block
* @api public
*/
visitYieldBlock: function(block) {},
/**
* Visit a `BlockComment`.
*
* @param {Comment} comment
* @api public
*/
visitBlockComment: function(comment){
if (!comment.buffer) return;
if (this.pp) this.prettyIndent(1, true);
this.buffer('<!--' + (comment.val || ''));
this.visit(comment.block, comment);
if (this.pp) this.prettyIndent(1, true);
this.buffer('-->');
},
/**
* Visit `code`, respecting buffer / escape flags.
* If the code is followed by a block, wrap it in
* a self-calling function.
*
* @param {Code} code
* @api public
*/
visitCode: function(code){
// Wrap code blocks with {}.
// we only wrap unbuffered code blocks ATM
// since they are usually flow control
// Buffer code
if (code.buffer) {
var val = code.val.trim();
val = 'null == (pug_interp = '+val+') ? "" : pug_interp';
if (code.mustEscape !== false) val = this.runtime('escape') + '(' + val + ')';
this.bufferExpression(val);
} else {
this.buf.push(code.val);
}
// Block support
if (code.block) {
if (!code.buffer) this.buf.push('{');
this.visit(code.block, code);
if (!code.buffer) this.buf.push('}');
}
},
/**
* Visit `Conditional`.
*
* @param {Conditional} cond
* @api public
*/
visitConditional: function(cond){
var test = cond.test;
this.buf.push('if (' + test + ') {');
this.visit(cond.consequent, cond);
this.buf.push('}')
if (cond.alternate) {
if (cond.alternate.type === 'Conditional') {
this.buf.push('else')
this.visitConditional(cond.alternate);
} else {
this.buf.push('else {');
this.visit(cond.alternate, cond);
this.buf.push('}');
}
}
},
/**
* Visit `While`.
*
* @param {While} loop
* @api public
*/
visitWhile: function(loop){
var test = loop.test;
this.buf.push('while (' + test + ') {');
this.visit(loop.block, loop);
this.buf.push('}');
},
/**
* Visit `each` block.
*
* @param {Each} each
* @api public
*/
visitEach: function(each){
var indexVarName = each.key || 'pug_index' + this.eachCount;
this.eachCount++;
this.buf.push(''
+ '// iterate ' + each.obj + '\n'
+ ';(function(){\n'
+ ' var $$obj = ' + each.obj + ';\n'
+ ' if (\'number\' == typeof $$obj.length) {');
if (each.alternate) {
this.buf.push(' if ($$obj.length) {');
}
this.buf.push(''
+ ' for (var ' + indexVarName + ' = 0, $$l = $$obj.length; ' + indexVarName + ' < $$l; ' + indexVarName + '++) {\n'
+ ' var ' + each.val + ' = $$obj[' + indexVarName + '];');
this.visit(each.block, each);
this.buf.push(' }');
if (each.alternate) {
this.buf.push(' } else {');
this.visit(each.alternate, each);
this.buf.push(' }');
}
this.buf.push(''
+ ' } else {\n'
+ ' var $$l = 0;\n'
+ ' for (var ' + indexVarName + ' in $$obj) {\n'
+ ' $$l++;\n'
+ ' var ' + each.val + ' = $$obj[' + indexVarName + '];');
this.visit(each.block, each);
this.buf.push(' }');
if (each.alternate) {
this.buf.push(' if ($$l === 0) {');
this.visit(each.alternate, each);
this.buf.push(' }');
}
this.buf.push(' }\n}).call(this);\n');
},
/**
* Visit `attrs`.
*
* @param {Array} attrs
* @api public
*/
visitAttributes: function(attrs, attributeBlocks){
if (attributeBlocks.length) {
if (attrs.length) {
var val = this.attrs(attrs);
attributeBlocks.unshift(val);
}
if (attributeBlocks.length > 1) {
this.bufferExpression(this.runtime('attrs') + '(' + this.runtime('merge') + '([' + attributeBlocks.join(',') + ']), ' + stringify(this.terse) + ')');
} else {
this.bufferExpression(this.runtime('attrs') + '(' + attributeBlocks[0] + ', ' + stringify(this.terse) + ')');
}
} else if (attrs.length) {
this.attrs(attrs, true);
}
},
/**
* Compile attributes.
*/
attrs: function(attrs, buffer){
var res = compileAttrs(attrs, {
terse: this.terse,
format: buffer ? 'html' : 'object',
runtime: this.runtime.bind(this)
});
if (buffer) {
this.bufferExpression(res);
}
return res;
},
/**
* Compile attribute blocks.
*/
attributeBlocks: function (attributeBlocks) {
return attributeBlocks && attributeBlocks.slice().map(function(attrBlock){
return attrBlock.val;
});
}
};
function tagCanInline(tag) {
function isInline(node){
// Recurse if the node is a block
if (node.type === 'Block') return node.nodes.every(isInline);
// When there is a YieldBlock here, it is an indication that the file is
// expected to be included but is not. If this is the case, the block
// must be empty.
if (node.type === 'YieldBlock') return true;
return (node.type === 'Text' && !/\n/.test(node.val)) || node.isInline;
}
return tag.block.nodes.every(isInline);
}