vnode.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. import {
  2. isArray,
  3. isFunction,
  4. isString,
  5. isObject,
  6. EMPTY_ARR,
  7. extend,
  8. normalizeClass,
  9. normalizeStyle,
  10. PatchFlags,
  11. ShapeFlags,
  12. SlotFlags,
  13. isOn
  14. } from '@vue/shared'
  15. import {
  16. ComponentInternalInstance,
  17. Data,
  18. ConcreteComponent,
  19. ClassComponent,
  20. Component,
  21. isClassComponent
  22. } from './component'
  23. import { RawSlots } from './componentSlots'
  24. import { isProxy, Ref, toRaw, ReactiveFlags, isRef } from '@vue/reactivity'
  25. import { AppContext } from './apiCreateApp'
  26. import {
  27. SuspenseImpl,
  28. isSuspense,
  29. SuspenseBoundary,
  30. normalizeSuspenseChildren
  31. } from './components/Suspense'
  32. import { DirectiveBinding } from './directives'
  33. import { TransitionHooks } from './components/BaseTransition'
  34. import { warn } from './warning'
  35. import { TeleportImpl, isTeleport } from './components/Teleport'
  36. import {
  37. currentRenderingInstance,
  38. currentScopeId
  39. } from './componentRenderContext'
  40. import { RendererNode, RendererElement } from './renderer'
  41. import { NULL_DYNAMIC_COMPONENT } from './helpers/resolveAssets'
  42. import { hmrDirtyComponents } from './hmr'
  43. import { convertLegacyComponent } from './compat/component'
  44. import { convertLegacyVModelProps } from './compat/componentVModel'
  45. import { defineLegacyVNodeProperties } from './compat/renderFn'
  46. import { convertLegacyRefInFor } from './compat/ref'
  47. export const Fragment = (Symbol(__DEV__ ? 'Fragment' : undefined) as any) as {
  48. __isFragment: true
  49. new (): {
  50. $props: VNodeProps
  51. }
  52. }
  53. export const Text = Symbol(__DEV__ ? 'Text' : undefined)
  54. export const Comment = Symbol(__DEV__ ? 'Comment' : undefined)
  55. export const Static = Symbol(__DEV__ ? 'Static' : undefined)
  56. export type VNodeTypes =
  57. | string
  58. | VNode
  59. | Component
  60. | typeof Text
  61. | typeof Static
  62. | typeof Comment
  63. | typeof Fragment
  64. | typeof TeleportImpl
  65. | typeof SuspenseImpl
  66. export type VNodeRef =
  67. | string
  68. | Ref
  69. | ((ref: object | null, refs: Record<string, any>) => void)
  70. export type VNodeNormalizedRefAtom = {
  71. i: ComponentInternalInstance
  72. r: VNodeRef
  73. f?: boolean // v2 compat only, refInFor marker
  74. }
  75. export type VNodeNormalizedRef =
  76. | VNodeNormalizedRefAtom
  77. | (VNodeNormalizedRefAtom)[]
  78. type VNodeMountHook = (vnode: VNode) => void
  79. type VNodeUpdateHook = (vnode: VNode, oldVNode: VNode) => void
  80. export type VNodeHook =
  81. | VNodeMountHook
  82. | VNodeUpdateHook
  83. | VNodeMountHook[]
  84. | VNodeUpdateHook[]
  85. // https://github.com/microsoft/TypeScript/issues/33099
  86. export type VNodeProps = {
  87. key?: string | number
  88. ref?: VNodeRef
  89. // vnode hooks
  90. onVnodeBeforeMount?: VNodeMountHook | VNodeMountHook[]
  91. onVnodeMounted?: VNodeMountHook | VNodeMountHook[]
  92. onVnodeBeforeUpdate?: VNodeUpdateHook | VNodeUpdateHook[]
  93. onVnodeUpdated?: VNodeUpdateHook | VNodeUpdateHook[]
  94. onVnodeBeforeUnmount?: VNodeMountHook | VNodeMountHook[]
  95. onVnodeUnmounted?: VNodeMountHook | VNodeMountHook[]
  96. }
  97. type VNodeChildAtom =
  98. | VNode
  99. | string
  100. | number
  101. | boolean
  102. | null
  103. | undefined
  104. | void
  105. export type VNodeArrayChildren = Array<VNodeArrayChildren | VNodeChildAtom>
  106. export type VNodeChild = VNodeChildAtom | VNodeArrayChildren
  107. export type VNodeNormalizedChildren =
  108. | string
  109. | VNodeArrayChildren
  110. | RawSlots
  111. | null
  112. export interface VNode<
  113. HostNode = RendererNode,
  114. HostElement = RendererElement,
  115. ExtraProps = { [key: string]: any }
  116. > {
  117. /**
  118. * @internal
  119. */
  120. __v_isVNode: true
  121. /**
  122. * @internal
  123. */
  124. [ReactiveFlags.SKIP]: true
  125. type: VNodeTypes
  126. props: (VNodeProps & ExtraProps) | null
  127. key: string | number | null
  128. ref: VNodeNormalizedRef | null
  129. /**
  130. * SFC only. This is assigned on vnode creation using currentScopeId
  131. * which is set alongside currentRenderingInstance.
  132. */
  133. scopeId: string | null
  134. /**
  135. * SFC only. This is assigned to:
  136. * - Slot fragment vnodes with :slotted SFC styles.
  137. * - Component vnodes (during patch/hydration) so that its root node can
  138. * inherit the component's slotScopeIds
  139. */
  140. slotScopeIds: string[] | null
  141. children: VNodeNormalizedChildren
  142. component: ComponentInternalInstance | null
  143. dirs: DirectiveBinding[] | null
  144. transition: TransitionHooks<HostElement> | null
  145. // DOM
  146. el: HostNode | null
  147. anchor: HostNode | null // fragment anchor
  148. target: HostElement | null // teleport target
  149. targetAnchor: HostNode | null // teleport target anchor
  150. staticCount: number // number of elements contained in a static vnode
  151. // suspense
  152. suspense: SuspenseBoundary | null
  153. ssContent: VNode | null
  154. ssFallback: VNode | null
  155. // optimization only
  156. shapeFlag: number
  157. patchFlag: number
  158. dynamicProps: string[] | null
  159. dynamicChildren: VNode[] | null
  160. // application root node only
  161. appContext: AppContext | null
  162. }
  163. // Since v-if and v-for are the two possible ways node structure can dynamically
  164. // change, once we consider v-if branches and each v-for fragment a block, we
  165. // can divide a template into nested blocks, and within each block the node
  166. // structure would be stable. This allows us to skip most children diffing
  167. // and only worry about the dynamic nodes (indicated by patch flags).
  168. export const blockStack: (VNode[] | null)[] = []
  169. let currentBlock: VNode[] | null = null
  170. /**
  171. * Open a block.
  172. * This must be called before `createBlock`. It cannot be part of `createBlock`
  173. * because the children of the block are evaluated before `createBlock` itself
  174. * is called. The generated code typically looks like this:
  175. *
  176. * ```js
  177. * function render() {
  178. * return (openBlock(),createBlock('div', null, [...]))
  179. * }
  180. * ```
  181. * disableTracking is true when creating a v-for fragment block, since a v-for
  182. * fragment always diffs its children.
  183. *
  184. * @private
  185. */
  186. export function openBlock(disableTracking = false) {
  187. blockStack.push((currentBlock = disableTracking ? null : []))
  188. }
  189. export function closeBlock() {
  190. blockStack.pop()
  191. currentBlock = blockStack[blockStack.length - 1] || null
  192. }
  193. // Whether we should be tracking dynamic child nodes inside a block.
  194. // Only tracks when this value is > 0
  195. // We are not using a simple boolean because this value may need to be
  196. // incremented/decremented by nested usage of v-once (see below)
  197. let isBlockTreeEnabled = 1
  198. /**
  199. * Block tracking sometimes needs to be disabled, for example during the
  200. * creation of a tree that needs to be cached by v-once. The compiler generates
  201. * code like this:
  202. *
  203. * ``` js
  204. * _cache[1] || (
  205. * setBlockTracking(-1),
  206. * _cache[1] = createVNode(...),
  207. * setBlockTracking(1),
  208. * _cache[1]
  209. * )
  210. * ```
  211. *
  212. * @private
  213. */
  214. export function setBlockTracking(value: number) {
  215. isBlockTreeEnabled += value
  216. }
  217. /**
  218. * Create a block root vnode. Takes the same exact arguments as `createVNode`.
  219. * A block root keeps track of dynamic nodes within the block in the
  220. * `dynamicChildren` array.
  221. *
  222. * @private
  223. */
  224. export function createBlock(
  225. type: VNodeTypes | ClassComponent,
  226. props?: Record<string, any> | null,
  227. children?: any,
  228. patchFlag?: number,
  229. dynamicProps?: string[]
  230. ): VNode {
  231. const vnode = createVNode(
  232. type,
  233. props,
  234. children,
  235. patchFlag,
  236. dynamicProps,
  237. true /* isBlock: prevent a block from tracking itself */
  238. )
  239. // save current block children on the block vnode
  240. vnode.dynamicChildren =
  241. isBlockTreeEnabled > 0 ? currentBlock || (EMPTY_ARR as any) : null
  242. // close block
  243. closeBlock()
  244. // a block is always going to be patched, so track it as a child of its
  245. // parent block
  246. if (isBlockTreeEnabled > 0 && currentBlock) {
  247. currentBlock.push(vnode)
  248. }
  249. return vnode
  250. }
  251. export function isVNode(value: any): value is VNode {
  252. return value ? value.__v_isVNode === true : false
  253. }
  254. export function isSameVNodeType(n1: VNode, n2: VNode): boolean {
  255. if (
  256. __DEV__ &&
  257. n2.shapeFlag & ShapeFlags.COMPONENT &&
  258. hmrDirtyComponents.has(n2.type as ConcreteComponent)
  259. ) {
  260. // HMR only: if the component has been hot-updated, force a reload.
  261. return false
  262. }
  263. return n1.type === n2.type && n1.key === n2.key
  264. }
  265. let vnodeArgsTransformer:
  266. | ((
  267. args: Parameters<typeof _createVNode>,
  268. instance: ComponentInternalInstance | null
  269. ) => Parameters<typeof _createVNode>)
  270. | undefined
  271. /**
  272. * Internal API for registering an arguments transform for createVNode
  273. * used for creating stubs in the test-utils
  274. * It is *internal* but needs to be exposed for test-utils to pick up proper
  275. * typings
  276. */
  277. export function transformVNodeArgs(transformer?: typeof vnodeArgsTransformer) {
  278. vnodeArgsTransformer = transformer
  279. }
  280. const createVNodeWithArgsTransform = (
  281. ...args: Parameters<typeof _createVNode>
  282. ): VNode => {
  283. return _createVNode(
  284. ...(vnodeArgsTransformer
  285. ? vnodeArgsTransformer(args, currentRenderingInstance)
  286. : args)
  287. )
  288. }
  289. export const InternalObjectKey = `__vInternal`
  290. const normalizeKey = ({ key }: VNodeProps): VNode['key'] =>
  291. key != null ? key : null
  292. const normalizeRef = ({ ref }: VNodeProps): VNodeNormalizedRefAtom | null => {
  293. return (ref != null
  294. ? isString(ref) || isRef(ref) || isFunction(ref)
  295. ? { i: currentRenderingInstance, r: ref }
  296. : ref
  297. : null) as any
  298. }
  299. export const createVNode = (__DEV__
  300. ? createVNodeWithArgsTransform
  301. : _createVNode) as typeof _createVNode
  302. function _createVNode(
  303. type: VNodeTypes | ClassComponent | typeof NULL_DYNAMIC_COMPONENT,
  304. props: (Data & VNodeProps) | null = null,
  305. children: unknown = null,
  306. patchFlag: number = 0,
  307. dynamicProps: string[] | null = null,
  308. isBlockNode = false
  309. ): VNode {
  310. if (!type || type === NULL_DYNAMIC_COMPONENT) {
  311. if (__DEV__ && !type) {
  312. warn(`Invalid vnode type when creating vnode: ${type}.`)
  313. }
  314. type = Comment
  315. }
  316. if (isVNode(type)) {
  317. // createVNode receiving an existing vnode. This happens in cases like
  318. // <component :is="vnode"/>
  319. // #2078 make sure to merge refs during the clone instead of overwriting it
  320. const cloned = cloneVNode(type, props, true /* mergeRef: true */)
  321. if (children) {
  322. normalizeChildren(cloned, children)
  323. }
  324. return cloned
  325. }
  326. // class component normalization.
  327. if (isClassComponent(type)) {
  328. type = type.__vccOpts
  329. }
  330. // 2.x async/functional component compat
  331. if (__COMPAT__) {
  332. type = convertLegacyComponent(type, currentRenderingInstance)
  333. }
  334. // class & style normalization.
  335. if (props) {
  336. // for reactive or proxy objects, we need to clone it to enable mutation.
  337. if (isProxy(props) || InternalObjectKey in props) {
  338. props = extend({}, props)
  339. }
  340. let { class: klass, style } = props
  341. if (klass && !isString(klass)) {
  342. props.class = normalizeClass(klass)
  343. }
  344. if (isObject(style)) {
  345. // reactive state objects need to be cloned since they are likely to be
  346. // mutated
  347. if (isProxy(style) && !isArray(style)) {
  348. style = extend({}, style)
  349. }
  350. props.style = normalizeStyle(style)
  351. }
  352. }
  353. // encode the vnode type information into a bitmap
  354. const shapeFlag = isString(type)
  355. ? ShapeFlags.ELEMENT
  356. : __FEATURE_SUSPENSE__ && isSuspense(type)
  357. ? ShapeFlags.SUSPENSE
  358. : isTeleport(type)
  359. ? ShapeFlags.TELEPORT
  360. : isObject(type)
  361. ? ShapeFlags.STATEFUL_COMPONENT
  362. : isFunction(type)
  363. ? ShapeFlags.FUNCTIONAL_COMPONENT
  364. : 0
  365. if (__DEV__ && shapeFlag & ShapeFlags.STATEFUL_COMPONENT && isProxy(type)) {
  366. type = toRaw(type)
  367. warn(
  368. `Vue received a Component which was made a reactive object. This can ` +
  369. `lead to unnecessary performance overhead, and should be avoided by ` +
  370. `marking the component with \`markRaw\` or using \`shallowRef\` ` +
  371. `instead of \`ref\`.`,
  372. `\nComponent that was made reactive: `,
  373. type
  374. )
  375. }
  376. const vnode: VNode = {
  377. __v_isVNode: true,
  378. __v_skip: true,
  379. type,
  380. props,
  381. key: props && normalizeKey(props),
  382. ref: props && normalizeRef(props),
  383. scopeId: currentScopeId,
  384. slotScopeIds: null,
  385. children: null,
  386. component: null,
  387. suspense: null,
  388. ssContent: null,
  389. ssFallback: null,
  390. dirs: null,
  391. transition: null,
  392. el: null,
  393. anchor: null,
  394. target: null,
  395. targetAnchor: null,
  396. staticCount: 0,
  397. shapeFlag,
  398. patchFlag,
  399. dynamicProps,
  400. dynamicChildren: null,
  401. appContext: null
  402. }
  403. // validate key
  404. if (__DEV__ && vnode.key !== vnode.key) {
  405. warn(`VNode created with invalid key (NaN). VNode type:`, vnode.type)
  406. }
  407. normalizeChildren(vnode, children)
  408. // normalize suspense children
  409. if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
  410. const { content, fallback } = normalizeSuspenseChildren(vnode)
  411. vnode.ssContent = content
  412. vnode.ssFallback = fallback
  413. }
  414. if (
  415. isBlockTreeEnabled > 0 &&
  416. // avoid a block node from tracking itself
  417. !isBlockNode &&
  418. // has current parent block
  419. currentBlock &&
  420. // presence of a patch flag indicates this node needs patching on updates.
  421. // component nodes also should always be patched, because even if the
  422. // component doesn't need to update, it needs to persist the instance on to
  423. // the next vnode so that it can be properly unmounted later.
  424. (patchFlag > 0 || shapeFlag & ShapeFlags.COMPONENT) &&
  425. // the EVENTS flag is only for hydration and if it is the only flag, the
  426. // vnode should not be considered dynamic due to handler caching.
  427. patchFlag !== PatchFlags.HYDRATE_EVENTS
  428. ) {
  429. currentBlock.push(vnode)
  430. }
  431. if (__COMPAT__) {
  432. convertLegacyVModelProps(vnode)
  433. convertLegacyRefInFor(vnode)
  434. defineLegacyVNodeProperties(vnode)
  435. }
  436. return vnode
  437. }
  438. export function cloneVNode<T, U>(
  439. vnode: VNode<T, U>,
  440. extraProps?: Data & VNodeProps | null,
  441. mergeRef = false
  442. ): VNode<T, U> {
  443. // This is intentionally NOT using spread or extend to avoid the runtime
  444. // key enumeration cost.
  445. const { props, ref, patchFlag, children } = vnode
  446. const mergedProps = extraProps ? mergeProps(props || {}, extraProps) : props
  447. const cloned: VNode = {
  448. __v_isVNode: true,
  449. __v_skip: true,
  450. type: vnode.type,
  451. props: mergedProps,
  452. key: mergedProps && normalizeKey(mergedProps),
  453. ref:
  454. extraProps && extraProps.ref
  455. ? // #2078 in the case of <component :is="vnode" ref="extra"/>
  456. // if the vnode itself already has a ref, cloneVNode will need to merge
  457. // the refs so the single vnode can be set on multiple refs
  458. mergeRef && ref
  459. ? isArray(ref)
  460. ? ref.concat(normalizeRef(extraProps)!)
  461. : [ref, normalizeRef(extraProps)!]
  462. : normalizeRef(extraProps)
  463. : ref,
  464. scopeId: vnode.scopeId,
  465. slotScopeIds: vnode.slotScopeIds,
  466. children:
  467. __DEV__ && patchFlag === PatchFlags.HOISTED && isArray(children)
  468. ? (children as VNode[]).map(deepCloneVNode)
  469. : children,
  470. target: vnode.target,
  471. targetAnchor: vnode.targetAnchor,
  472. staticCount: vnode.staticCount,
  473. shapeFlag: vnode.shapeFlag,
  474. // if the vnode is cloned with extra props, we can no longer assume its
  475. // existing patch flag to be reliable and need to add the FULL_PROPS flag.
  476. // note: perserve flag for fragments since they use the flag for children
  477. // fast paths only.
  478. patchFlag:
  479. extraProps && vnode.type !== Fragment
  480. ? patchFlag === -1 // hoisted node
  481. ? PatchFlags.FULL_PROPS
  482. : patchFlag | PatchFlags.FULL_PROPS
  483. : patchFlag,
  484. dynamicProps: vnode.dynamicProps,
  485. dynamicChildren: vnode.dynamicChildren,
  486. appContext: vnode.appContext,
  487. dirs: vnode.dirs,
  488. transition: vnode.transition,
  489. // These should technically only be non-null on mounted VNodes. However,
  490. // they *should* be copied for kept-alive vnodes. So we just always copy
  491. // them since them being non-null during a mount doesn't affect the logic as
  492. // they will simply be overwritten.
  493. component: vnode.component,
  494. suspense: vnode.suspense,
  495. ssContent: vnode.ssContent && cloneVNode(vnode.ssContent),
  496. ssFallback: vnode.ssFallback && cloneVNode(vnode.ssFallback),
  497. el: vnode.el,
  498. anchor: vnode.anchor
  499. }
  500. if (__COMPAT__) {
  501. defineLegacyVNodeProperties(cloned)
  502. }
  503. return cloned as any
  504. }
  505. /**
  506. * Dev only, for HMR of hoisted vnodes reused in v-for
  507. * https://github.com/vitejs/vite/issues/2022
  508. */
  509. function deepCloneVNode(vnode: VNode): VNode {
  510. const cloned = cloneVNode(vnode)
  511. if (isArray(vnode.children)) {
  512. cloned.children = (vnode.children as VNode[]).map(deepCloneVNode)
  513. }
  514. return cloned
  515. }
  516. /**
  517. * @private
  518. */
  519. export function createTextVNode(text: string = ' ', flag: number = 0): VNode {
  520. return createVNode(Text, null, text, flag)
  521. }
  522. /**
  523. * @private
  524. */
  525. export function createStaticVNode(
  526. content: string,
  527. numberOfNodes: number
  528. ): VNode {
  529. // A static vnode can contain multiple stringified elements, and the number
  530. // of elements is necessary for hydration.
  531. const vnode = createVNode(Static, null, content)
  532. vnode.staticCount = numberOfNodes
  533. return vnode
  534. }
  535. /**
  536. * @private
  537. */
  538. export function createCommentVNode(
  539. text: string = '',
  540. // when used as the v-else branch, the comment node must be created as a
  541. // block to ensure correct updates.
  542. asBlock: boolean = false
  543. ): VNode {
  544. return asBlock
  545. ? (openBlock(), createBlock(Comment, null, text))
  546. : createVNode(Comment, null, text)
  547. }
  548. export function normalizeVNode(child: VNodeChild): VNode {
  549. if (child == null || typeof child === 'boolean') {
  550. // empty placeholder
  551. return createVNode(Comment)
  552. } else if (isArray(child)) {
  553. // fragment
  554. return createVNode(Fragment, null, child)
  555. } else if (typeof child === 'object') {
  556. // already vnode, this should be the most common since compiled templates
  557. // always produce all-vnode children arrays
  558. return child.el === null ? child : cloneVNode(child)
  559. } else {
  560. // strings and numbers
  561. return createVNode(Text, null, String(child))
  562. }
  563. }
  564. // optimized normalization for template-compiled render fns
  565. export function cloneIfMounted(child: VNode): VNode {
  566. return child.el === null ? child : cloneVNode(child)
  567. }
  568. export function normalizeChildren(vnode: VNode, children: unknown) {
  569. let type = 0
  570. const { shapeFlag } = vnode
  571. if (children == null) {
  572. children = null
  573. } else if (isArray(children)) {
  574. type = ShapeFlags.ARRAY_CHILDREN
  575. } else if (typeof children === 'object') {
  576. if (shapeFlag & ShapeFlags.ELEMENT || shapeFlag & ShapeFlags.TELEPORT) {
  577. // Normalize slot to plain children for plain element and Teleport
  578. const slot = (children as any).default
  579. if (slot) {
  580. // _c marker is added by withCtx() indicating this is a compiled slot
  581. slot._c && (slot._d = false)
  582. normalizeChildren(vnode, slot())
  583. slot._c && (slot._d = true)
  584. }
  585. return
  586. } else {
  587. type = ShapeFlags.SLOTS_CHILDREN
  588. const slotFlag = (children as RawSlots)._
  589. if (!slotFlag && !(InternalObjectKey in children!)) {
  590. // if slots are not normalized, attach context instance
  591. // (compiled / normalized slots already have context)
  592. ;(children as RawSlots)._ctx = currentRenderingInstance
  593. } else if (slotFlag === SlotFlags.FORWARDED && currentRenderingInstance) {
  594. // a child component receives forwarded slots from the parent.
  595. // its slot type is determined by its parent's slot type.
  596. if (
  597. currentRenderingInstance.vnode.patchFlag & PatchFlags.DYNAMIC_SLOTS
  598. ) {
  599. ;(children as RawSlots)._ = SlotFlags.DYNAMIC
  600. vnode.patchFlag |= PatchFlags.DYNAMIC_SLOTS
  601. } else {
  602. ;(children as RawSlots)._ = SlotFlags.STABLE
  603. }
  604. }
  605. }
  606. } else if (isFunction(children)) {
  607. children = { default: children, _ctx: currentRenderingInstance }
  608. type = ShapeFlags.SLOTS_CHILDREN
  609. } else {
  610. children = String(children)
  611. // force teleport children to array so it can be moved around
  612. if (shapeFlag & ShapeFlags.TELEPORT) {
  613. type = ShapeFlags.ARRAY_CHILDREN
  614. children = [createTextVNode(children as string)]
  615. } else {
  616. type = ShapeFlags.TEXT_CHILDREN
  617. }
  618. }
  619. vnode.children = children as VNodeNormalizedChildren
  620. vnode.shapeFlag |= type
  621. }
  622. export function mergeProps(...args: (Data & VNodeProps)[]) {
  623. const ret = extend({}, args[0])
  624. for (let i = 1; i < args.length; i++) {
  625. const toMerge = args[i]
  626. for (const key in toMerge) {
  627. if (key === 'class') {
  628. if (ret.class !== toMerge.class) {
  629. ret.class = normalizeClass([ret.class, toMerge.class])
  630. }
  631. } else if (key === 'style') {
  632. ret.style = normalizeStyle([ret.style, toMerge.style])
  633. } else if (isOn(key)) {
  634. const existing = ret[key]
  635. const incoming = toMerge[key]
  636. if (existing !== incoming) {
  637. ret[key] = existing
  638. ? [].concat(existing as any, incoming as any)
  639. : incoming
  640. }
  641. } else if (key !== '') {
  642. ret[key] = toMerge[key]
  643. }
  644. }
  645. }
  646. return ret
  647. }