From 868ef4b309720f77586834e7a73e4b341ed4737a Mon Sep 17 00:00:00 2001 From: Eric Nielsen Date: Wed, 15 Jul 2015 14:40:48 -0300 Subject: [PATCH 1/4] #1056 Fixed grammar for nested raw blocks --- lib/handlebars/compiler/helpers.js | 4 ++-- src/handlebars.l | 12 +++++++++--- src/handlebars.yy | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/handlebars/compiler/helpers.js b/lib/handlebars/compiler/helpers.js index e3eb7864a..1c8ab0d3b 100644 --- a/lib/handlebars/compiler/helpers.js +++ b/lib/handlebars/compiler/helpers.js @@ -70,7 +70,7 @@ export function prepareMustache(path, params, hash, open, strip, locInfo) { return new this.MustacheStatement(path, params, hash, escaped, strip, this.locInfo(locInfo)); } -export function prepareRawBlock(openRawBlock, content, close, locInfo) { +export function prepareRawBlock(openRawBlock, contents, close, locInfo) { if (openRawBlock.path.original !== close) { let errorNode = {loc: openRawBlock.path.loc}; @@ -78,7 +78,7 @@ export function prepareRawBlock(openRawBlock, content, close, locInfo) { } locInfo = this.locInfo(locInfo); - let program = new this.Program([content], null, {}, locInfo); + let program = new this.Program(contents, null, {}, locInfo); return new this.BlockStatement( openRawBlock.path, openRawBlock.params, openRawBlock.hash, diff --git a/src/handlebars.l b/src/handlebars.l index ff2128355..4d2cd6294 100644 --- a/src/handlebars.l +++ b/src/handlebars.l @@ -49,12 +49,18 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/{LOOKAHEAD} return 'CONTENT'; } +// nested raw block will create stacked 'raw' condition +"{{{{"/[^/] this.begin('raw'); return 'CONTENT'; "{{{{/"[^\s!"#%-,\.\/;->@\[-\^`\{-~]+/[=}\s\/.]"}}}}" { - yytext = yytext.substr(5, yyleng-9); this.popState(); - return 'END_RAW_BLOCK'; + if (this.conditionStack[this.conditionStack.length-1] === 'raw') { + return 'CONTENT'; + } else { + yytext = yytext.substr(5, yyleng-9); + return 'END_RAW_BLOCK'; + } } -[^\x00]*?/("{{{{/") { return 'CONTENT'; } +[^\x00]*?/("{{{{") { return 'CONTENT'; } [\s\S]*?"--"{RIGHT_STRIP}?"}}" { this.popState(); diff --git a/src/handlebars.yy b/src/handlebars.yy index 2424e27fd..ecc79afc6 100644 --- a/src/handlebars.yy +++ b/src/handlebars.yy @@ -26,7 +26,7 @@ content ; rawBlock - : openRawBlock content END_RAW_BLOCK -> yy.prepareRawBlock($1, $2, $3, @$) + : openRawBlock content+ END_RAW_BLOCK -> yy.prepareRawBlock($1, $2, $3, @$) ; openRawBlock From b9fe7ce618a9bf4f969a2b97f455fccb2ac42101 Mon Sep 17 00:00:00 2001 From: Eric Nielsen Date: Wed, 15 Jul 2015 14:41:43 -0300 Subject: [PATCH 2/4] #1056 Added spec for nested raw block --- spec/helpers.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spec/helpers.js b/spec/helpers.js index 54ef0f288..f3257a502 100644 --- a/spec/helpers.js +++ b/spec/helpers.js @@ -28,6 +28,16 @@ describe('helpers', function() { 'raw block helper gets raw content'); }); + it('helper for nested raw block gets raw content', function() { + var string = '{{{{a}}}} {{{{b}}}} {{{{/b}}}} {{{{/a}}}}'; + var helpers = { + a: function(options) { + return options.fn(); + } + }; + shouldCompileTo(string, [{}, helpers], ' {{{{b}}}} {{{{/b}}}} '); + }); + it('helper block with complex lookup expression', function() { var string = '{{#goodbyes}}{{../name}}{{/goodbyes}}'; var hash = {name: 'Alan'}; From 2f9495c1df52eb47b0e84d77fc964cef4581581c Mon Sep 17 00:00:00 2001 From: Eric Nielsen Date: Thu, 16 Jul 2015 15:15:06 -0300 Subject: [PATCH 3/4] Added spec message --- spec/helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/helpers.js b/spec/helpers.js index f3257a502..00bcb79cb 100644 --- a/spec/helpers.js +++ b/spec/helpers.js @@ -35,7 +35,7 @@ describe('helpers', function() { return options.fn(); } }; - shouldCompileTo(string, [{}, helpers], ' {{{{b}}}} {{{{/b}}}} '); + shouldCompileTo(string, [{}, helpers], ' {{{{b}}}} {{{{/b}}}} ', 'raw block helper should get nested raw block as raw content'); }); it('helper block with complex lookup expression', function() { From aa7a45b44350caf35cd47a46a818271fb355b3f3 Mon Sep 17 00:00:00 2001 From: Eric Nielsen Date: Sun, 19 Jul 2015 12:34:44 -0300 Subject: [PATCH 4/4] Added comment about Jison's topState() --- src/handlebars.l | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/handlebars.l b/src/handlebars.l index 4d2cd6294..f7df8f55c 100644 --- a/src/handlebars.l +++ b/src/handlebars.l @@ -53,6 +53,9 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/{LOOKAHEAD} "{{{{"/[^/] this.begin('raw'); return 'CONTENT'; "{{{{/"[^\s!"#%-,\.\/;->@\[-\^`\{-~]+/[=}\s\/.]"}}}}" { this.popState(); + // Should be using `this.topState()` below, but it currently + // returns the second top instead of the first top. Opened an + // issue about it at /~https://github.com/zaach/jison/issues/291 if (this.conditionStack[this.conditionStack.length-1] === 'raw') { return 'CONTENT'; } else {