monaco-editor icon indicating copy to clipboard operation
monaco-editor copied to clipboard

line jump up and down when type chinese, only in safari

Open ishowshao opened this issue 1 year ago • 7 comments

Reproducible in vscode.dev or in VS Code Desktop?

  • [x] Not reproducible in vscode.dev or VS Code Desktop

Reproducible in the monaco editor playground?

Monaco Editor Playground Link

https://microsoft.github.io/monaco-editor/playground.html?source=v0.52.2#XQAAAAKVAQAAAAAAAABBqQkHQ5NjdMjwa-jY7SIQ9S7DNlzs5W-mwj0fe1ZCDRFc9ws9XQE0SJE1jc2VKxhaLFIw9vEWSxW3yscwzrv7RgmSaumWUF-TBOsVJ4S4FwfCDQb5-2uUHXTVAefbVSd9IzaZ_vv9VR9KugsKimARHSWlrTPOQHROITHxmAeFEs9_m3VHdSe7_Etv6WpgzlH4mZ0NfkLMCQ74SvdsAK27mb5mqjbl7XLwR2o6TRZQPp4HjPlVBKf0HTNMZZm5MI3zMZ8uxujbFuz-4DgvI0JxXDeOxuoYpO1Z3ywvkovLJSqgWzSKKJ179jGVooILSemcz2C90dTFS3A9UqOJA-HBQ6RIWY9yTZBvLgERzqHUvIas0lTrKnXNdzlbWabFgzlulE-OxylZ_8M3dZo

Monaco Editor Playground Code


Reproduction Steps

  1. open safari on macOS
  2. use safari visit Playground Link
  3. type some chinese use macOS System input method
  4. Only the text after the first line has a problem, the first line has no problem

Actual (Problematic) Behavior

No response

Expected Behavior

Don't jump up and down

Additional Context

No response

ishowshao avatar Dec 28 '24 16:12 ishowshao

up

ishowshao avatar Feb 27 '25 16:02 ishowshao

0.31.0 is ok, but it is too old...

ishowshao avatar Mar 31 '25 12:03 ishowshao

I reproduced this with Monaco 0.55.1 on Safari using the macOS Chinese IME. Chrome is unaffected.

window.EditContext is undefined in Safari, so this occurs in TextAreaEditContext, not NativeEditContext.

During composition, Monaco repeatedly assigns both textarea.scrollTop and textarea.scrollLeft in textAreaEditContext.ts. I tested stabilizing these values for the duration of one composition session:

  • Stabilizing only scrollTop changed the vertical jitter into horizontal jitter.
  • Stabilizing both scrollTop and scrollLeft removed the jitter completely.

This strongly suggests that Safari’s native IME scrolling and Monaco’s composition textarea scrolling are competing during each compositionupdate.

A local workaround is to record both offsets after Monaco handles compositionstart, then restore them on compositionupdate and scroll until compositionend.

The behavior also appears related to VS Code commit b1cc6fd349 (“Improve textarea rendering when it is visible for IME input”), which changed composition rendering to expose the current line and align it using textarea scrolling. This matches the earlier observation that Monaco 0.31.0 did not reproduce the problem.

vapao avatar Jul 13 '26 04:07 vapao

[!NOTE] Superseded in part by the follow-up below. The "clamped as it is written" reading in this comment turned out to be wrong: the writes are never clamped, and reordering them does not fix anything. The offsets are moved after the write. The measurements below still stand.

Same symptom in a Tauri app (WKWebView) on macOS with Monaco 0.55.1, and instrumenting it points at an ordering bug rather than at Safari and Monaco competing.

What the numbers say

I recorded what Monaco writes to the composition overlay (textarea.inputarea) against what the element actually keeps, while composing on the second line of the page (lineHeight 21):

nlBefore   1     newlines before the caret in the textarea's value
wanted     21    = nlBefore * lineHeight, Monaco's own formula
kept       18    during composition
kept       21    after compositionend

The overlay is one row tall and shows a scrolled page of text, so those 3 missing pixels are exactly the "sinks while composing, jumps back when the text is committed" this issue describes. On the first line of the page nlBefore is 0, nothing has to be scrolled, and there is nothing to lose — which is why the first line never jitters (reproduction step 4 above).

Two things this is not: scrollHeight stays a whole multiple of lineHeight throughout, so no soft wrapping is involved, and forcing white-space: pre on the overlay changes nothing.

Where it comes from

In textAreaEditContext.ts the composition branch writes both offsets before _doRender() gives the overlay its new size:

https://github.com/microsoft/vscode/blob/main/src/vs/editor/browser/controller/editContext/textArea/textAreaEditContext.ts#L751-L754

while the non-composition branch, twenty lines further down, does it the other way around — _doRender() first, offsets afterwards:

https://github.com/microsoft/vscode/blob/main/src/vs/editor/browser/controller/editContext/textArea/textAreaEditContext.ts#L799-L811

So during composition the write lands while the element still has the previous frame's geometry, and the browser clamps it against a scrollable range that is not the one it will have a moment later. Chromium tolerates this, WebKit does not, which is what "only in safari" means here.

What removes it

Capturing what Monaco writes and replaying both offsets on the next frame — after the geometry has landed — removes the jitter completely. Replaying only scrollTop leaves horizontal jitter, which is the same thing @vapao measured.

I have not yet tested whether moving the two writes below the _doRender() call is enough on its own in WebKit. I am testing that now and will report back; if it holds, the fix is a two-line reorder that makes the composition branch agree with its sibling.

PathGao avatar Aug 28 '26 16:08 PathGao

Correction to my previous comment, and a full instrumented trace. The write is never clamped, and reordering the two writes does not fix it — I built Monaco with the reorder applied and the jitter is unchanged. What actually happens is that the overlay is scrolled after the write, by a constant amount, every time the caret advances.

The cycle, one keystroke at a time

Logged from inside a WKWebView app (Monaco 0.55.1, lineHeight 21, caret on line 2 of the page):

compositionupdate "a j"
  WRITE  top 21 -> kept 21   [sh 42 ch 21 max 21 rows 2 nlBefore 1]   no clamp, range is sufficient
  WRITE  left 0 -> kept 0
  SCROLL top 18 left 1  (asked 21, off by 3)   [sh 42 ch 21 max 21]   the element scrolls itself
  WRITE  top 21 -> kept 21                                            next render puts it back
  SCROLL top 21 left 0

Over three composed lines: 102 self-scrolls, every single one off by exactly 3px, and 1px horizontally. On line 3 it is asked 42 → 39 — the same 3, so it is not proportional to the row. scrollHeight - clientHeight is 21 (and 42) at the moment it moves, so the position it lands on is reachable; nothing is being clamped. The only CLAMPED writes in the whole trace are 27 horizontal ones, all after compositionend, all on content with no horizontal overflow.

That 18 ⇄ 21 flip is the jitter: the composed row is drawn 3px low, then snapped back on the next render, once per keystroke.

What the 3px is

The overlay is one row tall and Monaco aligns the row by scrolling to nlBefore * lineHeight. After the marked text is inserted the caret advances (sel grows in the trace), and WebKit scrolls the focused editable by the minimum needed to make the caret visible. The caret box is shorter than the line box, so the minimum lands short of row-aligned — by a constant that depends on the font, not on the row.

This also explains the two exemptions:

  • the first line of the page never jitters — the target offset is 0, and the minimum scroll is also 0, so there is nothing to disagree about (reproduction step 4 in the report)
  • "editor.accessibilitySupport": "off" removes it entirely — on macOS that path puts a short single-line string in the textarea instead of a page, so no scrolling is needed at all

Whose problem this is

Keeping the caret visible in a focused editable is something a browser is entitled to do, so aligning by scrollTop is not a position Monaco can hold. It only shows up in Safari because editContext defaults to true and is included: isChrome || isEdge || isNative, so Chromium runs NativeEditContext and never touches this code — the textarea path is now the fallback that WebKit and Firefox get.

Two directions that would remove the fight instead of re-fighting it every frame:

  1. Content: do not put a page of lines in the overlay when nothing indicates a screen reader is attached (AccessibilitySupport.Unknown), or at least not while a composition is running. Both offsets are then 0 by construction — this is what the Disabled path already does, and it is verified not to jitter.
  2. Geometry: give the overlay no scrollable overflow at all — full content height, aligned with top, clipped (a wrapper or clip-path) instead of scrolled. Layout is not something the browser will move underneath you; scroll offset is.

Re-asserting both offsets from the overlay's own scroll event also works, and I have it running locally, but it is a workaround, not a fix — it leaves the alignment depending on a value the browser owns.

Happy to prepare a PR for either direction if a maintainer says which one is wanted.

PathGao avatar Aug 28 '26 17:08 PathGao

Proposed fix: microsoft/vscode#333909 — getScreenReaderContent hands AccessibilitySupport.Unknown the same short content as Disabled (the write path already treats Unknown as "no screen reader", #192278), so the composition overlay has no overflow and nothing scrolls it. Verified in the WKWebView app above; Enabled is untouched.

PathGao avatar Sep 02 '26 00:09 PathGao

@hediet @hawkticehurst — when you have a moment: microsoft/vscode#333909 fixes this. It is a two-condition change in textAreaEditContext.ts (the content path treats AccessibilitySupport.Unknown the way the write path already does, so the composition overlay has no overflow for WebKit to scroll), verified in the WKWebView app that reproduces the report, with the Copilot review already addressed. The full trace of what WebKit does per keystroke is in my comments above.

PathGao avatar Sep 02 '26 00:09 PathGao