UNPKG

3.38 kBJavaScriptView Raw
1/* @flow */
2
3export default class VNode {
4 tag: string | void;
5 data: VNodeData | void;
6 children: ?Array<VNode>;
7 text: string | void;
8 elm: Node | void;
9 ns: string | void;
10 context: Component | void; // rendered in this component's scope
11 key: string | number | void;
12 componentOptions: VNodeComponentOptions | void;
13 componentInstance: Component | void; // component instance
14 parent: VNode | void; // component placeholder node
15
16 // strictly internal
17 raw: boolean; // contains raw HTML? (server only)
18 isStatic: boolean; // hoisted static node
19 isRootInsert: boolean; // necessary for enter transition check
20 isComment: boolean; // empty comment placeholder?
21 isCloned: boolean; // is a cloned node?
22 isOnce: boolean; // is a v-once node?
23 asyncFactory: Function | void; // async component factory function
24 asyncMeta: Object | void;
25 isAsyncPlaceholder: boolean;
26 ssrContext: Object | void;
27 fnContext: Component | void; // real context vm for functional nodes
28 fnOptions: ?ComponentOptions; // for SSR caching
29 devtoolsMeta: ?Object; // used to store functional render context for devtools
30 fnScopeId: ?string; // functional scope id support
31
32 constructor (
33 tag?: string,
34 data?: VNodeData,
35 children?: ?Array<VNode>,
36 text?: string,
37 elm?: Node,
38 context?: Component,
39 componentOptions?: VNodeComponentOptions,
40 asyncFactory?: Function
41 ) {
42 this.tag = tag
43 this.data = data
44 this.children = children
45 this.text = text
46 this.elm = elm
47 this.ns = undefined
48 this.context = context
49 this.fnContext = undefined
50 this.fnOptions = undefined
51 this.fnScopeId = undefined
52 this.key = data && data.key
53 this.componentOptions = componentOptions
54 this.componentInstance = undefined
55 this.parent = undefined
56 this.raw = false
57 this.isStatic = false
58 this.isRootInsert = true
59 this.isComment = false
60 this.isCloned = false
61 this.isOnce = false
62 this.asyncFactory = asyncFactory
63 this.asyncMeta = undefined
64 this.isAsyncPlaceholder = false
65 }
66
67 // DEPRECATED: alias for componentInstance for backwards compat.
68 /* istanbul ignore next */
69 get child (): Component | void {
70 return this.componentInstance
71 }
72}
73
74export const createEmptyVNode = (text: string = '') => {
75 const node = new VNode()
76 node.text = text
77 node.isComment = true
78 return node
79}
80
81export function createTextVNode (val: string | number) {
82 return new VNode(undefined, undefined, undefined, String(val))
83}
84
85// optimized shallow clone
86// used for static nodes and slot nodes because they may be reused across
87// multiple renders, cloning them avoids errors when DOM manipulations rely
88// on their elm reference.
89export function cloneVNode (vnode: VNode): VNode {
90 const cloned = new VNode(
91 vnode.tag,
92 vnode.data,
93 // #7975
94 // clone children array to avoid mutating original in case of cloning
95 // a child.
96 vnode.children && vnode.children.slice(),
97 vnode.text,
98 vnode.elm,
99 vnode.context,
100 vnode.componentOptions,
101 vnode.asyncFactory
102 )
103 cloned.ns = vnode.ns
104 cloned.isStatic = vnode.isStatic
105 cloned.key = vnode.key
106 cloned.isComment = vnode.isComment
107 cloned.fnContext = vnode.fnContext
108 cloned.fnOptions = vnode.fnOptions
109 cloned.fnScopeId = vnode.fnScopeId
110 cloned.asyncMeta = vnode.asyncMeta
111 cloned.isCloned = true
112 return cloned
113}