Skip to content

Commit

Permalink
Replace string value rather than add omit flag
Browse files Browse the repository at this point in the history
  • Loading branch information
kpdecker committed Aug 23, 2014
1 parent 0528b91 commit ccd803f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
2 changes: 1 addition & 1 deletion lib/handlebars/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ Compiler.prototype = {
},

content: function(content) {
if (!content.omit) {
if (content.string) {
this.opcode('appendContent', content.string);
}
},
Expand Down
5 changes: 3 additions & 2 deletions lib/handlebars/compiler/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function checkWhitespace(isRoot, next1, next2, disallowIndent) {
function omitRight(statements, i) {
var first = statements[i == null ? 0 : i + 1];
if (first) {
first.omit = true;
first.string = '';
}
}

Expand All @@ -158,6 +158,7 @@ function omitLeft(statements, i) {

// We omit the last node if it's whitespace only and not preceeded by a non-content node.
if (last && /^[\s]*$/.test(last.string) && (!prev || prev.type === 'content')) {
return last.omit = true;
last.string = '';
return true;
}
}
68 changes: 34 additions & 34 deletions spec/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('ast', function() {
describe('BlockNode', function() {
it('should throw on mustache mismatch', function() {
shouldThrow(function() {
handlebarsEnv.parse("\n {{#foo}}{{/bar}}")
handlebarsEnv.parse("\n {{#foo}}{{/bar}}");
}, Handlebars.Exception, "foo doesn't match bar - 2:2");
});

Expand Down Expand Up @@ -255,60 +255,60 @@ describe('ast', function() {
describe('mustache', function() {
it('does not mark mustaches as standalone', function() {
var ast = Handlebars.parse(' {{comment}} ');
equals(ast.statements[0].omit, undefined);
equals(ast.statements[2].omit, undefined);
equals(!!ast.statements[0].string, true);
equals(!!ast.statements[2].string, true);
});
});
describe('blocks', function() {
it('marks block mustaches as standalone', function() {
var ast = Handlebars.parse(' {{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} '),
block = ast.statements[1];

equals(ast.statements[0].omit, true);
equals(ast.statements[0].string, '');

equals(block.program.statements[0].omit, true);
equals(block.program.statements[0].string, '');
equals(block.program.statements[1].string, 'foo\n');
equals(block.program.statements[2].omit, true);
equals(block.program.statements[2].string, '');

equals(block.inverse.statements[0].omit, true);
equals(block.inverse.statements[0].string, '');
equals(block.inverse.statements[1].string, ' bar \n');
equals(block.inverse.statements[2].omit, true);
equals(block.inverse.statements[2].string, '');

equals(ast.statements[2].omit, true);
equals(ast.statements[2].string, '');
});
it('marks initial block mustaches as standalone', function() {
var ast = Handlebars.parse('{{# comment}} \nfoo\n {{/comment}}'),
block = ast.statements[0];

equals(block.program.statements[0].omit, true);
equals(block.program.statements[0].string, '');
equals(block.program.statements[1].string, 'foo\n');
equals(block.program.statements[2].omit, true);
equals(block.program.statements[2].string, '');
});
it('marks mustaches with children as standalone', function() {
var ast = Handlebars.parse('{{# comment}} \n{{foo}}\n {{/comment}}'),
block = ast.statements[0];

equals(block.program.statements[0].omit, true);
equals(block.program.statements[0].string, '');
equals(block.program.statements[1].id.original, 'foo');
equals(block.program.statements[2].omit, undefined);
equals(block.program.statements[3].omit, true);
equals(block.program.statements[3].string, '');
});
it('marks nested block mustaches as standalone', function() {
var ast = Handlebars.parse('{{#foo}} \n{{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} \n{{/foo}}'),
statements = ast.statements[0].program.statements,
block = statements[1];

equals(statements[0].omit, true);
equals(statements[0].string, '');

equals(block.program.statements[0].omit, true);
equals(block.program.statements[0].string, '');
equals(block.program.statements[1].string, 'foo\n');
equals(block.program.statements[2].omit, true);
equals(block.program.statements[2].string, '');

equals(block.inverse.statements[0].omit, true);
equals(block.inverse.statements[0].string, '');
equals(block.inverse.statements[1].string, ' bar \n');
equals(block.inverse.statements[2].omit, true);
equals(block.inverse.statements[2].string, '');

equals(statements[0].omit, true);
equals(statements[0].string, '');
});
it('does not mark nested block mustaches as standalone', function() {
var ast = Handlebars.parse('{{#foo}} {{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} {{/foo}}'),
Expand All @@ -319,9 +319,9 @@ describe('ast', function() {

equals(block.program.statements[0].omit, undefined);
equals(block.program.statements[1].string, 'foo\n');
equals(block.program.statements[2].omit, true);
equals(block.program.statements[2].string, '');

equals(block.inverse.statements[0].omit, true);
equals(block.inverse.statements[0].string, '');
equals(block.inverse.statements[1].string, ' bar \n');
equals(block.inverse.statements[2].omit, undefined);

Expand All @@ -334,9 +334,9 @@ describe('ast', function() {

equals(block.program.statements[0].omit, undefined);
equals(block.program.statements[1].string, 'foo\n');
equals(block.program.statements[2].omit, true);
equals(block.program.statements[2].string, '');

equals(block.inverse.statements[0].omit, true);
equals(block.inverse.statements[0].string, '');
equals(block.inverse.statements[1].string, ' bar \n');
equals(block.inverse.statements[2].omit, undefined);

Expand All @@ -349,27 +349,27 @@ describe('ast', function() {

equals(ast.statements[0].omit, undefined);

equals(block.program.statements[0].omit, true);
equals(block.program.statements[0].string, '');
equals(block.program.statements[1].string, 'foo\n');
equals(block.program.statements[2].omit, true);
equals(block.program.statements[2].string, '');

equals(block.inverse.statements[0].omit, true);
equals(block.inverse.statements[0].string, '');
equals(block.inverse.statements[1].string, ' bar \n');
equals(block.inverse.statements[2].omit, true);
equals(block.inverse.statements[2].string, '');

equals(ast.statements[2].omit, true);
equals(ast.statements[2].string, '');
});
});
describe('partials', function() {
it('marks partial as standalone', function() {
var ast = Handlebars.parse('{{> partial }} ');
equals(ast.statements[1].omit, true);
equals(ast.statements[1].string, '');
});
it('marks indented partial as standalone', function() {
var ast = Handlebars.parse(' {{> partial }} ');
equals(ast.statements[0].omit, true);
equals(ast.statements[0].string, '');
equals(ast.statements[1].indent, ' ');
equals(ast.statements[2].omit, true);
equals(ast.statements[2].string, '');
});
it('marks those around content as not standalone', function() {
var ast = Handlebars.parse('a{{> partial }}');
Expand All @@ -382,12 +382,12 @@ describe('ast', function() {
describe('comments', function() {
it('marks comment as standalone', function() {
var ast = Handlebars.parse('{{! comment }} ');
equals(ast.statements[1].omit, true);
equals(ast.statements[1].string, '');
});
it('marks indented comment as standalone', function() {
var ast = Handlebars.parse(' {{! comment }} ');
equals(ast.statements[0].omit, true);
equals(ast.statements[2].omit, true);
equals(ast.statements[0].string, '');
equals(ast.statements[2].string, '');
});
it('marks those around content as not standalone', function() {
var ast = Handlebars.parse('a{{! comment }}');
Expand Down

0 comments on commit ccd803f

Please sign in to comment.