| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- /* @flow */
- import { isPrimitive } from 'core/util/index'
- import VNode, { createTextVNode } from 'core/vdom/vnode'
- export function normalizeChildren (children: any): ?Array<VNode> {
- return isPrimitive(children)
- ? [createTextVNode(children)]
- : Array.isArray(children)
- ? normalizeArrayChildren(children)
- : undefined
- }
- function normalizeArrayChildren (children: any, nestedIndex?: string): Array<VNode> {
- const res = []
- let i, c, last
- for (i = 0; i < children.length; i++) {
- c = children[i]
- if (c == null || typeof c === 'boolean') continue
- last = res[res.length - 1]
- // nested
- if (Array.isArray(c)) {
- res.push.apply(res, normalizeArrayChildren(c, `${nestedIndex || ''}_${i}`))
- } else if (isPrimitive(c)) {
- if (last && last.text) {
- last.text += String(c)
- } else if (c !== '') {
- // convert primitive to vnode
- res.push(createTextVNode(c))
- }
- } else {
- if (c.text && last && last.text) {
- res[res.length - 1] = createTextVNode(last.text + c.text)
- } else {
- // default key for nested array children (likely generated by v-for)
- if (c.tag && c.key == null && nestedIndex != null) {
- c.key = `__vlist${nestedIndex}_${i}__`
- }
- res.push(c)
- }
- }
- }
- return res
- }
|