From d684c52401b49a126ef9d9e7ba65163e11f4cc6a Mon Sep 17 00:00:00 2001 From: luwuer Date: Thu, 17 Dec 2020 11:38:21 +0800 Subject: [PATCH] chore(runtime-dom): set el prop with error handling (#2766) --- packages/runtime-dom/src/modules/props.ts | 36 +++++++++++++---------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/packages/runtime-dom/src/modules/props.ts b/packages/runtime-dom/src/modules/props.ts index 068701642f8..79d7a4e672d 100644 --- a/packages/runtime-dom/src/modules/props.ts +++ b/packages/runtime-dom/src/modules/props.ts @@ -4,6 +4,21 @@ import { warn } from '@vue/runtime-core' +// Set prop with error handling,throws warnings when change readonly prop. +function setProp(el: any, key: string, value: any) { + try { + el[key] = value + } catch (e) { + if (__DEV__) { + warn( + `Failed setting prop "${key}" on <${el.tagName.toLowerCase()}>: ` + + `value ${value} is invalid.`, + e + ) + } + } +} + // functions. The user is responsible for using them with only trusted content. export function patchDOMProp( el: any, @@ -21,7 +36,7 @@ export function patchDOMProp( if (prevChildren) { unmountChildren(prevChildren, parentComponent, parentSuspense) } - el[key] = value == null ? '' : value + setProp(el, key, value == null ? '' : value) return } @@ -40,31 +55,20 @@ export function patchDOMProp( const type = typeof el[key] if (value === '' && type === 'boolean') { // e.g.