Skip to content

Commit

Permalink
fix(runtime-core): avoid hoisted vnodes retaining detached DOM nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 authored and chrislone committed Feb 4, 2023
1 parent b250eb5 commit d66dc8e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 39 deletions.
35 changes: 3 additions & 32 deletions packages/compiler-core/__tests__/transforms/hoistStatic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
import {
FRAGMENT,
RENDER_LIST,
CREATE_TEXT,
NORMALIZE_CLASS
} from '../../src/runtimeHelpers'
import { transformElement } from '../../src/transforms/transformElement'
Expand Down Expand Up @@ -378,36 +377,6 @@ describe('compiler: hoistStatic transform', () => {
expect(generate(root).code).toMatchSnapshot()
})

test('hoist static text node between elements', () => {
const root = transformWithHoist(`<div>static<div>static</div></div>`)
expect(root.hoists).toMatchObject([
{
callee: CREATE_TEXT,
arguments: [
{
type: NodeTypes.TEXT,
content: `static`
}
]
},
{
type: NodeTypes.VNODE_CALL,
tag: `"div"`
},
{
type: NodeTypes.JS_ARRAY_EXPRESSION,
elements: [
{
type: NodeTypes.TEXT_CALL
},
{
type: NodeTypes.ELEMENT
}
]
}
])
})

describe('prefixIdentifiers', () => {
test('hoist nested static tree with static interpolation', () => {
const root = transformWithHoist(
Expand Down Expand Up @@ -618,7 +587,9 @@ describe('compiler: hoistStatic transform', () => {
})

test('should NOT hoist SVG with directives', () => {
const root = transformWithHoist(`<div><svg v-foo><path d="M2,3H5.5L12"/></svg></div>`)
const root = transformWithHoist(
`<div><svg v-foo><path d="M2,3H5.5L12"/></svg></div>`
)
expect(root.hoists.length).toBe(2)
expect(generate(root).code).toMatchSnapshot()
})
Expand Down
6 changes: 0 additions & 6 deletions packages/compiler-core/src/transforms/hoistStatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,6 @@ function walk(
}
}
}
} else if (
child.type === NodeTypes.TEXT_CALL &&
getConstantType(child.content, context) >= ConstantTypes.CAN_HOIST
) {
child.codegenNode = context.hoist(child.codegenNode)
hoistedCount++
}

// walk further
Expand Down
5 changes: 4 additions & 1 deletion packages/runtime-core/src/vnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,10 @@ export function normalizeVNode(child: VNodeChild): VNode {

// optimized normalization for template-compiled render fns
export function cloneIfMounted(child: VNode): VNode {
return child.el === null || child.memo ? child : cloneVNode(child)
return (child.el === null && child.patchFlag !== PatchFlags.HOISTED) ||
child.memo
? child
: cloneVNode(child)
}

export function normalizeChildren(vnode: VNode, children: unknown) {
Expand Down

0 comments on commit d66dc8e

Please sign in to comment.