Backtestsvuerun-20261005-155915#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 render functions to accept `innerHTML` and children together without crashing: use `innerHTML` when it is provided, otherwise render the children. The crash comes from the renderer itself. It mounts children into an element whose content `innerHTML`/`textContent` then replaces. On the next patch, those vnode children point to DOM nodes that no longer exist (or are no longer in the element). The patch fails or corrupts the DOM, which matches the `__asyncLoader` TypeError in the report. **Patch A** changes no runtime behavior. It only adds a dev-mode warning when a non-nullish `innerHTML`/`textContent` is combined with non-empty children. - It does not fix the stale-children patching problem, and it does not toggle content dynamically as the reporter wants. - In the issue's exact repro (`innerHTML: undefined`), Patch A deliberately emits no warning and changes nothing. - Its tests only check that warnings fire. None of them would catch the crash or the toggle behavior, so they show nothing about the reported bug. **Patch B** fixes the root cause in the renderer: - `mountElement` skips children when a content override is present. - `patchElement` handles every transition: override→children mounts fresh, children→override unmounts the rendered children (lifecycle hooks fire), and override→override leaves the children untouched. It also clears the TEXT patch flag. - `unmount` skips children that were never rendered. - It uses `!= null` consistently, so empty strings and `0` count as overrides. The same rule is applied in hydration and in the SSR renderer, keeping client and server consistent. - It handles a subtle optimized-mode case: compiled non-block elements whose dynamic descendants are tracked by the parent block keep their children mounted. A dedicated test covers this. Patch B's tests exercise exactly the reporter's scenario (toggling `innerHTML` between undefined and a value while children change). They cover text and array children, component mount/unmount hooks, keyed reordering while overridden, hydration, and SSR edge values. These tests would fail on the old code. Patch B is larger and touches core renderer paths, which adds some risk. Even so, it is the only patch that actually resolves the reported behavior, so B is clearly better.
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
18m 25s
Wall-clock time in the container
Full test suite
No new failures
3,651 passed · 6 skipped · 3,657 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
287vs 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/__tests__/hydration.spec.tsindex 537ba236c..c66a16318 100644@@ -2043,6 +2043,35 @@ describe('SSR hydration', () => { expect(vnode.el).toBe(container.firstChild) }) + test('element with innerHTML / textContent and children', async () => {+ const html = ref<string | undefined>('<b>x</b>')+ const text = ref<string | undefined>('<i>y</i>')+ const App = {+ render: () =>+ h('div', [+ h('p', { innerHTML: html.value }, 'a'),+ h('p', { textContent: text.value }, [h('span', 'b')]),+ ]),+ }+ const ssrHtml = await renderToString(createSSRApp(App))+ expect(ssrHtml).toBe(+ `<div><p><b>x</b></p><p><i>y</i></p></div>`,+ )+ const { container } = mountWithHydration(ssrHtml, App.render)+ expect(container.innerHTML).toBe(ssrHtml)+ expect(`Hydration`).not.toHaveBeenWarned()++ html.value = undefined+ text.value = undefined+ await nextTick()+ expect(container.innerHTML).toBe(`<div><p>a</p><p><span>b</span></p></div>`)++ html.value = '<b>z</b>'+ text.value = 'w'+ await nextTick()+ expect(container.innerHTML).toBe(`<div><p><b>z</b></p><p>w</p></div>`)+ })+ // #10607 test('update component stable slot (prod + optimized mode)', async () => { __DEV__ = falsepackages/runtime-core/src/hydration.tsindex aaf41064f..c6d731701 100644@@ -32,7 +32,11 @@ import { normalizeStyle, stringifyStyle, } from '@vue/shared'-import { type RendererInternals, needTransition } from './renderer'+import {+ type RendererInternals,+ hasContentOverride,+ needTransition,+} from './renderer' import { setRef } from './rendererTemplateRef' import { type SuspenseBoundary,@@ -429,11 +433,9 @@ export function createHydrationFunctions( } // children- if (- shapeFlag & ShapeFlags.ARRAY_CHILDREN &&+ if (hasContentOverride(props)) { // skip if element has innerHTML / textContent- !(props && (props.innerHTML || props.textContent))- ) {+ } else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) { let next = hydrateChildren( el.firstChild, vnode,packages/runtime-core/src/renderer.tsindex c0fab7373..decbec0e3 100644@@ -674,7 +674,10 @@ function baseCreateRenderer( // mount children first, since some props may rely on child content // being already rendered, e.g. `<select value>`- if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {+ // children are skipped if innerHTML / textContent take over the content+ if (skipsChildren(vnode, optimized)) {+ // noop+ } else if (shapeFlag & ShapeFlags.TEXT_CHILDREN) { hostSetElementText(el, vnode.children as string) } else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) { mountChildren(@@ -855,6 +858,10 @@ function baseCreateRenderer( } parentComponent && toggleRecurse(parentComponent, true) + // children are not rendered while innerHTML / textContent is set+ const oldSkipsChildren = skipsChildren(n1, optimized)+ const newSkipsChildren = skipsChildren(n2, optimized)+ if ( // HMR updated, force full diff (__DEV__ && isHmrUpdating) ||@@ -871,14 +878,43 @@ 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)- ) {+ if (hasContentOverride(oldProps) && !hasContentOverride(newProps)) { hostSetElementText(el, '') } - if (dynamicChildren) {+ if (oldSkipsChildren || newSkipsChildren) {+ if (!oldSkipsChildren) {+ // content is being taken over: unmount the rendered children+ if (n1.shapeFlag & ShapeFlags.ARRAY_CHILDREN) {+ unmountChildren(+ n1.children as VNode[],+ parentComponent,+ parentSuspense,+ true,+ )+ } else if (n1.shapeFlag & ShapeFlags.TEXT_CHILDREN) {+ hostSetElementText(el, '')+ }+ } else if (!newSkipsChildren) {+ // old children were never rendered, so mount the new ones fresh+ if (n2.shapeFlag & ShapeFlags.TEXT_CHILDREN) {+ hostSetElementText(el, n2.children as string)+ } else if (n2.shapeFlag & ShapeFlags.ARRAY_CHILDREN) {+ mountChildren(+ n2.children as VNodeArrayChildren,+ el,+ null,+ parentComponent,+ parentSuspense,+ resolveChildrenNamespace(n2, namespace),+ slotScopeIds,+ optimized,+ )+ }+ }+ // children have been handled above+ if (patchFlag > 0) patchFlag &= ~PatchFlags.TEXT+ } else if (dynamicChildren) { patchBlockChildren( n1.dynamicChildren!, dynamicChildren,@@ -2210,6 +2246,11 @@ function baseCreateRenderer( internals, doRemove, )+ } else if (+ shapeFlag & ShapeFlags.ELEMENT &&+ skipsChildren(vnode, optimized)+ ) {+ // children were never rendered (innerHTML / textContent), skip } else if ( dynamicChildren && // #5154@@ -2471,6 +2512,30 @@ function baseCreateRenderer( } } +/**+ * Whether the element's content is provided by `innerHTML` / `textContent`+ * props, in which case its vnode children are not rendered.+ * @internal+ */+export function hasContentOverride(props: Data | null): boolean {+ return !!props && (props.innerHTML != null || props.textContent != null)+}++/**+ * Whether the element's children are skipped in favor of innerHTML /+ * textContent. Array children of a compiled non-block element are excluded,+ * since their dynamic descendants are tracked by (and patched through) the+ * parent block and therefore must stay mounted.+ */+function skipsChildren(vnode: VNode, optimized: boolean): boolean {+ return (+ hasContentOverride(vnode.props) &&+ (!optimized ||+ !!vnode.dynamicChildren ||+ !(vnode.shapeFlag & ShapeFlags.ARRAY_CHILDREN))+ )+}+ function resolveChildrenNamespace( { type, props }: VNode, currentNamespace: ElementNamespace,packages/runtime-dom/__tests__/patchProps.spec.tsindex 304d9f9c1..e8f0bea70 100644@@ -1,7 +1,10 @@ import { patchProp } from '../src/patchProp' import {+ createElementBlock,+ createElementVNode, h, nextTick,+ openBlock, ref, render, vModelCheckbox,@@ -180,6 +183,154 @@ describe('runtime-dom: props patching', () => { expect(fn).toHaveBeenCalled() }) + test('innerHTML / textContent w/ text children', async () => {+ const root = document.createElement('div')+ const html = ref<string | undefined>(undefined)+ const text = ref('a')+ const Comp = {+ render: () => h('div', { innerHTML: html.value }, text.value),+ }+ render(h(Comp), root)+ expect(root.innerHTML).toBe(`<div>a</div>`)++ html.value = '<b>x</b>'+ await nextTick()+ expect(root.innerHTML).toBe(`<div><b>x</b></div>`)++ // children changes must not clobber innerHTML+ text.value = 'b'+ await nextTick()+ expect(root.innerHTML).toBe(`<div><b>x</b></div>`)++ html.value = undefined+ await nextTick()+ expect(root.innerHTML).toBe(`<div>b</div>`)++ render(null, root)+ expect(root.innerHTML).toBe(``)+ })++ test('innerHTML w/ array children', async () => {+ const mounted = vi.fn()+ const unmounted = vi.fn()+ const Child = {+ render: () => h('span', 'child'),+ mounted,+ unmounted,+ }+ const root = document.createElement('div')+ const html = ref<string | undefined>('<i>x</i>')+ const list = ref([1, 2])+ const Comp = {+ render: () =>+ h('div', { innerHTML: html.value }, [+ ...list.value.map(i => h('p', { key: i }, i)),+ h(Child),+ ]),+ }+ render(h(Comp), root)+ expect(root.innerHTML).toBe(`<div><i>x</i></div>`)+ expect(mounted).not.toHaveBeenCalled()++ // children changes (incl. insertions) must not clobber innerHTML+ list.value = [3, 1, 2]+ await nextTick()+ expect(root.innerHTML).toBe(`<div><i>x</i></div>`)+ expect(mounted).not.toHaveBeenCalled()++ html.value = undefined+ await nextTick()+ expect(root.innerHTML).toBe(+ `<div><p>3</p><p>1</p><p>2</p><span>child</span></div>`,+ )+ expect(mounted).toHaveBeenCalledTimes(1)++ list.value = [2, 3]+ await nextTick()+ expect(root.innerHTML).toBe(`<div><p>2</p><p>3</p><span>child</span></div>`)++ html.value = '<i>y</i>'+ await nextTick()+ expect(root.innerHTML).toBe(`<div><i>y</i></div>`)+ expect(unmounted).toHaveBeenCalledTimes(1)++ html.value = undefined+ await nextTick()+ expect(root.innerHTML).toBe(`<div><p>2</p><p>3</p><span>child</span></div>`)+ expect(mounted).toHaveBeenCalledTimes(2)++ render(null, root)+ expect(root.innerHTML).toBe(``)+ expect(unmounted).toHaveBeenCalledTimes(2)+ })++ test('textContent w/ array children', async () => {+ const mounted = vi.fn()+ const unmounted = vi.fn()+ const Child = {+ render: () => h('span', 'child'),+ mounted,+ unmounted,+ }+ const root = document.createElement('div')+ const content = ref<string | undefined>('<i>x</i>')+ const Comp = {+ render: () => h('div', { textContent: content.value }, [h(Child)]),+ }+ render(h(Comp), root)+ expect(root.innerHTML).toBe(`<div><i>x</i></div>`)+ expect(mounted).not.toHaveBeenCalled()++ content.value = undefined+ await nextTick()+ expect(root.innerHTML).toBe(`<div><span>child</span></div>`)+ expect(mounted).toHaveBeenCalledTimes(1)++ content.value = 'y'+ await nextTick()+ expect(root.innerHTML).toBe(`<div>y</div>`)+ expect(unmounted).toHaveBeenCalledTimes(1)++ // unmounting an element whose children were never rendered+ render(null, root)+ expect(root.innerHTML).toBe(``)+ expect(unmounted).toHaveBeenCalledTimes(1)+ })++ test('innerHTML w/ dynamic children tracked by parent block', async () => {+ // compiled from `<div><div :innerHTML="html"><span>{{ msg }}</span></div></div>`+ const root = document.createElement('div')+ const html = ref<string | undefined>('<b>html</b>')+ const msg = ref('a')+ const Comp = {+ render: () => (+ openBlock(),+ createElementBlock('div', null, [+ createElementVNode(+ 'div',+ { innerHTML: html.value },+ [createElementVNode('span', null, msg.value, 1 /* TEXT */)],+ 8 /* PROPS */,+ ['innerHTML'],+ ),+ ])+ ),+ }+ render(h(Comp), root)+ expect(root.innerHTML).toBe(`<div><div><b>html</b></div></div>`)++ msg.value = 'b'+ await nextTick()+ expect(root.innerHTML).toBe(`<div><div><b>html</b></div></div>`)++ html.value = '<i>other</i>'+ await nextTick()+ expect(root.innerHTML).toBe(`<div><div><i>other</i></div></div>`)++ render(null, root)+ expect(root.innerHTML).toBe(``)+ })+ // #1049 test('set value as-is for non string-value props', () => { const el = document.createElement('video')packages/server-renderer/__tests__/render.spec.tsindex f537a6219..3042b7d8a 100644@@ -680,6 +680,18 @@ function testRender(type: string, render: typeof renderToString) { ).toBe(`<div>${escapeHtml(`<span>hello</span>`)}</div>`) }) + test('empty innerHTML / textContent override children', async () => {+ expect(+ await render(h('div', { innerHTML: '' }, [h('span', 'ignored')])),+ ).toBe(`<div></div>`)+ expect(+ await render(h('div', { textContent: 0 }, [h('span', 'ignored')])),+ ).toBe(`<div>0</div>`)+ expect(await render(h('div', { innerHTML: undefined }, 'a'))).toBe(+ `<div>a</div>`,+ )+ })+ test('textarea value', async () => { expect( await render(packages/server-renderer/src/render.tsindex c2bde4d1e..296a8a7fd 100644@@ -350,10 +350,10 @@ function renderElementVNode( if (!isVoidTag(tag)) { let hasChildrenOverride = false if (props) {- if (props.innerHTML) {+ if (props.innerHTML != null) { hasChildrenOverride = true- push(props.innerHTML)- } else if (props.textContent) {+ push(String(props.innerHTML))+ } else if (props.textContent != null) { hasChildrenOverride = true push(escapeHtml(props.textContent)) } else if (tag === 'textarea' && props.value) {
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 () => {