Fix else/elif tags for nested if statements
This commit is contained in:
parent
d618e7b0da
commit
a8023be2e2
18
if_tag.go
18
if_tag.go
@ -75,11 +75,20 @@ type elif struct {
|
|||||||
// findInner finds the inner elif and else tags in a block
|
// findInner finds the inner elif and else tags in a block
|
||||||
// passed to the if tag.
|
// passed to the if tag.
|
||||||
func (it ifTag) findInner(tc *TagContext, block []ast.Node) (innerTags, error) {
|
func (it ifTag) findInner(tc *TagContext, block []ast.Node) (innerTags, error) {
|
||||||
|
// Depth keeps track of nested if statements. We only want to look for
|
||||||
|
// else/elif tags within the current if tag, not any nested ones.
|
||||||
|
depth := 0
|
||||||
var out innerTags
|
var out innerTags
|
||||||
for i, node := range block {
|
for i, node := range block {
|
||||||
if tag, ok := node.(ast.Tag); ok {
|
switch tag := node.(type) {
|
||||||
|
case ast.Tag:
|
||||||
switch tag.Name.Value {
|
switch tag.Name.Value {
|
||||||
|
case "if":
|
||||||
|
depth++
|
||||||
case "elif":
|
case "elif":
|
||||||
|
if depth != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if out.endRoot == 0 {
|
if out.endRoot == 0 {
|
||||||
out.endRoot = i
|
out.endRoot = i
|
||||||
}
|
}
|
||||||
@ -91,6 +100,9 @@ func (it ifTag) findInner(tc *TagContext, block []ast.Node) (innerTags, error) {
|
|||||||
value: tag.Params[0],
|
value: tag.Params[0],
|
||||||
})
|
})
|
||||||
case "else":
|
case "else":
|
||||||
|
if depth != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if out.elseIndex != 0 {
|
if out.elseIndex != 0 {
|
||||||
return innerTags{}, tc.PosError(tag, "cannot have more than one else tag in an if tag")
|
return innerTags{}, tc.PosError(tag, "cannot have more than one else tag in an if tag")
|
||||||
}
|
}
|
||||||
@ -100,6 +112,10 @@ func (it ifTag) findInner(tc *TagContext, block []ast.Node) (innerTags, error) {
|
|||||||
out.elseIndex = i
|
out.elseIndex = i
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
case ast.EndTag:
|
||||||
|
if tag.Name.Value == "if" {
|
||||||
|
depth--
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if out.endRoot == 0 {
|
if out.endRoot == 0 {
|
||||||
|
Loading…
Reference in New Issue
Block a user