From 48be86ebf816b07c09655b244bd04a9f3a6dc744 Mon Sep 17 00:00:00 2001 From: oyyd Date: Sun, 10 Jan 2016 15:18:39 +0800 Subject: [PATCH 1/2] support comment whitespace control --- src/parser.js | 11 ++++++++--- tests/parser.js | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/parser.js b/src/parser.js index 6aac3d79..0c9a7832 100644 --- a/src/parser.js +++ b/src/parser.js @@ -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*$/, ''); } @@ -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: ' + diff --git a/tests/parser.js b/tests/parser.js index 4bb9bf9e..d22236fb 100644 --- a/tests/parser.js +++ b/tests/parser.js @@ -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'), From fc50f44941dd8c429bd4b65930ef15d6a7eaf835 Mon Sep 17 00:00:00 2001 From: oyyd Date: Sun, 10 Jan 2016 15:32:21 +0800 Subject: [PATCH 2/2] code style --- src/parser.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/parser.js b/src/parser.js index 0c9a7832..1f9e7fb6 100644 --- a/src/parser.js +++ b/src/parser.js @@ -1189,7 +1189,7 @@ var Parser = Object.extend({ // Same for the succeding block start token if(nextToken && - ((nextToken.type === lexer.TOKEN_BLOCK_START && + ((nextToken.type === lexer.TOKEN_BLOCK_START && nextVal.charAt(nextVal.length - 1) === '-') || (nextToken.type === lexer.TOKEN_COMMENT && nextVal.charAt(this.tokens.tags.COMMENT_START.length) @@ -1220,7 +1220,7 @@ var Parser = Object.extend({ } else if(tok.type === lexer.TOKEN_COMMENT) { this.dropLeadingWhitespace = tok.value.charAt( - tok.value.length - this.tokens.tags.COMMENT_END.length- 1 + tok.value.length - this.tokens.tags.COMMENT_END.length - 1 ) === '-'; } else { // Ignore comments, otherwise this should be an error