codesandbox-client
codesandbox-client copied to clipboard
Broken prototype chain for elements
🐛 bug report
Preflight Checklist
- [x] I have read the Contributing Guidelines for this project.
- [x] I agree to follow the Code of Conduct that this project adheres to.
- [x] I have searched the issue tracker for an issue that matches the one I want to file, without success.
Description of the problem
The class hierarchy for elements is broken.
EventTarget -> Node -> Element -> HTMLElement -> HTMLButtonElement
How has this issue affected you? What are you trying to accomplish?
Using an element as a parameter to e.g. Range.setStart results in TypeError: Failed to execute 'setStart' on 'Range': parameter 1 is not of type 'Node'.
Codesandbox is our primary platform for online reproduction at @testing-library/user-event.
user-event@14 supports Selection. With this bug all pointer interactions with elements that don't implement their own selection (everything except <input>,<textarea>) result in an error.
To Reproduce
test('Element extends Node', () => {
expect(document.body).toBeInstanceOf(Node) // fails
})
test('select per Range', () => {
document.body.innerHTML = `<div>foo</div>`
const range = new Range()
range.setStart(document.body.firstChild, 0) // throws
range.setEnd(document.body.firstChild, 1)
document.getSelection().addRange(range)
})
Link to sandbox: link (optional)
Your Environment
| Software | Name/Version |
|---|---|
| Сodesandbox | |
| Browser | Chrome 99.0.4844.82 |
| Operating System | Ubuntu 20.04.4 LTS |
It looks like the prototype chain in the JSDOM implementation is correct, but the setup of the jest-jsdom environment is broken.
There is a local Node variable that shadows global.Node. The elements in the document are created per global constructors, but the Range implementation checks against the (or another) local Node variable.
test('element extends global.Node', () => {
expect(document.body).toBeInstanceOf(global.Node)
expect(global.Node).toBe(window.Node)
expect(global.Node).toBe(document.defaultView.Node)
}) // success
Workaround: Use constructor from defaultView.
test('select per defaultView.Range', () => {
document.body.innerHTML = `<div>foo</div>`
const range = new document.defaultView.Range()
range.setStart(document.body.firstChild, 0)
range.setEnd(document.body.firstChild, 1)
document.getSelection().addRange(range)
})