Skip to content

Commit ea7c353

Browse files
committed
refactor normalizeChildren
1 parent 4f82c6a commit ea7c353

File tree

1 file changed

+39
-38
lines changed

1 file changed

+39
-38
lines changed

src/core/vdom/helpers/normalize-children.js

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,50 @@
33
import { isPrimitive } from 'core/util/index'
44
import VNode from 'core/vdom/vnode'
55

6-
export function normalizeChildren (
7-
children: any,
8-
ns: string | void,
9-
nestedIndex: string | void
10-
): Array<VNode> | void {
11-
if (isPrimitive(children)) {
12-
return [createTextVNode(children)]
13-
}
14-
if (Array.isArray(children)) {
15-
const res = []
16-
for (let i = 0, l = children.length; i < l; i++) {
17-
const c = children[i]
18-
const last = res[res.length - 1]
19-
// nested
20-
if (Array.isArray(c)) {
21-
res.push.apply(res, normalizeChildren(c, ns, `${nestedIndex || ''}_${i}`))
22-
} else if (isPrimitive(c)) {
23-
if (last && last.text) {
24-
last.text += String(c)
25-
} else if (c !== '') {
26-
// convert primitive to vnode
27-
res.push(createTextVNode(c))
6+
export function normalizeChildren (children: any, ns: ?string): Array<VNode> | void {
7+
return isPrimitive(children)
8+
? [createTextVNode(children)]
9+
: Array.isArray(children)
10+
? normalizeArrayChildren(children, ns)
11+
: undefined
12+
}
13+
14+
function normalizeArrayChildren (children: any, ns: ?string, nestedIndex?: string): Array<VNode> {
15+
const res = []
16+
let i, c, last
17+
for (i = 0; i < children.length; i++) {
18+
c = children[i]
19+
if (c == null) continue
20+
last = res[res.length - 1]
21+
// nested
22+
if (Array.isArray(c)) {
23+
res.push.apply(res, normalizeArrayChildren(c, ns, `${nestedIndex || ''}_${i}`))
24+
} else if (isPrimitive(c)) {
25+
if (last && last.text) {
26+
last.text += String(c)
27+
} else if (c !== '') {
28+
// convert primitive to vnode
29+
res.push(createTextVNode(c))
30+
}
31+
} else {
32+
if (c.text && last && last.text) {
33+
if (!last.isCloned) {
34+
last.text += c.text
35+
}
36+
} else {
37+
// inherit parent namespace
38+
if (ns) {
39+
applyNS(c, ns)
2840
}
29-
} else if (c instanceof VNode) {
30-
if (c.text && last && last.text) {
31-
if (!last.isCloned) {
32-
last.text += c.text
33-
}
34-
} else {
35-
// inherit parent namespace
36-
if (ns) {
37-
applyNS(c, ns)
38-
}
39-
// default key for nested array children (likely generated by v-for)
40-
if (c.tag && c.key == null && nestedIndex != null) {
41-
c.key = `__vlist${nestedIndex}_${i}__`
42-
}
43-
res.push(c)
41+
// default key for nested array children (likely generated by v-for)
42+
if (c.tag && c.key == null && nestedIndex != null) {
43+
c.key = `__vlist${nestedIndex}_${i}__`
4444
}
45+
res.push(c)
4546
}
4647
}
47-
return res
4848
}
49+
return res
4950
}
5051

5152
function createTextVNode (val) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy