|
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'; |
5 | 5 |
|
6 | 6 | interface Branch { |
7 | | - exp?: string | null |
8 | | - el: Element |
| 7 | + exp?: string | null; |
| 8 | + el: Element; |
9 | 9 | } |
10 | 10 |
|
11 | 11 | export const _if = (el: Element, exp: string, ctx: Context) => { |
12 | 12 | 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.`); |
14 | 14 | } |
15 | 15 |
|
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); |
19 | 19 |
|
20 | 20 | const branches: Branch[] = [ |
21 | 21 | { |
22 | 22 | exp, |
23 | | - el |
24 | | - } |
25 | | - ] |
| 23 | + el, |
| 24 | + }, |
| 25 | + ]; |
26 | 26 |
|
27 | 27 | // locate else branch |
28 | | - let elseEl: Element | null |
29 | | - let elseExp: string | null |
| 28 | + let elseEl: Element | null; |
| 29 | + let elseExp: string | null; |
30 | 30 | while ((elseEl = el.nextElementSibling)) { |
31 | | - elseExp = null |
| 31 | + elseExp = null; |
32 | 32 | if ( |
33 | 33 | checkAttr(elseEl, 'v-else') === '' || |
34 | 34 | (elseExp = checkAttr(elseEl, 'v-else-if')) |
35 | 35 | ) { |
36 | | - parent.removeChild(elseEl) |
37 | | - branches.push({ exp: elseExp, el: elseEl }) |
| 36 | + parent.removeChild(elseEl); |
| 37 | + branches.push({ exp: elseExp, el: elseEl }); |
38 | 38 | } else { |
39 | | - break |
| 39 | + break; |
40 | 40 | } |
41 | 41 | } |
42 | 42 |
|
43 | | - const nextNode = el.nextSibling |
44 | | - parent.removeChild(el) |
| 43 | + const nextNode = el.nextSibling; |
| 44 | + parent.removeChild(el); |
45 | 45 |
|
46 | | - let block: Block | undefined |
47 | | - let activeBranchIndex: number = -1 |
| 46 | + let block: Block | undefined; |
| 47 | + let activeBranchIndex: number = -1; |
48 | 48 |
|
49 | 49 | const removeActiveBlock = () => { |
50 | 50 | 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; |
54 | 54 | } |
55 | | - } |
| 55 | + }; |
56 | 56 |
|
57 | 57 | ctx.effect(() => { |
58 | 58 | for (let i = 0; i < branches.length; i++) { |
59 | | - const { exp, el } = branches[i] |
| 59 | + const { exp, el } = branches[i]; |
60 | 60 | if (!exp || evaluate(ctx.scope, exp)) { |
61 | 61 | 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; |
67 | 67 | } |
68 | | - return |
| 68 | + return; |
69 | 69 | } |
70 | 70 | } |
71 | 71 | // no matched branch. |
72 | | - activeBranchIndex = -1 |
73 | | - removeActiveBlock() |
74 | | - }) |
| 72 | + activeBranchIndex = -1; |
| 73 | + removeActiveBlock(); |
| 74 | + }); |
75 | 75 |
|
76 | | - return nextNode |
77 | | -} |
| 76 | + return nextNode; |
| 77 | +}; |
0 commit comments