Skip to content

Commit

Permalink
Merge pull request #632 from oyyd/fix
Browse files Browse the repository at this point in the history
Support whitespace control in comment blocks, close #332
  • Loading branch information
carljm committed Jan 13, 2016
2 parents 212b56d + fc50f44 commit 0b6dd68
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
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)
=== '-'))) {
// 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

0 comments on commit 0b6dd68

Please sign in to comment.