Skip to content

Commit 20bc7ba

Browse files
committed
fix(runtime-dom): should not coerce nullish values to empty strings for non-string dom props
fix #1049 close #1092, close #1093, close #1094
1 parent 68e1ce8 commit 20bc7ba

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ describe('runtime-dom: props patching', () => {
66
const el = document.createElement('div')
77
patchProp(el, 'id', null, 'foo')
88
expect(el.id).toBe('foo')
9+
// prop with string value should be set to empty string on null values
910
patchProp(el, 'id', null, null)
1011
expect(el.id).toBe('')
1112
})
@@ -75,4 +76,20 @@ describe('runtime-dom: props patching', () => {
7576
expect(root.innerHTML).toBe(`<div>bar</div>`)
7677
expect(fn).toHaveBeenCalled()
7778
})
79+
80+
// #1049
81+
test('set value as-is for non string-value props', () => {
82+
const el = document.createElement('video')
83+
// jsdom doesn't really support video playback. srcObject in a real browser
84+
// should default to `null`, but in jsdom it's `undefined`.
85+
// anyway, here we just want to make sure Vue doesn't set non-string props
86+
// to an empty string on nullish values - it should reset to its default
87+
// value.
88+
const intiialValue = el.srcObject
89+
const fakeObject = {}
90+
patchProp(el, 'srcObject', null, fakeObject)
91+
expect(el.srcObject).not.toBe(fakeObject)
92+
patchProp(el, 'srcObject', null, null)
93+
expect(el.srcObject).toBe(intiialValue)
94+
})
7895
})

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ export function patchDOMProp(
3131
if (value === '' && typeof el[key] === 'boolean') {
3232
// e.g. <select multiple> compiles to { multiple: '' }
3333
el[key] = true
34+
} else if (value == null && typeof el[key] === 'string') {
35+
// e.g. <div :id="null">
36+
el[key] = ''
3437
} else {
35-
el[key] = value == null ? '' : value
38+
el[key] = value
3639
}
3740
}

0 commit comments

Comments
 (0)