Skip to content

Commit

Permalink
fix if/else binding
Browse files Browse the repository at this point in the history
  • Loading branch information
1cg committed Aug 4, 2023
1 parent 1a3a5fb commit 1f65858
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/_hyperscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,9 @@
peekToken(value, peek, type) {
peek = peek || 0;
type = type || "IDENTIFIER";
return this.tokens[peek] && this.tokens[peek].value === value && this.tokens[peek].type === type
if(this.tokens[peek] && this.tokens[peek].value === value && this.tokens[peek].type === type){
return this.tokens[peek];
}
}

/**
Expand Down Expand Up @@ -5526,9 +5528,15 @@
tokens.matchToken("then"); // optional 'then'
var trueBranch = parser.parseElement("commandList", tokens);
var nestedIfStmt = false;
if (tokens.matchToken("else") || tokens.matchToken("otherwise")) {
nestedIfStmt = tokens.peekToken("if");
var falseBranch = parser.parseElement("commandList", tokens);
let elseToken = tokens.matchToken("else") || tokens.matchToken("otherwise");
if (elseToken) {
let elseIfIfToken = tokens.peekToken("if");
nestedIfStmt = elseIfIfToken != null && elseIfIfToken.line === elseToken.line;
if (nestedIfStmt) {
var falseBranch = parser.parseElement("command", tokens);
} else {
var falseBranch = parser.parseElement("commandList", tokens);
}
}
if (tokens.hasMore() && !nestedIfStmt) {
tokens.requireToken("end");
Expand Down
22 changes: 22 additions & 0 deletions test/commands/if.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,26 @@ describe("the if command", function () {
delete window.tmp;
});

it("if on new line does not join w/ else", function () {
window.tmp = false
var d1 = make("<div _='on click \n" +
" if window.tmp then\n" +
" else\n" +
" if window.tmp then" +
" end\n" +
" put \"foo\" into me\n" +
" end\n" +
" '" +
"</div>");
d1.click();
d1.innerHTML.should.equal("foo");

window.tmp = true
d1.innerHTML = "";
d1.click();
d1.innerHTML.should.equal("");

delete window.tmp;
});

});

0 comments on commit 1f65858

Please sign in to comment.