diff --git a/lib/handlebars/compiler/base.js b/lib/handlebars/compiler/base.js index 7075d9bd8..85c2997b4 100644 --- a/lib/handlebars/compiler/base.js +++ b/lib/handlebars/compiler/base.js @@ -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)); } diff --git a/lib/handlebars/compiler/whitespace-control.js b/lib/handlebars/compiler/whitespace-control.js index 5b76944dc..d1b743d7e 100644 --- a/lib/handlebars/compiler/whitespace-control.js +++ b/lib/handlebars/compiler/whitespace-control.js @@ -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; @@ -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)) { @@ -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); @@ -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); diff --git a/spec/blocks.js b/spec/blocks.js index 3584ed788..71c9045d2 100644 --- a/spec/blocks.js +++ b/spec/blocks.js @@ -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');