Skip to content

Conversation

jeremy
Copy link
Member

@jeremy jeremy commented Oct 4, 2025

Safari 26 positions the cursor incorrectly after smart quotes text substitution, causing it to jump back 1-2 characters.

After insertReplacementText events, the cursor is incorrectly positioned. Trix's selectionDidChange handler syncs, corrupting its internal state.

Hack to work around it:

  • Detect whether replacement is "at cursor" (smart quotes) vs "before cursor" (autocorrect) with <= 2 char distance heuristic.
  • For at-cursor replacements: manually calculate correct positioning accounting for length diff and triggering character (+1); set cursor in Trix; skip 3 input events to prevent corrupt sync; prevent selectionDidChange from syncing in the meantime; restore cursor position in requestAnimationFrame (no visible cursor movement).

To reproduce the issue: enable Edit > Substitutions > Smart Quotes in Safari, hit Enter twice then "I'll " - including the final space. The cursor should remain after the final space, no after the replaced apostrophe. Similarly with autocorrect: the cursor should not move to the end of the spelling-corrected word.

/cc @rosa

@jeremy jeremy force-pushed the safari-smart-quotes-cursor-fix branch from 7596228 to 5a239e1 Compare October 4, 2025 19:34
@jeremy jeremy requested a review from Copilot October 4, 2025 21:43
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a Safari 26 bug where the cursor position is incorrect after smart quote text substitution. The cursor incorrectly jumps back 1-2 characters instead of remaining at the expected position after the replacement.

  • Implements a workaround to detect "at cursor" vs "before cursor" text replacements using distance heuristics
  • Manually calculates and sets correct cursor positioning for smart quotes while preventing Safari's buggy sync
  • Adds comprehensive test coverage for smart quotes and autocorrect scenarios

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/trix/models/selection_manager.js Adds check to prevent selection sync during Safari smart quotes workaround
src/trix/controllers/level_2_input_controller.js Implements the main workaround logic with cursor positioning calculations and event handling
src/test/system/level_2_input_test.js Adds three test cases covering smart quotes and autocorrect behavior

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@jeremy jeremy force-pushed the safari-smart-quotes-cursor-fix branch from 5a239e1 to 3205c34 Compare October 4, 2025 22:38
@jeremy jeremy requested a review from Copilot October 4, 2025 22:38
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Safari 26 positions the cursor incorrectly after smart quotes text
substitution, causing it to jump back 1-2 characters.

After insertReplacementText events, the cursor is incorrectly
positioned. Trix's selectionDidChange handler syncs, corrupting its
internal state.

Hack to work around it:
* Detect whether replacement is "at cursor" (smart quotes) vs
  "before cursor" (autocorrect) with <= 2 char distance heuristic.
* For at-cursor replacements: manually calculate correct positioning
  accounting for length diff and triggering character (+1); set cursor
  in Trix; skip 3 input events to prevent corrupt sync; prevent
  selectionDidChange from syncing in the meantime; restore cursor
  position in requestAnimationFrame (no visible cursor movement).

To reproduce the issue: enable Edit > Substitutions > Smart Quotes in
Safari, hit Enter twice then "I'll " - including the final space. The
cursor should remain after the final space, no after the replaced
apostrophe. Similarly with autocorrect: the cursor should not move to
the end of the spelling-corrected word.
@jeremy jeremy force-pushed the safari-smart-quotes-cursor-fix branch from 3205c34 to 32165b8 Compare October 5, 2025 00:24
@jeremy jeremy requested a review from Copilot October 5, 2025 00:25
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 3 out of 4 changed files in this pull request and generated no new comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant