Skip to content

Commit 98bee59

Browse files
committed
fix(runtime-core): should catch dom prop set TypeErrors
based on #1051
1 parent c5e7d8b commit 98bee59

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

packages/runtime-dom/__tests__/patchProps.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { patchProp } from '../src/patchProp'
22
import { render, h } from '../src'
3+
import { mockWarn } from '@vue/shared'
34

45
describe('runtime-dom: props patching', () => {
6+
mockWarn()
7+
58
test('basic', () => {
69
const el = document.createElement('div')
710
patchProp(el, 'id', null, 'foo')
@@ -92,4 +95,16 @@ describe('runtime-dom: props patching', () => {
9295
patchProp(el, 'srcObject', null, null)
9396
expect(el.srcObject).toBe(intiialValue)
9497
})
98+
99+
test('catch and warn prop set TypeError', () => {
100+
const el = document.createElement('div')
101+
Object.defineProperty(el, 'someProp', {
102+
set() {
103+
throw new TypeError('Invalid type')
104+
}
105+
})
106+
patchProp(el, 'someProp', null, 'foo')
107+
108+
expect(`Failed setting prop "someProp" on <div>`).toHaveBeenWarnedLast()
109+
})
95110
})

packages/runtime-dom/src/modules/props.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// __UNSAFE__
22
// Reason: potentially setting innerHTML.
33
// This can come from explicit usage of v-html or innerHTML as a prop in render
4+
5+
import { warn } from '@vue/runtime-core'
6+
47
// functions. The user is reponsible for using them with only trusted content.
58
export function patchDOMProp(
69
el: any,
@@ -35,6 +38,17 @@ export function patchDOMProp(
3538
// e.g. <div :id="null">
3639
el[key] = ''
3740
} else {
38-
el[key] = value
41+
// some properties perform value validation and throw
42+
try {
43+
el[key] = value
44+
} catch (e) {
45+
if (__DEV__) {
46+
warn(
47+
`Failed setting prop "${key}" on <${el.tagName.toLowerCase()}>: ` +
48+
`value ${value} is invalid.`,
49+
e
50+
)
51+
}
52+
}
3953
}
4054
}

0 commit comments

Comments
 (0)