Skip to content

Commit 8937e27

Browse files
committed
fix: v-if nested in a v-for template tag
1 parent e5ce42a commit 8937e27

File tree

1 file changed

+39
-39
lines changed

1 file changed

+39
-39
lines changed

src/directives/if.ts

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,77 @@
1-
import { Block } from '../block'
2-
import { evaluate } from '../eval'
3-
import { checkAttr } from '../utils'
4-
import { Context } from '../context'
1+
import { Block } from '../block';
2+
import { evaluate } from '../eval';
3+
import { checkAttr } from '../utils';
4+
import { Context } from '../context';
55

66
interface Branch {
7-
exp?: string | null
8-
el: Element
7+
exp?: string | null;
8+
el: Element;
99
}
1010

1111
export const _if = (el: Element, exp: string, ctx: Context) => {
1212
if (import.meta.env.DEV && !exp.trim()) {
13-
console.warn(`v-if expression cannot be empty.`)
13+
console.warn(`v-if expression cannot be empty.`);
1414
}
1515

16-
const parent = el.parentElement!
17-
const anchor = new Comment('v-if')
18-
parent.insertBefore(anchor, el)
16+
const parent = el.parentElement ? el.parentElement : el.parentNode!;
17+
const anchor = new Comment('v-if');
18+
parent.insertBefore(anchor, el);
1919

2020
const branches: Branch[] = [
2121
{
2222
exp,
23-
el
24-
}
25-
]
23+
el,
24+
},
25+
];
2626

2727
// locate else branch
28-
let elseEl: Element | null
29-
let elseExp: string | null
28+
let elseEl: Element | null;
29+
let elseExp: string | null;
3030
while ((elseEl = el.nextElementSibling)) {
31-
elseExp = null
31+
elseExp = null;
3232
if (
3333
checkAttr(elseEl, 'v-else') === '' ||
3434
(elseExp = checkAttr(elseEl, 'v-else-if'))
3535
) {
36-
parent.removeChild(elseEl)
37-
branches.push({ exp: elseExp, el: elseEl })
36+
parent.removeChild(elseEl);
37+
branches.push({ exp: elseExp, el: elseEl });
3838
} else {
39-
break
39+
break;
4040
}
4141
}
4242

43-
const nextNode = el.nextSibling
44-
parent.removeChild(el)
43+
const nextNode = el.nextSibling;
44+
parent.removeChild(el);
4545

46-
let block: Block | undefined
47-
let activeBranchIndex: number = -1
46+
let block: Block | undefined;
47+
let activeBranchIndex: number = -1;
4848

4949
const removeActiveBlock = () => {
5050
if (block) {
51-
parent.insertBefore(anchor, block.el)
52-
block.remove()
53-
block = undefined
51+
parent.insertBefore(anchor, block.el);
52+
block.remove();
53+
block = undefined;
5454
}
55-
}
55+
};
5656

5757
ctx.effect(() => {
5858
for (let i = 0; i < branches.length; i++) {
59-
const { exp, el } = branches[i]
59+
const { exp, el } = branches[i];
6060
if (!exp || evaluate(ctx.scope, exp)) {
6161
if (i !== activeBranchIndex) {
62-
removeActiveBlock()
63-
block = new Block(el, ctx)
64-
block.insert(parent, anchor)
65-
parent.removeChild(anchor)
66-
activeBranchIndex = i
62+
removeActiveBlock();
63+
block = new Block(el, ctx);
64+
block.insert(parent, anchor);
65+
parent.removeChild(anchor);
66+
activeBranchIndex = i;
6767
}
68-
return
68+
return;
6969
}
7070
}
7171
// no matched branch.
72-
activeBranchIndex = -1
73-
removeActiveBlock()
74-
})
72+
activeBranchIndex = -1;
73+
removeActiveBlock();
74+
});
7575

76-
return nextNode
77-
}
76+
return nextNode;
77+
};

0 commit comments

Comments
 (0)