vnode.ts 20 KB

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