iOS: Return at the end of a line sometimes inserts two line breaks #582

Closed
opened 2026-10-18 13:05:23 +00:00 by kayg · 2 comments
Owner

Reported by kayg on a real iPhone (iOS 27, production calternal.cloud, Safari).

Sometimes when I tap enter or return on mobile at the end of a sentence, at the end of a line basically, it does two returns instead of one, so I go two lines down.

After typing a log list item and pressing Return, a new empty bullet appears, but the caret sits one line below the bullet: an extra line break inside the new item, or two items.

Expected

Exactly one line break per Return, in every case (also after an autocorrected word or with a pending composition). Shift+Return and soft continuation stay as they are; hardware-keyboard Enter on iPad and Mac Safari keeps working.

Plan

  1. Reproduce in the iPhone simulator with the software keyboard and log keydown / beforeinput / input / composition / CodeMirror transactions.
  2. Fix the root cause.
  3. Test that replays the captured iOS event sequences.
  4. Verify in the simulator with 20+ real Returns.
Reported by kayg on a real iPhone (iOS 27, production calternal.cloud, Safari). > Sometimes when I tap enter or return on mobile at the end of a sentence, at the end of a line basically, it does two returns instead of one, so I go two lines down. After typing a log list item and pressing Return, a new empty bullet appears, but the caret sits one line below the bullet: an extra line break inside the new item, or two items. ## Expected Exactly one line break per Return, in every case (also after an autocorrected word or with a pending composition). Shift+Return and soft continuation stay as they are; hardware-keyboard Enter on iPad and Mac Safari keeps working. ## Plan 1. Reproduce in the iPhone simulator with the software keyboard and log keydown / beforeinput / input / composition / CodeMirror transactions. 2. Fix the root cause. 3. Test that replays the captured iOS event sequences. 4. Verify in the simulator with 20+ real Returns.
Author
Owner

Root cause (found and reproduced)

Not autocorrect, not composition. The editor turns those off (autocorrect=off, spellcheck=false, autocapitalize=off, writingsuggestions=false), and no composition* events fire in any of the logs below.

On iOS Safari does not report what Return did; it edits the DOM itself and CodeMirror reads that back as the key (pendingIOSKey). CodeMirror also starts a 50 ms timer on keydown: if Safari's edit has not shown up by then, it runs the key's command (our list continuation) anyway. On a busy phone Safari's edit arrives after the 50 ms. The command already made the new bullet, and Safari's own line break then lands on top of it: two returns from one tap, an empty bullet with the caret one line below (the screenshot). Backspace and Delete share the path (two characters deleted).

Event logs (iPhone 18 Pro simulator, iOS 27.0, real taps on the software keyboard)

The simulator is fast, so Safari's edit comes ~25 ms after keydown and the bug does not show by itself. To emulate a busy phone the page holds its main thread 70 ms after the Return keydown (an extra keydown listener added through Web Inspector, nothing in the app).

Normal order (no load), one line break:

204607 keydown Enter            trusted
204612 beforeinput insertLineBreak
204612 input insertLineBreak
204613 keydown Enter            made up by CodeMirror
204613 tr input   [[501,501,"\n- "]]

Late (main thread held 70 ms), dev, two line breaks:

51190 keydown Enter             trusted
51282 keydown Enter             made up by CodeMirror (its 50 ms timer)
51285 tr input   [[500,500,"\n- "]]        <- the new bullet
51290 beforeinput insertLineBreak            <- Safari's own edit, too late
51295 tr input.type [[503,503,"\n\n"]]       <- the extra line break(s) inside the new item

Same late case with the fix: the beforeinput is cancelled, nothing else is inserted.

52867 keydown Enter             trusted
52957 keydown Enter             made up by CodeMirror
52960 tr input   [[498,498,"\n- "]]
52965 beforeinput insertLineBreak            (cancelled)

Results in the simulator (real taps)

dev (before) fix (after)
Return at the end of a log line with a tag, main thread held 70 ms, 6 Returns 6 of 6 doubled (6 extra raw breaks, list broken) 6 of 6 single
Return at the end of a paragraph, held 70 ms, 4 4 of 4 doubled 4 of 4 single
Return at the end of a to-do, held 70 ms, 4 4 of 4 doubled 4 of 4 single
Return at the end of a log line with a time range, no hold, 5 5 single 5 single
Return, no hold, tag line, 5 n/a 5 single
Return, hold 45 ms (time range), 4 n/a 4 single
Return, hold 100 ms (paragraph), 4 n/a 4 single
Return, no hold (to-do), 3 n/a 3 single
Backspace x3 after typing abcd, held 70 ms 6 characters gone (...aayus) 3 gone (...aayushya)

35 Returns with the fix, 0 extra breaks. Frames: before https://git.kayg.org/attachments/2d5f7c5b-8178-4a45-bfbc-97e99050dbef (the empty bullet with the caret a line below, bullets lost), after https://git.kayg.org/attachments/7f70b792-2a96-4d9b-95a9-0e1630ea827e. Short recordings: before https://git.kayg.org/attachments/1915d171-107f-4cd6-bee2-950c831cdf4a, after https://git.kayg.org/attachments/c2520ae2-9123-43b5-babf-541f73f642ff.

Fix and tests

PR: #583 (into dev, not merged). packages/editor/src/input/ios-keys.ts cancels Safari's own edit (beforeinput preventDefault) when CodeMirror has already run the key's command for that press. Unit tests plus WebKit e2e that replay both captured orders; the late cases fail without the fix.

## Root cause (found and reproduced) Not autocorrect, not composition. The editor turns those off (`autocorrect=off`, `spellcheck=false`, `autocapitalize=off`, `writingsuggestions=false`), and no `composition*` events fire in any of the logs below. On iOS Safari does not report what Return did; it edits the DOM itself and CodeMirror reads that back as the key (`pendingIOSKey`). CodeMirror also starts a **50 ms timer** on `keydown`: if Safari's edit has not shown up by then, it runs the key's command (our list continuation) anyway. On a busy phone Safari's edit arrives after the 50 ms. The command already made the new bullet, and Safari's own line break then lands on top of it: two returns from one tap, an empty bullet with the caret one line below (the screenshot). Backspace and Delete share the path (two characters deleted). ## Event logs (iPhone 18 Pro simulator, iOS 27.0, real taps on the software keyboard) The simulator is fast, so Safari's edit comes ~25 ms after `keydown` and the bug does not show by itself. To emulate a busy phone the page holds its main thread 70 ms after the Return `keydown` (an extra `keydown` listener added through Web Inspector, nothing in the app). Normal order (no load), one line break: ``` 204607 keydown Enter trusted 204612 beforeinput insertLineBreak 204612 input insertLineBreak 204613 keydown Enter made up by CodeMirror 204613 tr input [[501,501,"\n- "]] ``` Late (main thread held 70 ms), dev, two line breaks: ``` 51190 keydown Enter trusted 51282 keydown Enter made up by CodeMirror (its 50 ms timer) 51285 tr input [[500,500,"\n- "]] <- the new bullet 51290 beforeinput insertLineBreak <- Safari's own edit, too late 51295 tr input.type [[503,503,"\n\n"]] <- the extra line break(s) inside the new item ``` Same late case with the fix: the `beforeinput` is cancelled, nothing else is inserted. ``` 52867 keydown Enter trusted 52957 keydown Enter made up by CodeMirror 52960 tr input [[498,498,"\n- "]] 52965 beforeinput insertLineBreak (cancelled) ``` ## Results in the simulator (real taps) | | dev (before) | fix (after) | |---|---|---| | Return at the end of a log line with a tag, main thread held 70 ms, 6 Returns | 6 of 6 doubled (6 extra raw breaks, list broken) | 6 of 6 single | | Return at the end of a paragraph, held 70 ms, 4 | 4 of 4 doubled | 4 of 4 single | | Return at the end of a to-do, held 70 ms, 4 | 4 of 4 doubled | 4 of 4 single | | Return at the end of a log line with a time range, no hold, 5 | 5 single | 5 single | | Return, no hold, tag line, 5 | n/a | 5 single | | Return, hold 45 ms (time range), 4 | n/a | 4 single | | Return, hold 100 ms (paragraph), 4 | n/a | 4 single | | Return, no hold (to-do), 3 | n/a | 3 single | | Backspace x3 after typing `abcd`, held 70 ms | 6 characters gone (`...aayus`) | 3 gone (`...aayushya`) | 35 Returns with the fix, 0 extra breaks. Frames: before https://git.kayg.org/attachments/2d5f7c5b-8178-4a45-bfbc-97e99050dbef (the empty bullet with the caret a line below, bullets lost), after https://git.kayg.org/attachments/7f70b792-2a96-4d9b-95a9-0e1630ea827e. Short recordings: before https://git.kayg.org/attachments/1915d171-107f-4cd6-bee2-950c831cdf4a, after https://git.kayg.org/attachments/c2520ae2-9123-43b5-babf-541f73f642ff. ## Fix and tests PR: https://git.kayg.org/kayg/calternal/pulls/583 (into dev, not merged). `packages/editor/src/input/ios-keys.ts` cancels Safari's own edit (`beforeinput` `preventDefault`) when CodeMirror has already run the key's command for that press. Unit tests plus WebKit e2e that replay both captured orders; the late cases fail without the fix.
Author
Owner

Closing during the 26 Oct handover cleanup: One line break per Return on iOS landed in dev (PRs #583, #586).

Closing during the 26 Oct handover cleanup: One line break per Return on iOS landed in dev (PRs #583, #586).
kayg closed this issue 2026-10-26 08:46:44 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
kayg/calternal#582
No description provided.