-
Notifications
You must be signed in to change notification settings - Fork 252
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
userEvent.type on contenteditable #442
Comments
Hi @ph-fritsche thanks for raising this :). I think that it can be a problem with |
There is a misconception Lines 114 to 118 in a5b3350
The select/range offsets are not equivalent to the number of chars selected, but the DOM offset.
The typing should take place on |
That's right. When this part was done the For example this issue |
The selection itself seems to be working as expected. I think we should try to make Meanwhile we should note it as a known limitation here and reimplement the selection handling:
This should fix all test cases aside those with a previous selection. That would be overridden by |
FirstWhile experimenting with @testing-library/user-event I reached the following related issue, with jest in terminal: The I am using:
I will try to find more information about this issue, but I am not sure I will. If there is something I can do to help the work on this progress, please let me know. SecondAnother issue which, I think, is related to this issue is that the two buttons in the following example both throw an exception: https://codesandbox.io/s/usereventtype-on-contenteditable-forked-fqo5d?file=/index.js This code sandbox uses the latest version of the dependencies: Screenshots |
Resolved in #779 There seems to be still an issue with codesandbox's But the following tests pass in the current test environment: import userEvent from "#src"
function app() {
document.body.innerHTML = ""
document.body.innerHTML = `
<div id="input" contenteditable>foo</div>
<div id="output"></div>
<button id="select">Select</button>
<button id="start">Start</button>
<button id="end">End</button>
`
const input = document.getElementById("input")
const output = document.getElementById("output")
const buttonSelect = document.getElementById("select")
const buttonStart = document.getElementById("start")
const buttonEnd = document.getElementById("end")
function selectInput() {
const selection = document.getSelection()
input.focus()
selection.removeAllRanges()
selection.selectAllChildren(input)
return selection
}
buttonSelect.addEventListener("click", () => selectInput())
buttonStart.addEventListener("click", () => selectInput().collapseToStart())
buttonEnd.addEventListener("click", () => selectInput().collapseToEnd())
input.addEventListener("input", (event) => {
output.innerHTML = event.target.textContent
})
return { input, output, buttonSelect, buttonStart, buttonEnd }
}
test("Type over content", () => {
const { input, output, buttonSelect } = app()
expect(input.textContent).toBe("foo")
expect(output.textContent).toBe("")
userEvent.click(buttonSelect)
userEvent.keyboard("bar")
expect(output.textContent).toBe("bar")
})
test("Type before content", () => {
const { input, output, buttonStart } = app()
expect(input.textContent).toBe("foo")
expect(output.textContent).toBe("")
userEvent.click(buttonStart)
userEvent.keyboard("bar")
expect(output.textContent).toBe("barfoo")
})
test("Type after content", () => {
const { input, output, buttonEnd } = app()
expect(input.textContent).toBe("foo")
expect(output.textContent).toBe("")
userEvent.click(buttonEnd)
userEvent.keyboard("bar")
expect(output.textContent).toBe("foobar")
}) |
userEvent.type()
on an element withcontenteditable
does not honor the cursor position / selection.I've set up a code example to demonstrate the problem here:
https://codesandbox.io/s/usereventtype-on-contenteditable-obxib?file=/src/app.test.js
The input field with the content
foo
has an EventListener that just copies InputEventsevent.target.textContent
on anotherdiv
.When you click one of the buttons and start typing
bar
, you can see your input being inserted as expected.Trying to emulate the behavior per
userEvent.type
fails.When the cursor is in front of
foo
, it setstextContent
tofoobar
instead ofbarfoo
.When the cursor is behind
foo
, it setstextContent
tofbaroo
instead iffoobar
.When the content is selected, it sets
textContent
tobaroo
instead ofbar
.The text was updated successfully, but these errors were encountered: