Skip to content

Commit 42626c0

Browse files
committed
Embedded helper produces invalid template for {{else if}} block fix #619
1 parent 5375ccf commit 42626c0

4 files changed

Lines changed: 61 additions & 37 deletions

File tree

handlebars/src/main/java/com/github/jknack/handlebars/internal/Block.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public Template body() {
309309

310310
@Override
311311
public String text() {
312-
return text(true);
312+
return text(true, true);
313313
}
314314

315315
/**
@@ -318,7 +318,7 @@ public String text() {
318318
* @param complete True if the inner block should be added.
319319
* @return A string version of this block.
320320
*/
321-
private String text(final boolean complete) {
321+
private String text(final boolean complete, boolean close) {
322322
StringBuilder buffer = new StringBuilder();
323323
buffer.append(startDelimiter).append(type).append(name);
324324
String params = paramsToString(this.params);
@@ -335,15 +335,25 @@ private String text(final boolean complete) {
335335
buffer.append(endDelimiter);
336336
if (complete) {
337337
buffer.append(body == null ? "" : body.text());
338-
buffer.append(inverse == Template.EMPTY ? "" : "{{" + inverseLabel + "}}" + inverse.text());
338+
if (inverse != EMPTY) {
339+
if (inverse instanceof Block) {
340+
String elseif = ((Block) inverse).text(true, false);
341+
buffer.append(elseif);
342+
} else {
343+
buffer.append(startDelimiter).append(inverseLabel).append(endDelimiter)
344+
.append(inverse.text());
345+
}
346+
}
339347
} else {
340348
buffer.append("\n...\n");
341349
}
342-
buffer.append(startDelimiter);
343-
if (type.equals("{{")) {
344-
buffer.append("{{");
350+
if (close) {
351+
buffer.append(startDelimiter);
352+
if (type.equals("{{")) {
353+
buffer.append("{{");
354+
}
355+
buffer.append('/').append(name).append(endDelimiter);
345356
}
346-
buffer.append('/').append(name).append(endDelimiter);
347357
return buffer.toString();
348358
}
349359

handlebars/src/main/java/com/github/jknack/handlebars/internal/TemplateBuilder.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public Template visitBlock(final BlockContext ctx) {
216216
}
217217

218218
hasTag(true);
219-
Block block = null;
219+
Block block;
220220
if (decorator) {
221221
block = new BlockDecorator(handlebars, name, false, params(sexpr.param()),
222222
hash(sexpr.hash()), blockParams(ctx.blockParams()), level == 1);
@@ -258,12 +258,16 @@ public Template visitBlock(final BlockContext ctx) {
258258
SexprContext elseexpr = elseStmtChain.sexpr();
259259
Token elsenameStart = elseexpr.QID().getSymbol();
260260
String elsename = elsenameStart.getText();
261-
Block elseblock = new Block(handlebars, elsename, false, "#", params(elseexpr.param()),
261+
String type = elseStmtChain.inverseToken.getText();
262+
if (type.equals("else")) {
263+
type = "else ";
264+
}
265+
Block elseblock = new Block(handlebars, elsename, false, type, params(elseexpr.param()),
262266
hash(elseexpr.hash()), blockParams(elseStmtChain.blockParams()));
263267
elseblock.filename(source.filename());
264268
elseblock.position(elsenameStart.getLine(), elsenameStart.getCharPositionInLine());
265269
elseblock.startDelimiter(startDelim);
266-
elseblock.endDelimiter(elseBlock.stop.getText());
270+
elseblock.endDelimiter(elseStmtChain.END().getText());
267271
Template elsebody = visitBody(elseStmtChain.unlessBody);
268272
elseblock.body(elsebody);
269273

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,14 @@
11
package com.github.jknack.handlebars.issues;
22

3-
import com.github.jknack.handlebars.Handlebars;
43
import com.github.jknack.handlebars.v4Test;
5-
import org.junit.BeforeClass;
6-
import org.junit.Test;
7-
84
import static org.junit.Assert.assertEquals;
9-
10-
import java.io.IOException;
11-
import java.util.Arrays;
5+
import org.junit.Test;
126

137
public class Issue614 extends v4Test {
148

15-
@Override protected void configure(Handlebars handlebars) {
16-
try {
17-
handlebars.registerHelpers("helpers.js",
18-
"Handlebars.registerHelper('number', function(value) {\n"
19-
+ " return value;\n"
20-
+ "});\n"
21-
+ "\n"
22-
+ "Handlebars.registerHelper('len', function(array) {\n"
23-
+ " return array.length;\n"
24-
+ "});");
25-
} catch (Exception x) {
26-
throw new IllegalStateException(x);
27-
}
28-
}
29-
309
@Test
3110
public void shouldFavorIntOverDouble() throws Exception {
32-
shouldCompileTo("{{number this}}", $("hash", 3), "3");
33-
shouldCompileTo("{{number this}}", $("hash", 3.1), "3.1");
34-
35-
shouldCompileTo("{{len this}}", $("hash", new Object[]{1, 2, 3, 4}), "4");
36-
shouldCompileTo("{{len this}}", $("hash", Arrays.asList(1, 2, 3, 4)), "4");
11+
String text = compile("{{#if a}}a{{else if b}}b{{else}}c{{/if}}").text();
12+
assertEquals("{{#if a}}a{{else if b}}b{{else}}c{{/if}}", text);
3713
}
3814
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.github.jknack.handlebars.issues;
2+
3+
import com.github.jknack.handlebars.Handlebars;
4+
import com.github.jknack.handlebars.v4Test;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
9+
public class Issue619 extends v4Test {
10+
11+
@Override protected void configure(Handlebars handlebars) {
12+
try {
13+
handlebars.registerHelpers("helpers.js",
14+
"Handlebars.registerHelper('number', function(value) {\n"
15+
+ " return value;\n"
16+
+ "});\n"
17+
+ "\n"
18+
+ "Handlebars.registerHelper('len', function(array) {\n"
19+
+ " return array.length;\n"
20+
+ "});");
21+
} catch (Exception x) {
22+
throw new IllegalStateException(x);
23+
}
24+
}
25+
26+
@Test
27+
public void shouldFavorIntOverDouble() throws Exception {
28+
shouldCompileTo("{{number this}}", $("hash", 3), "3");
29+
shouldCompileTo("{{number this}}", $("hash", 3.1), "3.1");
30+
31+
shouldCompileTo("{{len this}}", $("hash", new Object[]{1, 2, 3, 4}), "4");
32+
shouldCompileTo("{{len this}}", $("hash", Arrays.asList(1, 2, 3, 4)), "4");
33+
}
34+
}

0 commit comments

Comments
 (0)