Skip to content

Commit

Permalink
Merge branch 'mmun-remove-block-mustache'
Browse files Browse the repository at this point in the history
  • Loading branch information
kpdecker committed Nov 9, 2014
2 parents ac208b1 + bf8cc50 commit 4415f31
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 44 deletions.
18 changes: 2 additions & 16 deletions lib/handlebars/compiler/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ var AST = {
this.strip.inlineStandalone = true;
},

BlockNode: function(mustache, program, inverse, strip, locInfo) {
BlockNode: function(sexpr, program, inverse, strip, locInfo) {
LocationInfo.call(this, locInfo);

this.type = 'block';
this.mustache = mustache;
this.sexpr = sexpr;
this.program = program;
this.inverse = inverse;
this.strip = strip;
Expand All @@ -93,20 +93,6 @@ var AST = {
}
},

RawBlockNode: function(mustache, content, close, locInfo) {
LocationInfo.call(this, locInfo);

if (mustache.sexpr.id.original !== close) {
throw new Exception(mustache.sexpr.id.original + " doesn't match " + close, this);
}

content = new AST.ContentNode(content, locInfo);

this.type = 'block';
this.mustache = mustache;
this.program = new AST.ProgramNode([content], {}, locInfo);
},

ContentNode: function(string, locInfo) {
LocationInfo.call(this, locInfo);
this.type = "content";
Expand Down
3 changes: 1 addition & 2 deletions lib/handlebars/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Compiler.prototype = {
},

block: function(block) {
var mustache = block.mustache,
var sexpr = block.sexpr,
program = block.program,
inverse = block.inverse;

Expand All @@ -120,7 +120,6 @@ Compiler.prototype = {
inverse = this.compileProgram(inverse);
}

var sexpr = mustache.sexpr;
var type = this.classifySexpr(sexpr);

if (type === "helper") {
Expand Down
34 changes: 27 additions & 7 deletions lib/handlebars/compiler/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,32 @@ export function stripComment(comment) {
.replace(/-?-?~?\}\}$/, '');
}

export function prepareRawBlock(openRawBlock, content, close, locInfo) {
/*jshint -W040 */
if (openRawBlock.sexpr.id.original !== close) {
var errorNode = {
firstLine: openRawBlock.sexpr.firstLine,
firstColumn: openRawBlock.sexpr.firstColumn
};

throw new Exception(openRawBlock.sexpr.id.original + " doesn't match " + close, errorNode);
}

var program = new this.ProgramNode([content], {}, locInfo);

export function prepareBlock(mustache, program, inverseAndProgram, close, inverted, locInfo) {
return new this.BlockNode(openRawBlock.sexpr, program, undefined, undefined, locInfo);
}

export function prepareBlock(openBlock, program, inverseAndProgram, close, inverted, locInfo) {
/*jshint -W040 */
// When we are chaining inverse calls, we will not have a close path
if (close && close.path && (mustache.sexpr.id.original !== close.path.original)) {
throw new Exception(mustache.sexpr.id.original + ' doesn\'t match ' + close.path.original, mustache);
if (close && close.path && openBlock.sexpr.id.original !== close.path.original) {
var errorNode = {
firstLine: openBlock.sexpr.firstLine,
firstColumn: openBlock.sexpr.firstColumn
};

throw new Exception(openBlock.sexpr.id.original + ' doesn\'t match ' + close.path.original, errorNode);
}

// Safely handle a chained inverse that does not have a non-conditional inverse
Expand All @@ -40,7 +60,7 @@ export function prepareBlock(mustache, program, inverseAndProgram, close, invert
}

var strip = {
left: mustache.strip.left,
left: openBlock.strip.left,
right: close.strip.right,

// Determine the standalone candiacy. Basically flag our content as being possibly standalone
Expand All @@ -49,7 +69,7 @@ export function prepareBlock(mustache, program, inverseAndProgram, close, invert
closeStandalone: isPrevWhitespace((firstInverse || program).statements)
};

if (mustache.strip.right) {
if (openBlock.strip.right) {
omitRight(program.statements, null, true);
}

Expand Down Expand Up @@ -81,9 +101,9 @@ export function prepareBlock(mustache, program, inverseAndProgram, close, invert
}

if (inverted) {
return new this.BlockNode(mustache, inverse, program, strip, locInfo);
return new this.BlockNode(openBlock.sexpr, inverse, program, strip, locInfo);
} else {
return new this.BlockNode(mustache, program, inverse, strip, locInfo);
return new this.BlockNode(openBlock.sexpr, program, inverse, strip, locInfo);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/handlebars/compiler/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ PrintVisitor.prototype.block = function(block) {

out = out + this.pad("BLOCK:");
this.padding++;
out = out + this.accept(block.mustache);
out = out + this.pad(this.accept(block.sexpr));
if (block.program) {
out = out + this.pad("PROGRAM:");
this.padding++;
Expand Down
2 changes: 1 addition & 1 deletion spec/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('ast', function() {
it('should throw on mustache mismatch', function() {
shouldThrow(function() {
handlebarsEnv.parse("\n {{#foo}}{{/bar}}");
}, Handlebars.Exception, "foo doesn't match bar - 2:2");
}, Handlebars.Exception, "foo doesn't match bar - 2:5");
});

it('stores location info', function(){
Expand Down
24 changes: 12 additions & 12 deletions spec/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,51 +109,51 @@ describe('parser', function() {
});

it('parses an inverse section', function() {
equals(ast_for("{{#foo}} bar {{^}} baz {{/foo}}"), "BLOCK:\n {{ ID:foo [] }}\n PROGRAM:\n CONTENT[ ' bar ' ]\n {{^}}\n CONTENT[ ' baz ' ]\n");
equals(ast_for("{{#foo}} bar {{^}} baz {{/foo}}"), "BLOCK:\n ID:foo []\n PROGRAM:\n CONTENT[ ' bar ' ]\n {{^}}\n CONTENT[ ' baz ' ]\n");
});

it('parses an inverse (else-style) section', function() {
equals(ast_for("{{#foo}} bar {{else}} baz {{/foo}}"), "BLOCK:\n {{ ID:foo [] }}\n PROGRAM:\n CONTENT[ ' bar ' ]\n {{^}}\n CONTENT[ ' baz ' ]\n");
equals(ast_for("{{#foo}} bar {{else}} baz {{/foo}}"), "BLOCK:\n ID:foo []\n PROGRAM:\n CONTENT[ ' bar ' ]\n {{^}}\n CONTENT[ ' baz ' ]\n");
});

it('parses multiple inverse sections', function() {
equals(ast_for("{{#foo}} bar {{else if bar}}{{else}} baz {{/foo}}"), "BLOCK:\n {{ ID:foo [] }}\n PROGRAM:\n CONTENT[ ' bar ' ]\n {{^}}\n BLOCK:\n {{ ID:if [ID:bar] }}\n PROGRAM:\n {{^}}\n CONTENT[ ' baz ' ]\n");
equals(ast_for("{{#foo}} bar {{else if bar}}{{else}} baz {{/foo}}"), "BLOCK:\n ID:foo []\n PROGRAM:\n CONTENT[ ' bar ' ]\n {{^}}\n BLOCK:\n ID:if [ID:bar]\n PROGRAM:\n {{^}}\n CONTENT[ ' baz ' ]\n");
});

it('parses empty blocks', function() {
equals(ast_for("{{#foo}}{{/foo}}"), "BLOCK:\n {{ ID:foo [] }}\n PROGRAM:\n");
equals(ast_for("{{#foo}}{{/foo}}"), "BLOCK:\n ID:foo []\n PROGRAM:\n");
});

it('parses empty blocks with empty inverse section', function() {
equals(ast_for("{{#foo}}{{^}}{{/foo}}"), "BLOCK:\n {{ ID:foo [] }}\n PROGRAM:\n {{^}}\n");
equals(ast_for("{{#foo}}{{^}}{{/foo}}"), "BLOCK:\n ID:foo []\n PROGRAM:\n {{^}}\n");
});

it('parses empty blocks with empty inverse (else-style) section', function() {
equals(ast_for("{{#foo}}{{else}}{{/foo}}"), "BLOCK:\n {{ ID:foo [] }}\n PROGRAM:\n {{^}}\n");
equals(ast_for("{{#foo}}{{else}}{{/foo}}"), "BLOCK:\n ID:foo []\n PROGRAM:\n {{^}}\n");
});

it('parses non-empty blocks with empty inverse section', function() {
equals(ast_for("{{#foo}} bar {{^}}{{/foo}}"), "BLOCK:\n {{ ID:foo [] }}\n PROGRAM:\n CONTENT[ ' bar ' ]\n {{^}}\n");
equals(ast_for("{{#foo}} bar {{^}}{{/foo}}"), "BLOCK:\n ID:foo []\n PROGRAM:\n CONTENT[ ' bar ' ]\n {{^}}\n");
});

it('parses non-empty blocks with empty inverse (else-style) section', function() {
equals(ast_for("{{#foo}} bar {{else}}{{/foo}}"), "BLOCK:\n {{ ID:foo [] }}\n PROGRAM:\n CONTENT[ ' bar ' ]\n {{^}}\n");
equals(ast_for("{{#foo}} bar {{else}}{{/foo}}"), "BLOCK:\n ID:foo []\n PROGRAM:\n CONTENT[ ' bar ' ]\n {{^}}\n");
});

it('parses empty blocks with non-empty inverse section', function() {
equals(ast_for("{{#foo}}{{^}} bar {{/foo}}"), "BLOCK:\n {{ ID:foo [] }}\n PROGRAM:\n {{^}}\n CONTENT[ ' bar ' ]\n");
equals(ast_for("{{#foo}}{{^}} bar {{/foo}}"), "BLOCK:\n ID:foo []\n PROGRAM:\n {{^}}\n CONTENT[ ' bar ' ]\n");
});

it('parses empty blocks with non-empty inverse (else-style) section', function() {
equals(ast_for("{{#foo}}{{else}} bar {{/foo}}"), "BLOCK:\n {{ ID:foo [] }}\n PROGRAM:\n {{^}}\n CONTENT[ ' bar ' ]\n");
equals(ast_for("{{#foo}}{{else}} bar {{/foo}}"), "BLOCK:\n ID:foo []\n PROGRAM:\n {{^}}\n CONTENT[ ' bar ' ]\n");
});

it('parses a standalone inverse section', function() {
equals(ast_for("{{^foo}}bar{{/foo}}"), "BLOCK:\n {{ ID:foo [] }}\n {{^}}\n CONTENT[ 'bar' ]\n");
equals(ast_for("{{^foo}}bar{{/foo}}"), "BLOCK:\n ID:foo []\n {{^}}\n CONTENT[ 'bar' ]\n");
});
it('throws on old inverse section', function() {
shouldThrow(function() {
equals(ast_for("{{else foo}}bar{{/foo}}"), "BLOCK:\n {{ ID:foo [] }}\n {{^}}\n CONTENT[ 'bar' ]\n");
ast_for("{{else foo}}bar{{/foo}}");
}, Error);
});

Expand Down
14 changes: 9 additions & 5 deletions src/handlebars.yy
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ statement
| block -> $1
| rawBlock -> $1
| partial -> $1
| CONTENT -> new yy.ContentNode($1, @$)
| content -> $1
| COMMENT -> new yy.CommentNode(yy.stripComment($1), yy.stripFlags($1, $1), @$)
;

content
: CONTENT -> new yy.ContentNode($1, @$)
;

rawBlock
: openRawBlock CONTENT END_RAW_BLOCK -> new yy.RawBlockNode($1, $2, $3, @$)
: openRawBlock content END_RAW_BLOCK -> yy.prepareRawBlock($1, $2, $3, @$)
;

openRawBlock
: OPEN_RAW_BLOCK sexpr CLOSE_RAW_BLOCK -> new yy.MustacheNode($2, null, '', '', @$)
: OPEN_RAW_BLOCK sexpr CLOSE_RAW_BLOCK -> { sexpr: $2 }
;

block
Expand All @@ -35,11 +39,11 @@ block
;

openBlock
: OPEN_BLOCK sexpr CLOSE -> new yy.MustacheNode($2, null, $1, yy.stripFlags($1, $3), @$)
: OPEN_BLOCK sexpr CLOSE -> { sexpr: $2, strip: yy.stripFlags($1, $3) }
;

openInverse
: OPEN_INVERSE sexpr CLOSE -> new yy.MustacheNode($2, null, $1, yy.stripFlags($1, $3), @$)
: OPEN_INVERSE sexpr CLOSE -> { sexpr: $2, strip: yy.stripFlags($1, $3) }
;

openInverseChain
Expand Down

0 comments on commit 4415f31

Please sign in to comment.