1 |
|
2 |
|
3 | import config from '../config'
|
4 | import VNode, { createEmptyVNode } from './vnode'
|
5 | import { createComponent } from './create-component'
|
6 | import { traverse } from '../observer/traverse'
|
7 |
|
8 | import {
|
9 | warn,
|
10 | isDef,
|
11 | isUndef,
|
12 | isTrue,
|
13 | isObject,
|
14 | isPrimitive,
|
15 | resolveAsset
|
16 | } from '../util/index'
|
17 |
|
18 | import {
|
19 | normalizeChildren,
|
20 | simpleNormalizeChildren
|
21 | } from './helpers/index'
|
22 |
|
23 | const SIMPLE_NORMALIZE = 1
|
24 | const ALWAYS_NORMALIZE = 2
|
25 |
|
26 |
|
27 |
|
28 | export function createElement (
|
29 | context: Component,
|
30 | tag: any,
|
31 | data: any,
|
32 | children: any,
|
33 | normalizationType: any,
|
34 | alwaysNormalize: boolean
|
35 | ): VNode | Array<VNode> {
|
36 | if (Array.isArray(data) || isPrimitive(data)) {
|
37 | normalizationType = children
|
38 | children = data
|
39 | data = undefined
|
40 | }
|
41 | if (isTrue(alwaysNormalize)) {
|
42 | normalizationType = ALWAYS_NORMALIZE
|
43 | }
|
44 | return _createElement(context, tag, data, children, normalizationType)
|
45 | }
|
46 |
|
47 | export function _createElement (
|
48 | context: Component,
|
49 | tag?: string | Class<Component> | Function | Object,
|
50 | data?: VNodeData,
|
51 | children?: any,
|
52 | normalizationType?: number
|
53 | ): VNode | Array<VNode> {
|
54 | if (isDef(data) && isDef((data: any).__ob__)) {
|
55 | process.env.NODE_ENV !== 'production' && warn(
|
56 | `Avoid using observed data object as vnode data: ${JSON.stringify(data)}\n` +
|
57 | 'Always create fresh vnode data objects in each render!',
|
58 | context
|
59 | )
|
60 | return createEmptyVNode()
|
61 | }
|
62 |
|
63 | if (isDef(data) && isDef(data.is)) {
|
64 | tag = data.is
|
65 | }
|
66 | if (!tag) {
|
67 |
|
68 | return createEmptyVNode()
|
69 | }
|
70 |
|
71 | if (process.env.NODE_ENV !== 'production' &&
|
72 | isDef(data) && isDef(data.key) && !isPrimitive(data.key)
|
73 | ) {
|
74 | if (!__WEEX__ || !('@binding' in data.key)) {
|
75 | warn(
|
76 | 'Avoid using non-primitive value as key, ' +
|
77 | 'use string/number value instead.',
|
78 | context
|
79 | )
|
80 | }
|
81 | }
|
82 |
|
83 | if (Array.isArray(children) &&
|
84 | typeof children[0] === 'function'
|
85 | ) {
|
86 | data = data || {}
|
87 | data.scopedSlots = { default: children[0] }
|
88 | children.length = 0
|
89 | }
|
90 | if (normalizationType === ALWAYS_NORMALIZE) {
|
91 | children = normalizeChildren(children)
|
92 | } else if (normalizationType === SIMPLE_NORMALIZE) {
|
93 | children = simpleNormalizeChildren(children)
|
94 | }
|
95 | let vnode, ns
|
96 | if (typeof tag === 'string') {
|
97 | let Ctor
|
98 | ns = (context.$vnode && context.$vnode.ns) || config.getTagNamespace(tag)
|
99 | if (config.isReservedTag(tag)) {
|
100 |
|
101 | if (process.env.NODE_ENV !== 'production' && isDef(data) && isDef(data.nativeOn) && data.tag !== 'component') {
|
102 | warn(
|
103 | `The .native modifier for v-on is only valid on components but it was used on <${tag}>.`,
|
104 | context
|
105 | )
|
106 | }
|
107 | vnode = new VNode(
|
108 | config.parsePlatformTagName(tag), data, children,
|
109 | undefined, undefined, context
|
110 | )
|
111 | } else if ((!data || !data.pre) && isDef(Ctor = resolveAsset(context.$options, 'components', tag))) {
|
112 |
|
113 | vnode = createComponent(Ctor, data, context, children, tag)
|
114 | } else {
|
115 |
|
116 |
|
117 |
|
118 | vnode = new VNode(
|
119 | tag, data, children,
|
120 | undefined, undefined, context
|
121 | )
|
122 | }
|
123 | } else {
|
124 |
|
125 | vnode = createComponent(tag, data, context, children)
|
126 | }
|
127 | if (Array.isArray(vnode)) {
|
128 | return vnode
|
129 | } else if (isDef(vnode)) {
|
130 | if (isDef(ns)) applyNS(vnode, ns)
|
131 | if (isDef(data)) registerDeepBindings(data)
|
132 | return vnode
|
133 | } else {
|
134 | return createEmptyVNode()
|
135 | }
|
136 | }
|
137 |
|
138 | function applyNS (vnode, ns, force) {
|
139 | vnode.ns = ns
|
140 | if (vnode.tag === 'foreignObject') {
|
141 |
|
142 | ns = undefined
|
143 | force = true
|
144 | }
|
145 | if (isDef(vnode.children)) {
|
146 | for (let i = 0, l = vnode.children.length; i < l; i++) {
|
147 | const child = vnode.children[i]
|
148 | if (isDef(child.tag) && (
|
149 | isUndef(child.ns) || (isTrue(force) && child.tag !== 'svg'))) {
|
150 | applyNS(child, ns, force)
|
151 | }
|
152 | }
|
153 | }
|
154 | }
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | function registerDeepBindings (data) {
|
160 | if (isObject(data.style)) {
|
161 | traverse(data.style)
|
162 | }
|
163 | if (isObject(data.class)) {
|
164 | traverse(data.class)
|
165 | }
|
166 | }
|