Skip to content

chore(runtime-dom): set el prop with error handling (#2766) #2837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions packages/runtime-dom/src/modules/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand All @@ -40,31 +55,20 @@ export function patchDOMProp(
const type = typeof el[key]
if (value === '' && type === 'boolean') {
// e.g. <select multiple> compiles to { multiple: '' }
el[key] = true
setProp(el, key, true)
return
} else if (value == null && type === 'string') {
// e.g. <div :id="null">
el[key] = ''
setProp(el, key, '')
el.removeAttribute(key)
return
} else if (type === 'number') {
// e.g. <img :width="null">
el[key] = 0
setProp(el, key, 0)
el.removeAttribute(key)
return
}
}

// 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
)
}
}
setProp(el, key, value)
}