Backtestsvuerun-20261021-133314#5081
Throw an error on createVNode with using innerHTML and children at the same time
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
The issue asks for no error and for the element to show innerHTML when provided, otherwise the children, with both changing dynamically. On the base tree the original crash no longer reproduces (the mount test passes on base), so the live defect is the dynamic case: when innerHTML/textContent goes from a value back to nullish while children are unchanged, the element stays empty because the old child vnodes are still considered mounted but their DOM was replaced. Patch B addresses exactly that in patchElement: it broadens the reset condition from truthy to `!= null` (so an empty-string innerHTML also unsets correctly) and, after clearing the element, puts the still-mounted old children's DOM back (text via hostSetElementText, arrays via move) so the subsequent diff operates on real content. Its tests cover text children, array children with component reuse (no remount), empty-string toggles, textContent, and a compiled block with PROPS patch flags, plus SSR nullish cases; the independent toggle tests go from FAIL on base to PASS with B. Patch A does not change runtime behaviour at all; it only adds a dev warning telling users not to combine innerHTML/textContent with children, which runs contrary to the issue's stated expectation and leaves all three independent toggle scenarios failing exactly as on base. Its tests pass only because they assert the new warning text, not any fix. B's approach carries some risk (moving vnodes whose DOM was detached by innerHTML), but it is targeted, commented, and well exercised, including the block-tree path. B is the better fix.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
8m 27s
Wall-clock time in the container
Full test suite
No new failures
1,761 passed · 1 skipped · 1,762 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
178vs 81 merged
Changed lines, ours vs merged
The issue
View on GitHubVersion
3.2.24
Reproduction link
Steps to reproduce
import { createApp, defineComponent, h } from "vue";
const App = defineComponent({
setup() {
return () => h("div", { innerHTML: undefined }, "a");
}
});
createApp(App).mount("#app");
I want to change innerHTML of props and children dynamic at same time.
What is expected?
no Error and use innerHTML if provided or children content.
What is actually happening?
throw an error.
TypeError Cannot read properties of undefined (reading '__asyncLoader')
<!-- generated by vue-issues. DO NOT REMOVE -->
The two patches
packages/runtime-core/src/renderer.tsindex c0fab7373..b7ade6196 100644@@ -872,10 +872,22 @@ function baseCreateRenderer( // #9135 innerHTML / textContent unset needs to happen before possible // new children mount if (- (oldProps.innerHTML && newProps.innerHTML == null) ||- (oldProps.textContent && newProps.textContent == null)+ (oldProps.innerHTML != null && newProps.innerHTML == null) ||+ (oldProps.textContent != null && newProps.textContent == null) ) { hostSetElementText(el, '')+ // the old children's DOM was replaced by innerHTML / textContent but the+ // old child vnodes are still mounted (and may already have been patched+ // in place by a parent block). Put their DOM back so the diff below+ // operates on the actual element content.+ if (n1.shapeFlag & ShapeFlags.TEXT_CHILDREN) {+ hostSetElementText(el, n1.children as string)+ } else if (n1.shapeFlag & ShapeFlags.ARRAY_CHILDREN) {+ const c1 = n1.children as VNode[]+ for (let i = 0; i < c1.length; i++) {+ move(c1[i], el, null, MoveType.REORDER)+ }+ } } if (dynamicChildren) {packages/runtime-dom/__tests__/patchProps.spec.tsindex 304d9f9c1..fb3e96abf 100644@@ -1,12 +1,17 @@ import { patchProp } from '../src/patchProp' import {+ createElementBlock,+ createElementVNode, h, nextTick,+ openBlock, ref, render,+ toDisplayString, vModelCheckbox, withDirectives, } from '../src'+import { PatchFlags } from '@vue/shared' describe('runtime-dom: props patching', () => { test('basic', () => {@@ -165,6 +170,151 @@ describe('runtime-dom: props patching', () => { expect(root.innerHTML).toBe(`<div></div>`) }) + test('mount nullish innerHTML w/ children', () => {+ const root = document.createElement('div')+ render(h('div', { innerHTML: undefined }, 'a'), root)+ expect(root.innerHTML).toBe(`<div>a</div>`)++ const root2 = document.createElement('div')+ render(h('div', { innerHTML: null }, [h('span', 'b')]), root2)+ expect(root2.innerHTML).toBe(`<div><span>b</span></div>`)++ const root3 = document.createElement('div')+ render(h('div', { textContent: undefined }, 'c'), root3)+ expect(root3.innerHTML).toBe(`<div>c</div>`)+ })++ test('toggle innerHTML w/ unchanged text children', async () => {+ const root = document.createElement('div')+ const html = ref<string | undefined>('<i>x</i>')+ const Comp = {+ render: () => h('div', { innerHTML: html.value }, 'a'),+ }+ render(h(Comp), root)+ expect(root.innerHTML).toBe(`<div><i>x</i></div>`)++ html.value = undefined+ await nextTick()+ expect(root.innerHTML).toBe(`<div>a</div>`)++ html.value = '<i>y</i>'+ await nextTick()+ expect(root.innerHTML).toBe(`<div><i>y</i></div>`)++ html.value = undefined+ await nextTick()+ expect(root.innerHTML).toBe(`<div>a</div>`)+ })++ test('toggle innerHTML w/ unchanged array children', async () => {+ const root = document.createElement('div')+ const html = ref<string | null>('<i>x</i>')+ const mounted = vi.fn()+ const unmounted = vi.fn()+ const Child = {+ render: () => h('span', 'b'),+ mounted,+ unmounted,+ }+ const Comp = {+ render: () => h('div', { innerHTML: html.value }, [h(Child)]),+ }+ render(h(Comp), root)+ expect(root.innerHTML).toBe(`<div><i>x</i></div>`)++ html.value = null+ await nextTick()+ expect(root.innerHTML).toBe(`<div><span>b</span></div>`)+ // the existing child instance is reused, not remounted+ expect(unmounted).toHaveBeenCalledTimes(0)+ expect(mounted).toHaveBeenCalledTimes(1)++ html.value = '<i>y</i>'+ await nextTick()+ expect(root.innerHTML).toBe(`<div><i>y</i></div>`)+ })++ test('toggle empty-string innerHTML / textContent w/ children', () => {+ const root = document.createElement('div')+ render(h('div', { innerHTML: '' }, 'a'), root)+ expect(root.innerHTML).toBe(`<div></div>`)+ render(h('div', { innerHTML: undefined }, 'a'), root)+ expect(root.innerHTML).toBe(`<div>a</div>`)++ const root2 = document.createElement('div')+ render(h('div', { textContent: '' }, [h('span', 'b')]), root2)+ expect(root2.innerHTML).toBe(`<div></div>`)+ render(h('div', { textContent: null }, [h('span', 'b')]), root2)+ expect(root2.innerHTML).toBe(`<div><span>b</span></div>`)+ })++ test('toggle innerHTML w/ children in compiled block', async () => {+ const root = document.createElement('div')+ const html = ref<string | null>('<i>x</i>')+ const msg = ref('a')+ const Comp = {+ render: () => (+ openBlock(),+ createElementBlock('section', null, [+ createElementVNode(+ 'div',+ { innerHTML: html.value },+ [+ createElementVNode(+ 'span',+ null,+ toDisplayString(msg.value),+ PatchFlags.TEXT,+ ),+ ],+ PatchFlags.PROPS,+ ['innerHTML'],+ ),+ ])+ ),+ }+ render(h(Comp), root)+ expect(root.innerHTML).toBe(`<section><div><i>x</i></div></section>`)++ html.value = null+ await nextTick()+ expect(root.innerHTML).toBe(`<section><div><span>a</span></div></section>`)++ // subsequent updates must patch the live DOM+ msg.value = 'b'+ await nextTick()+ expect(root.innerHTML).toBe(`<section><div><span>b</span></div></section>`)++ html.value = '<i>y</i>'+ msg.value = 'c'+ await nextTick()+ expect(root.innerHTML).toBe(`<section><div><i>y</i></div></section>`)++ html.value = null+ await nextTick()+ expect(root.innerHTML).toBe(`<section><div><span>c</span></div></section>`)+ })++ test('toggle textContent w/ unchanged children', async () => {+ const root = document.createElement('div')+ const text = ref<string | undefined>('x')+ const Comp = {+ render: () => h('div', { textContent: text.value }, 'a'),+ }+ render(h(Comp), root)+ expect(root.innerHTML).toBe(`<div>x</div>`)++ text.value = undefined+ await nextTick()+ expect(root.innerHTML).toBe(`<div>a</div>`)++ const root2 = document.createElement('div')+ render(h('div', { textContent: 'x' }, [h('span', 'b')]), root2)+ expect(root2.innerHTML).toBe(`<div>x</div>`)+ render(h('div', { textContent: null }, [h('span', 'b')]), root2)+ expect(root2.innerHTML).toBe(`<div><span>b</span></div>`)+ })+ test('textContent unmount prev children', () => { const fn = vi.fn() const comp = {packages/server-renderer/__tests__/render.spec.tsindex f537a6219..baac8f2b9 100644@@ -666,6 +666,18 @@ function testRender(type: string, render: typeof renderToString) { ).toBe(`<div><span>hello</span></div>`) }) + test('nullish innerHTML / textContent w/ children', async () => {+ expect(await render(h('div', { innerHTML: undefined }, 'a'))).toBe(+ `<div>a</div>`,+ )+ expect(+ await render(h('div', { innerHTML: null }, [h('span', 'b')])),+ ).toBe(`<div><span>b</span></div>`)+ expect(await render(h('div', { textContent: undefined }, 'c'))).toBe(+ `<div>c</div>`,+ )+ })+ test('textContent', async () => { expect( await render(
packages/runtime-core/__tests__/vnode.spec.tsindex 3a88f302b..000ded438 100644@@ -92,6 +92,50 @@ describe('vnode', () => { expect(`VNode created with invalid key (NaN)`).toHaveBeenWarned() }) + // #5081+ describe('children overridden by innerHTML / textContent', () => {+ test('warn on text children', () => {+ createVNode('div', { innerHTML: '<span/>' }, 'hello')+ expect(+ `The \`innerHTML\` prop on <div> will override its children`,+ ).toHaveBeenWarned()+ })++ test('warn on array children', () => {+ createVNode('div', { innerHTML: '<span/>' }, [createVNode('span')])+ expect(+ `The \`innerHTML\` prop on <div> will override its children`,+ ).toHaveBeenWarned()+ })++ test('warn on textContent', () => {+ createVNode('div', { textContent: 'text' }, 'hello')+ expect(+ `The \`textContent\` prop on <div> will override its children`,+ ).toHaveBeenWarned()+ })++ test('no warning when the prop is nullish', () => {+ createVNode('div', { innerHTML: undefined }, 'hello')+ createVNode('div', { innerHTML: null }, 'hello')+ createVNode('div', { textContent: undefined }, 'hello')+ expect(`will override its children`).not.toHaveBeenWarned()+ })++ test('no warning without renderable children', () => {+ createVNode('div', { innerHTML: '<span/>' })+ createVNode('div', { innerHTML: '<span/>' }, '')+ createVNode('div', { innerHTML: '<span/>' }, [])+ expect(`will override its children`).not.toHaveBeenWarned()+ })++ test('no warning for component vnodes', () => {+ const Comp = { props: ['innerHTML'], render: () => null }+ createVNode(Comp, { innerHTML: '<span/>' }, { default: () => 'slot' })+ expect(`will override its children`).not.toHaveBeenWarned()+ })+ })+ test('create with class component', () => { class Component { $props: anypackages/runtime-core/src/vnode.tsindex e9745c503..30d7fdfda 100644@@ -499,6 +499,25 @@ function createBaseVNode( warn(`VNode created with invalid key (NaN). VNode type:`, vnode.type) } + // #5081 validate children that will be silently discarded by innerHTML /+ // textContent. The template compiler already errors on `v-html` / `v-text`+ // used with children, but render functions have no such check.+ if (__DEV__ && props && vnode.shapeFlag & ShapeFlags.ELEMENT) {+ const overwritingProp =+ props.innerHTML != null+ ? 'innerHTML'+ : props.textContent != null+ ? 'textContent'+ : null+ if (overwritingProp && hasContentChildren(vnode.children)) {+ warn(+ `The \`${overwritingProp}\` prop on <${vnode.type as string}> will ` ++ `override its children. Remove either the \`${overwritingProp}\` ` ++ `prop or the children.`,+ )+ }+ }+ // track vnode for block tree if ( isBlockTreeEnabled > 0 &&@@ -528,6 +547,18 @@ function createBaseVNode( export { createBaseVNode as createElementVNode } +/**+ * dev only+ * Whether children would actually render something. Empty text and empty+ * arrays are ignored, mirroring the compiler's `node.children.length` check+ * for `v-html` / `v-text`.+ */+function hasContentChildren(children: VNode['children']): boolean {+ if (isString(children)) return children !== ''+ if (isArray(children)) return children.length > 0+ return false+}+ export const createVNode = ( __DEV__ ? createVNodeWithArgsTransform : _createVNode ) as typeof _createVNodepackages/server-renderer/__tests__/render.spec.tsindex f537a6219..37de08bd4 100644@@ -664,6 +664,9 @@ function testRender(type: string, render: typeof renderToString) { ), ), ).toBe(`<div><span>hello</span></div>`)+ expect(+ `The \`innerHTML\` prop on <div> will override its children`,+ ).toHaveBeenWarned() }) test('textContent', async () => {@@ -678,6 +681,9 @@ function testRender(type: string, render: typeof renderToString) { ), ), ).toBe(`<div>${escapeHtml(`<span>hello</span>`)}</div>`)+ expect(+ `The \`textContent\` prop on <div> will override its children`,+ ).toHaveBeenWarned() }) test('textarea value', async () => {