Skip to content

Commit

Permalink
fix(runtime-dom): remove attrs with nullish values
Browse files Browse the repository at this point in the history
fix #1576
  • Loading branch information
yyx990803 committed Jul 14, 2020
1 parent 00ab9e2 commit cb6a091
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/runtime-dom/__tests__/patchProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
1 change: 1 addition & 0 deletions packages/runtime-dom/src/modules/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit cb6a091

Please sign in to comment.