Backtestsvuerun-20261001-022054#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 that a render function using `innerHTML` and children together not throw. It should render `innerHTML` when that prop is set and the children otherwise, including when the prop changes dynamically. Patch B fixes this behavior in `patchElement`. When `innerHTML`/`textContent` goes from set to null or undefined, the old children's DOM has already been wiped. Patch B re-attaches it: it moves array children back into `el`, or restores the text children. The later children diff then runs against live DOM instead of detached nodes. Patch B also tightens the condition from truthy to `!= null`, so an empty string still counts as a previously set prop. Its tests cover the reported mount case with `innerHTML: undefined`, which should no longer throw. They also cover toggling between a value and undefined/null with text children, with array children, and with a compiled block containing optimized children and a component. The block test also checks the component is not unmounted. The repeated value → undefined toggles would likely fail on unpatched code, because the old children's DOM would be lost. These tests directly exercise what the user asked for. Patch A changes no runtime behavior. It only adds a dev warning when a non-null `innerHTML`/`textContent` is combined with non-empty children. The tests are tidy and consistent, but they only check for the warning. They do not address the crash or the dynamic toggle the user described, and they do not make children render again after `innerHTML` is removed. On correctness, which weighs most, Patch B addresses the actual problem and Patch A does not. Patch B carries some risk from moving vnodes during patch, but its block/component test reduces that concern.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
7m 25s9 turns
Wall-clock time in the container
Full test suite
No new failures
3,648 passed · 6 skipped · 3,654 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
122vs 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..667318534 100644@@ -872,10 +872,19 @@ 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 wiped by innerHTML / textContent; re-attach+ // it so the children diff below runs against live DOM+ if (n1.shapeFlag & ShapeFlags.ARRAY_CHILDREN) {+ for (const child of n1.children as VNode[]) {+ move(child, el, null, MoveType.REORDER)+ }+ } else if (n1.shapeFlag & ShapeFlags.TEXT_CHILDREN) {+ hostSetElementText(el, n1.children as string)+ } } if (dynamicChildren) {packages/runtime-dom/__tests__/patchProps.spec.tsindex 304d9f9c1..436f02868 100644@@ -1,12 +1,20 @@ import { patchProp } from '../src/patchProp' import {+ type VNode,+ createElementBlock,+ createElementVNode,+ createVNode, h, nextTick,+ onUnmounted,+ openBlock, ref, render,+ toDisplayString, vModelCheckbox, withDirectives, } from '../src'+import { PatchFlags } from '@vue/shared' describe('runtime-dom: props patching', () => { test('basic', () => {@@ -165,6 +173,107 @@ describe('runtime-dom: props patching', () => { expect(root.innerHTML).toBe(`<div></div>`) }) + test('mount innerHTML w/ undefined value and text children', () => {+ const root = document.createElement('div')+ expect(() =>+ render(h('div', { innerHTML: undefined }, 'a'), root),+ ).not.toThrow()+ expect(root.innerHTML).toBe(`<div>a</div>`)+ })++ test('mount innerHTML w/ undefined value and array children', () => {+ const root = document.createElement('div')+ expect(() =>+ render(h('div', { innerHTML: undefined }, [h('span', 'a')]), root),+ ).not.toThrow()+ expect(root.innerHTML).toBe(`<div><span>a</span></div>`)+ })++ test('toggle innerHTML between undefined and a value w/ text children', () => {+ const root = document.createElement('div')+ render(h('div', { innerHTML: undefined }, 'a'), root)+ expect(root.innerHTML).toBe(`<div>a</div>`)++ // innerHTML provided -> innerHTML wins over children+ render(h('div', { innerHTML: '<b>x</b>' }, 'a'), root)+ expect(root.innerHTML).toBe(`<div><b>x</b></div>`)++ // innerHTML removed -> children content is rendered again+ render(h('div', { innerHTML: undefined }, 'a'), root)+ expect(root.innerHTML).toBe(`<div>a</div>`)++ render(h('div', { innerHTML: '<b>x</b>' }, 'a'), root)+ expect(root.innerHTML).toBe(`<div><b>x</b></div>`)++ render(h('div', { innerHTML: null }, 'a'), root)+ expect(root.innerHTML).toBe(`<div>a</div>`)+ })++ test('toggle innerHTML between undefined and a value w/ array children', () => {+ const root = document.createElement('div')+ render(h('div', { innerHTML: undefined }, [h('span', 'a')]), root)+ expect(root.innerHTML).toBe(`<div><span>a</span></div>`)++ // innerHTML provided -> innerHTML wins over children+ render(h('div', { innerHTML: '<b>x</b>' }, [h('span', 'a')]), root)+ expect(root.innerHTML).toBe(`<div><b>x</b></div>`)++ // innerHTML removed -> children content is rendered again+ render(h('div', { innerHTML: undefined }, [h('span', 'a')]), root)+ expect(root.innerHTML).toBe(`<div><span>a</span></div>`)++ render(h('div', { innerHTML: '<b>x</b>' }, [h('span', 'a')]), root)+ expect(root.innerHTML).toBe(`<div><b>x</b></div>`)++ render(h('div', { innerHTML: null }, [h('span', 'a')]), root)+ expect(root.innerHTML).toBe(`<div><span>a</span></div>`)+ })++ test('toggle innerHTML to undefined w/ optimized children inside a block', () => {+ const unmounted = vi.fn()+ const Comp = {+ props: ['x'],+ setup(props: any) {+ onUnmounted(unmounted)+ return () => h('i', props.x)+ },+ }+ const root = document.createElement('div')+ const renderApp = (html: string | undefined, a: string): VNode => (+ openBlock(),+ createElementBlock('div', null, [+ createElementVNode(+ 'div',+ { innerHTML: html },+ [+ createElementVNode(+ 'span',+ null,+ toDisplayString(a),+ PatchFlags.TEXT,+ ),+ createVNode(Comp, { x: a }, null, PatchFlags.PROPS, ['x']),+ ],+ PatchFlags.PROPS,+ ['innerHTML'],+ ),+ ])+ )++ render(renderApp(undefined, 'a'), root)+ expect(root.innerHTML).toBe(`<div><div><span>a</span><i>a</i></div></div>`)++ render(renderApp('<b>x</b>', 'a'), root)+ expect(root.innerHTML).toBe(`<div><div><b>x</b></div></div>`)++ render(renderApp(undefined, 'b'), root)+ expect(root.innerHTML).toBe(`<div><div><span>b</span><i>b</i></div></div>`)++ render(renderApp(undefined, 'c'), root)+ expect(root.innerHTML).toBe(`<div><div><span>c</span><i>c</i></div></div>`)+ expect(unmounted).not.toHaveBeenCalled()+ })+ test('textContent unmount prev children', () => { const fn = vi.fn() const comp = {
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 () => {
What the agent reported
I fixed the bug, but the crash from the report no longer happens on this branch: mounting h('div', { innerHTML: undefined }, 'a') already renders <div>a</div> without an error. The part of the request that was still broken was switching dynamically. If innerHTML went from a value back to undefined/null while the children stayed the same, the element was left empty (<div></div>) instead of showing the children. The full suite passes with the fix (3654 tests, 0 failures, 6 skipped).
What was wrong: Setting innerHTML wipes the element's existing child DOM. When it was later unset, patchElement in packages/runtime-core/src/renderer.ts cleared the element and then compared old and new children. They were identical, so nothing was put back.
What I changed (renderer.ts, in the existing #9135 block of patchElement):
- After clearing the element, the old children's DOM is put back in order: element and component children through
move(child, el, null, MoveType.REORDER), text children by setting the text again. The normal children comparison then runs against live DOM. - The condition is now
oldProps.innerHTML != null(and the same fortextContent) instead of a truthiness check. An old value of''also wipes the children, so it has to trigger the same restore.
My first version unmounted the old children and mounted new ones. The review found that this breaks compiled templates: a child component's live instance would be torn down while the template's update tracking still pointed at it, so later updates would never reach the page. I replaced that version with the one above.
Tests (new, in packages/runtime-dom/__tests__/patchProps.spec.ts; no existing tests changed):
- Mount with
innerHTML: undefinedand text children →<div>a</div>, no error. - The same with array children →
<div><span>a</span></div>. - Toggle
undefined→'<b>x</b>'→undefined→'<b>x</b>'→nullwith text children. - The same toggle with array children.
- The compiled-template case: an
innerHTMLtoggle inside a block, with a child component, then a second update to check the page still changes and the component is never unmounted.
Tests 3 and 4 failed before the fix (expected '<div></div>' to be '<div>a</div>'). Test 5 failed against my first version. All of them pass now. Tests 1 and 2 already passed before the fix; I kept them because they are the exact scenario from the report and guard against the old crash returning.
./bt-test refused file arguments (exit 64), so every run used the full suite.
Requirements:
h("div", { innerHTML: undefined }, "a")mounts without error: already true before my change (tests 1 and 2 passed beforehand); the__asyncLoadererror never appeared in any run.- Use
innerHTMLwhen it is provided: covered by tests 3–5 (<div><b>x</b></div>). - Otherwise show the children: this was broken when switching back from a value. Fixed, and covered by tests 3–5 for
undefinedandnull, with text children, array children, and the compiled case.
The changes are uncommitted in the working tree, and no scratch files were left behind.