Skip to content

Commit 73a5633

Browse files
author
Jason Yu
committed
fix(runtime-core): allow setting props to object if prop is not expecting string
1 parent a5bb1d0 commit 73a5633

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,37 @@ describe('runtime-dom: props patching', () => {
7575
expect(root.innerHTML).toBe(`<div>bar</div>`)
7676
expect(fn).toHaveBeenCalled()
7777
})
78+
79+
// #1049
80+
test('set domProps where string is not accepted', () => {
81+
const realCreateElement = document.createElement.bind(document)
82+
const spyCreateElement = jest
83+
.spyOn(document, 'createElement')
84+
.mockImplementation(tagName => {
85+
const el = realCreateElement(tagName)
86+
let srcObject: any = undefined
87+
Object.defineProperty(el, 'srcObject', {
88+
enumerable: true,
89+
set(v) {
90+
if (typeof v === 'string') {
91+
throw new TypeError(
92+
`Failed to set the 'srcObject' property on 'HTMLMediaElement'`
93+
)
94+
}
95+
srcObject = v
96+
},
97+
get() {
98+
return srcObject
99+
}
100+
})
101+
return el
102+
})
103+
104+
const el = document.createElement('video')
105+
106+
patchProp(el, 'srcObject', undefined, null)
107+
108+
expect(el.srcObject).toBeNull()
109+
spyCreateElement.mockRestore()
110+
})
78111
})

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,21 @@ export function patchDOMProp(
3131
if (value === '' && typeof el[key] === 'boolean') {
3232
// e.g. <select multiple> compiles to { multiple: '' }
3333
el[key] = true
34-
} else {
34+
} else if (isStringAttribute(el.tagName, key)) {
3535
el[key] = value == null ? '' : value
36+
} else {
37+
el[key] = value
38+
}
39+
}
40+
41+
function isStringAttribute(tagName: any, key: string) {
42+
try {
43+
// some dom properties accept '' but not other strings, e.g. <video>.volume
44+
document.createElement(tagName)[key] = 'test string'
45+
return true
46+
} catch (e) {
47+
if (e.name !== 'TypeError') throw e
48+
49+
return false
3650
}
3751
}

0 commit comments

Comments
 (0)