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

Support whitespace control in comment blocks, close #332 #632

Merged
merged 2 commits into from
Jan 13, 2016
Merged
Show file tree
Hide file tree
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
11 changes: 8 additions & 3 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1189,8 +1189,11 @@ var Parser = Object.extend({

// Same for the succeding block start token
if(nextToken &&
nextToken.type === lexer.TOKEN_BLOCK_START &&
nextVal.charAt(nextVal.length - 1) === '-') {
((nextToken.type === lexer.TOKEN_BLOCK_START &&
nextVal.charAt(nextVal.length - 1) === '-') ||
(nextToken.type === lexer.TOKEN_COMMENT &&
nextVal.charAt(this.tokens.tags.COMMENT_START.length)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you choose to use this.tokens.tags.COMMENT_START.length here instead of nextVal.length - 1 as used in the existing clause?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the TOKEN_COMMENT contains COMMENT_START and COMMENT_END. It's a whole token so that the nextVal here is not {%{#.

Maybe breaking the TOKEN_COMMENT into three parts would be better. But It need change more code so that I use the tag instead.

=== '-'))) {
// TODO: this could be optimized (don't use regex)
data = data.replace(/\s*$/, '');
}
Expand All @@ -1216,7 +1219,9 @@ var Parser = Object.extend({
buf.push(new nodes.Output(tok.lineno, tok.colno, [e]));
}
else if(tok.type === lexer.TOKEN_COMMENT) {
this.dropLeadingWhitespace = false;
this.dropLeadingWhitespace = tok.value.charAt(
tok.value.length - this.tokens.tags.COMMENT_END.length - 1
) === '-';
} else {
// Ignore comments, otherwise this should be an error
this.fail('Unexpected token at top-level: ' +
Expand Down
24 changes: 24 additions & 0 deletions tests/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,30 @@
[nodes.Output,
[nodes.TemplateData, 'hi']]]]]);

isAST(parser.parse('hello \n{#- comment #}'),
[nodes.Root,
[nodes.Output,
[nodes.TemplateData, 'hello']]]);

isAST(parser.parse('{# comment -#} \n world'),
[nodes.Root,
[nodes.Output,
[nodes.TemplateData, 'world']]]);

isAST(parser.parse('hello \n{#- comment -#} \n world'),
[nodes.Root,
[nodes.Output,
[nodes.TemplateData, 'hello']],
[nodes.Output,
[nodes.TemplateData, 'world']]]);

isAST(parser.parse('hello \n{# - comment - #} \n world'),
[nodes.Root,
[nodes.Output,
[nodes.TemplateData, 'hello \n']],
[nodes.Output,
[nodes.TemplateData, ' \n world']]]);

// The from statement required a special case so make sure to
// test it
isAST(parser.parse('{% from x import y %}\n hi \n'),
Expand Down