question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Textarea `type` does not respect selection range

See original GitHub issue
  • @testing-library/user-event version: 12.1.4
  • Testing Framework and version: jest
  • DOM Environment: jsdom

Relevant code or config: Check out this pull request. I added a test to demonstrate the use case I ran into and to show the bug. I don’t intent to merge the PR, it’s simple there to as a simple reproducible version of my issue.

What you did:

  • Textarea has some text "foo"
  • Call textarea.setSelectionRange(0,0) to put cursor at the beginning of the textarea.
  • Call type to add some text "bar"
  • Expected textarea value should have "barfoo"

What happened:

  • textarea value is "foobar"

Image of running the test locally: image

Reproduction repository: Check out this pull request.

Somewhat hilariously, I’m actually using the library to simulate user behavior on Roam. I know the library is not designed for that and meant for testing, is there any concern with using it in this way? I was able to reproduce the issue I was running into in a testing use case anyway, as evidenced by the linked pull request above.

Here’s my relevant use case anyway: https://github.com/dvargas92495/roam-js-extensions/blob/master/src/entries/todont.ts#L51-L53

Problem description: I would expect moving the cursor using setSelectionRange would result in subsequent type calls to respect that invocation.

Suggested solution: I haven’t dove into the user-event source code yet, but if we agree that this is a bug, I would love to take a shot a solving this!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dvargas92495commented, Sep 12, 2020

ah I needed to do both:

element.setSelectionRange(0,0);
await userEvent.type(element, "Prefix: ", {initialSelectionStart: 0, initialSelectionEnd: 0});

going to close the linked PR! Thanks for your help!

0reactions
marcosvega91commented, Sep 12, 2020

I was supsecting this but I didn’t try yet. I was thinking if this is really correct.

The the parameters are used only when the initial selection is 0. I guess they should be used everytime they are provided

Read more comments on GitHub >

github_iconTop Results From Across the Web

Replacing text inside textarea without focus - Stack Overflow
1) When text in the textarea is selected, then the button is clicked, the selected text is replaced by the text in the...
Read more >
Why is textarea seperate from input? Is it even necessary?
I'm more wondering why the INPUT with type=text and TEXTAREA elements are separated at all, if TEXTAREA is only a larger version of...
Read more >
The Textarea element - HTML: HyperText Markup Language
The form element that the <textarea> element is associated with (its "form owner"). The value of the attribute must be the id of...
Read more >
4.10.13 The textarea element — HTML5: Edition for Web ...
The cols and rows attributes are limited to only non-negative numbers greater than zero. The cols attribute's default value is 20. The rows...
Read more >
HTML Standard
Full table of contents. 1 Introduction. 1.1 Where does this specification fit? 1.2 Is this HTML5? 1.3 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found