Skip to content

Commit db38ecc

Browse files
committed
chore: special handling text node
1 parent 5b917ab commit db38ecc

File tree

2 files changed

+64
-16
lines changed

2 files changed

+64
-16
lines changed

packages/runtime-vapor/__tests__/dom/prop.spec.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ describe('patchProp', () => {
506506
expect(html()).toBe('<div>foo</div>')
507507
})
508508

509-
test('with multiple roots component', async () => {
509+
test('with component renders multiple roots nodes', async () => {
510510
const Comp = defineVaporComponent({
511511
setup() {
512512
return [
@@ -527,6 +527,25 @@ describe('patchProp', () => {
527527
expect(html()).toBe('<div>child</div><div>child</div>')
528528
expect('Extraneous non-props attributes (textContent)').toHaveBeenWarned()
529529
})
530+
531+
test('with component renders text node', async () => {
532+
const Comp = defineVaporComponent({
533+
setup() {
534+
return template('child')()
535+
},
536+
})
537+
const value = ref('foo')
538+
const { html } = define({
539+
setup() {
540+
const n1 = createComponent(Comp, null, null, true)
541+
renderEffect(() => setBlockText(n1, toDisplayString(value)))
542+
return n1
543+
},
544+
}).render()
545+
546+
expect(html()).toBe('child')
547+
expect('Extraneous non-props attributes (textContent)').toHaveBeenWarned()
548+
})
530549
})
531550

532551
describe('setBlockHtml', () => {
@@ -579,7 +598,7 @@ describe('patchProp', () => {
579598
expect(html()).toBe('<div><p>foo</p></div>')
580599
})
581600

582-
test('with multiple roots component', async () => {
601+
test('with component renders multiple roots', async () => {
583602
const Comp = defineVaporComponent({
584603
setup() {
585604
return [
@@ -600,5 +619,24 @@ describe('patchProp', () => {
600619
expect(html()).toBe('<div>child</div><div>child</div>')
601620
expect('Extraneous non-props attributes (innerHTML)').toHaveBeenWarned()
602621
})
622+
623+
test('with component renders text node', async () => {
624+
const Comp = defineVaporComponent({
625+
setup() {
626+
return template('child')()
627+
},
628+
})
629+
const value = ref('<p>foo</p>')
630+
const { html } = define({
631+
setup() {
632+
const n1 = createComponent(Comp, null, null, true)
633+
renderEffect(() => setBlockHtml(n1, value.value))
634+
return n1
635+
},
636+
}).render()
637+
638+
expect(html()).toBe('child')
639+
expect('Extraneous non-props attributes (innerHTML)').toHaveBeenWarned()
640+
})
603641
})
604642
})

packages/runtime-vapor/src/dom/prop.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,19 +209,30 @@ export function setBlockText(
209209
}
210210
}
211211

212+
/**
213+
* dev only
214+
*/
215+
function warnOnArrayBlock(prop: string): void {
216+
warn(
217+
`Extraneous non-props attributes (` +
218+
`${prop}) ` +
219+
`were passed to component but could not be automatically inherited ` +
220+
`because component renders text or multiple root nodes.`,
221+
)
222+
}
223+
212224
function setTextToBlock(block: Block, value: any): void {
213225
if (block instanceof Node) {
214-
;(block as Element).textContent = value
226+
if (block instanceof Element) {
227+
block.textContent = value
228+
} else if (__DEV__) {
229+
warnOnArrayBlock('textContent')
230+
}
215231
} else if (isVaporComponent(block)) {
216232
setTextToBlock(block.block, value)
217233
} else if (isArray(block)) {
218234
if (__DEV__) {
219-
warn(
220-
`Extraneous non-props attributes (` +
221-
`textContent) ` +
222-
`were passed to component but could not be automatically inherited ` +
223-
`because component renders multiple root nodes.`,
224-
)
235+
warnOnArrayBlock('textContent')
225236
}
226237
} else {
227238
setTextToBlock(block.nodes, value)
@@ -247,17 +258,16 @@ export function setBlockHtml(
247258

248259
function setHtmlToBlock(block: Block, value: any): void {
249260
if (block instanceof Node) {
250-
;(block as Element).innerHTML = value
261+
if (block instanceof Element) {
262+
block.innerHTML = value
263+
} else if (__DEV__) {
264+
warnOnArrayBlock('innerHTML')
265+
}
251266
} else if (isVaporComponent(block)) {
252267
setHtmlToBlock(block.block, value)
253268
} else if (isArray(block)) {
254269
if (__DEV__) {
255-
warn(
256-
`Extraneous non-props attributes (` +
257-
`innerHTML) ` +
258-
`were passed to component but could not be automatically inherited ` +
259-
`because component renders multiple root nodes.`,
260-
)
270+
warnOnArrayBlock('innerHTML')
261271
}
262272
} else {
263273
setHtmlToBlock(block.nodes, value)

0 commit comments

Comments
 (0)