Backtestsvuerun-20261009-161443#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 A = our agent, Patch B = the merged fix.
Patch A changes runtime behavior at the point where a crash or corruption can happen. In `patchElement`, when `innerHTML`/`textContent` goes from set to unset, the old children's DOM nodes have already been overwritten. Patching them in place therefore operates on nodes that no longer exist, which is the kind of failure the issue reports. A unmounts the old array children, which also runs component unmount hooks, and then mounts the new text or array children fresh. It also switches the guard from a truthiness check to `!= null`, so an empty-string `innerHTML` is handled correctly. Its tests cover the exact issue scenario (`innerHTML: undefined` with text children) and several transitions: undefined to html to undefined, empty string to undefined, array children, component children with an unmount count, and `textContent`. These transition tests would plausibly fail before the change, and an SSR test checks that undefined values fall back to children. Patch B only adds a dev-mode warning when a non-nullish `innerHTML`/`textContent` is combined with children. It explicitly does not warn for nullish values, which is the issue's case, and it makes no runtime change, so the reported error and the expected behavior (use children when `innerHTML` is undefined) are not addressed. B's tests only check warnings. On correctness, the primary criterion, A clearly wins.
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
6m 49s
Wall-clock time in the container
Full test suite
No new failures
1,760 passed · 1 skipped · 1,761 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
134vs 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..454a8f5bc 100644@@ -872,13 +872,35 @@ 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, '')- }-- if (dynamicChildren) {+ // #5110 the old children's DOM was overwritten by innerHTML /+ // textContent, so they can't be patched in place - unmount them and+ // mount the new children fresh.+ if (n1.shapeFlag & ShapeFlags.ARRAY_CHILDREN) {+ unmountChildren(+ n1.children as VNode[],+ parentComponent,+ parentSuspense,+ )+ }+ 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 && !!dynamicChildren,+ )+ }+ } else if (dynamicChildren) { patchBlockChildren( n1.dynamicChildren!, dynamicChildren,packages/runtime-dom/__tests__/patchProps.spec.tsindex 304d9f9c1..7303a3cf2 100644@@ -165,6 +165,99 @@ describe('runtime-dom: props patching', () => { expect(root.innerHTML).toBe(`<div></div>`) }) + test('innerHTML w/ undefined value and children', async () => {+ const root = document.createElement('div')+ const html = ref<string | undefined>(undefined)+ const Comp = {+ render: () => h('div', { innerHTML: html.value }, 'a'),+ }+ render(h(Comp), root)+ expect(root.innerHTML).toBe(`<div>a</div>`)++ html.value = '<b>foo</b>'+ await nextTick()+ expect(root.innerHTML).toBe(`<div><b>foo</b></div>`)++ html.value = undefined+ await nextTick()+ expect(root.innerHTML).toBe(`<div>a</div>`)+ })++ test('innerHTML w/ empty string value and children', async () => {+ const root = document.createElement('div')+ const html = ref<string | undefined>('')+ const Comp = {+ render: () => h('div', { innerHTML: html.value }, 'a'),+ }+ render(h(Comp), root)+ expect(root.innerHTML).toBe(`<div></div>`)++ html.value = undefined+ await nextTick()+ expect(root.innerHTML).toBe(`<div>a</div>`)+ })++ test('innerHTML w/ undefined value and array children', async () => {+ const root = document.createElement('div')+ const html = ref<string | undefined>(undefined)+ const Comp = {+ render: () =>+ h('div', { innerHTML: html.value }, [h('span', 'a'), h('span', 'b')]),+ }+ render(h(Comp), root)+ expect(root.innerHTML).toBe(`<div><span>a</span><span>b</span></div>`)++ html.value = 'foo'+ await nextTick()+ expect(root.innerHTML).toBe(`<div>foo</div>`)++ html.value = undefined+ await nextTick()+ expect(root.innerHTML).toBe(`<div><span>a</span><span>b</span></div>`)+ })++ test('innerHTML w/ children: mount and unset w/ component children', async () => {+ const unmounted = vi.fn()+ const Child = {+ render: () => h('span', 'child'),+ unmounted,+ }+ const root = document.createElement('div')+ const html = ref<string | undefined>('foo')+ const Comp = {+ render: () => h('div', { innerHTML: html.value }, [h(Child)]),+ }+ render(h(Comp), root)+ expect(root.innerHTML).toBe(`<div>foo</div>`)++ html.value = undefined+ await nextTick()+ expect(root.innerHTML).toBe(`<div><span>child</span></div>`)+ expect(unmounted).toHaveBeenCalledTimes(1)++ render(null, root)+ expect(root.innerHTML).toBe(``)+ expect(unmounted).toHaveBeenCalledTimes(2)+ })++ test('textContent w/ undefined value and children', async () => {+ const root = document.createElement('div')+ const text = ref<string | undefined>(undefined)+ const Comp = {+ render: () => h('div', { textContent: text.value }, 'a'),+ }+ render(h(Comp), root)+ expect(root.innerHTML).toBe(`<div>a</div>`)++ text.value = 'foo'+ await nextTick()+ expect(root.innerHTML).toBe(`<div>foo</div>`)++ text.value = undefined+ await nextTick()+ expect(root.innerHTML).toBe(`<div>a</div>`)+ })+ test('textContent unmount prev children', () => { const fn = vi.fn() const comp = {packages/server-renderer/__tests__/render.spec.tsindex f537a6219..9a432a3a0 100644@@ -680,6 +680,15 @@ function testRender(type: string, render: typeof renderToString) { ).toBe(`<div>${escapeHtml(`<span>hello</span>`)}</div>`) }) + test('innerHTML / textContent w/ undefined value and children', async () => {+ expect(await render(h('div', { innerHTML: undefined }, 'a'))).toBe(+ `<div>a</div>`,+ )+ expect(+ await render(h('div', { textContent: undefined }, [h('span', 'a')])),+ ).toBe(`<div><span>a</span></div>`)+ })+ test('textarea value', 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 () => {