]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(custom-elements): fix number type props casting check
authorEvan You <yyx990803@gmail.com>
Fri, 11 Nov 2022 07:18:46 +0000 (15:18 +0800)
committerEvan You <yyx990803@gmail.com>
Fri, 11 Nov 2022 07:20:28 +0000 (15:20 +0800)
fix #5793
adapted from #5794

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

index 09ee971d225824934b7157225f589dafebd7c746..6c991b9f5230736b9fb9b47090957892e4f9268e 100644 (file)
@@ -228,6 +228,25 @@ describe('defineCustomElement', () => {
       await nextTick()
       expect(el.shadowRoot!.innerHTML).toMatchInlineSnapshot('"<div>foo</div>"')
     })
+
+    // # 5793
+    test('set number value in dom property', () => {
+      const E = defineCustomElement({
+        props: {
+          'max-age': Number
+        },
+        render() {
+          // @ts-ignore
+          return `max age: ${this.maxAge}/type: ${typeof this.maxAge}`
+        }
+      })
+      customElements.define('my-element-number-property', E)
+      const el = document.createElement('my-element-number-property') as any
+      container.appendChild(el)
+      el.maxAge = 50
+      expect(el.maxAge).toBe(50)
+      expect(el.shadowRoot.innerHTML).toBe('max age: 50/type: number')
+    })
   })
 
   describe('attrs', () => {
index ebb1eccc0bd7d59fddde3489ff482f2a9997acab..ecb558b17a4f93e9e67f422fb5df5514619c785b 100644 (file)
@@ -234,11 +234,15 @@ export class VueElement extends BaseClass {
       // cast Number-type props set before resolve
       let numberProps
       if (props && !isArray(props)) {
-        for (const key in this._props) {
+        for (const key in props) {
           const opt = props[key]
           if (opt === Number || (opt && opt.type === Number)) {
-            this._props[key] = toNumber(this._props[key])
-            ;(numberProps || (numberProps = Object.create(null)))[key] = true
+            if (key in this._props) {
+              this._props[key] = toNumber(this._props[key])
+            }
+            ;(numberProps || (numberProps = Object.create(null)))[
+              camelize(key)
+            ] = true
           }
         }
       }