]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-dom): prevent setting state as attribute for custom elements (#11165)
authorTycho <jh.leong@outlook.com>
Sat, 22 Jun 2024 08:42:12 +0000 (16:42 +0800)
committerGitHub <noreply@github.com>
Sat, 22 Jun 2024 08:42:12 +0000 (16:42 +0800)
close #11163

packages/runtime-dom/__tests__/customElement.spec.ts
packages/runtime-dom/src/patchProp.ts

index 276536c2c3149dca966aeccb59b28a60a2ca7ebc..62ba166b030fd64172c4434b987504c9084b3eb4 100644 (file)
@@ -9,6 +9,7 @@ import {
   inject,
   nextTick,
   ref,
+  render,
   renderSlot,
 } from '../src'
 
@@ -137,7 +138,7 @@ describe('defineCustomElement', () => {
 
   describe('props', () => {
     const E = defineCustomElement({
-      props: ['foo', 'bar', 'bazQux'],
+      props: ['foo', 'bar', 'bazQux', 'value'],
       render() {
         return [
           h('div', null, this.foo),
@@ -147,6 +148,12 @@ describe('defineCustomElement', () => {
     })
     customElements.define('my-el-props', E)
 
+    test('renders custom element w/ correct object prop value', () => {
+      render(h('my-el-props', { value: { x: 1 } }), container)
+      const el = container.children[0]
+      expect((el as any).value).toEqual({ x: 1 })
+    })
+
     test('props via attribute', async () => {
       // bazQux should map to `baz-qux` attribute
       container.innerHTML = `<my-el-props foo="hello" baz-qux="bye"></my-el-props>`
index 0da0ff1f272a82be08d09d5607781e47c649b1ce..e7b733c74aff93ed52acb860a86cf506b91e2d0e 100644 (file)
@@ -54,7 +54,11 @@ export const patchProp: DOMRendererOptions['patchProp'] = (
     )
     // #6007 also set form state as attributes so they work with
     // <input type="reset"> or libs / extensions that expect attributes
-    if (key === 'value' || key === 'checked' || key === 'selected') {
+    // #11163 custom elements may use value as an prop and set it as object
+    if (
+      !el.tagName.includes('-') &&
+      (key === 'value' || key === 'checked' || key === 'selected')
+    ) {
       patchAttr(el, key, nextValue, isSVG, parentComponent, key !== 'value')
     }
   } else {