diff options
-rw-r--r-- | lib/handlebars/compiler/ast.js | 7 | ||||
-rw-r--r-- | lib/handlebars/compiler/helpers.js | 5 | ||||
-rw-r--r-- | spec/ast.js | 2 | ||||
-rw-r--r-- | spec/basic.js | 3 | ||||
-rw-r--r-- | spec/tokenizer.js | 6 | ||||
-rw-r--r-- | src/handlebars.l | 16 | ||||
-rw-r--r-- | src/handlebars.yy | 2 |
7 files changed, 29 insertions, 12 deletions
diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js index 49bdc33..e05ceec 100644 --- a/lib/handlebars/compiler/ast.js +++ b/lib/handlebars/compiler/ast.js @@ -196,14 +196,13 @@ var AST = { this.stringModeValue = bool === "true"; }, - CommentNode: function(comment, locInfo) { + CommentNode: function(comment, strip, locInfo) { LocationInfo.call(this, locInfo); this.type = "comment"; this.comment = comment; - this.strip = { - inlineStandalone: true - }; + this.strip = strip; + strip.inlineStandalone = true; } }; diff --git a/lib/handlebars/compiler/helpers.js b/lib/handlebars/compiler/helpers.js index 758c740..20f13d6 100644 --- a/lib/handlebars/compiler/helpers.js +++ b/lib/handlebars/compiler/helpers.js @@ -7,6 +7,11 @@ export function stripFlags(open, close) { }; } +export function stripComment(comment) { + return comment.replace(/^\{\{~?\!-?-?/, '') + .replace(/-?-?~?\}\}$/, ''); +} + export function prepareBlock(mustache, program, inverseAndProgram, close, inverted, locInfo) { /*jshint -W040 */ diff --git a/spec/ast.js b/spec/ast.js index c28e876..7118c72 100644 --- a/spec/ast.js +++ b/spec/ast.js @@ -137,7 +137,7 @@ describe('ast', function() { describe("CommentNode", function(){ it('stores location info', function(){ - var comment = new handlebarsEnv.AST.CommentNode("HI", LOCATION_INFO); + var comment = new handlebarsEnv.AST.CommentNode("HI", {}, LOCATION_INFO); testLocationInfoStorage(comment); }); }); diff --git a/spec/basic.js b/spec/basic.js index 8a9c116..139d6d0 100644 --- a/spec/basic.js +++ b/spec/basic.js @@ -33,6 +33,9 @@ describe("basic context", function() { shouldCompileTo("{{! Goodbye}}Goodbye\n{{cruel}}\n{{world}}!", {cruel: "cruel", world: "world"}, "Goodbye\ncruel\nworld!", "comments are ignored"); + + shouldCompileTo(' {{~! comment ~}} blah', {}, 'blah'); + shouldCompileTo(' {{~!-- long-comment --~}} blah', {}, 'blah'); }); it("boolean", function() { diff --git a/spec/tokenizer.js b/spec/tokenizer.js index 0f0dc0b..6f5cc38 100644 --- a/spec/tokenizer.js +++ b/spec/tokenizer.js @@ -217,19 +217,19 @@ describe('Tokenizer', function() { it('tokenizes a comment as "COMMENT"', function() { var result = tokenize("foo {{! this is a comment }} bar {{ baz }}"); shouldMatchTokens(result, ['CONTENT', 'COMMENT', 'CONTENT', 'OPEN', 'ID', 'CLOSE']); - shouldBeToken(result[1], "COMMENT", " this is a comment "); + shouldBeToken(result[1], "COMMENT", "{{! this is a comment }}"); }); it('tokenizes a block comment as "COMMENT"', function() { var result = tokenize("foo {{!-- this is a {{comment}} --}} bar {{ baz }}"); shouldMatchTokens(result, ['CONTENT', 'COMMENT', 'CONTENT', 'OPEN', 'ID', 'CLOSE']); - shouldBeToken(result[1], "COMMENT", " this is a {{comment}} "); + shouldBeToken(result[1], "COMMENT", "{{!-- this is a {{comment}} --}}"); }); it('tokenizes a block comment with whitespace as "COMMENT"', function() { var result = tokenize("foo {{!-- this is a\n{{comment}}\n--}} bar {{ baz }}"); shouldMatchTokens(result, ['CONTENT', 'COMMENT', 'CONTENT', 'OPEN', 'ID', 'CLOSE']); - shouldBeToken(result[1], "COMMENT", " this is a\n{{comment}}\n"); + shouldBeToken(result[1], "COMMENT", "{{!-- this is a\n{{comment}}\n--}}"); }); it('tokenizes open and closing blocks as OPEN_BLOCK, ID, CLOSE ..., OPEN_ENDBLOCK ID CLOSE', function() { diff --git a/src/handlebars.l b/src/handlebars.l index 0f420e7..fbeec34 100644 --- a/src/handlebars.l +++ b/src/handlebars.l @@ -56,7 +56,10 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/{LOOKAHEAD} } <raw>[^\x00]*?/("{{{{/") { return 'CONTENT'; } -<com>[\s\S]*?"--}}" strip(0,4); this.popState(); return 'COMMENT'; +<com>[\s\S]*?"--"{RIGHT_STRIP}?"}}" { + this.popState(); + return 'COMMENT'; +} <mu>"(" return 'OPEN_SEXPR'; <mu>")" return 'CLOSE_SEXPR'; @@ -76,8 +79,15 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/{LOOKAHEAD} <mu>"{{"{LEFT_STRIP}?\s*"else" return 'OPEN_INVERSE'; <mu>"{{"{LEFT_STRIP}?"{" return 'OPEN_UNESCAPED'; <mu>"{{"{LEFT_STRIP}?"&" return 'OPEN'; -<mu>"{{!--" this.popState(); this.begin('com'); -<mu>"{{!"[\s\S]*?"}}" strip(3,5); this.popState(); return 'COMMENT'; +<mu>"{{"{LEFT_STRIP}?"!--" { + this.unput(yytext); + this.popState(); + this.begin('com'); +} +<mu>"{{"{LEFT_STRIP}?"!"[\s\S]*?"}}" { + this.popState(); + return 'COMMENT'; +} <mu>"{{"{LEFT_STRIP}? return 'OPEN'; <mu>"=" return 'EQUALS'; diff --git a/src/handlebars.yy b/src/handlebars.yy index a8d288f..ccc0d22 100644 --- a/src/handlebars.yy +++ b/src/handlebars.yy @@ -18,7 +18,7 @@ statement | rawBlock -> $1 | partial -> $1 | CONTENT -> new yy.ContentNode($1, @$) - | COMMENT -> new yy.CommentNode($1, @$) + | COMMENT -> new yy.CommentNode(yy.stripComment($1), yy.stripFlags($1, $1), @$) ; rawBlock |