Skip to content

Commit

Permalink
Add ignoreStandalone compiler option
Browse files Browse the repository at this point in the history
Fixes #1072
  • Loading branch information
kpdecker committed Aug 13, 2015
1 parent 269dd49 commit ea3a5a1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/handlebars/compiler/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export function parse(input, options) {
return new yy.SourceLocation(options && options.srcName, locInfo);
};

let strip = new WhitespaceControl();
let strip = new WhitespaceControl(options);
return strip.accept(parser.parse(input));
}
14 changes: 9 additions & 5 deletions lib/handlebars/compiler/whitespace-control.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import Visitor from './visitor';

function WhitespaceControl() {
function WhitespaceControl(options = {}) {
this.options = options;
}
WhitespaceControl.prototype = new Visitor();

WhitespaceControl.prototype.Program = function(program) {
const doStandalone = !this.options.ignoreStandalone;

let isRoot = !this.isRootSeen;
this.isRootSeen = true;

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

if (inlineStandalone) {
if (doStandalone && inlineStandalone) {
omitRight(body, i);

if (omitLeft(body, i)) {
Expand All @@ -42,13 +45,13 @@ WhitespaceControl.prototype.Program = function(program) {
}
}
}
if (openStandalone) {
if (doStandalone && openStandalone) {
omitRight((current.program || current.inverse).body);

// Strip out the previous content node if it's whitespace only
omitLeft(body, i);
}
if (closeStandalone) {
if (doStandalone && closeStandalone) {
// Always strip the next node
omitRight(body, i);

Expand Down Expand Up @@ -106,7 +109,8 @@ WhitespaceControl.prototype.BlockStatement = function(block) {
}

// Find standalone else statments
if (isPrevWhitespace(program.body)
if (!this.options.ignoreStandalone
&& isPrevWhitespace(program.body)
&& isNextWhitespace(firstInverse.body)) {
omitLeft(program.body);
omitRight(firstInverse.body);
Expand Down
10 changes: 10 additions & 0 deletions spec/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ describe('blocks', function() {
shouldCompileTo('{{#people}}\n{{name}}\n{{^}}\n{{none}}\n{{/people}}\n', {none: 'No people'},
'No people\n');
});
it('block standalone else sections can be disabled', function() {
shouldCompileTo(
'{{#people}}\n{{name}}\n{{^}}\n{{none}}\n{{/people}}\n',
[{none: 'No people'}, {}, {}, {ignoreStandalone: true}],
'\nNo people\n\n');
shouldCompileTo(
'{{#none}}\n{{.}}\n{{^}}\nFail\n{{/none}}\n',
[{none: 'No people'}, {}, {}, {ignoreStandalone: true}],
'\nNo people\n\n');
});
it('block standalone chained else sections', function() {
shouldCompileTo('{{#people}}\n{{name}}\n{{else if none}}\n{{none}}\n{{/people}}\n', {none: 'No people'},
'No people\n');
Expand Down

0 comments on commit ea3a5a1

Please sign in to comment.