Skip to content

Commit eb6ceb8

Browse files
committed
Fixed rendering error when using nested higher-order sections
Fixes #274
1 parent b9701d5 commit eb6ceb8

File tree

2 files changed

+22
-39
lines changed

2 files changed

+22
-39
lines changed

mustache.js

+7-24
Original file line numberDiff line numberDiff line change
@@ -301,24 +301,6 @@ var Mustache;
301301
return exports.escape(this._name(name, context));
302302
};
303303

304-
/**
305-
* Calculates the bounds of the section represented by the given `token` in
306-
* the original template by drilling down into nested sections to find the
307-
* last token that is part of that section. Returns an array of [start, end].
308-
*/
309-
function sectionBounds(token) {
310-
var start = token[3];
311-
var end = start;
312-
313-
var tokens;
314-
while ((tokens = token[4]) && tokens.length) {
315-
token = tokens[tokens.length - 1];
316-
end = token[3];
317-
}
318-
319-
return [start, end];
320-
}
321-
322304
/**
323305
* Low-level function that compiles the given `tokens` into a function
324306
* that accepts three arguments: a Writer, a Context, and the template.
@@ -346,7 +328,7 @@ var Mustache;
346328

347329
switch (token[0]) {
348330
case "#":
349-
sectionText = template.slice.apply(template, sectionBounds(token));
331+
sectionText = template.slice(token[3], token[5]);
350332
buffer += writer._section(token[1], context, sectionText, subRender(i, token[4], template));
351333
break;
352334
case "^":
@@ -373,8 +355,9 @@ var Mustache;
373355

374356
/**
375357
* Forms the given array of `tokens` into a nested tree structure where
376-
* tokens that represent a section have a fifth item: an array that contains
377-
* all tokens in that section.
358+
* tokens that represent a section have two additional items: 1) an array of
359+
* all tokens that appear in that section and 2) the index in the original
360+
* template that represents the end of that section.
378361
*/
379362
function nestTokens(tokens) {
380363
var tree = [];
@@ -392,9 +375,9 @@ var Mustache;
392375
collector = token[4] = [];
393376
break;
394377
case '/':
395-
sections.pop();
396-
var length = sections.length;
397-
collector = length > 0 ? sections[length - 1][4] : tree;
378+
var section = sections.pop();
379+
section[5] = token[2];
380+
collector = sections.length > 0 ? sections[sections.length - 1][4] : tree;
398381
break;
399382
default:
400383
collector.push(token);

test/parse-test.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,29 @@ var expectations = {
2828
'a\n{{hi}} b \n' : [ [ 'text', 'a\n', 0, 2 ], [ 'name', 'hi', 2, 8 ], [ 'text', ' b \n', 8, 12 ] ],
2929
'a\n {{hi}} \nb' : [ [ 'text', 'a\n ', 0, 3 ], [ 'name', 'hi', 3, 9 ], [ 'text', ' \nb', 9, 12 ] ],
3030
'a\n {{!hi}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '!', 'hi', 3, 10 ], [ 'text', 'b', 12, 13 ] ],
31-
'a\n{{#a}}{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [] ], [ 'text', 'b', 15, 16 ] ],
32-
'a\n {{#a}}{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [] ], [ 'text', 'b', 16, 17 ] ],
33-
'a\n {{#a}}{{/a}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [] ], [ 'text', 'b', 17, 18 ] ],
34-
'a\n{{#a}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [] ], [ 'text', 'b', 16, 17 ] ],
35-
'a\n {{#a}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [] ], [ 'text', 'b', 17, 18 ] ],
36-
'a\n {{#a}}\n{{/a}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [] ], [ 'text', 'b', 18, 19 ] ],
37-
'a\n{{#a}}\n{{/a}}\n{{#b}}\n{{/b}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [] ], [ '#', 'b', 16, 22, [] ], [ 'text', 'b', 30, 31 ] ],
38-
'a\n {{#a}}\n{{/a}}\n{{#b}}\n{{/b}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [] ], [ '#', 'b', 17, 23, [] ], [ 'text', 'b', 31, 32 ] ],
39-
'a\n {{#a}}\n{{/a}}\n{{#b}}\n{{/b}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [] ], [ '#', 'b', 17, 23, [] ], [ 'text', 'b', 32, 33 ] ],
40-
'a\n{{#a}}\n{{#b}}\n{{/b}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [ [ '#', 'b', 9, 15, [] ] ] ], [ 'text', 'b', 30, 31 ] ],
41-
'a\n {{#a}}\n{{#b}}\n{{/b}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [ [ '#', 'b', 10, 16, [] ] ] ], [ 'text', 'b', 31, 32 ] ],
42-
'a\n {{#a}}\n{{#b}}\n{{/b}}\n{{/a}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [ [ '#', 'b', 10, 16, [] ] ] ], [ 'text', 'b', 32, 33 ] ],
31+
'a\n{{#a}}{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [], 8 ], [ 'text', 'b', 15, 16 ] ],
32+
'a\n {{#a}}{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 9 ], [ 'text', 'b', 16, 17 ] ],
33+
'a\n {{#a}}{{/a}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 9 ], [ 'text', 'b', 17, 18 ] ],
34+
'a\n{{#a}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [], 9 ], [ 'text', 'b', 16, 17 ] ],
35+
'a\n {{#a}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 10 ], [ 'text', 'b', 17, 18 ] ],
36+
'a\n {{#a}}\n{{/a}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 10 ], [ 'text', 'b', 18, 19 ] ],
37+
'a\n{{#a}}\n{{/a}}\n{{#b}}\n{{/b}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [], 9 ], [ '#', 'b', 16, 22, [], 23 ], [ 'text', 'b', 30, 31 ] ],
38+
'a\n {{#a}}\n{{/a}}\n{{#b}}\n{{/b}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 10 ], [ '#', 'b', 17, 23, [], 24 ], [ 'text', 'b', 31, 32 ] ],
39+
'a\n {{#a}}\n{{/a}}\n{{#b}}\n{{/b}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 10 ], [ '#', 'b', 17, 23, [], 24 ], [ 'text', 'b', 32, 33 ] ],
40+
'a\n{{#a}}\n{{#b}}\n{{/b}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [ [ '#', 'b', 9, 15, [], 16 ] ], 23 ], [ 'text', 'b', 30, 31 ] ],
41+
'a\n {{#a}}\n{{#b}}\n{{/b}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [ [ '#', 'b', 10, 16, [], 17 ] ], 24 ], [ 'text', 'b', 31, 32 ] ],
42+
'a\n {{#a}}\n{{#b}}\n{{/b}}\n{{/a}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [ [ '#', 'b', 10, 16, [], 17 ] ], 24 ], [ 'text', 'b', 32, 33 ] ],
4343
'{{>abc}}' : [ [ '>', 'abc', 0, 8 ] ],
4444
'{{> abc }}' : [ [ '>', 'abc', 0, 10 ] ],
4545
'{{ > abc }}' : [ [ '>', 'abc', 0, 11 ] ],
4646
'{{=<% %>=}}' : [ [ '=', '<% %>', 0, 11 ] ],
4747
'{{= <% %> =}}' : [ [ '=', '<% %>', 0, 13 ] ],
4848
'{{=<% %>=}}<%={{ }}=%>' : [ [ '=', '<% %>', 0, 11 ], [ '=', '{{ }}', 11, 22 ] ],
4949
'{{=<% %>=}}<%hi%>' : [ [ '=', '<% %>', 0, 11 ], [ 'name', 'hi', 11, 17 ] ],
50-
'{{#a}}{{/a}}hi{{#b}}{{/b}}\n' : [ [ '#', 'a', 0, 6, [] ], [ 'text', 'hi', 12, 14 ], [ '#', 'b', 14, 20, [] ], [ 'text', '\n', 26, 27 ] ],
51-
'{{a}}\n{{b}}\n\n{{#c}}\n{{/c}}\n' : [ [ 'name', 'a', 0, 5 ], [ 'text', '\n', 5, 6 ], [ 'name', 'b', 6, 11 ], [ 'text', '\n\n', 11, 13 ], [ '#', 'c', 13, 19, [] ] ],
50+
'{{#a}}{{/a}}hi{{#b}}{{/b}}\n' : [ [ '#', 'a', 0, 6, [], 6 ], [ 'text', 'hi', 12, 14 ], [ '#', 'b', 14, 20, [], 20 ], [ 'text', '\n', 26, 27 ] ],
51+
'{{a}}\n{{b}}\n\n{{#c}}\n{{/c}}\n' : [ [ 'name', 'a', 0, 5 ], [ 'text', '\n', 5, 6 ], [ 'name', 'b', 6, 11 ], [ 'text', '\n\n', 11, 13 ], [ '#', 'c', 13, 19, [], 20 ] ],
5252
'{{#foo}}\n {{#a}}\n {{b}}\n {{/a}}\n{{/foo}}\n'
53-
: [ [ '#', 'foo', 0, 8, [ [ '#', 'a', 11, 17, [ [ 'text', ' ', 18, 22 ], [ 'name', 'b', 22, 27 ], [ 'text', '\n', 27, 28 ] ] ] ] ] ]
53+
: [ [ '#', 'foo', 0, 8, [ [ '#', 'a', 11, 17, [ [ 'text', ' ', 18, 22 ], [ 'name', 'b', 22, 27 ], [ 'text', '\n', 27, 28 ] ], 30 ] ], 37 ] ]
5454
};
5555

5656
describe('Mustache.parse', function () {

0 commit comments

Comments
 (0)