handlebars-lang / handlebars.js

Minimal templating on steroids.
http://handlebarsjs.com
MIT License
17.81k stars 2.04k forks source link

Whitespace control in one {{else if}} branch also affects a different branch #2031

Open andersk opened 3 months ago

andersk commented 3 months ago

In this test case, I’ve used whitespace control on just one side of just one {{else if}} branch:

const Handlebars = require("handlebars");
const source = `\
{{#if a}}
a
{{else if b}}
b
{{else if c}}
c
{{~else if d}}
d
{{else if e}}
e
{{else if f}}
f
{{else if g}}
g
{{/if}}
`;
const template = Handlebars.compile(source);
console.log(JSON.stringify(template({a: 1})));
console.log(JSON.stringify(template({b: 1})));
console.log(JSON.stringify(template({c: 1})));
console.log(JSON.stringify(template({d: 1})));
console.log(JSON.stringify(template({e: 1})));
console.log(JSON.stringify(template({f: 1})));
console.log(JSON.stringify(template({g: 1})));

But two of the outputs are affected:

"a\n"
"b\n"
"c"
"d\n"
"e"
"f\n"
"g\n"

I expect the whitespace control to only affect "c", so "e" should be "e\n".

Using Handlebars 4.7.8 on Node.js 20.11.1.

Playground link.