Skip to content

Text selection deleted on focus change #39025

Closed

Description

@MichaelMcDonnell

Describe the bug:
The selected text in a text input is deleted when the focus changes.

To Reproduce:

  1. Write some text in a text input.
  2. Select some text in a text input.
  3. Click outside the text input.
  4. Click back into the text input.
  5. Notice the selected text is gone.

I have created a small example at https://michaelmcdonnell.github.io/servo/selecttext.html that includes some other test cases. There seems to be a couple of different issues with text selection.

Platform:
Linux, Fedora 42, Framework Laptop 13 AMD Ryzen 7040Series

UPDATE 1: This is a bug in winit. The Wayland IME implementation is incorrect. The Windows and macOS winit IME implementations work fine. The issue I filed was almost immediately closed and I was told it is working as expected. There is a fundamental disagreement about how IME is supposed to work.

UPDATE 2: The empty Ime::Preedit events have been fixed. I still get Ime::Enabled events though. It might be enough to fix this specific issue. I tried upgrading winit but there has unfortunately been several breaking changes so it will require some more work.

Activity

  1. added
    C-untriaged
    on Aug 29, 2025
  2. added
    I-wrong
    A-content/editing
    and removed
    C-untriaged
    on Aug 29, 2025
  3. MichaelMcDonnell commented on Sep 1, 2025

    @MichaelMcDonnell
    ContributorAuthor

    I can get the bug to go away if I comment out the compositionupdate handling code in handle_event in HTMLInputElement. That also fixes issue #39026.

    I'm not using an input method editor (IME) but Servo gets composition events (from IME) despite that. Firefox does not get composition events unless you use an IME. I think a good start is to only create composition events if an IME is present.

    UPDATE: The compositionupdate code was added in PR #35535

    UPDATE 2: I simplified the composition (IME) example from MDN and put it on my GitHub website. When running in Servo the text input get IME messages that Firefox's text input doesn't get.

    UPDATE 3: The problem also goes away if I comment out the code to enable IME self.winit_window.set_ime_allowed(true).

  4. MichaelMcDonnell commented on Sep 1, 2025

    @MichaelMcDonnell
    ContributorAuthor

    The IME events are coming from winit and passed on to servoshell in headed_window.rs. Maybe this is a winit bug, or we are using it the wrong way?

  5. MichaelMcDonnell commented on Sep 4, 2025

    @MichaelMcDonnell
    ContributorAuthor

    @dklassic Do you have any idea what is going on or any tips on how to proceed?

  6. dklassic commented on Sep 5, 2025

    @dklassic
    Contributor

    @dklassic Do you have any idea what is going on or any tips on how to proceed?

    I tried to replicate it on macOS with ime enabled and disabled which both doesn't result in the issue you mentioned, or maybe I'm misunderstanding the procedure:

    2025-09-05.10.06.14.mov

    Accompanying your results I would assume this might be a winit bug on Linux which misfires an ime event resulting in the overwrite of the selected text.

  7. MichaelMcDonnell commented on Sep 5, 2025

    @MichaelMcDonnell
    ContributorAuthor

    Thanks @dklassic! The behavior in your video looks correct. Yeah, it sounds like it might be a winit bug on Linux.

    I'm getting Preedit events with empty data as soon as I click into the text input. I'm not using an IME. Do you know if that is expected behavior? I would expect that I shouldn't get any Preedit events until I activate an IME?

    Sorry, if some of these questions don't make sense. I don't have any experience implementing IME support.

  8. dklassic commented on Sep 5, 2025

    @dklassic
    Contributor

    I'm getting Preedit events with empty data as soon as I click into the text input. I'm not using an IME. Do you know if that is expected behavior? I would expect that I shouldn't get any Preedit events until I activate an IME?

    I would assume the same, and honestly I would assume activating IME alone shouldn’t result in any event sent unless any actual keystroke was made.

    Alternatively I think we could also choose to ignore IME event when the data is empty which might also make sense.

  9. MichaelMcDonnell commented on Sep 5, 2025

    @MichaelMcDonnell
    ContributorAuthor

    I'm getting Preedit events with empty data as soon as I click into the text input. I'm not using an IME. Do you know if that is expected behavior? I would expect that I shouldn't get any Preedit events until I activate an IME?

    I would assume the same, and honestly I would assume activating IME alone shouldn’t result in any event sent unless any actual keystroke was made.

    Thanks! I'm happy to hear that I'm not completely off track 😀 I'll reach out to the winit project.

    Alternatively I think we could also choose to ignore IME event when the data is empty which might also make sense.

    Ok, that sounds like a reasonable work around if it can't be fixed in winit.

  10. kchibisov commented on Sep 11, 2025

    @kchibisov

    empty preedit tells that it got cleared https://docs.rs/winit/latest/winit/event/enum.Ime.html#variant.Preedit . if you had it empty already, nothing should be done really.

    Well, winit can in theory try to emit less events, but docs clearly say that it clears preedit not text in your editor.

  11. MichaelMcDonnell commented on Sep 12, 2025

    @MichaelMcDonnell
    ContributorAuthor

    @kchibisov, as far as I understand IME, you shouldn't receive a preedit event unless you have opened an input editor (for example with Super+.). This works on Windows and macOS (I think this is what @dkclassic saw too, please correct me if I'm wrong). On Wayland you get preedit events without opening an input editor.

  12. kchibisov commented on Sep 12, 2025

    @kchibisov

    @MichaelMcDonnell API doesn't say so though. But as I said on winit issue, your issue is because your IME is broken in the first place to send event which it shouldn't send, which my workaround kind of masks.

  13. mrobinson commented on Jan 19, 2026

    @mrobinson
    Member

    @MichaelMcDonnell Can you try this again now that #41979 has landed?

  14. MichaelMcDonnell commented on Jan 22, 2026

    @MichaelMcDonnell
    ContributorAuthor

    Thanks @mrobinson! I can confirm that is has been fixed 😀

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-content/editingI-wrong

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions