Skip to content
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

fix(runtime-dom): use setAttribute to set DOM prop #2803

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
12 changes: 0 additions & 12 deletions packages/runtime-dom/__tests__/patchProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,6 @@ describe('runtime-dom: props patching', () => {
expect(el.srcObject).toBe(initialValue)
})

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()
})

// #1576
test('remove attribute when value is falsy', () => {
const el = document.createElement('div')
Expand Down
23 changes: 7 additions & 16 deletions packages/runtime-dom/src/modules/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// 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 responsible for using them with only trusted content.
export function patchDOMProp(
el: any,
Expand Down Expand Up @@ -36,11 +34,11 @@ export function patchDOMProp(
return
}

const type = typeof el[key]
if (value === '' || value == null) {
const type = typeof el[key]
if (value === '' && type === 'boolean') {
// e.g. <select multiple> compiles to { multiple: '' }
el[key] = true
if (type === 'boolean') {
// e.g. <select multiple> compiles to { multiple: '' } or { multiple: false }
el[key] = value != null
return
} else if (value == null && type === 'string') {
// e.g. <div :id="null">
Expand All @@ -55,16 +53,9 @@ export function patchDOMProp(
}
}

// some properties perform value validation and throw
try {
if (type === 'function') {
el[key] = value
} catch (e) {
if (__DEV__) {
warn(
`Failed setting prop "${key}" on <${el.tagName.toLowerCase()}>: ` +
`value ${value} is invalid.`,
e
)
}
} else {
el.setAttribute(key, value)
}
}