Skip to content

Commit a8c435d

Browse files
committed
refactor: relocate getRootElement to custom.ts and update custom directive tests to use template helper.
1 parent 02a013a commit a8c435d

File tree

3 files changed

+47
-51
lines changed

3 files changed

+47
-51
lines changed

packages/runtime-vapor/__tests__/directives/customDirective.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
type VaporDirective,
44
createComponent,
55
defineVaporComponent,
6+
template,
67
withVaporDirectives,
78
} from '../../src'
89
import { nextTick, watchEffect } from '@vue/runtime-dom'
@@ -58,8 +59,7 @@ describe('custom directive', () => {
5859
// Child component with single root
5960
const Child = defineVaporComponent({
6061
render() {
61-
const el = document.createElement('div')
62-
return el
62+
return template('<div></div>', true)()
6363
},
6464
})
6565

@@ -92,7 +92,7 @@ describe('custom directive', () => {
9292
// Child component with multiple roots
9393
const Child = defineVaporComponent({
9494
render() {
95-
return [document.createElement('div'), document.createElement('span')]
95+
return [template('<div></div>')(), template('<span></span>')()]
9696
},
9797
})
9898

packages/runtime-vapor/src/component.ts

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ import {
100100
isLastInsertion,
101101
resetInsertionState,
102102
} from './insertionState'
103-
import { DynamicFragment, isFragment } from './fragment'
103+
import { DynamicFragment } from './fragment'
104104
import type { VaporElement } from './apiDefineVaporCustomElement'
105105

106106
export { currentInstance } from '@vue/runtime-dom'
@@ -831,48 +831,6 @@ export function getExposed(
831831
}
832832
}
833833

834-
export function getRootElement(
835-
block: Block,
836-
recurse: boolean = true,
837-
): Element | undefined {
838-
if (block instanceof Element) {
839-
return block
840-
}
841-
842-
if (recurse && isVaporComponent(block)) {
843-
return getRootElement(block.block, recurse)
844-
}
845-
846-
if (isFragment(block)) {
847-
const { nodes } = block
848-
if (nodes instanceof Element && (nodes as any).$root) {
849-
return nodes
850-
}
851-
return getRootElement(nodes, recurse)
852-
}
853-
854-
// The root node contains comments. It is necessary to filter out
855-
// the comment nodes and return a single root node.
856-
// align with vdom behavior
857-
if (isArray(block)) {
858-
let singleRoot: Element | undefined
859-
let hasComment = false
860-
for (const b of block) {
861-
if (b instanceof Comment) {
862-
hasComment = true
863-
continue
864-
}
865-
const thisRoot = getRootElement(b, recurse)
866-
// only return root if there is exactly one eligible root in the array
867-
if (!thisRoot || singleRoot) {
868-
return
869-
}
870-
singleRoot = thisRoot
871-
}
872-
return hasComment ? singleRoot : undefined
873-
}
874-
}
875-
876834
export function filterSingleRootElement(block: Block): Element | undefined {
877835
let singleRoot
878836
if (block instanceof Element) {

packages/runtime-vapor/src/directives/custom.ts

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { type DirectiveModifiers, onScopeDispose, warn } from '@vue/runtime-dom'
2-
import {
3-
type VaporComponentInstance,
4-
getRootElement,
5-
isVaporComponent,
6-
} from '../component'
2+
import { type VaporComponentInstance, isVaporComponent } from '../component'
3+
import type { Block } from '../block'
4+
import { isFragment } from '../fragment'
5+
import { isArray } from '@vue/shared'
76

87
// !! vapor directive is different from vdom directives
98
export type VaporDirective = (
@@ -47,3 +46,42 @@ export function withVaporDirectives(
4746
}
4847
}
4948
}
49+
50+
export function getRootElement(block: Block): Element | undefined {
51+
if (block instanceof Element && (block as any).$root) {
52+
return block
53+
}
54+
55+
if (isVaporComponent(block)) {
56+
return getRootElement(block.block)
57+
}
58+
59+
if (isFragment(block)) {
60+
const { nodes } = block
61+
if (nodes instanceof Element && (nodes as any).$root) {
62+
return nodes
63+
}
64+
return getRootElement(nodes)
65+
}
66+
67+
// The root node contains comments. It is necessary to filter out
68+
// the comment nodes and return a single root node.
69+
// align with vdom behavior
70+
if (isArray(block)) {
71+
let singleRoot: Element | undefined
72+
let hasComment = false
73+
for (const b of block) {
74+
if (b instanceof Comment) {
75+
hasComment = true
76+
continue
77+
}
78+
const thisRoot = getRootElement(b)
79+
// only return root if there is exactly one eligible root in the array
80+
if (!thisRoot || singleRoot) {
81+
return
82+
}
83+
singleRoot = thisRoot
84+
}
85+
return hasComment ? singleRoot : undefined
86+
}
87+
}

0 commit comments

Comments
 (0)