]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): should catch dom prop set TypeErrors
authorEvan You <yyx990803@gmail.com>
Fri, 1 May 2020 22:47:27 +0000 (18:47 -0400)
committerEvan You <yyx990803@gmail.com>
Fri, 1 May 2020 22:47:27 +0000 (18:47 -0400)
based on #1051

packages/runtime-dom/__tests__/patchProps.spec.ts
packages/runtime-dom/src/modules/props.ts

index fc5fdde057c4803004b07504c090cdc8f835fb2c..6ffa6845ceaedb518e8e4d09583445163e2754cb 100644 (file)
@@ -1,7 +1,10 @@
 import { patchProp } from '../src/patchProp'
 import { render, h } from '../src'
+import { mockWarn } from '@vue/shared'
 
 describe('runtime-dom: props patching', () => {
+  mockWarn()
+
   test('basic', () => {
     const el = document.createElement('div')
     patchProp(el, 'id', null, 'foo')
@@ -92,4 +95,16 @@ describe('runtime-dom: props patching', () => {
     patchProp(el, 'srcObject', null, null)
     expect(el.srcObject).toBe(intiialValue)
   })
+
+  test('catch and warn prop set TypeError', () => {
+    const el = document.createElement('div')
+    Object.defineProperty(el, 'someProp', {
+      set() {
+        throw new TypeError('Invalid type')
+      }
+    })
+    patchProp(el, 'someProp', null, 'foo')
+
+    expect(`Failed setting prop "someProp" on <div>`).toHaveBeenWarnedLast()
+  })
 })
index 4a9b34c526ebdd1e7ee473866f9295138e9231fd..fed5c6c90e5a01a73aa9e6bf2bae5a1144b3c3dc 100644 (file)
@@ -1,6 +1,9 @@
 // __UNSAFE__
 // Reason: potentially setting innerHTML.
 // This can come from explicit usage of v-html or innerHTML as a prop in render
+
+import { warn } from '@vue/runtime-core'
+
 // functions. The user is reponsible for using them with only trusted content.
 export function patchDOMProp(
   el: any,
@@ -35,6 +38,17 @@ export function patchDOMProp(
     // e.g. <div :id="null">
     el[key] = ''
   } else {
-    el[key] = value
+    // some properties perform value validation and throw
+    try {
+      el[key] = value
+    } catch (e) {
+      if (__DEV__) {
+        warn(
+          `Failed setting prop "${key}" on <${el.tagName.toLowerCase()}>: ` +
+            `value ${value} is invalid.`,
+          e
+        )
+      }
+    }
   }
 }