basecamp / basecamp/trix

Calling editor.setSelectedText in system test

Open
#1,109 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
20k
Forks
1.1k
Avg merge
3d 12h
Merged PRs (30d)
13

Description

Our team is updating our Rails 7.0.8 app to Trix 2.0.7 from 1.3.1, which introduced a failure in one of our system tests that calls `setSelectedRange`:

```rb
content = "Hello world"
fill_in_rich_text_area with: content
find(:rich_text_area).execute_script("this.editor.setSelectedRange(#{content.length})")
```

On 1.3.1, this worked as expected; the text area was filled and then selection was moved to the end. On 2.0.7, the `setSelectedRange` call doesn't work; the selection stays at [0,0].

I haven't been able to find the root cause, and haven't found a way to get `setSelectedRange` working, but replacing `fill_in_rich_text_area` with Capybara's `set` method works around the issue:

```rb
content = "Hello world"
find(:rich_text_area).set(content)
```

I'm curious if there's a known change to `loadHTML` (called by [`fill_in_rich_text_area`](https://github.com/rails/rails/blob/fc734f28e65ef8829a1a939ee6702c1f349a1d5a/actiontext/lib/action_text/system_test_helper.rb#L33)) or `setSelectedRange` in 2.x that might have caused this behavior, but I understand this isn't necessarily a Trix bug, so feel free to close this as you see fit. I mainly wanted to note the issue and workaround for anyone else who might encounter it.

##### Details

* Trix version: 2.0.7
* Browser name and version: Chrome 119.0.6045.159 (via system test)
* Operating system: macOS 14.1

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with actiontext/lib/action_text/system_test_helper.rb at the fill_in_rich_text_area entry point, then reproduce the Trix 2.0.7 behavior in the shown Chrome system test using setSelectedRange and loadHTML. Compare the selection result with Capybara's set workaround; done means identifying and fixing the regression or documenting that the workaround is required.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, rails
Domain
frontend, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.