Skip to content

Commit ea3a5a1

Browse files
committed
Add ignoreStandalone compiler option
Fixes #1072
1 parent 269dd49 commit ea3a5a1

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

lib/handlebars/compiler/base.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ export function parse(input, options) {
2020
return new yy.SourceLocation(options && options.srcName, locInfo);
2121
};
2222

23-
let strip = new WhitespaceControl();
23+
let strip = new WhitespaceControl(options);
2424
return strip.accept(parser.parse(input));
2525
}

lib/handlebars/compiler/whitespace-control.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import Visitor from './visitor';
22

3-
function WhitespaceControl() {
3+
function WhitespaceControl(options = {}) {
4+
this.options = options;
45
}
56
WhitespaceControl.prototype = new Visitor();
67

78
WhitespaceControl.prototype.Program = function(program) {
9+
const doStandalone = !this.options.ignoreStandalone;
10+
811
let isRoot = !this.isRootSeen;
912
this.isRootSeen = true;
1013

@@ -31,7 +34,7 @@ WhitespaceControl.prototype.Program = function(program) {
3134
omitLeft(body, i, true);
3235
}
3336

34-
if (inlineStandalone) {
37+
if (doStandalone && inlineStandalone) {
3538
omitRight(body, i);
3639

3740
if (omitLeft(body, i)) {
@@ -42,13 +45,13 @@ WhitespaceControl.prototype.Program = function(program) {
4245
}
4346
}
4447
}
45-
if (openStandalone) {
48+
if (doStandalone && openStandalone) {
4649
omitRight((current.program || current.inverse).body);
4750

4851
// Strip out the previous content node if it's whitespace only
4952
omitLeft(body, i);
5053
}
51-
if (closeStandalone) {
54+
if (doStandalone && closeStandalone) {
5255
// Always strip the next node
5356
omitRight(body, i);
5457

@@ -106,7 +109,8 @@ WhitespaceControl.prototype.BlockStatement = function(block) {
106109
}
107110

108111
// Find standalone else statments
109-
if (isPrevWhitespace(program.body)
112+
if (!this.options.ignoreStandalone
113+
&& isPrevWhitespace(program.body)
110114
&& isNextWhitespace(firstInverse.body)) {
111115
omitLeft(program.body);
112116
omitRight(firstInverse.body);

spec/blocks.js

+10
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ describe('blocks', function() {
125125
shouldCompileTo('{{#people}}\n{{name}}\n{{^}}\n{{none}}\n{{/people}}\n', {none: 'No people'},
126126
'No people\n');
127127
});
128+
it('block standalone else sections can be disabled', function() {
129+
shouldCompileTo(
130+
'{{#people}}\n{{name}}\n{{^}}\n{{none}}\n{{/people}}\n',
131+
[{none: 'No people'}, {}, {}, {ignoreStandalone: true}],
132+
'\nNo people\n\n');
133+
shouldCompileTo(
134+
'{{#none}}\n{{.}}\n{{^}}\nFail\n{{/none}}\n',
135+
[{none: 'No people'}, {}, {}, {ignoreStandalone: true}],
136+
'\nNo people\n\n');
137+
});
128138
it('block standalone chained else sections', function() {
129139
shouldCompileTo('{{#people}}\n{{name}}\n{{else if none}}\n{{none}}\n{{/people}}\n', {none: 'No people'},
130140
'No people\n');

0 commit comments

Comments
 (0)