fix(runtime-dom): remove attrs with nullish values

fix #1576
This commit is contained in:
Evan You 2020-07-14 16:25:21 -04:00
parent 00ab9e2e85
commit cb6a0915c5
2 changed files with 18 additions and 0 deletions

View File

@ -107,4 +107,21 @@ describe('runtime-dom: props patching', () => {
expect(`Failed setting prop "someProp" on <div>`).toHaveBeenWarnedLast()
})
// #1576
test('remove attribute when value is falsy', () => {
const el = document.createElement('div')
patchProp(el, 'id', null, '')
expect(el.hasAttribute('id')).toBe(true)
patchProp(el, 'id', null, null)
expect(el.hasAttribute('id')).toBe(false)
patchProp(el, 'id', null, '')
expect(el.hasAttribute('id')).toBe(true)
patchProp(el, 'id', null, undefined)
expect(el.hasAttribute('id')).toBe(false)
patchProp(el, 'id', null, '')
expect(el.hasAttribute('id')).toBe(true)
})
})

View File

@ -37,6 +37,7 @@ export function patchDOMProp(
} else if (value == null && typeof el[key] === 'string') {
// e.g. <div :id="null">
el[key] = ''
el.removeAttribute(key)
} else {
// some properties perform value validation and throw
try {