Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE - 1121] Change parsing rule of block to disallow multiple else #1154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ block
:
startToken = START_BLOCK DECORATOR? sexpr blockParams? END
thenBody=body
elseBlock*
elseBlock
END_BLOCK nameEnd=QID END
;

Expand All @@ -97,8 +97,8 @@ sexpr

elseBlock
:
elseStmt
| elseStmtChain
elseStmtChain*
elseStmt?
;

elseStmt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,53 +240,53 @@ public Template visitBlock(final BlockContext ctx) {
}
// else
Block elseroot = block;
for (ElseBlockContext elseBlock : ctx.elseBlock()) {
ElseStmtContext elseStmt = elseBlock.elseStmt();
if (elseStmt != null) {
// basic else
Template unless = visitBody(elseStmt.unlessBody);
if (unless != null) {
String inverseLabel = elseStmt.inverseToken.getText();
if (inverseLabel.startsWith(startDelim)) {
inverseLabel = inverseLabel.substring(startDelim.length());
}
if (inverseLabel.endsWith("~")) {
inverseLabel = inverseLabel.substring(0, inverseLabel.length() - 1);
}
elseroot.inverse(inverseLabel, unless);
}
} else {
// else chain
ElseStmtChainContext elseStmtChain = elseBlock.elseStmtChain();
SexprContext elseexpr = elseStmtChain.sexpr();
Token elsenameStart = elseexpr.QID().getSymbol();
String elsename = elsenameStart.getText();
String type = elseStmtChain.inverseToken.getText();
if (type.equals("else")) {
type = "else ";
}
Block elseblock =
new Block(
handlebars,
elsename,
false,
type,
params(elseexpr.param()),
hash(elseexpr.hash()),
blockParams(elseStmtChain.blockParams()));
elseblock.filename(source.filename());
elseblock.position(elsenameStart.getLine(), elsenameStart.getCharPositionInLine());
elseblock.startDelimiter(startDelim);
elseblock.endDelimiter(elseStmtChain.END().getText());
Template elsebody = visitBody(elseStmtChain.unlessBody);
elseblock.body(elsebody);

String inverseLabel = elseStmtChain.inverseToken.getText();
ElseBlockContext elseBlock = ctx.elseBlock();
ElseStmtContext elseStmt = elseBlock.elseStmt();
List<ElseStmtChainContext> elseStmtChain = elseBlock.elseStmtChain();
for (ElseStmtChainContext elseStmtChainContext : elseStmtChain) {
// else if chain
SexprContext elseexpr = elseStmtChainContext.sexpr();
Token elsenameStart = elseexpr.QID().getSymbol();
String elsename = elsenameStart.getText();
String type = elseStmtChainContext.inverseToken.getText();
if (type.equals("else")) {
type = "else ";
}
Block elseblock =
new Block(
handlebars,
elsename,
false,
type,
params(elseexpr.param()),
hash(elseexpr.hash()),
blockParams(elseStmtChainContext.blockParams()));
elseblock.filename(source.filename());
elseblock.position(elsenameStart.getLine(), elsenameStart.getCharPositionInLine());
elseblock.startDelimiter(startDelim);
elseblock.endDelimiter(elseStmtChainContext.END().getText());
Template elsebody = visitBody(elseStmtChainContext.unlessBody);
elseblock.body(elsebody);

String inverseLabel = elseStmtChainContext.inverseToken.getText();
if (inverseLabel.startsWith(startDelim)) {
inverseLabel = inverseLabel.substring(startDelim.length());
}
elseroot.inverse(inverseLabel, elseblock);
elseroot = elseblock;
}
if (elseStmt != null) {
// basic else
Template unless = visitBody(elseStmt.unlessBody);
if (unless != null) {
String inverseLabel = elseStmt.inverseToken.getText();
if (inverseLabel.startsWith(startDelim)) {
inverseLabel = inverseLabel.substring(startDelim.length());
}
elseroot.inverse(inverseLabel, elseblock);
elseroot = elseblock;
if (inverseLabel.endsWith("~")) {
inverseLabel = inverseLabel.substring(0, inverseLabel.length() - 1);
}
elseroot.inverse(inverseLabel, unless);
}
}
hasTag(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public class ParsingErrorTest extends AbstractTest {
"idx3",
"{{list.[]}}",
"idx4",
"{{list.[}}");
"{{list.[}}",
"multipleElse",
"{{#if true}} b1 {{else}} b2 {{else}} b3 {{/if}}");

@Test
public void correctPath() throws IOException {
Expand Down Expand Up @@ -166,6 +168,11 @@ public void idx4() throws IOException {
assertThrows(HandlebarsException.class, () -> parse("idx4"));
}

@Test
public void multipleElse() throws IOException {
assertThrows(HandlebarsException.class, () -> parse("multipleElse"));
}

private void parse(final String candidate) throws IOException {
try {
String input = (String) source.get(candidate);
Expand Down