Skip to content

Commit f1edc35

Browse files
committed
Whitespace control else failure fix #478
1 parent 22bc4b3 commit f1edc35

5 files changed

Lines changed: 30 additions & 4 deletions

File tree

handlebars/src/main/antlr4/com/github/jknack/handlebars/internal/HbsLexer.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ BOOLEAN
293293

294294
ELSE
295295
:
296-
'~'? 'else' '~'?
296+
'else'
297297
;
298298

299299
QID

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public boolean decorate() {
261261
public Template inverse(final String inverseLabel, final Template inverse) {
262262
notNull(inverseLabel, "The inverseLabel can't be null.");
263263
isTrue(inverseLabel.equals("^") || inverseLabel.equals("else"),
264-
"The inverseLabel must be one of '^' or 'else'.");
264+
"The inverseLabel must be one of '^' or 'else'. Found: " + inverseLabel);
265265
this.inverseLabel = inverseLabel;
266266
this.inverse = notNull(inverse, "The inverse's template is required.");
267267
return this;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,10 @@ public Template visitBlock(final BlockContext ctx) {
229229
block.filename(source.filename());
230230
block.position(nameStart.getLine(), nameStart.getCharPositionInLine());
231231
String startDelim = ctx.start.getText();
232+
String endDelim = ctx.stop.getText();
232233
startDelim = startDelim.substring(0, startDelim.length() - 1);
233234
block.startDelimiter(startDelim);
234-
block.endDelimiter(ctx.stop.getText());
235+
block.endDelimiter(endDelim);
235236

236237
Template body = visitBody(ctx.thenBody);
237238
if (body != null) {
@@ -249,6 +250,9 @@ public Template visitBlock(final BlockContext ctx) {
249250
if (inverseLabel.startsWith(startDelim)) {
250251
inverseLabel = inverseLabel.substring(startDelim.length());
251252
}
253+
if (inverseLabel.endsWith("~")) {
254+
inverseLabel = inverseLabel.substring(0, inverseLabel.length() - 1);
255+
}
252256
elseroot.inverse(inverseLabel, unless);
253257
}
254258
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ private void trim(final Token startToken, final Token endToken) {
167167
}
168168

169169
String end = text(endToken);
170-
if (end.indexOf("~") == 0) {
170+
if (end.indexOf("~") >= 0) {
171171
pending += 1;
172172
}
173173

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.github.jknack.handlebars.issues;
2+
3+
import com.github.jknack.handlebars.v4Test;
4+
import org.junit.Test;
5+
6+
import java.io.IOException;
7+
8+
public class Issue478 extends v4Test {
9+
10+
@Test
11+
public void whiteSpaceControlShouldWorkOnElse() throws IOException {
12+
shouldCompileTo("\n{{~#if bold~}}\n<b>\n{{else}}\n<i>\n{{~/if~}}\n", $("hash", $("bold", true)), "<b>\n");
13+
shouldCompileTo("\n{{~#if bold~}}\n<b>\n{{else}}\n<i>\n{{~/if~}}\n", $("hash", $("bold", false)), "\n<i>");
14+
15+
shouldCompileTo("\n{{~#if bold~}}\n<b>\n{{~else~}}\n<i>\n{{~/if~}}\n", $("hash", $("bold", true)), "<b>");
16+
shouldCompileTo("\n{{~#if bold~}}\n<b>\n{{~else~}}\n<i>\n{{~/if~}}\n", $("hash", $("bold", false)), "<i>");
17+
18+
shouldCompileTo("\n{{~#if bold~}}\n<b>\n{{~^~}}\n<i>\n{{~/if~}}\n", $("hash", $("bold", true)), "<b>");
19+
shouldCompileTo("\n{{~#if bold~}}\n<b>\n{{~^~}}\n<i>\n{{~/if~}}\n", $("hash", $("bold", false)), "<i>");
20+
}
21+
22+
}

0 commit comments

Comments
 (0)