Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): directives should be handled correctly with fragment #11902

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/runtime-core/src/componentRenderUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from './component'
import {
Comment,
Fragment,
type VNode,
type VNodeArrayChildren,
blockStack,
Expand Down Expand Up @@ -234,6 +235,11 @@ export function renderComponentRoot(
}
}

if (!__DEV__ && root.type === Fragment) {
const singleRoot = filterSingleRoot(root.children as VNodeArrayChildren)
if (singleRoot) root = singleRoot
}

// inherit directives
if (vnode.dirs) {
if (__DEV__ && !isElementRoot(root)) {
Expand Down
26 changes: 23 additions & 3 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2102,7 +2102,22 @@ function baseCreateRenderer(
return
}

const shouldInvokeDirs = shapeFlag & ShapeFlags.ELEMENT && dirs
let shouldInvokeDirs = shapeFlag & ShapeFlags.ELEMENT && dirs
// #5407
let root: VNode | undefined = undefined
if (
__DEV__ &&
vnode.patchFlag > 0 &&
vnode.patchFlag & PatchFlags.DEV_ROOT_FRAGMENT
) {
const singleRoot = filterSingleRoot(vnode.children as VNodeArrayChildren)
if (singleRoot) {
root = singleRoot
shouldInvokeDirs =
singleRoot.shapeFlag & ShapeFlags.ELEMENT && singleRoot.dirs
}
}

const shouldInvokeVnodeHook = !isAsyncWrapper(vnode)

let vnodeHook: VNodeHook | undefined | null
Expand All @@ -2122,7 +2137,12 @@ function baseCreateRenderer(
}

if (shouldInvokeDirs) {
invokeDirectiveHook(vnode, null, parentComponent, 'beforeUnmount')
invokeDirectiveHook(
root || vnode,
null,
parentComponent,
'beforeUnmount',
)
}

if (shapeFlag & ShapeFlags.TELEPORT) {
Expand Down Expand Up @@ -2175,7 +2195,7 @@ function baseCreateRenderer(
queuePostRenderEffect(() => {
vnodeHook && invokeVNodeHook(vnodeHook, parentComponent, vnode)
shouldInvokeDirs &&
invokeDirectiveHook(vnode, null, parentComponent, 'unmounted')
invokeDirectiveHook(root || vnode, null, parentComponent, 'unmounted')
}, parentSuspense)
}
}
Expand Down
36 changes: 35 additions & 1 deletion packages/vue/__tests__/runtimeCompilerOptions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createApp } from 'vue'
import { createApp, nextTick, ref } from 'vue'

describe('config.compilerOptions', () => {
test('isCustomElement', () => {
Expand Down Expand Up @@ -76,6 +76,40 @@ describe('per-component compilerOptions', () => {
__DEV__ = true
})

test('fragment with comments and directives', async () => {
const mounted = vi.fn()
const unmounted = vi.fn()
const foobar = {
mounted,
unmounted,
}
const show = ref(true)
const app = createApp({
directives: {
foobar,
},
components: {
WithCommentNode: {
template: `<!-- Comment Node --><h1>With comment node</h1>`,
},
WithoutCommentNode: {
template: `<h1>Without comment node</h1>`,
},
},
setup() {
return { show }
},
template: `<with-comment-node v-foobar v-if="show"></with-comment-node>
<without-comment-node v-foobar v-if="show"></without-comment-node>`,
})
const root = document.createElement('div')
app.mount(root)
expect(mounted).toHaveBeenCalledTimes(2)
show.value = false
await nextTick()
expect(unmounted).toHaveBeenCalledTimes(2)
})

test('whitespace', () => {
const app = createApp({
template: `<div><span/>\n <span/></div>`,
Expand Down