Skip to content

Commit

Permalink
Merge pull request #631 from oyyd/fix
Browse files Browse the repository at this point in the history
Fix unexpected whitespace control behavior, close #595.
  • Loading branch information
carljm committed Jan 8, 2016
2 parents 6b89b04 + 0d82f39 commit 430e2fc
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,7 @@ var Parser = Object.extend({
data)]));
}
else if(tok.type === lexer.TOKEN_BLOCK_START) {
this.dropLeadingWhitespace = false;
var n = this.parseStatement();
if(!n) {
break;
Expand All @@ -1211,12 +1212,16 @@ var Parser = Object.extend({
else if(tok.type === lexer.TOKEN_VARIABLE_START) {
var e = this.parseExpression();
this.advanceAfterVariableEnd();
this.dropLeadingWhitespace = false;
buf.push(new nodes.Output(tok.lineno, tok.colno, [e]));
}
else if(tok.type !== lexer.TOKEN_COMMENT) {
else if(tok.type === lexer.TOKEN_COMMENT) {
this.dropLeadingWhitespace = false;
} else {
// Ignore comments, otherwise this should be an error
this.fail('Unexpected token at top-level: ' +
tok.type, tok.lineno, tok.colno);

}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1223,5 +1223,25 @@
finish(done);
});


it('should control whitespaces correctly', function(done) {
equal(
'{% if true -%}{{"hello"}} {{"world"}}{% endif %}',
'hello world'
);

equal(
'{% if true -%}{% if true %} {{"hello"}} {{"world"}}'
+ '{% endif %}{% endif %}',
' hello world'
);

equal(
'{% if true -%}{# comment #} {{"hello"}}{% endif %}',
' hello'
);

finish(done);
});
});
})();
40 changes: 40 additions & 0 deletions tests/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,46 @@
[nodes.Symbol, 'y']]],
[nodes.Output,
[nodes.TemplateData, 'hi \n']]]);

isAST(parser.parse('{% if x -%}{{y}} {{z}}{% endif %}'),
[nodes.Root,
[nodes.If,
[nodes.Symbol, 'x'],
[nodes.NodeList,
[nodes.Output,
[nodes.Symbol, 'y']],
[nodes.Output,
// the value of TemplateData should be ' ' instead of ''
[nodes.TemplateData, ' ']],
[nodes.Output,
[nodes.Symbol, 'z']]]]]);

isAST(parser.parse('{% if x -%}{% if y %} {{z}}{% endif %}{% endif %}'),
[nodes.Root,
[nodes.If,
[nodes.Symbol, 'x'],
[nodes.NodeList,
[nodes.If,
[nodes.Symbol, 'y'],
[nodes.NodeList,
[nodes.Output,
// the value of TemplateData should be ' ' instead of ''
[nodes.TemplateData, ' ']],
[nodes.Output,
[nodes.Symbol, 'z']]
]]]]]);

isAST(parser.parse('{% if x -%}{# comment #} {{z}}{% endif %}'),
[nodes.Root,
[nodes.If,
[nodes.Symbol, 'x'],
[nodes.NodeList,
[nodes.Output,
// the value of TemplateData should be ' ' instead of ''
[nodes.TemplateData, ' ']],
[nodes.Output,
[nodes.Symbol, 'z']]]]]);

});

it('should throw errors', function() {
Expand Down

0 comments on commit 430e2fc

Please sign in to comment.