Skip to content

Commit

Permalink
fix(runtime-dom): attribute should be removed with nullish values (#2679
Browse files Browse the repository at this point in the history
)

fix #2677
  • Loading branch information
edison1105 authored Dec 1, 2020
1 parent 64d4681 commit fb6b9f8
Show file tree
Hide file tree
Showing 2 changed files with 21 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 @@ -120,6 +120,23 @@ describe('runtime-dom: props patching', () => {

patchProp(el, 'id', null, '')
expect(el.hasAttribute('id')).toBe(true)

// #2677
const img = document.createElement('img')
patchProp(img, 'width', null, '')
expect(el.hasAttribute('width')).toBe(false)
patchProp(img, 'width', null, 0)
expect(img.hasAttribute('width')).toBe(true)

patchProp(img, 'width', null, null)
expect(img.hasAttribute('width')).toBe(false)
patchProp(img, 'width', null, 0)
expect(img.hasAttribute('width')).toBe(true)

patchProp(img, 'width', null, undefined)
expect(img.hasAttribute('width')).toBe(false)
patchProp(img, 'width', null, 0)
expect(img.hasAttribute('width')).toBe(true)
})

test('form attribute', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/runtime-dom/src/modules/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export function patchDOMProp(
// e.g. <div :id="null">
el[key] = ''
el.removeAttribute(key)
} else if ((value == null || value === '') && typeof el[key] === 'number') {
// e.g. <img :width="null">
el[key] = 0
el.removeAttribute(key)
} else {
// some properties perform value validation and throw
try {
Expand Down

0 comments on commit fb6b9f8

Please sign in to comment.