|
| 1 | +/* @flow */ |
| 2 | + |
| 3 | +import { isPrimitive } from 'core/util/index' |
| 4 | +import VNode from 'core/vdom/vnode' |
| 5 | + |
| 6 | +export function normalizeChildren ( |
| 7 | + children: any, |
| 8 | + ns: string | void, |
| 9 | + nestedIndex: number | 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, 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)) |
| 28 | + } |
| 29 | + } else if (c instanceof VNode) { |
| 30 | + if (c.text && last && last.text) { |
| 31 | + last.text += c.text |
| 32 | + } else { |
| 33 | + // inherit parent namespace |
| 34 | + if (ns) { |
| 35 | + applyNS(c, ns) |
| 36 | + } |
| 37 | + // default key for nested array children (likely generated by v-for) |
| 38 | + if (c.tag && c.key == null && nestedIndex != null) { |
| 39 | + c.key = `__vlist_${nestedIndex}_${i}__` |
| 40 | + } |
| 41 | + res.push(c) |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + return res |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function createTextVNode (val) { |
| 50 | + return new VNode(undefined, undefined, undefined, String(val)) |
| 51 | +} |
| 52 | + |
| 53 | +function applyNS (vnode, ns) { |
| 54 | + if (vnode.tag && !vnode.ns) { |
| 55 | + vnode.ns = ns |
| 56 | + if (vnode.children) { |
| 57 | + for (let i = 0, l = vnode.children.length; i < l; i++) { |
| 58 | + applyNS(vnode.children[i], ns) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments