Skip to content
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
125 changes: 125 additions & 0 deletions packages/runtime-vapor/__tests__/componentAttrs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,131 @@ describe('attribute fallthrough', () => {
expect(host.innerHTML).toBe('<div id="b">2</div>')
})

it('should allow attrs to fallthrough on component with comment at root', async () => {
const t0 = template('<!--comment-->')
const t1 = template('<div>')
const { component: Child } = define({
props: ['foo'],
setup(props: any) {
const n0 = t0()
const n1 = t1()
renderEffect(() => setElementText(n1, props.foo))
return [n0, n1]
},
})

const foo = ref(1)
const id = ref('a')
const { host } = define({
setup() {
return createComponent(
Child,
{
foo: () => foo.value,
id: () => id.value,
},
null,
true,
)
},
}).render()
expect(host.innerHTML).toBe('<!--comment--><div id="a">1</div>')

foo.value++
await nextTick()
expect(host.innerHTML).toBe('<!--comment--><div id="a">2</div>')

id.value = 'b'
await nextTick()
expect(host.innerHTML).toBe('<!--comment--><div id="b">2</div>')
})

it('should allow attrs to fallthrough on component with single-element array root', async () => {
const t0 = template('<div>')
const { component: Child } = define({
props: ['foo'],
setup(props: any) {
const n0 = t0()
renderEffect(() => setElementText(n0, props.foo))
return [n0]
},
})

const foo = ref(1)
const id = ref('a')
const { host } = define({
setup() {
return createComponent(
Child,
{
foo: () => foo.value,
id: () => id.value,
},
null,
true,
)
},
}).render()
expect(host.innerHTML).toBe('<div id="a">1</div>')

foo.value++
await nextTick()
expect(host.innerHTML).toBe('<div id="a">2</div>')

id.value = 'b'
await nextTick()
expect(host.innerHTML).toBe('<div id="b">2</div>')
})

it('should not allow attrs to fallthrough on component with multiple roots', async () => {
const t0 = template('<span>')
const t1 = template('<div>')
const { component: Child } = define({
props: ['foo'],
setup(props: any) {
const n0 = t0()
const n1 = t1()
renderEffect(() => setElementText(n1, props.foo))
return [n0, n1]
},
})

const foo = ref(1)
const id = ref('a')
const { host } = define({
setup() {
return createComponent(
Child,
{
foo: () => foo.value,
id: () => id.value,
},
null,
true,
)
},
}).render()
expect(host.innerHTML).toBe('<span></span><div>1</div>')
})

it('should not allow attrs to fallthrough on component with single comment root', async () => {
const t0 = template('<!--comment-->')
const { component: Child } = define({
setup() {
const n0 = t0()
return [n0]
},
})

const id = ref('a')
const { host } = define({
setup() {
return createComponent(Child, { id: () => id.value }, null, true)
},
}).render()
expect(host.innerHTML).toBe('<!--comment-->')
})

it('should not fallthrough if explicitly pass inheritAttrs: false', async () => {
const t0 = template('<div>', true)
const { component: Child } = define({
Expand Down
30 changes: 25 additions & 5 deletions packages/runtime-vapor/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ import {
setActiveSub,
unref,
} from '@vue/reactivity'
import { EMPTY_OBJ, invokeArrayFns, isFunction, isString } from '@vue/shared'
import {
EMPTY_OBJ,
invokeArrayFns,
isArray,
isFunction,
isString,
} from '@vue/shared'
import {
type DynamicPropsSource,
type RawProps,
Expand Down Expand Up @@ -255,7 +261,7 @@ export function createComponent(
component.inheritAttrs !== false &&
Object.keys(instance.attrs).length
) {
const el = getRootElement(instance)
const el = getRootElement(instance.block)
if (el) {
renderEffect(() => {
isApplyingFallthroughProps = true
Expand Down Expand Up @@ -579,9 +585,7 @@ export function getExposed(
}
}

function getRootElement({
block,
}: VaporComponentInstance): Element | undefined {
function getRootElement(block: Block): Element | undefined {
if (block instanceof Element) {
return block
}
Expand All @@ -592,4 +596,20 @@ function getRootElement({
return nodes
}
}

if (isArray(block)) {
let singleRoot: Element | undefined
for (const b of block) {
if (b instanceof Comment) {
continue
}
const thisRoot = getRootElement(b)
// only return root if there is exactly one eligible root in the array
if (!thisRoot || singleRoot) {
return
}
singleRoot = thisRoot
}
return singleRoot
}
}
Loading