Skip to content

Commit 654687b

Browse files
committed
refactor: remove filterSingleRootElement and refine attribute fallthrough for Teleport and DynamicFragment with a new root flag.
1 parent b603d83 commit 654687b

File tree

3 files changed

+29
-54
lines changed

3 files changed

+29
-54
lines changed

packages/runtime-vapor/src/component.ts

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ import {
9090
setCurrentHydrationNode,
9191
} from './dom/hydration'
9292
import { _next, createElement } from './dom/node'
93-
import { type TeleportFragment, isVaporTeleport } from './components/Teleport'
93+
import { TeleportFragment, isVaporTeleport } from './components/Teleport'
9494
import {
9595
type KeepAliveInstance,
9696
findParentKeepAlive,
@@ -407,14 +407,17 @@ export function setupComponent(
407407
component.inheritAttrs !== false &&
408408
Object.keys(instance.attrs).length
409409
) {
410-
const root = filterSingleRootElement(instance.block)
410+
const root = getRootElement(instance.block, false)
411411
if (root) {
412412
renderEffect(() => applyFallthroughProps(root, instance.attrs))
413413
} else if (
414414
__DEV__ &&
415-
!instance.accessedAttrs &&
416-
isArray(instance.block) &&
417-
instance.block.length
415+
((!instance.accessedAttrs &&
416+
isArray(instance.block) &&
417+
instance.block.length) ||
418+
// preventing attrs fallthrough on Teleport
419+
// consistent with VDOM Teleport behavior
420+
instance.block instanceof TeleportFragment)
418421
) {
419422
warnExtraneousAttributes(instance.attrs)
420423
}
@@ -873,6 +876,7 @@ export function getRootElement(
873876
}
874877

875878
if (isFragment(block)) {
879+
if (block instanceof DynamicFragment) block.root = true
876880
const { nodes } = block
877881
if (nodes instanceof Element && (nodes as any).$root) {
878882
return nodes
@@ -901,22 +905,3 @@ export function getRootElement(
901905
return hasComment ? singleRoot : undefined
902906
}
903907
}
904-
905-
export function filterSingleRootElement(block: Block): Element | undefined {
906-
let singleRoot
907-
if (block instanceof Element) {
908-
singleRoot = block
909-
} else if (isArray(block)) {
910-
for (const b of block) {
911-
if (b instanceof Element) {
912-
if (singleRoot) {
913-
// has more than 1 non-comment child
914-
return
915-
} else {
916-
singleRoot = b
917-
}
918-
}
919-
}
920-
}
921-
return singleRoot
922-
}

packages/runtime-vapor/src/components/Teleport.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ import {
99
queuePostFlushCb,
1010
resolveTeleportTarget,
1111
warn,
12-
warnExtraneousAttributes,
1312
} from '@vue/runtime-dom'
1413
import { type Block, type BlockFn, insert, remove } from '../block'
1514
import { createComment, createTextNode, querySelector } from '../dom/node'
1615
import {
1716
type LooseRawProps,
1817
type LooseRawSlots,
19-
type VaporComponentInstance,
2018
isVaporComponent,
2119
} from '../component'
2220
import { rawPropsProxyHandlers } from '../componentProps'
@@ -148,18 +146,6 @@ export class TeleportFragment extends VaporFragment<Block[]> {
148146
this.$transition = applyTransitionHooks(this.nodes, this.$transition)
149147
}
150148

151-
// preventing attrs fallthrough
152-
// consistent with VDOM Teleport behavior
153-
const instance = this.parentComponent as VaporComponentInstance
154-
// TODO: check if component root
155-
if (
156-
__DEV__ &&
157-
instance.hasFallthrough &&
158-
Object.keys(instance.attrs).length
159-
) {
160-
warnExtraneousAttributes(instance.attrs)
161-
}
162-
163149
insert(
164150
this.nodes,
165151
(this.mountContainer = parent),

packages/runtime-vapor/src/fragment.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export class DynamicFragment extends VaporFragment {
7777
current?: BlockFn
7878
fallback?: BlockFn
7979
anchorLabel?: string
80+
root?: boolean
8081

8182
// get the kept-alive scope when used in keep-alive
8283
getScope?: (key: any) => EffectScope | undefined
@@ -195,29 +196,32 @@ export class DynamicFragment extends VaporFragment {
195196
this.$transition = applyTransitionHooks(this.nodes, transition)
196197
}
197198

198-
// fallthrough attrs
199-
// TODO: check if component root
200-
if (
201-
this.parentComponent &&
202-
(this.parentComponent as VaporComponentInstance)!.hasFallthrough &&
203-
Object.keys(this.parentComponent!.attrs).length
204-
) {
205-
if (this.nodes instanceof Element) {
206-
applyFallthroughProps(this.nodes, this.parentComponent!.attrs)
207-
} else if (__DEV__ && this.anchorLabel === 'slot') {
208-
// preventing attrs fallthrough
209-
// consistent with VDOM Teleport behavior
210-
warnExtraneousAttributes(this.parentComponent!.attrs)
211-
}
212-
}
213-
214199
if (this.beforeMount) {
215200
this.beforeMount.forEach(hook =>
216201
hook(this.current, this.nodes, this.scope!),
217202
)
218203
}
219204

220205
if (parent) {
206+
// fallthrough attrs
207+
if (
208+
this.root &&
209+
this.parentComponent &&
210+
(this.parentComponent as VaporComponentInstance)!.hasFallthrough &&
211+
Object.keys(this.parentComponent!.attrs).length
212+
) {
213+
if (this.nodes instanceof Element) {
214+
applyFallthroughProps(this.nodes, this.parentComponent!.attrs)
215+
} else if (
216+
__DEV__ &&
217+
// preventing attrs fallthrough on slots
218+
// consistent with VDOM slots behavior
219+
this.anchorLabel === 'slot'
220+
) {
221+
warnExtraneousAttributes(this.parentComponent!.attrs)
222+
}
223+
}
224+
221225
insert(this.nodes, parent, this.anchor)
222226
if (this.updated) {
223227
this.updated.forEach(hook => hook(this.nodes))

0 commit comments

Comments
 (0)