Skip to content

Commit 684a8de

Browse files
authored
dx(runtime-core): warn when innerHTML/textContent overrides children (#15194)
close #5081
1 parent ac7431c commit 684a8de

3 files changed

Lines changed: 81 additions & 0 deletions

File tree

‎packages/runtime-core/__tests__/vnode.spec.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,50 @@ describe('vnode', () => {
9292
expect(`VNode created with invalid key (NaN)`).toHaveBeenWarned()
9393
})
9494

95+
// #5081
96+
describe('children overridden by innerHTML / textContent', () => {
97+
test('warn on text children', () => {
98+
createVNode('div', { innerHTML: '<span/>' }, 'hello')
99+
expect(
100+
`The \`innerHTML\` prop on <div> will override its children`,
101+
).toHaveBeenWarned()
102+
})
103+
104+
test('warn on array children', () => {
105+
createVNode('div', { innerHTML: '<span/>' }, [createVNode('span')])
106+
expect(
107+
`The \`innerHTML\` prop on <div> will override its children`,
108+
).toHaveBeenWarned()
109+
})
110+
111+
test('warn on textContent', () => {
112+
createVNode('div', { textContent: 'text' }, 'hello')
113+
expect(
114+
`The \`textContent\` prop on <div> will override its children`,
115+
).toHaveBeenWarned()
116+
})
117+
118+
test('no warning when the prop is nullish', () => {
119+
createVNode('div', { innerHTML: undefined }, 'hello')
120+
createVNode('div', { innerHTML: null }, 'hello')
121+
createVNode('div', { textContent: undefined }, 'hello')
122+
expect(`will override its children`).not.toHaveBeenWarned()
123+
})
124+
125+
test('no warning without renderable children', () => {
126+
createVNode('div', { innerHTML: '<span/>' })
127+
createVNode('div', { innerHTML: '<span/>' }, '')
128+
createVNode('div', { innerHTML: '<span/>' }, [])
129+
expect(`will override its children`).not.toHaveBeenWarned()
130+
})
131+
132+
test('no warning for component vnodes', () => {
133+
const Comp = { props: ['innerHTML'], render: () => null }
134+
createVNode(Comp, { innerHTML: '<span/>' }, { default: () => 'slot' })
135+
expect(`will override its children`).not.toHaveBeenWarned()
136+
})
137+
})
138+
95139
test('create with class component', () => {
96140
class Component {
97141
$props: any

‎packages/runtime-core/src/vnode.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,25 @@ function createBaseVNode(
499499
warn(`VNode created with invalid key (NaN). VNode type:`, vnode.type)
500500
}
501501

502+
// #5081 validate children that will be silently discarded by innerHTML /
503+
// textContent. The template compiler already errors on `v-html` / `v-text`
504+
// used with children, but render functions have no such check.
505+
if (__DEV__ && props && vnode.shapeFlag & ShapeFlags.ELEMENT) {
506+
const overwritingProp =
507+
props.innerHTML != null
508+
? 'innerHTML'
509+
: props.textContent != null
510+
? 'textContent'
511+
: null
512+
if (overwritingProp && hasContentChildren(vnode.children)) {
513+
warn(
514+
`The \`${overwritingProp}\` prop on <${vnode.type as string}> will ` +
515+
`override its children. Remove either the \`${overwritingProp}\` ` +
516+
`prop or the children.`,
517+
)
518+
}
519+
}
520+
502521
// track vnode for block tree
503522
if (
504523
isBlockTreeEnabled > 0 &&
@@ -528,6 +547,18 @@ function createBaseVNode(
528547

529548
export { createBaseVNode as createElementVNode }
530549

550+
/**
551+
* dev only
552+
* Whether children would actually render something. Empty text and empty
553+
* arrays are ignored, mirroring the compiler's `node.children.length` check
554+
* for `v-html` / `v-text`.
555+
*/
556+
function hasContentChildren(children: VNode['children']): boolean {
557+
if (isString(children)) return children !== ''
558+
if (isArray(children)) return children.length > 0
559+
return false
560+
}
561+
531562
export const createVNode = (
532563
__DEV__ ? createVNodeWithArgsTransform : _createVNode
533564
) as typeof _createVNode

‎packages/server-renderer/__tests__/render.spec.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,9 @@ function testRender(type: string, render: typeof renderToString) {
664664
),
665665
),
666666
).toBe(`<div><span>hello</span></div>`)
667+
expect(
668+
`The \`innerHTML\` prop on <div> will override its children`,
669+
).toHaveBeenWarned()
667670
})
668671

669672
test('textContent', async () => {
@@ -678,6 +681,9 @@ function testRender(type: string, render: typeof renderToString) {
678681
),
679682
),
680683
).toBe(`<div>${escapeHtml(`<span>hello</span>`)}</div>`)
684+
expect(
685+
`The \`textContent\` prop on <div> will override its children`,
686+
).toHaveBeenWarned()
681687
})
682688

683689
test('textarea value', async () => {

0 commit comments

Comments
 (0)