Skip to content

Commit cb6a091

Browse files
committed
fix(runtime-dom): remove attrs with nullish values
fix #1576
1 parent 00ab9e2 commit cb6a091

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

packages/runtime-dom/__tests__/patchProps.spec.ts

+17
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,21 @@ describe('runtime-dom: props patching', () => {
107107

108108
expect(`Failed setting prop "someProp" on <div>`).toHaveBeenWarnedLast()
109109
})
110+
111+
// #1576
112+
test('remove attribute when value is falsy', () => {
113+
const el = document.createElement('div')
114+
patchProp(el, 'id', null, '')
115+
expect(el.hasAttribute('id')).toBe(true)
116+
patchProp(el, 'id', null, null)
117+
expect(el.hasAttribute('id')).toBe(false)
118+
119+
patchProp(el, 'id', null, '')
120+
expect(el.hasAttribute('id')).toBe(true)
121+
patchProp(el, 'id', null, undefined)
122+
expect(el.hasAttribute('id')).toBe(false)
123+
124+
patchProp(el, 'id', null, '')
125+
expect(el.hasAttribute('id')).toBe(true)
126+
})
110127
})

packages/runtime-dom/src/modules/props.ts

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export function patchDOMProp(
3737
} else if (value == null && typeof el[key] === 'string') {
3838
// e.g. <div :id="null">
3939
el[key] = ''
40+
el.removeAttribute(key)
4041
} else {
4142
// some properties perform value validation and throw
4243
try {

0 commit comments

Comments
 (0)