Skip to content

fix(runtime-core): allow setting props to object if prop is not expecting string #1092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/runtime-dom/__tests__/patchProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,37 @@ describe('runtime-dom: props patching', () => {
expect(root.innerHTML).toBe(`<div>bar</div>`)
expect(fn).toHaveBeenCalled()
})

// #1049
test('set domProps where string is not accepted', () => {
const realCreateElement = document.createElement.bind(document)
const spyCreateElement = jest
.spyOn(document, 'createElement')
.mockImplementation(tagName => {
const el = realCreateElement(tagName)
let srcObject: any = undefined
Object.defineProperty(el, 'srcObject', {
enumerable: true,
set(v) {
if (typeof v === 'string') {
throw new TypeError(
`Failed to set the 'srcObject' property on 'HTMLMediaElement'`
)
}
srcObject = v
},
get() {
return srcObject
}
})
return el
})

const el = document.createElement('video')

patchProp(el, 'srcObject', undefined, null)

expect(el.srcObject).toBeNull()
spyCreateElement.mockRestore()
})
})
16 changes: 15 additions & 1 deletion packages/runtime-dom/src/modules/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,21 @@ export function patchDOMProp(
if (value === '' && typeof el[key] === 'boolean') {
// e.g. <select multiple> compiles to { multiple: '' }
el[key] = true
} else {
} else if (isStringAttribute(el.tagName, key)) {
el[key] = value == null ? '' : value
} else {
el[key] = value
}
}

function isStringAttribute(tagName: any, key: string) {
try {
// some dom properties accept '' but not other strings, e.g. <video>.volume
document.createElement(tagName)[key] = 'test string'
return true
} catch (e) {
if (e.name !== 'TypeError') throw e

return false
}
}