Backtestsvuerun-20261005-124535#12124

Issue #12124 Merged fix PR #12125 Run 5 Oct 2026 · run-20261005-124535 vuejs/core

defineCustomElement can accidentally override members of the Element prototype

Better

Our patch was judged better than the merged fix.

In this judgment, Patch A = our agent, Patch B = the merged fix.

The core complaint is that defineCustomElement replaces built-in Element members like `tagName`. Patch A fixes this; Patch B only warns about it.

Patch A:
- Correctness: it stops `Object.defineProperty` from shadowing any key in `HTMLElement.prototype`, so `tagName` stays the real tag name and methods like `click` and `focus` keep working. The prop still works through its attribute. It also adds a `patchProp` branch so a Vue parent rendering the CE can still pass these props, including object values like `title`, via `_setProp`. It applies the same guard to exposed members. This resolves the issue at its root while keeping the prop usable, which matches the reporter's first expectation: the attribute without a conflicting property.
- Tests: they assert that `tagName` and `click` stay native, that there is no own descriptor, that attribute updates still re-render, that Vue-parent object props work, and that the warnings fire. These would fail before the change.
- Risk: a breaking-behavior concern for anyone relying on property access for props named like native members (e.g. `id`, `title`). It mitigates this with a dev warning and the patchProp path.

Patch B:
- Correctness: it only adds a dev warning and still overwrites the native member. Its own test asserts `e.tagName` becomes `'foo'` and `hasAttribute` becomes an object, which locks in the broken behavior the issue complains about.
- A warning is one acceptable outcome named in the issue, but it leaves the element in a broken state (e.g. `hasAttribute` is no longer callable, which can break other libraries and DOM code).
- Tests: they mostly verify the warning text, plus an unrelated extra assertion.

On correctness, which is weighted highest, Patch A clearly wins. Its added `patchProp` complexity is justified, and its tests are more meaningful.

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

10m 5s

Wall-clock time in the container

Full test suite

No new failures

3,667 passed · 6 skipped · 3,673 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

131vs 79 merged

Changed lines, ours vs merged

Vue version

3.4.5

Link to minimal reproduction

https://stackblitz.com/edit/vitejs-vite-aob5zf?file=index.html

Steps to reproduce

I defined a Vue component with tagName as a property, and used that, together with defineCustomElement to define the hello-world webcomponent.

0 is obviously already defined in the Element prototype.

What is expected?

I expected to maybe be able to create _only_ the tag-name attribute (and no matching tagName property), or a warning that I'm accidentally overwriting important Element properties. Or a way to remap the tag-name attribute to a different property.

What is actually happening?

Vue's defineCustomElement happily overwrites the tagName property of the Element prototype without warning.

I know this is user error, but I was surprised that Vue let me.

System Info

_No response_

Any additional comments?

In Lit, you can be explicit about what property/attribute something maps to, that would be super cool here too:

@property({ attribute: 'tag-name' }) nonConfictingPropertyName;

We managed to put us in a situation where the attribute (which is not problematic) is causing issues because Vue generated an override for the tagName property (which is problematic), while we don't use that often.

The two patches

Our patch+130 −1
packages/runtime-dom/__tests__/customElement.spec.tsindex 27c7121c2..640441e3f 100644@@ -188,6 +188,76 @@ describe('defineCustomElement', () => {       )     }) +    test('props should not override native members', async () => {+      const E = defineCustomElement({+        props: {+          tagName: String,+          click: String,+        },+        render() {+          return h('div', null, `${this.tagName} ${this.click}`)+        },+      })+      customElements.define('my-el-native-props', E)+      container.innerHTML = `<my-el-native-props tag-name="foo" click="bar"></my-el-native-props>`+      const e = container.childNodes[0] as VueElement & any+      // native members are untouched+      expect(e.tagName).toBe('MY-EL-NATIVE-PROPS')+      expect(typeof e.click).toBe('function')+      expect(Object.getOwnPropertyDescriptor(e, 'tagName')).toBeUndefined()+      // props still work via attributes+      expect(e.shadowRoot!.innerHTML).toBe('<div>foo bar</div>')+      e.setAttribute('tag-name', 'changed')+      await nextTick()+      expect(e.shadowRoot!.innerHTML).toBe('<div>changed bar</div>')+      expect(+        `Prop "tagName" conflicts with a native property of the custom element`,+      ).toHaveBeenWarned()+      expect(+        `Prop "click" conflicts with a native property of the custom element`,+      ).toHaveBeenWarned()+    })++    test('props conflicting with native members passed from vue parent', async () => {+      const E = defineCustomElement({+        props: {+          tagName: String,+          title: Object,+        },+        render() {+          return h('div', null, `${this.tagName} ${this.title?.a}`)+        },+      })+      customElements.define('my-el-native-props-parent', E)+      render(+        h('my-el-native-props-parent', {+          'tag-name': 'foo',+          title: { a: 1 },+        }),+        container,+      )+      const e = container.childNodes[0] as VueElement & any+      expect(e.tagName).toBe('MY-EL-NATIVE-PROPS-PARENT')+      expect(e.shadowRoot!.innerHTML).toBe('<div>foo 1</div>')+      render(+        h('my-el-native-props-parent', {+          'tag-name': 'changed',+          title: { a: 2 },+        }),+        container,+      )+      await nextTick()+      expect(e.tagName).toBe('MY-EL-NATIVE-PROPS-PARENT')+      expect(e.shadowRoot!.innerHTML).toBe('<div>changed 2</div>')+      expect(`Failed setting prop`).not.toHaveBeenWarned()+      expect(+        `Prop "tagName" conflicts with a native property of the custom element`,+      ).toHaveBeenWarned()+      expect(+        `Prop "title" conflicts with a native property of the custom element`,+      ).toHaveBeenWarned()+    })+     test('props via properties', async () => {       const e = new E()       e.foo = 'one'@@ -2124,6 +2194,30 @@ describe('defineCustomElement', () => {         `[Vue warn]: Exposed property "value" already exists on custom element.`,       ).toHaveBeenWarned()     })++    test('expose should not override native members', () => {+      const E = defineCustomElement({+        setup(_, { expose }) {+          expose({+            tagName: 'foo',+            focus: 'bar',+          })+          return () => h('div', 'hi')+        },+      })+      customElements.define('my-el-expose-native', E)++      container.innerHTML = `<my-el-expose-native></my-el-expose-native>`+      const e = container.childNodes[0] as VueElement & any+      expect(e.tagName).toBe('MY-EL-EXPOSE-NATIVE')+      expect(typeof e.focus).toBe('function')+      expect(+        `[Vue warn]: Exposed property "tagName" already exists on custom element.`,+      ).toHaveBeenWarned()+      expect(+        `[Vue warn]: Exposed property "focus" already exists on custom element.`,+      ).toHaveBeenWarned()+    })   })    test('async & nested custom elements', async () => {packages/runtime-dom/src/apiCustomElement.tsindex 9422f58d6..2b9624a6b 100644@@ -42,6 +42,7 @@ import {   toNumber, } from '@vue/shared' import { createApp, createSSRApp, render } from '.'+import { isNativeElementMember } from './patchProp'  // marker for attr removal const REMOVAL = {}@@ -454,7 +455,7 @@ export class VueElement     const exposed = this._instance && this._instance.exposed     if (!exposed) return     for (const key in exposed) {-      if (!hasOwn(this, key)) {+      if (!hasOwn(this, key) && !isNativeElementMember(key)) {         // exposed properties are readonly         Object.defineProperty(this, key, {           // unwrap ref to be consistent with public instance behavior@@ -479,6 +480,18 @@ export class VueElement      // defining getter/setters on prototype     for (const key of declaredPropKeys.map(camelize)) {+      // never shadow built-in Element members (e.g. `tagName`), the prop is+      // still settable via its attribute+      if (isNativeElementMember(key)) {+        if (__DEV__) {+          warn(+            `Prop "${key}" conflicts with a native property of the custom ` ++              `element and will not be exposed as a property on it. ` ++              `It can still be set via the "${hyphenate(key)}" attribute.`,+          )+        }+        continue+      }       Object.defineProperty(this, key, {         get(this: VueElement) {           return this._getProp(key)packages/runtime-dom/src/patchProp.tsindex 095a89403..6b3a48f34 100644@@ -20,6 +20,14 @@ const isNativeOn = (key: string) =>   key.charCodeAt(2) > 96 &&   key.charCodeAt(2) < 123 +/**+ * whether the key is a built-in member of HTMLElement (e.g. `tagName`), which+ * custom elements must never shadow+ * @internal+ */+export const isNativeElementMember = (key: string): boolean =>+  typeof HTMLElement !== 'undefined' && key in HTMLElement.prototype+ type DOMRendererOptions = RendererOptions<Node, Element>  export const patchProp: DOMRendererOptions['patchProp'] = (@@ -40,6 +48,20 @@ export const patchProp: DOMRendererOptions['patchProp'] = (     if (!isModelListener(key)) {       patchEvent(el, key, prevValue, nextValue, parentComponent)     }+  } else if (+    // declared props conflicting with native element members are not defined+    // as properties on the custom element, set them on the CE directly+    (el as VueElement)._isVueCE &&+    isNativeElementMember(camelize(key)) &&+    shouldSetAsPropForVueCE(el as VueElement, key)+  ) {+    ;(el as VueElement)._setProp(+      camelize(key),+      nextValue,+      true,+      // @ts-expect-error _patching is private+      !(el as VueElement)._patching,+    )   } else if (     key[0] === '.'       ? ((key = key.slice(1)), true) 
Merged fix · PR #12125+78 −1
packages/runtime-dom/__tests__/customElement.spec.tsindex 27c7121c2..8763625bd 100644@@ -237,6 +237,7 @@ describe('defineCustomElement', () => {       await nextTick()       expect(e.shadowRoot!.innerHTML).toBe('<div>foo2</div><div>bar2</div>')       expect(e.getAttribute('foo')).toBe('foo2')+      expect(e.foo).toBe('foo2')       expect(e.hasAttribute('bar')).toBe(false)        // change prop then attr@@ -2489,6 +2490,76 @@ describe('defineCustomElement', () => {     expect(e.shadowRoot!.innerHTML).toBe(`false,boolean`)   }) +  test('prop name conflicts with native property', () => {+    const E = defineCustomElement({+      props: {+        tagName: {+          type: String,+        },+      },+      render() {+        return this.tagName+      },+    })+    customElements.define('el-attr-tag-name', E)+    container.innerHTML = '<el-attr-tag-name tag-name="foo">'+    const e = container.childNodes[0] as VueElement+    expect(e.shadowRoot!.innerHTML).toBe(`foo`)+    expect(e.tagName).toBe('foo')+    expect(+      `[Vue warn]: Custom element prop "tagName" conflicts with an existing property on the element and will overwrite it.`,+    ).toHaveBeenWarned()+  })++  test('prop name conflicts with reflected native property', async () => {+    const E = defineCustomElement({+      props: {+        id: String,+      },+      render() {+        return this.id+      },+    })+    customElements.define('el-attr-id', E)+    container.innerHTML = '<el-attr-id id="foo">'+    const e = container.childNodes[0] as VueElement++    e.setAttribute('id', 'bar')+    await nextTick()+    expect(e.shadowRoot!.innerHTML).toBe(`bar`)+    expect(e.id).toBe('bar')+    expect(+      `[Vue warn]: Custom element prop "id" conflicts with an existing property on the element and will overwrite it.`,+    ).toHaveBeenWarned()+  })++  test('native method conflict when rendered by Vue', () => {+    const value = { text: 'foo' }+    const E = defineCustomElement({+      props: {+        hasAttribute: Object,+      },+      render() {+        return this.hasAttribute!.text+      },+    })+    customElements.define('el-vue-has-attribute', E)++    render(+      h('el-vue-has-attribute', {+        'has-attribute': value,+      }),+      container,+    )++    const e = container.firstChild as VueElement+    expect(e.shadowRoot!.innerHTML).toBe('foo')+    expect((e as any).hasAttribute).toBe(value)+    expect(+      `[Vue warn]: Custom element prop "hasAttribute" conflicts with an existing property on the element and will overwrite it.`,+    ).toHaveBeenWarned()+  })+   test('hyphenated attr removal', async () => {     const E = defineCustomElement({       props: {packages/runtime-dom/src/apiCustomElement.tsindex 9422f58d6..ede68a516 100644@@ -477,8 +477,14 @@ export class VueElement       }     } -    // defining getter/setters on prototype+    // define getter/setters for declared props     for (const key of declaredPropKeys.map(camelize)) {+      if (__DEV__ && key in Object.getPrototypeOf(this)) {+        warn(+          `Custom element prop "${key}" conflicts with an existing property ` ++            `on the element and will overwrite it.`,+        )+      }       Object.defineProperty(this, key, {         get(this: VueElement) {           return this._getProp(key)