Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unexpected whitespace control behavior, close #595. #631

Merged
merged 1 commit into from
Jan 8, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
@@ -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;
@@ -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);

}
}

20 changes: 20 additions & 0 deletions tests/compiler.js
Original file line number Diff line number Diff line change
@@ -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
@@ -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() {