| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550 |
- import {
- Comment,
- Fragment,
- Static,
- Text,
- type VNode,
- type VNodeArrayChildren,
- type VNodeHook,
- type VNodeProps,
- cloneIfMounted,
- createVNode,
- invokeVNodeHook,
- isSameVNodeType,
- normalizeVNode,
- } from './vnode'
- import {
- type ComponentInternalInstance,
- type ComponentOptions,
- type Data,
- type LifecycleHook,
- createComponentInstance,
- setupComponent,
- } from './component'
- import {
- filterSingleRoot,
- renderComponentRoot,
- shouldUpdateComponent,
- updateHOCHostEl,
- } from './componentRenderUtils'
- import {
- EMPTY_ARR,
- EMPTY_OBJ,
- NOOP,
- PatchFlags,
- ShapeFlags,
- def,
- getGlobalThis,
- invokeArrayFns,
- isArray,
- isReservedProp,
- } from '@vue/shared'
- import {
- type SchedulerJob,
- SchedulerJobFlags,
- type SchedulerJobs,
- flushPostFlushCbs,
- flushPreFlushCbs,
- queueJob,
- queuePostFlushCb,
- } from './scheduler'
- import {
- EffectFlags,
- ReactiveEffect,
- pauseTracking,
- resetTracking,
- } from '@vue/reactivity'
- import { updateProps } from './componentProps'
- import { updateSlots } from './componentSlots'
- import { popWarningContext, pushWarningContext, warn } from './warning'
- import { type CreateAppFunction, createAppAPI } from './apiCreateApp'
- import { setRef } from './rendererTemplateRef'
- import {
- type SuspenseBoundary,
- type SuspenseImpl,
- isSuspense,
- queueEffectWithSuspense,
- } from './components/Suspense'
- import {
- TeleportEndKey,
- type TeleportImpl,
- type TeleportVNode,
- } from './components/Teleport'
- import { type KeepAliveContext, isKeepAlive } from './components/KeepAlive'
- import { isHmrUpdating, registerHMR, unregisterHMR } from './hmr'
- import { type RootHydrateFunction, createHydrationFunctions } from './hydration'
- import { invokeDirectiveHook } from './directives'
- import { endMeasure, startMeasure } from './profiling'
- import {
- devtoolsComponentAdded,
- devtoolsComponentRemoved,
- devtoolsComponentUpdated,
- setDevtoolsHook,
- } from './devtools'
- import { initFeatureFlags } from './featureFlags'
- import { isAsyncWrapper } from './apiAsyncComponent'
- import { isCompatEnabled } from './compat/compatConfig'
- import { DeprecationTypes } from './compat/compatConfig'
- import type { TransitionHooks } from './components/BaseTransition'
- export interface Renderer<HostElement = RendererElement> {
- render: RootRenderFunction<HostElement>
- createApp: CreateAppFunction<HostElement>
- }
- export interface HydrationRenderer extends Renderer<Element | ShadowRoot> {
- hydrate: RootHydrateFunction
- }
- export type ElementNamespace = 'svg' | 'mathml' | undefined
- export type RootRenderFunction<HostElement = RendererElement> = (
- vnode: VNode | null,
- container: HostElement,
- namespace?: ElementNamespace,
- ) => void
- export interface RendererOptions<
- HostNode = RendererNode,
- HostElement = RendererElement,
- > {
- patchProp(
- el: HostElement,
- key: string,
- prevValue: any,
- nextValue: any,
- namespace?: ElementNamespace,
- parentComponent?: ComponentInternalInstance | null,
- ): void
- insert(el: HostNode, parent: HostElement, anchor?: HostNode | null): void
- remove(el: HostNode): void
- createElement(
- type: string,
- namespace?: ElementNamespace,
- isCustomizedBuiltIn?: string,
- vnodeProps?: (VNodeProps & { [key: string]: any }) | null,
- ): HostElement
- createText(text: string): HostNode
- createComment(text: string): HostNode
- setText(node: HostNode, text: string): void
- setElementText(node: HostElement, text: string): void
- parentNode(node: HostNode): HostElement | null
- nextSibling(node: HostNode): HostNode | null
- querySelector?(selector: string): HostElement | null
- setScopeId?(el: HostElement, id: string): void
- cloneNode?(node: HostNode): HostNode
- insertStaticContent?(
- content: string,
- parent: HostElement,
- anchor: HostNode | null,
- namespace: ElementNamespace,
- start?: HostNode | null,
- end?: HostNode | null,
- ): [HostNode, HostNode]
- }
- // Renderer Node can technically be any object in the context of core renderer
- // logic - they are never directly operated on and always passed to the node op
- // functions provided via options, so the internal constraint is really just
- // a generic object.
- export interface RendererNode {
- [key: string | symbol]: any
- }
- export interface RendererElement extends RendererNode {}
- // An object exposing the internals of a renderer, passed to tree-shakeable
- // features so that they can be decoupled from this file. Keys are shortened
- // to optimize bundle size.
- export interface RendererInternals<
- HostNode = RendererNode,
- HostElement = RendererElement,
- > {
- p: PatchFn
- um: UnmountFn
- r: RemoveFn
- m: MoveFn
- mt: MountComponentFn
- mc: MountChildrenFn
- pc: PatchChildrenFn
- pbc: PatchBlockChildrenFn
- n: NextFn
- o: RendererOptions<HostNode, HostElement>
- }
- // These functions are created inside a closure and therefore their types cannot
- // be directly exported. In order to avoid maintaining function signatures in
- // two places, we declare them once here and use them inside the closure.
- type PatchFn = (
- n1: VNode | null, // null means this is a mount
- n2: VNode,
- container: RendererElement,
- anchor?: RendererNode | null,
- parentComponent?: ComponentInternalInstance | null,
- parentSuspense?: SuspenseBoundary | null,
- namespace?: ElementNamespace,
- slotScopeIds?: string[] | null,
- optimized?: boolean,
- ) => void
- type MountChildrenFn = (
- children: VNodeArrayChildren,
- container: RendererElement,
- anchor: RendererNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- namespace: ElementNamespace,
- slotScopeIds: string[] | null,
- optimized: boolean,
- start?: number,
- ) => void
- type PatchChildrenFn = (
- n1: VNode | null,
- n2: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- namespace: ElementNamespace,
- slotScopeIds: string[] | null,
- optimized: boolean,
- ) => void
- type PatchBlockChildrenFn = (
- oldChildren: VNode[],
- newChildren: VNode[],
- fallbackContainer: RendererElement,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- namespace: ElementNamespace,
- slotScopeIds: string[] | null,
- ) => void
- type MoveFn = (
- vnode: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- type: MoveType,
- parentSuspense?: SuspenseBoundary | null,
- ) => void
- type NextFn = (vnode: VNode) => RendererNode | null
- type UnmountFn = (
- vnode: VNode,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- doRemove?: boolean,
- optimized?: boolean,
- ) => void
- type RemoveFn = (vnode: VNode) => void
- type UnmountChildrenFn = (
- children: VNode[],
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- doRemove?: boolean,
- optimized?: boolean,
- start?: number,
- ) => void
- export type MountComponentFn = (
- initialVNode: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- namespace: ElementNamespace,
- optimized: boolean,
- ) => void
- type ProcessTextOrCommentFn = (
- n1: VNode | null,
- n2: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- ) => void
- export type SetupRenderEffectFn = (
- instance: ComponentInternalInstance,
- initialVNode: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- parentSuspense: SuspenseBoundary | null,
- namespace: ElementNamespace,
- optimized: boolean,
- ) => void
- export enum MoveType {
- ENTER,
- LEAVE,
- REORDER,
- }
- export const queuePostRenderEffect: (
- fn: SchedulerJobs,
- suspense: SuspenseBoundary | null,
- ) => void = __FEATURE_SUSPENSE__
- ? __TEST__
- ? // vitest can't seem to handle eager circular dependency
- (fn: Function | Function[], suspense: SuspenseBoundary | null) =>
- queueEffectWithSuspense(fn, suspense)
- : queueEffectWithSuspense
- : queuePostFlushCb
- /**
- * The createRenderer function accepts two generic arguments:
- * HostNode and HostElement, corresponding to Node and Element types in the
- * host environment. For example, for runtime-dom, HostNode would be the DOM
- * `Node` interface and HostElement would be the DOM `Element` interface.
- *
- * Custom renderers can pass in the platform specific types like this:
- *
- * ``` js
- * const { render, createApp } = createRenderer<Node, Element>({
- * patchProp,
- * ...nodeOps
- * })
- * ```
- */
- export function createRenderer<
- HostNode = RendererNode,
- HostElement = RendererElement,
- >(options: RendererOptions<HostNode, HostElement>): Renderer<HostElement> {
- return baseCreateRenderer<HostNode, HostElement>(options)
- }
- // Separate API for creating hydration-enabled renderer.
- // Hydration logic is only used when calling this function, making it
- // tree-shakable.
- export function createHydrationRenderer(
- options: RendererOptions<Node, Element>,
- ): HydrationRenderer {
- return baseCreateRenderer(options, createHydrationFunctions)
- }
- // overload 1: no hydration
- function baseCreateRenderer<
- HostNode = RendererNode,
- HostElement = RendererElement,
- >(options: RendererOptions<HostNode, HostElement>): Renderer<HostElement>
- // overload 2: with hydration
- function baseCreateRenderer(
- options: RendererOptions<Node, Element>,
- createHydrationFns: typeof createHydrationFunctions,
- ): HydrationRenderer
- // implementation
- function baseCreateRenderer(
- options: RendererOptions,
- createHydrationFns?: typeof createHydrationFunctions,
- ): any {
- // compile-time feature flags check
- if (__ESM_BUNDLER__ && !__TEST__) {
- initFeatureFlags()
- }
- const target = getGlobalThis()
- target.__VUE__ = true
- if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
- setDevtoolsHook(target.__VUE_DEVTOOLS_GLOBAL_HOOK__, target)
- }
- const {
- insert: hostInsert,
- remove: hostRemove,
- patchProp: hostPatchProp,
- createElement: hostCreateElement,
- createText: hostCreateText,
- createComment: hostCreateComment,
- setText: hostSetText,
- setElementText: hostSetElementText,
- parentNode: hostParentNode,
- nextSibling: hostNextSibling,
- setScopeId: hostSetScopeId = NOOP,
- insertStaticContent: hostInsertStaticContent,
- } = options
- // Note: functions inside this closure should use `const xxx = () => {}`
- // style in order to prevent being inlined by minifiers.
- const patch: PatchFn = (
- n1,
- n2,
- container,
- anchor = null,
- parentComponent = null,
- parentSuspense = null,
- namespace = undefined,
- slotScopeIds = null,
- optimized = __DEV__ && isHmrUpdating ? false : !!n2.dynamicChildren,
- ) => {
- if (n1 === n2) {
- return
- }
- // patching & not same type, unmount old tree
- if (n1 && !isSameVNodeType(n1, n2)) {
- anchor = getNextHostNode(n1)
- unmount(n1, parentComponent, parentSuspense, true)
- n1 = null
- }
- if (n2.patchFlag === PatchFlags.BAIL) {
- optimized = false
- n2.dynamicChildren = null
- }
- const { type, ref, shapeFlag } = n2
- switch (type) {
- case Text:
- processText(n1, n2, container, anchor)
- break
- case Comment:
- processCommentNode(n1, n2, container, anchor)
- break
- case Static:
- if (n1 == null) {
- mountStaticNode(n2, container, anchor, namespace)
- } else if (__DEV__) {
- patchStaticNode(n1, n2, container, namespace)
- }
- break
- case Fragment:
- processFragment(
- n1,
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- )
- break
- default:
- if (shapeFlag & ShapeFlags.ELEMENT) {
- processElement(
- n1,
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- )
- } else if (shapeFlag & ShapeFlags.COMPONENT) {
- processComponent(
- n1,
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- )
- } else if (shapeFlag & ShapeFlags.TELEPORT) {
- ;(type as typeof TeleportImpl).process(
- n1 as TeleportVNode,
- n2 as TeleportVNode,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- internals,
- )
- } else if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
- ;(type as typeof SuspenseImpl).process(
- n1,
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- internals,
- )
- } else if (__DEV__) {
- warn('Invalid VNode type:', type, `(${typeof type})`)
- }
- }
- // set ref
- if (ref != null && parentComponent) {
- setRef(ref, n1 && n1.ref, parentSuspense, n2 || n1, !n2)
- }
- }
- const processText: ProcessTextOrCommentFn = (n1, n2, container, anchor) => {
- if (n1 == null) {
- hostInsert(
- (n2.el = hostCreateText(n2.children as string)),
- container,
- anchor,
- )
- } else {
- const el = (n2.el = n1.el!)
- if (n2.children !== n1.children) {
- hostSetText(el, n2.children as string)
- }
- }
- }
- const processCommentNode: ProcessTextOrCommentFn = (
- n1,
- n2,
- container,
- anchor,
- ) => {
- if (n1 == null) {
- hostInsert(
- (n2.el = hostCreateComment((n2.children as string) || '')),
- container,
- anchor,
- )
- } else {
- // there's no support for dynamic comments
- n2.el = n1.el
- }
- }
- const mountStaticNode = (
- n2: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- namespace: ElementNamespace,
- ) => {
- // static nodes are only present when used with compiler-dom/runtime-dom
- // which guarantees presence of hostInsertStaticContent.
- ;[n2.el, n2.anchor] = hostInsertStaticContent!(
- n2.children as string,
- container,
- anchor,
- namespace,
- n2.el,
- n2.anchor,
- )
- }
- /**
- * Dev / HMR only
- */
- const patchStaticNode = (
- n1: VNode,
- n2: VNode,
- container: RendererElement,
- namespace: ElementNamespace,
- ) => {
- // static nodes are only patched during dev for HMR
- if (n2.children !== n1.children) {
- const anchor = hostNextSibling(n1.anchor!)
- // remove existing
- removeStaticNode(n1)
- // insert new
- ;[n2.el, n2.anchor] = hostInsertStaticContent!(
- n2.children as string,
- container,
- anchor,
- namespace,
- )
- } else {
- n2.el = n1.el
- n2.anchor = n1.anchor
- }
- }
- const moveStaticNode = (
- { el, anchor }: VNode,
- container: RendererElement,
- nextSibling: RendererNode | null,
- ) => {
- let next
- while (el && el !== anchor) {
- next = hostNextSibling(el)
- hostInsert(el, container, nextSibling)
- el = next
- }
- hostInsert(anchor!, container, nextSibling)
- }
- const removeStaticNode = ({ el, anchor }: VNode) => {
- let next
- while (el && el !== anchor) {
- next = hostNextSibling(el)
- hostRemove(el)
- el = next
- }
- hostRemove(anchor!)
- }
- const processElement = (
- n1: VNode | null,
- n2: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- namespace: ElementNamespace,
- slotScopeIds: string[] | null,
- optimized: boolean,
- ) => {
- if (n2.type === 'svg') {
- namespace = 'svg'
- } else if (n2.type === 'math') {
- namespace = 'mathml'
- }
- if (n1 == null) {
- mountElement(
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- )
- } else {
- patchElement(
- n1,
- n2,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- )
- }
- }
- const mountElement = (
- vnode: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- namespace: ElementNamespace,
- slotScopeIds: string[] | null,
- optimized: boolean,
- ) => {
- let el: RendererElement
- let vnodeHook: VNodeHook | undefined | null
- const { props, shapeFlag, transition, dirs } = vnode
- el = vnode.el = hostCreateElement(
- vnode.type as string,
- namespace,
- props && props.is,
- props,
- )
- // mount children first, since some props may rely on child content
- // being already rendered, e.g. `<select value>`
- if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
- hostSetElementText(el, vnode.children as string)
- } else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- mountChildren(
- vnode.children as VNodeArrayChildren,
- el,
- null,
- parentComponent,
- parentSuspense,
- resolveChildrenNamespace(vnode, namespace),
- slotScopeIds,
- optimized,
- )
- }
- if (dirs) {
- invokeDirectiveHook(vnode, null, parentComponent, 'created')
- }
- // scopeId
- setScopeId(el, vnode, vnode.scopeId, slotScopeIds, parentComponent)
- // props
- if (props) {
- for (const key in props) {
- if (key !== 'value' && !isReservedProp(key)) {
- hostPatchProp(el, key, null, props[key], namespace, parentComponent)
- }
- }
- /**
- * Special case for setting value on DOM elements:
- * - it can be order-sensitive (e.g. should be set *after* min/max, #2325, #4024)
- * - it needs to be forced (#1471)
- * #2353 proposes adding another renderer option to configure this, but
- * the properties affects are so finite it is worth special casing it
- * here to reduce the complexity. (Special casing it also should not
- * affect non-DOM renderers)
- */
- if ('value' in props) {
- hostPatchProp(el, 'value', null, props.value, namespace)
- }
- if ((vnodeHook = props.onVnodeBeforeMount)) {
- invokeVNodeHook(vnodeHook, parentComponent, vnode)
- }
- }
- if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
- def(el, '__vnode', vnode, true)
- def(el, '__vueParentComponent', parentComponent, true)
- }
- if (dirs) {
- invokeDirectiveHook(vnode, null, parentComponent, 'beforeMount')
- }
- // #1583 For inside suspense + suspense not resolved case, enter hook should call when suspense resolved
- // #1689 For inside suspense + suspense resolved case, just call it
- const needCallTransitionHooks = needTransition(parentSuspense, transition)
- if (needCallTransitionHooks) {
- transition!.beforeEnter(el)
- }
- hostInsert(el, container, anchor)
- if (
- (vnodeHook = props && props.onVnodeMounted) ||
- needCallTransitionHooks ||
- dirs
- ) {
- queuePostRenderEffect(() => {
- vnodeHook && invokeVNodeHook(vnodeHook, parentComponent, vnode)
- needCallTransitionHooks && transition!.enter(el)
- dirs && invokeDirectiveHook(vnode, null, parentComponent, 'mounted')
- }, parentSuspense)
- }
- }
- const setScopeId = (
- el: RendererElement,
- vnode: VNode,
- scopeId: string | null,
- slotScopeIds: string[] | null,
- parentComponent: ComponentInternalInstance | null,
- ) => {
- if (scopeId) {
- hostSetScopeId(el, scopeId)
- }
- if (slotScopeIds) {
- for (let i = 0; i < slotScopeIds.length; i++) {
- hostSetScopeId(el, slotScopeIds[i])
- }
- }
- if (parentComponent) {
- let subTree = parentComponent.subTree
- if (
- __DEV__ &&
- subTree.patchFlag > 0 &&
- subTree.patchFlag & PatchFlags.DEV_ROOT_FRAGMENT
- ) {
- subTree =
- filterSingleRoot(subTree.children as VNodeArrayChildren) || subTree
- }
- if (
- vnode === subTree ||
- (isSuspense(subTree.type) &&
- (subTree.ssContent === vnode || subTree.ssFallback === vnode))
- ) {
- const parentVNode = parentComponent.vnode
- setScopeId(
- el,
- parentVNode,
- parentVNode.scopeId,
- parentVNode.slotScopeIds,
- parentComponent.parent,
- )
- }
- }
- }
- const mountChildren: MountChildrenFn = (
- children,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- namespace: ElementNamespace,
- slotScopeIds,
- optimized,
- start = 0,
- ) => {
- for (let i = start; i < children.length; i++) {
- const child = (children[i] = optimized
- ? cloneIfMounted(children[i] as VNode)
- : normalizeVNode(children[i]))
- patch(
- null,
- child,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- )
- }
- }
- const patchElement = (
- n1: VNode,
- n2: VNode,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- namespace: ElementNamespace,
- slotScopeIds: string[] | null,
- optimized: boolean,
- ) => {
- const el = (n2.el = n1.el!)
- if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
- el.__vnode = n2
- }
- let { patchFlag, dynamicChildren, dirs } = n2
- // #1426 take the old vnode's patch flag into account since user may clone a
- // compiler-generated vnode, which de-opts to FULL_PROPS
- patchFlag |= n1.patchFlag & PatchFlags.FULL_PROPS
- const oldProps = n1.props || EMPTY_OBJ
- const newProps = n2.props || EMPTY_OBJ
- let vnodeHook: VNodeHook | undefined | null
- // disable recurse in beforeUpdate hooks
- parentComponent && toggleRecurse(parentComponent, false)
- if ((vnodeHook = newProps.onVnodeBeforeUpdate)) {
- invokeVNodeHook(vnodeHook, parentComponent, n2, n1)
- }
- if (dirs) {
- invokeDirectiveHook(n2, n1, parentComponent, 'beforeUpdate')
- }
- parentComponent && toggleRecurse(parentComponent, true)
- if (__DEV__ && isHmrUpdating) {
- // HMR updated, force full diff
- patchFlag = 0
- optimized = false
- dynamicChildren = null
- }
- // #9135 innerHTML / textContent unset needs to happen before possible
- // new children mount
- if (
- (oldProps.innerHTML && newProps.innerHTML == null) ||
- (oldProps.textContent && newProps.textContent == null)
- ) {
- hostSetElementText(el, '')
- }
- if (dynamicChildren) {
- patchBlockChildren(
- n1.dynamicChildren!,
- dynamicChildren,
- el,
- parentComponent,
- parentSuspense,
- resolveChildrenNamespace(n2, namespace),
- slotScopeIds,
- )
- if (__DEV__) {
- // necessary for HMR
- traverseStaticChildren(n1, n2)
- }
- } else if (!optimized) {
- // full diff
- patchChildren(
- n1,
- n2,
- el,
- null,
- parentComponent,
- parentSuspense,
- resolveChildrenNamespace(n2, namespace),
- slotScopeIds,
- false,
- )
- }
- if (patchFlag > 0) {
- // the presence of a patchFlag means this element's render code was
- // generated by the compiler and can take the fast path.
- // in this path old node and new node are guaranteed to have the same shape
- // (i.e. at the exact same position in the source template)
- if (patchFlag & PatchFlags.FULL_PROPS) {
- // element props contain dynamic keys, full diff needed
- patchProps(el, oldProps, newProps, parentComponent, namespace)
- } else {
- // class
- // this flag is matched when the element has dynamic class bindings.
- if (patchFlag & PatchFlags.CLASS) {
- if (oldProps.class !== newProps.class) {
- hostPatchProp(el, 'class', null, newProps.class, namespace)
- }
- }
- // style
- // this flag is matched when the element has dynamic style bindings
- if (patchFlag & PatchFlags.STYLE) {
- hostPatchProp(el, 'style', oldProps.style, newProps.style, namespace)
- }
- // props
- // This flag is matched when the element has dynamic prop/attr bindings
- // other than class and style. The keys of dynamic prop/attrs are saved for
- // faster iteration.
- // Note dynamic keys like :[foo]="bar" will cause this optimization to
- // bail out and go through a full diff because we need to unset the old key
- if (patchFlag & PatchFlags.PROPS) {
- // if the flag is present then dynamicProps must be non-null
- const propsToUpdate = n2.dynamicProps!
- for (let i = 0; i < propsToUpdate.length; i++) {
- const key = propsToUpdate[i]
- const prev = oldProps[key]
- const next = newProps[key]
- // #1471 force patch value
- if (next !== prev || key === 'value') {
- hostPatchProp(el, key, prev, next, namespace, parentComponent)
- }
- }
- }
- }
- // text
- // This flag is matched when the element has only dynamic text children.
- if (patchFlag & PatchFlags.TEXT) {
- if (n1.children !== n2.children) {
- hostSetElementText(el, n2.children as string)
- }
- }
- } else if (!optimized && dynamicChildren == null) {
- // unoptimized, full diff
- patchProps(el, oldProps, newProps, parentComponent, namespace)
- }
- if ((vnodeHook = newProps.onVnodeUpdated) || dirs) {
- queuePostRenderEffect(() => {
- vnodeHook && invokeVNodeHook(vnodeHook, parentComponent, n2, n1)
- dirs && invokeDirectiveHook(n2, n1, parentComponent, 'updated')
- }, parentSuspense)
- }
- }
- // The fast path for blocks.
- const patchBlockChildren: PatchBlockChildrenFn = (
- oldChildren,
- newChildren,
- fallbackContainer,
- parentComponent,
- parentSuspense,
- namespace: ElementNamespace,
- slotScopeIds,
- ) => {
- for (let i = 0; i < newChildren.length; i++) {
- const oldVNode = oldChildren[i]
- const newVNode = newChildren[i]
- // Determine the container (parent element) for the patch.
- const container =
- // oldVNode may be an errored async setup() component inside Suspense
- // which will not have a mounted element
- oldVNode.el &&
- // - In the case of a Fragment, we need to provide the actual parent
- // of the Fragment itself so it can move its children.
- (oldVNode.type === Fragment ||
- // - In the case of different nodes, there is going to be a replacement
- // which also requires the correct parent container
- !isSameVNodeType(oldVNode, newVNode) ||
- // - In the case of a component, it could contain anything.
- oldVNode.shapeFlag & (ShapeFlags.COMPONENT | ShapeFlags.TELEPORT))
- ? hostParentNode(oldVNode.el)!
- : // In other cases, the parent container is not actually used so we
- // just pass the block element here to avoid a DOM parentNode call.
- fallbackContainer
- patch(
- oldVNode,
- newVNode,
- container,
- null,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- true,
- )
- }
- }
- const patchProps = (
- el: RendererElement,
- oldProps: Data,
- newProps: Data,
- parentComponent: ComponentInternalInstance | null,
- namespace: ElementNamespace,
- ) => {
- if (oldProps !== newProps) {
- if (oldProps !== EMPTY_OBJ) {
- for (const key in oldProps) {
- if (!isReservedProp(key) && !(key in newProps)) {
- hostPatchProp(
- el,
- key,
- oldProps[key],
- null,
- namespace,
- parentComponent,
- )
- }
- }
- }
- for (const key in newProps) {
- // empty string is not valid prop
- if (isReservedProp(key)) continue
- const next = newProps[key]
- const prev = oldProps[key]
- // defer patching value
- if (next !== prev && key !== 'value') {
- hostPatchProp(el, key, prev, next, namespace, parentComponent)
- }
- }
- if ('value' in newProps) {
- hostPatchProp(el, 'value', oldProps.value, newProps.value, namespace)
- }
- }
- }
- const processFragment = (
- n1: VNode | null,
- n2: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- namespace: ElementNamespace,
- slotScopeIds: string[] | null,
- optimized: boolean,
- ) => {
- const fragmentStartAnchor = (n2.el = n1 ? n1.el : hostCreateText(''))!
- const fragmentEndAnchor = (n2.anchor = n1 ? n1.anchor : hostCreateText(''))!
- let { patchFlag, dynamicChildren, slotScopeIds: fragmentSlotScopeIds } = n2
- if (
- __DEV__ &&
- // #5523 dev root fragment may inherit directives
- (isHmrUpdating || patchFlag & PatchFlags.DEV_ROOT_FRAGMENT)
- ) {
- // HMR updated / Dev root fragment (w/ comments), force full diff
- patchFlag = 0
- optimized = false
- dynamicChildren = null
- }
- // check if this is a slot fragment with :slotted scope ids
- if (fragmentSlotScopeIds) {
- slotScopeIds = slotScopeIds
- ? slotScopeIds.concat(fragmentSlotScopeIds)
- : fragmentSlotScopeIds
- }
- if (n1 == null) {
- hostInsert(fragmentStartAnchor, container, anchor)
- hostInsert(fragmentEndAnchor, container, anchor)
- // a fragment can only have array children
- // since they are either generated by the compiler, or implicitly created
- // from arrays.
- mountChildren(
- // #10007
- // such fragment like `<></>` will be compiled into
- // a fragment which doesn't have a children.
- // In this case fallback to an empty array
- (n2.children || []) as VNodeArrayChildren,
- container,
- fragmentEndAnchor,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- )
- } else {
- if (
- patchFlag > 0 &&
- patchFlag & PatchFlags.STABLE_FRAGMENT &&
- dynamicChildren &&
- // #2715 the previous fragment could've been a BAILed one as a result
- // of renderSlot() with no valid children
- n1.dynamicChildren
- ) {
- // a stable fragment (template root or <template v-for>) doesn't need to
- // patch children order, but it may contain dynamicChildren.
- patchBlockChildren(
- n1.dynamicChildren,
- dynamicChildren,
- container,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- )
- if (__DEV__) {
- // necessary for HMR
- traverseStaticChildren(n1, n2)
- } else if (
- // #2080 if the stable fragment has a key, it's a <template v-for> that may
- // get moved around. Make sure all root level vnodes inherit el.
- // #2134 or if it's a component root, it may also get moved around
- // as the component is being moved.
- n2.key != null ||
- (parentComponent && n2 === parentComponent.subTree)
- ) {
- traverseStaticChildren(n1, n2, true /* shallow */)
- }
- } else {
- // keyed / unkeyed, or manual fragments.
- // for keyed & unkeyed, since they are compiler generated from v-for,
- // each child is guaranteed to be a block so the fragment will never
- // have dynamicChildren.
- patchChildren(
- n1,
- n2,
- container,
- fragmentEndAnchor,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- )
- }
- }
- }
- const processComponent = (
- n1: VNode | null,
- n2: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- namespace: ElementNamespace,
- slotScopeIds: string[] | null,
- optimized: boolean,
- ) => {
- n2.slotScopeIds = slotScopeIds
- if (n1 == null) {
- if (n2.shapeFlag & ShapeFlags.COMPONENT_KEPT_ALIVE) {
- ;(parentComponent!.ctx as KeepAliveContext).activate(
- n2,
- container,
- anchor,
- namespace,
- optimized,
- )
- } else {
- mountComponent(
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- namespace,
- optimized,
- )
- }
- } else {
- updateComponent(n1, n2, optimized)
- }
- }
- const mountComponent: MountComponentFn = (
- initialVNode,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- namespace: ElementNamespace,
- optimized,
- ) => {
- // 2.x compat may pre-create the component instance before actually
- // mounting
- const compatMountInstance =
- __COMPAT__ && initialVNode.isCompatRoot && initialVNode.component
- const instance: ComponentInternalInstance =
- compatMountInstance ||
- (initialVNode.component = createComponentInstance(
- initialVNode,
- parentComponent,
- parentSuspense,
- ))
- if (__DEV__ && instance.type.__hmrId) {
- registerHMR(instance)
- }
- if (__DEV__) {
- pushWarningContext(initialVNode)
- startMeasure(instance, `mount`)
- }
- // inject renderer internals for keepAlive
- if (isKeepAlive(initialVNode)) {
- ;(instance.ctx as KeepAliveContext).renderer = internals
- }
- // resolve props and slots for setup context
- if (!(__COMPAT__ && compatMountInstance)) {
- if (__DEV__) {
- startMeasure(instance, `init`)
- }
- setupComponent(instance, false, optimized)
- if (__DEV__) {
- endMeasure(instance, `init`)
- }
- }
- // setup() is async. This component relies on async logic to be resolved
- // before proceeding
- if (__FEATURE_SUSPENSE__ && instance.asyncDep) {
- // avoid hydration for hmr updating
- if (__DEV__ && isHmrUpdating) initialVNode.el = null
- parentSuspense &&
- parentSuspense.registerDep(instance, setupRenderEffect, optimized)
- // Give it a placeholder if this is not hydration
- // TODO handle self-defined fallback
- if (!initialVNode.el) {
- const placeholder = (instance.subTree = createVNode(Comment))
- processCommentNode(null, placeholder, container!, anchor)
- }
- } else {
- setupRenderEffect(
- instance,
- initialVNode,
- container,
- anchor,
- parentSuspense,
- namespace,
- optimized,
- )
- }
- if (__DEV__) {
- popWarningContext()
- endMeasure(instance, `mount`)
- }
- }
- const updateComponent = (n1: VNode, n2: VNode, optimized: boolean) => {
- const instance = (n2.component = n1.component)!
- if (shouldUpdateComponent(n1, n2, optimized)) {
- if (
- __FEATURE_SUSPENSE__ &&
- instance.asyncDep &&
- !instance.asyncResolved
- ) {
- // async & still pending - just update props and slots
- // since the component's reactive effect for render isn't set-up yet
- if (__DEV__) {
- pushWarningContext(n2)
- }
- updateComponentPreRender(instance, n2, optimized)
- if (__DEV__) {
- popWarningContext()
- }
- return
- } else {
- // normal update
- instance.next = n2
- // instance.update is the reactive effect.
- instance.update()
- }
- } else {
- // no update needed. just copy over properties
- n2.el = n1.el
- instance.vnode = n2
- }
- }
- const setupRenderEffect: SetupRenderEffectFn = (
- instance,
- initialVNode,
- container,
- anchor,
- parentSuspense,
- namespace: ElementNamespace,
- optimized,
- ) => {
- const componentUpdateFn = () => {
- if (!instance.isMounted) {
- let vnodeHook: VNodeHook | null | undefined
- const { el, props } = initialVNode
- const { bm, m, parent, root, type } = instance
- const isAsyncWrapperVNode = isAsyncWrapper(initialVNode)
- toggleRecurse(instance, false)
- // beforeMount hook
- if (bm) {
- invokeArrayFns(bm)
- }
- // onVnodeBeforeMount
- if (
- !isAsyncWrapperVNode &&
- (vnodeHook = props && props.onVnodeBeforeMount)
- ) {
- invokeVNodeHook(vnodeHook, parent, initialVNode)
- }
- if (
- __COMPAT__ &&
- isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS, instance)
- ) {
- instance.emit('hook:beforeMount')
- }
- toggleRecurse(instance, true)
- if (el && hydrateNode) {
- // vnode has adopted host node - perform hydration instead of mount.
- const hydrateSubTree = () => {
- if (__DEV__) {
- startMeasure(instance, `render`)
- }
- instance.subTree = renderComponentRoot(instance)
- if (__DEV__) {
- endMeasure(instance, `render`)
- }
- if (__DEV__) {
- startMeasure(instance, `hydrate`)
- }
- hydrateNode!(
- el as Node,
- instance.subTree,
- instance,
- parentSuspense,
- null,
- )
- if (__DEV__) {
- endMeasure(instance, `hydrate`)
- }
- }
- if (
- isAsyncWrapperVNode &&
- (type as ComponentOptions).__asyncHydrate
- ) {
- ;(type as ComponentOptions).__asyncHydrate!(
- el as Element,
- instance,
- hydrateSubTree,
- )
- } else {
- hydrateSubTree()
- }
- } else {
- // custom element style injection
- if (root.ce) {
- root.ce._injectChildStyle(type)
- }
- if (__DEV__) {
- startMeasure(instance, `render`)
- }
- const subTree = (instance.subTree = renderComponentRoot(instance))
- if (__DEV__) {
- endMeasure(instance, `render`)
- }
- if (__DEV__) {
- startMeasure(instance, `patch`)
- }
- patch(
- null,
- subTree,
- container,
- anchor,
- instance,
- parentSuspense,
- namespace,
- )
- if (__DEV__) {
- endMeasure(instance, `patch`)
- }
- initialVNode.el = subTree.el
- }
- // mounted hook
- if (m) {
- queuePostRenderEffect(m, parentSuspense)
- }
- // onVnodeMounted
- if (
- !isAsyncWrapperVNode &&
- (vnodeHook = props && props.onVnodeMounted)
- ) {
- const scopedInitialVNode = initialVNode
- queuePostRenderEffect(
- () => invokeVNodeHook(vnodeHook!, parent, scopedInitialVNode),
- parentSuspense,
- )
- }
- if (
- __COMPAT__ &&
- isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS, instance)
- ) {
- queuePostRenderEffect(
- () => instance.emit('hook:mounted'),
- parentSuspense,
- )
- }
- // activated hook for keep-alive roots.
- // #1742 activated hook must be accessed after first render
- // since the hook may be injected by a child keep-alive
- if (
- initialVNode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE ||
- (parent &&
- isAsyncWrapper(parent.vnode) &&
- parent.vnode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE)
- ) {
- instance.a && queuePostRenderEffect(instance.a, parentSuspense)
- if (
- __COMPAT__ &&
- isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS, instance)
- ) {
- queuePostRenderEffect(
- () => instance.emit('hook:activated'),
- parentSuspense,
- )
- }
- }
- instance.isMounted = true
- if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
- devtoolsComponentAdded(instance)
- }
- // #2458: deference mount-only object parameters to prevent memleaks
- initialVNode = container = anchor = null as any
- } else {
- let { next, bu, u, parent, vnode } = instance
- if (__FEATURE_SUSPENSE__) {
- const nonHydratedAsyncRoot = locateNonHydratedAsyncRoot(instance)
- // we are trying to update some async comp before hydration
- // this will cause crash because we don't know the root node yet
- if (nonHydratedAsyncRoot) {
- // only sync the properties and abort the rest of operations
- if (next) {
- next.el = vnode.el
- updateComponentPreRender(instance, next, optimized)
- }
- // and continue the rest of operations once the deps are resolved
- nonHydratedAsyncRoot.asyncDep!.then(() => {
- // the instance may be destroyed during the time period
- if (!instance.isUnmounted) {
- componentUpdateFn()
- }
- })
- return
- }
- }
- // updateComponent
- // This is triggered by mutation of component's own state (next: null)
- // OR parent calling processComponent (next: VNode)
- let originNext = next
- let vnodeHook: VNodeHook | null | undefined
- if (__DEV__) {
- pushWarningContext(next || instance.vnode)
- }
- // Disallow component effect recursion during pre-lifecycle hooks.
- toggleRecurse(instance, false)
- if (next) {
- next.el = vnode.el
- updateComponentPreRender(instance, next, optimized)
- } else {
- next = vnode
- }
- // beforeUpdate hook
- if (bu) {
- invokeArrayFns(bu)
- }
- // onVnodeBeforeUpdate
- if ((vnodeHook = next.props && next.props.onVnodeBeforeUpdate)) {
- invokeVNodeHook(vnodeHook, parent, next, vnode)
- }
- if (
- __COMPAT__ &&
- isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS, instance)
- ) {
- instance.emit('hook:beforeUpdate')
- }
- toggleRecurse(instance, true)
- // render
- if (__DEV__) {
- startMeasure(instance, `render`)
- }
- const nextTree = renderComponentRoot(instance)
- if (__DEV__) {
- endMeasure(instance, `render`)
- }
- const prevTree = instance.subTree
- instance.subTree = nextTree
- if (__DEV__) {
- startMeasure(instance, `patch`)
- }
- patch(
- prevTree,
- nextTree,
- // parent may have changed if it's in a teleport
- hostParentNode(prevTree.el!)!,
- // anchor may have changed if it's in a fragment
- getNextHostNode(prevTree),
- instance,
- parentSuspense,
- namespace,
- )
- if (__DEV__) {
- endMeasure(instance, `patch`)
- }
- next.el = nextTree.el
- if (originNext === null) {
- // self-triggered update. In case of HOC, update parent component
- // vnode el. HOC is indicated by parent instance's subTree pointing
- // to child component's vnode
- updateHOCHostEl(instance, nextTree.el)
- }
- // updated hook
- if (u) {
- queuePostRenderEffect(u, parentSuspense)
- }
- // onVnodeUpdated
- if ((vnodeHook = next.props && next.props.onVnodeUpdated)) {
- queuePostRenderEffect(
- () => invokeVNodeHook(vnodeHook!, parent, next!, vnode),
- parentSuspense,
- )
- }
- if (
- __COMPAT__ &&
- isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS, instance)
- ) {
- queuePostRenderEffect(
- () => instance.emit('hook:updated'),
- parentSuspense,
- )
- }
- if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
- devtoolsComponentUpdated(instance)
- }
- if (__DEV__) {
- popWarningContext()
- }
- }
- }
- // create reactive effect for rendering
- instance.scope.on()
- const effect = (instance.effect = new ReactiveEffect(componentUpdateFn))
- instance.scope.off()
- const update = (instance.update = effect.run.bind(effect))
- const job: SchedulerJob = (instance.job = effect.runIfDirty.bind(effect))
- job.i = instance
- job.id = instance.uid
- effect.scheduler = () => queueJob(job)
- // allowRecurse
- // #1801, #2043 component render effects should allow recursive updates
- toggleRecurse(instance, true)
- if (__DEV__) {
- effect.onTrack = instance.rtc
- ? e => invokeArrayFns(instance.rtc!, e)
- : void 0
- effect.onTrigger = instance.rtg
- ? e => invokeArrayFns(instance.rtg!, e)
- : void 0
- }
- update()
- }
- const updateComponentPreRender = (
- instance: ComponentInternalInstance,
- nextVNode: VNode,
- optimized: boolean,
- ) => {
- nextVNode.component = instance
- const prevProps = instance.vnode.props
- instance.vnode = nextVNode
- instance.next = null
- updateProps(instance, nextVNode.props, prevProps, optimized)
- updateSlots(instance, nextVNode.children, optimized)
- pauseTracking()
- // props update may have triggered pre-flush watchers.
- // flush them before the render update.
- flushPreFlushCbs(instance)
- resetTracking()
- }
- const patchChildren: PatchChildrenFn = (
- n1,
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- namespace: ElementNamespace,
- slotScopeIds,
- optimized = false,
- ) => {
- const c1 = n1 && n1.children
- const prevShapeFlag = n1 ? n1.shapeFlag : 0
- const c2 = n2.children
- const { patchFlag, shapeFlag } = n2
- // fast path
- if (patchFlag > 0) {
- if (patchFlag & PatchFlags.KEYED_FRAGMENT) {
- // this could be either fully-keyed or mixed (some keyed some not)
- // presence of patchFlag means children are guaranteed to be arrays
- patchKeyedChildren(
- c1 as VNode[],
- c2 as VNodeArrayChildren,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- )
- return
- } else if (patchFlag & PatchFlags.UNKEYED_FRAGMENT) {
- // unkeyed
- patchUnkeyedChildren(
- c1 as VNode[],
- c2 as VNodeArrayChildren,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- )
- return
- }
- }
- // children has 3 possibilities: text, array or no children.
- if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
- // text children fast path
- if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- unmountChildren(c1 as VNode[], parentComponent, parentSuspense)
- }
- if (c2 !== c1) {
- hostSetElementText(container, c2 as string)
- }
- } else {
- if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- // prev children was array
- if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- // two arrays, cannot assume anything, do full diff
- patchKeyedChildren(
- c1 as VNode[],
- c2 as VNodeArrayChildren,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- )
- } else {
- // no new children, just unmount old
- unmountChildren(c1 as VNode[], parentComponent, parentSuspense, true)
- }
- } else {
- // prev children was text OR null
- // new children is array OR null
- if (prevShapeFlag & ShapeFlags.TEXT_CHILDREN) {
- hostSetElementText(container, '')
- }
- // mount new if array
- if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- mountChildren(
- c2 as VNodeArrayChildren,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- )
- }
- }
- }
- }
- const patchUnkeyedChildren = (
- c1: VNode[],
- c2: VNodeArrayChildren,
- container: RendererElement,
- anchor: RendererNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- namespace: ElementNamespace,
- slotScopeIds: string[] | null,
- optimized: boolean,
- ) => {
- c1 = c1 || EMPTY_ARR
- c2 = c2 || EMPTY_ARR
- const oldLength = c1.length
- const newLength = c2.length
- const commonLength = Math.min(oldLength, newLength)
- let i
- for (i = 0; i < commonLength; i++) {
- const nextChild = (c2[i] = optimized
- ? cloneIfMounted(c2[i] as VNode)
- : normalizeVNode(c2[i]))
- patch(
- c1[i],
- nextChild,
- container,
- null,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- )
- }
- if (oldLength > newLength) {
- // remove old
- unmountChildren(
- c1,
- parentComponent,
- parentSuspense,
- true,
- false,
- commonLength,
- )
- } else {
- // mount new
- mountChildren(
- c2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- commonLength,
- )
- }
- }
- // can be all-keyed or mixed
- const patchKeyedChildren = (
- c1: VNode[],
- c2: VNodeArrayChildren,
- container: RendererElement,
- parentAnchor: RendererNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- namespace: ElementNamespace,
- slotScopeIds: string[] | null,
- optimized: boolean,
- ) => {
- let i = 0
- const l2 = c2.length
- let e1 = c1.length - 1 // prev ending index
- let e2 = l2 - 1 // next ending index
- // 1. sync from start
- // (a b) c
- // (a b) d e
- while (i <= e1 && i <= e2) {
- const n1 = c1[i]
- const n2 = (c2[i] = optimized
- ? cloneIfMounted(c2[i] as VNode)
- : normalizeVNode(c2[i]))
- if (isSameVNodeType(n1, n2)) {
- patch(
- n1,
- n2,
- container,
- null,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- )
- } else {
- break
- }
- i++
- }
- // 2. sync from end
- // a (b c)
- // d e (b c)
- while (i <= e1 && i <= e2) {
- const n1 = c1[e1]
- const n2 = (c2[e2] = optimized
- ? cloneIfMounted(c2[e2] as VNode)
- : normalizeVNode(c2[e2]))
- if (isSameVNodeType(n1, n2)) {
- patch(
- n1,
- n2,
- container,
- null,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- )
- } else {
- break
- }
- e1--
- e2--
- }
- // 3. common sequence + mount
- // (a b)
- // (a b) c
- // i = 2, e1 = 1, e2 = 2
- // (a b)
- // c (a b)
- // i = 0, e1 = -1, e2 = 0
- if (i > e1) {
- if (i <= e2) {
- const nextPos = e2 + 1
- const anchor = nextPos < l2 ? (c2[nextPos] as VNode).el : parentAnchor
- while (i <= e2) {
- patch(
- null,
- (c2[i] = optimized
- ? cloneIfMounted(c2[i] as VNode)
- : normalizeVNode(c2[i])),
- container,
- anchor,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- )
- i++
- }
- }
- }
- // 4. common sequence + unmount
- // (a b) c
- // (a b)
- // i = 2, e1 = 2, e2 = 1
- // a (b c)
- // (b c)
- // i = 0, e1 = 0, e2 = -1
- else if (i > e2) {
- while (i <= e1) {
- unmount(c1[i], parentComponent, parentSuspense, true)
- i++
- }
- }
- // 5. unknown sequence
- // [i ... e1 + 1]: a b [c d e] f g
- // [i ... e2 + 1]: a b [e d c h] f g
- // i = 2, e1 = 4, e2 = 5
- else {
- const s1 = i // prev starting index
- const s2 = i // next starting index
- // 5.1 build key:index map for newChildren
- const keyToNewIndexMap: Map<PropertyKey, number> = new Map()
- for (i = s2; i <= e2; i++) {
- const nextChild = (c2[i] = optimized
- ? cloneIfMounted(c2[i] as VNode)
- : normalizeVNode(c2[i]))
- if (nextChild.key != null) {
- if (__DEV__ && keyToNewIndexMap.has(nextChild.key)) {
- warn(
- `Duplicate keys found during update:`,
- JSON.stringify(nextChild.key),
- `Make sure keys are unique.`,
- )
- }
- keyToNewIndexMap.set(nextChild.key, i)
- }
- }
- // 5.2 loop through old children left to be patched and try to patch
- // matching nodes & remove nodes that are no longer present
- let j
- let patched = 0
- const toBePatched = e2 - s2 + 1
- let moved = false
- // used to track whether any node has moved
- let maxNewIndexSoFar = 0
- // works as Map<newIndex, oldIndex>
- // Note that oldIndex is offset by +1
- // and oldIndex = 0 is a special value indicating the new node has
- // no corresponding old node.
- // used for determining longest stable subsequence
- const newIndexToOldIndexMap = new Array(toBePatched)
- for (i = 0; i < toBePatched; i++) newIndexToOldIndexMap[i] = 0
- for (i = s1; i <= e1; i++) {
- const prevChild = c1[i]
- if (patched >= toBePatched) {
- // all new children have been patched so this can only be a removal
- unmount(prevChild, parentComponent, parentSuspense, true)
- continue
- }
- let newIndex
- if (prevChild.key != null) {
- newIndex = keyToNewIndexMap.get(prevChild.key)
- } else {
- // key-less node, try to locate a key-less node of the same type
- for (j = s2; j <= e2; j++) {
- if (
- newIndexToOldIndexMap[j - s2] === 0 &&
- isSameVNodeType(prevChild, c2[j] as VNode)
- ) {
- newIndex = j
- break
- }
- }
- }
- if (newIndex === undefined) {
- unmount(prevChild, parentComponent, parentSuspense, true)
- } else {
- newIndexToOldIndexMap[newIndex - s2] = i + 1
- if (newIndex >= maxNewIndexSoFar) {
- maxNewIndexSoFar = newIndex
- } else {
- moved = true
- }
- patch(
- prevChild,
- c2[newIndex] as VNode,
- container,
- null,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- )
- patched++
- }
- }
- // 5.3 move and mount
- // generate longest stable subsequence only when nodes have moved
- const increasingNewIndexSequence = moved
- ? getSequence(newIndexToOldIndexMap)
- : EMPTY_ARR
- j = increasingNewIndexSequence.length - 1
- // looping backwards so that we can use last patched node as anchor
- for (i = toBePatched - 1; i >= 0; i--) {
- const nextIndex = s2 + i
- const nextChild = c2[nextIndex] as VNode
- const anchor =
- nextIndex + 1 < l2 ? (c2[nextIndex + 1] as VNode).el : parentAnchor
- if (newIndexToOldIndexMap[i] === 0) {
- // mount new
- patch(
- null,
- nextChild,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- namespace,
- slotScopeIds,
- optimized,
- )
- } else if (moved) {
- // move if:
- // There is no stable subsequence (e.g. a reverse)
- // OR current node is not among the stable sequence
- if (j < 0 || i !== increasingNewIndexSequence[j]) {
- move(nextChild, container, anchor, MoveType.REORDER)
- } else {
- j--
- }
- }
- }
- }
- }
- const move: MoveFn = (
- vnode,
- container,
- anchor,
- moveType,
- parentSuspense = null,
- ) => {
- const { el, type, transition, children, shapeFlag } = vnode
- if (shapeFlag & ShapeFlags.COMPONENT) {
- move(vnode.component!.subTree, container, anchor, moveType)
- return
- }
- if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
- vnode.suspense!.move(container, anchor, moveType)
- return
- }
- if (shapeFlag & ShapeFlags.TELEPORT) {
- ;(type as typeof TeleportImpl).move(vnode, container, anchor, internals)
- return
- }
- if (type === Fragment) {
- hostInsert(el!, container, anchor)
- for (let i = 0; i < (children as VNode[]).length; i++) {
- move((children as VNode[])[i], container, anchor, moveType)
- }
- hostInsert(vnode.anchor!, container, anchor)
- return
- }
- if (type === Static) {
- moveStaticNode(vnode, container, anchor)
- return
- }
- // single nodes
- const needTransition =
- moveType !== MoveType.REORDER &&
- shapeFlag & ShapeFlags.ELEMENT &&
- transition
- if (needTransition) {
- if (moveType === MoveType.ENTER) {
- transition!.beforeEnter(el!)
- hostInsert(el!, container, anchor)
- queuePostRenderEffect(() => transition!.enter(el!), parentSuspense)
- } else {
- const { leave, delayLeave, afterLeave } = transition!
- const remove = () => hostInsert(el!, container, anchor)
- const performLeave = () => {
- leave(el!, () => {
- remove()
- afterLeave && afterLeave()
- })
- }
- if (delayLeave) {
- delayLeave(el!, remove, performLeave)
- } else {
- performLeave()
- }
- }
- } else {
- hostInsert(el!, container, anchor)
- }
- }
- const unmount: UnmountFn = (
- vnode,
- parentComponent,
- parentSuspense,
- doRemove = false,
- optimized = false,
- ) => {
- const {
- type,
- props,
- ref,
- children,
- dynamicChildren,
- shapeFlag,
- patchFlag,
- dirs,
- cacheIndex,
- } = vnode
- if (patchFlag === PatchFlags.BAIL) {
- optimized = false
- }
- // unset ref
- if (ref != null) {
- setRef(ref, null, parentSuspense, vnode, true)
- }
- // #6593 should clean memo cache when unmount
- if (cacheIndex != null) {
- parentComponent!.renderCache[cacheIndex] = undefined
- }
- if (shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE) {
- ;(parentComponent!.ctx as KeepAliveContext).deactivate(vnode)
- return
- }
- const shouldInvokeDirs = shapeFlag & ShapeFlags.ELEMENT && dirs
- const shouldInvokeVnodeHook = !isAsyncWrapper(vnode)
- let vnodeHook: VNodeHook | undefined | null
- if (
- shouldInvokeVnodeHook &&
- (vnodeHook = props && props.onVnodeBeforeUnmount)
- ) {
- invokeVNodeHook(vnodeHook, parentComponent, vnode)
- }
- if (shapeFlag & ShapeFlags.COMPONENT) {
- unmountComponent(vnode.component!, parentSuspense, doRemove)
- } else {
- if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
- vnode.suspense!.unmount(parentSuspense, doRemove)
- return
- }
- if (shouldInvokeDirs) {
- invokeDirectiveHook(vnode, null, parentComponent, 'beforeUnmount')
- }
- if (shapeFlag & ShapeFlags.TELEPORT) {
- ;(vnode.type as typeof TeleportImpl).remove(
- vnode,
- parentComponent,
- parentSuspense,
- internals,
- doRemove,
- )
- } else if (
- dynamicChildren &&
- // #5154
- // when v-once is used inside a block, setBlockTracking(-1) marks the
- // parent block with hasOnce: true
- // so that it doesn't take the fast path during unmount - otherwise
- // components nested in v-once are never unmounted.
- !dynamicChildren.hasOnce &&
- // #1153: fast path should not be taken for non-stable (v-for) fragments
- (type !== Fragment ||
- (patchFlag > 0 && patchFlag & PatchFlags.STABLE_FRAGMENT))
- ) {
- // fast path for block nodes: only need to unmount dynamic children.
- unmountChildren(
- dynamicChildren,
- parentComponent,
- parentSuspense,
- false,
- true,
- )
- } else if (
- (type === Fragment &&
- patchFlag &
- (PatchFlags.KEYED_FRAGMENT | PatchFlags.UNKEYED_FRAGMENT)) ||
- (!optimized && shapeFlag & ShapeFlags.ARRAY_CHILDREN)
- ) {
- unmountChildren(children as VNode[], parentComponent, parentSuspense)
- }
- if (doRemove) {
- remove(vnode)
- }
- }
- if (
- (shouldInvokeVnodeHook &&
- (vnodeHook = props && props.onVnodeUnmounted)) ||
- shouldInvokeDirs
- ) {
- queuePostRenderEffect(() => {
- vnodeHook && invokeVNodeHook(vnodeHook, parentComponent, vnode)
- shouldInvokeDirs &&
- invokeDirectiveHook(vnode, null, parentComponent, 'unmounted')
- }, parentSuspense)
- }
- }
- const remove: RemoveFn = vnode => {
- const { type, el, anchor, transition } = vnode
- if (type === Fragment) {
- if (
- __DEV__ &&
- vnode.patchFlag > 0 &&
- vnode.patchFlag & PatchFlags.DEV_ROOT_FRAGMENT &&
- transition &&
- !transition.persisted
- ) {
- ;(vnode.children as VNode[]).forEach(child => {
- if (child.type === Comment) {
- hostRemove(child.el!)
- } else {
- remove(child)
- }
- })
- } else {
- removeFragment(el!, anchor!)
- }
- return
- }
- if (type === Static) {
- removeStaticNode(vnode)
- return
- }
- const performRemove = () => {
- hostRemove(el!)
- if (transition && !transition.persisted && transition.afterLeave) {
- transition.afterLeave()
- }
- }
- if (
- vnode.shapeFlag & ShapeFlags.ELEMENT &&
- transition &&
- !transition.persisted
- ) {
- const { leave, delayLeave } = transition
- const performLeave = () => leave(el!, performRemove)
- if (delayLeave) {
- delayLeave(vnode.el!, performRemove, performLeave)
- } else {
- performLeave()
- }
- } else {
- performRemove()
- }
- }
- const removeFragment = (cur: RendererNode, end: RendererNode) => {
- // For fragments, directly remove all contained DOM nodes.
- // (fragment child nodes cannot have transition)
- let next
- while (cur !== end) {
- next = hostNextSibling(cur)!
- hostRemove(cur)
- cur = next
- }
- hostRemove(end)
- }
- const unmountComponent = (
- instance: ComponentInternalInstance,
- parentSuspense: SuspenseBoundary | null,
- doRemove?: boolean,
- ) => {
- if (__DEV__ && instance.type.__hmrId) {
- unregisterHMR(instance)
- }
- const { bum, scope, job, subTree, um, m, a } = instance
- invalidateMount(m)
- invalidateMount(a)
- // beforeUnmount hook
- if (bum) {
- invokeArrayFns(bum)
- }
- if (
- __COMPAT__ &&
- isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS, instance)
- ) {
- instance.emit('hook:beforeDestroy')
- }
- // stop effects in component scope
- scope.stop()
- // job may be null if a component is unmounted before its async
- // setup has resolved.
- if (job) {
- // so that scheduler will no longer invoke it
- job.flags! |= SchedulerJobFlags.DISPOSED
- unmount(subTree, instance, parentSuspense, doRemove)
- }
- // unmounted hook
- if (um) {
- queuePostRenderEffect(um, parentSuspense)
- }
- if (
- __COMPAT__ &&
- isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS, instance)
- ) {
- queuePostRenderEffect(
- () => instance.emit('hook:destroyed'),
- parentSuspense,
- )
- }
- queuePostRenderEffect(() => {
- instance.isUnmounted = true
- }, parentSuspense)
- // A component with async dep inside a pending suspense is unmounted before
- // its async dep resolves. This should remove the dep from the suspense, and
- // cause the suspense to resolve immediately if that was the last dep.
- if (
- __FEATURE_SUSPENSE__ &&
- parentSuspense &&
- parentSuspense.pendingBranch &&
- !parentSuspense.isUnmounted &&
- instance.asyncDep &&
- !instance.asyncResolved &&
- instance.suspenseId === parentSuspense.pendingId
- ) {
- parentSuspense.deps--
- if (parentSuspense.deps === 0) {
- parentSuspense.resolve()
- }
- }
- if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
- devtoolsComponentRemoved(instance)
- }
- }
- const unmountChildren: UnmountChildrenFn = (
- children,
- parentComponent,
- parentSuspense,
- doRemove = false,
- optimized = false,
- start = 0,
- ) => {
- for (let i = start; i < children.length; i++) {
- unmount(children[i], parentComponent, parentSuspense, doRemove, optimized)
- }
- }
- const getNextHostNode: NextFn = vnode => {
- if (vnode.shapeFlag & ShapeFlags.COMPONENT) {
- return getNextHostNode(vnode.component!.subTree)
- }
- if (__FEATURE_SUSPENSE__ && vnode.shapeFlag & ShapeFlags.SUSPENSE) {
- return vnode.suspense!.next()
- }
- const el = hostNextSibling((vnode.anchor || vnode.el)!)
- // #9071, #9313
- // teleported content can mess up nextSibling searches during patch so
- // we need to skip them during nextSibling search
- const teleportEnd = el && el[TeleportEndKey]
- return teleportEnd ? hostNextSibling(teleportEnd) : el
- }
- let isFlushing = false
- const render: RootRenderFunction = (vnode, container, namespace) => {
- if (vnode == null) {
- if (container._vnode) {
- unmount(container._vnode, null, null, true)
- }
- } else {
- patch(
- container._vnode || null,
- vnode,
- container,
- null,
- null,
- null,
- namespace,
- )
- }
- container._vnode = vnode
- if (!isFlushing) {
- isFlushing = true
- flushPreFlushCbs()
- flushPostFlushCbs()
- isFlushing = false
- }
- }
- const internals: RendererInternals = {
- p: patch,
- um: unmount,
- m: move,
- r: remove,
- mt: mountComponent,
- mc: mountChildren,
- pc: patchChildren,
- pbc: patchBlockChildren,
- n: getNextHostNode,
- o: options,
- }
- let hydrate: ReturnType<typeof createHydrationFunctions>[0] | undefined
- let hydrateNode: ReturnType<typeof createHydrationFunctions>[1] | undefined
- if (createHydrationFns) {
- ;[hydrate, hydrateNode] = createHydrationFns(
- internals as RendererInternals<Node, Element>,
- )
- }
- return {
- render,
- hydrate,
- createApp: createAppAPI(render, hydrate),
- }
- }
- function resolveChildrenNamespace(
- { type, props }: VNode,
- currentNamespace: ElementNamespace,
- ): ElementNamespace {
- return (currentNamespace === 'svg' && type === 'foreignObject') ||
- (currentNamespace === 'mathml' &&
- type === 'annotation-xml' &&
- props &&
- props.encoding &&
- props.encoding.includes('html'))
- ? undefined
- : currentNamespace
- }
- function toggleRecurse(
- { effect, job }: ComponentInternalInstance,
- allowed: boolean,
- ) {
- if (allowed) {
- effect.flags |= EffectFlags.ALLOW_RECURSE
- job.flags! |= SchedulerJobFlags.ALLOW_RECURSE
- } else {
- effect.flags &= ~EffectFlags.ALLOW_RECURSE
- job.flags! &= ~SchedulerJobFlags.ALLOW_RECURSE
- }
- }
- export function needTransition(
- parentSuspense: SuspenseBoundary | null,
- transition: TransitionHooks | null,
- ): boolean | null {
- return (
- (!parentSuspense || (parentSuspense && !parentSuspense.pendingBranch)) &&
- transition &&
- !transition.persisted
- )
- }
- /**
- * #1156
- * When a component is HMR-enabled, we need to make sure that all static nodes
- * inside a block also inherit the DOM element from the previous tree so that
- * HMR updates (which are full updates) can retrieve the element for patching.
- *
- * #2080
- * Inside keyed `template` fragment static children, if a fragment is moved,
- * the children will always be moved. Therefore, in order to ensure correct move
- * position, el should be inherited from previous nodes.
- */
- export function traverseStaticChildren(
- n1: VNode,
- n2: VNode,
- shallow = false,
- ): void {
- const ch1 = n1.children
- const ch2 = n2.children
- if (isArray(ch1) && isArray(ch2)) {
- for (let i = 0; i < ch1.length; i++) {
- // this is only called in the optimized path so array children are
- // guaranteed to be vnodes
- const c1 = ch1[i] as VNode
- let c2 = ch2[i] as VNode
- if (c2.shapeFlag & ShapeFlags.ELEMENT && !c2.dynamicChildren) {
- if (c2.patchFlag <= 0 || c2.patchFlag === PatchFlags.NEED_HYDRATION) {
- c2 = ch2[i] = cloneIfMounted(ch2[i] as VNode)
- c2.el = c1.el
- }
- if (!shallow && c2.patchFlag !== PatchFlags.BAIL)
- traverseStaticChildren(c1, c2)
- }
- // #6852 also inherit for text nodes
- if (c2.type === Text) {
- c2.el = c1.el
- }
- // also inherit for comment nodes, but not placeholders (e.g. v-if which
- // would have received .el during block patch)
- if (__DEV__ && c2.type === Comment && !c2.el) {
- c2.el = c1.el
- }
- }
- }
- }
- // https://en.wikipedia.org/wiki/Longest_increasing_subsequence
- function getSequence(arr: number[]): number[] {
- const p = arr.slice()
- const result = [0]
- let i, j, u, v, c
- const len = arr.length
- for (i = 0; i < len; i++) {
- const arrI = arr[i]
- if (arrI !== 0) {
- j = result[result.length - 1]
- if (arr[j] < arrI) {
- p[i] = j
- result.push(i)
- continue
- }
- u = 0
- v = result.length - 1
- while (u < v) {
- c = (u + v) >> 1
- if (arr[result[c]] < arrI) {
- u = c + 1
- } else {
- v = c
- }
- }
- if (arrI < arr[result[u]]) {
- if (u > 0) {
- p[i] = result[u - 1]
- }
- result[u] = i
- }
- }
- }
- u = result.length
- v = result[u - 1]
- while (u-- > 0) {
- result[u] = v
- v = p[v]
- }
- return result
- }
- function locateNonHydratedAsyncRoot(
- instance: ComponentInternalInstance,
- ): ComponentInternalInstance | undefined {
- const subComponent = instance.subTree.component
- if (subComponent) {
- if (subComponent.asyncDep && !subComponent.asyncResolved) {
- return subComponent
- } else {
- return locateNonHydratedAsyncRoot(subComponent)
- }
- }
- }
- export function invalidateMount(hooks: LifecycleHook): void {
- if (hooks) {
- for (let i = 0; i < hooks.length; i++)
- hooks[i].flags! |= SchedulerJobFlags.DISPOSED
- }
- }
|