vnode.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* @flow */
  2. export default class VNode {
  3. tag: string | void;
  4. data: VNodeData | void;
  5. children: ?Array<VNode>;
  6. text: string | void;
  7. elm: Node | void;
  8. ns: string | void;
  9. context: Component | void; // rendered in this component's scope
  10. functionalContext: Component | void; // only for functional component root nodes
  11. key: string | number | void;
  12. componentOptions: VNodeComponentOptions | void;
  13. componentInstance: Component | void; // component instance
  14. parent: VNode | void; // component placeholder node
  15. raw: boolean; // contains raw HTML? (server only)
  16. isStatic: boolean; // hoisted static node
  17. isRootInsert: boolean; // necessary for enter transition check
  18. isComment: boolean; // empty comment placeholder?
  19. isCloned: boolean; // is a cloned node?
  20. isOnce: boolean; // is a v-once node?
  21. constructor (
  22. tag?: string,
  23. data?: VNodeData,
  24. children?: ?Array<VNode>,
  25. text?: string,
  26. elm?: Node,
  27. context?: Component,
  28. componentOptions?: VNodeComponentOptions
  29. ) {
  30. this.tag = tag
  31. this.data = data
  32. this.children = children
  33. this.text = text
  34. this.elm = elm
  35. this.ns = undefined
  36. this.context = context
  37. this.functionalContext = undefined
  38. this.key = data && data.key
  39. this.componentOptions = componentOptions
  40. this.componentInstance = undefined
  41. this.parent = undefined
  42. this.raw = false
  43. this.isStatic = false
  44. this.isRootInsert = true
  45. this.isComment = false
  46. this.isCloned = false
  47. this.isOnce = false
  48. }
  49. // DEPRECATED: alias for componentInstance for backwards compat.
  50. get child (): Component | void {
  51. return this.componentInstance
  52. }
  53. }
  54. export const createEmptyVNode = () => {
  55. const node = new VNode()
  56. node.text = ''
  57. node.isComment = true
  58. return node
  59. }
  60. export function createTextVNode (val: string | number) {
  61. return new VNode(undefined, undefined, undefined, String(val))
  62. }
  63. // optimized shallow clone
  64. // used for static nodes and slot nodes because they may be reused across
  65. // multiple renders, cloning them avoids errors when DOM manipulations rely
  66. // on their elm reference.
  67. export function cloneVNode (vnode: VNode): VNode {
  68. const cloned = new VNode(
  69. vnode.tag,
  70. vnode.data,
  71. vnode.children,
  72. vnode.text,
  73. vnode.elm,
  74. vnode.context,
  75. vnode.componentOptions
  76. )
  77. cloned.ns = vnode.ns
  78. cloned.isStatic = vnode.isStatic
  79. cloned.key = vnode.key
  80. cloned.isCloned = true
  81. return cloned
  82. }
  83. export function cloneVNodes (vnodes: Array<VNode>): Array<VNode> {
  84. const res = new Array(vnodes.length)
  85. for (let i = 0; i < vnodes.length; i++) {
  86. res[i] = cloneVNode(vnodes[i])
  87. }
  88. return res
  89. }