Skip to content

Spacebar in text input causes click event #39028

Closed

Description

@MichaelMcDonnell

Describe the bug:
Typing a space with the spacebar in a text input causes a click event.

To Reproduce:

  1. Open my example which is a text input with an onclick listener that logs a message to the console.
  2. Open Servo from a terminal.
  3. Click in the text input and notice the message "Got onclick" in the terminal.
  4. Type one or more spaces using the spacebar.
  5. Notice in the terminal that "Got onclick" is printed every time spacebar is pressed.

Firefox and Chromium do not get the click event when spacebar is pressed. I checked it using their developer tools.

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

Activity

  1. added
    C-untriaged
    on Aug 29, 2025
  2. MichaelMcDonnell commented on Aug 29, 2025

    @MichaelMcDonnell
    ContributorAuthor

    I found the following code in script/dom/document_event_handler.rs:

    // This behavior is unspecced
    // We are supposed to dispatch synthetic click activation for Space and/or Return,
    // however *when* we do it is up to us.
    // Here, we're dispatching it after the key event so the script has a chance to cancel it
    // https://www.w3.org/Bugs/Public/show_bug.cgi?id=27337
    if (keyboard_event.event.key == Key::Named(NamedKey::Enter) ||
        keyboard_event.event.code == Code::Space) &&
        keyboard_event.event.state == KeyState::Up
    {
        if let Some(elem) = target.downcast::<Element>() {
            elem.upcast::<Node>()
                .fire_synthetic_pointer_event_not_trusted(DOMString::from("click"), can_gc);
        }
    }

    Pressing enter or spacebar triggers the onclick event. Neither Firefox nor Chromium fire these events.

  3. changed the title [-]Spacebar in text input causes onclick event[/-] [+]Spacebar in text input causes click event[/+] on Aug 29, 2025
  4. MichaelMcDonnell commented on Aug 29, 2025

    @MichaelMcDonnell
    ContributorAuthor

    There are some old specifications that say that some elements should receive a click event for accessibility reasons on enter or space. I noticed in Firefox if you focus a link with the tab key and then press enter, then you get a click event. You don't get a click event with space or the up key though.

    Is there a better specification that lists exactly which elements should generate a click event for which keys?

  5. added
    A-content/editing
    and removed
    C-untriaged
    on Aug 30, 2025
  6. added
    C-has-manual-testcase
    on Sep 5, 2025
  7. jdm commented on Sep 5, 2025

    @jdm
    Member

    I was assuming that the code you linked was attempting to implement https://html.spec.whatwg.org/#implicit-submission, although I don't see why we would be using an untrusted event in that case.

  8. jdm commented on Sep 5, 2025

    @jdm
    Member

    It's also possible that it was poorly implementing the behaviour of pressing spacebar to press a focused button element.

  9. jdm commented on Oct 9, 2025

    @jdm
    Member

    Step 5 of that last link seems important. I think the intended spec logic here is:

    • First dispatch a keydown event
    • If the event is handled, do nothing
    • Otherwise, if the focus is on an element with an activation behaviour, and the key is space or enter, fire an untrusted MouseEvent named click at the element
  10. MichaelMcDonnell commented on Oct 10, 2025

    @MichaelMcDonnell
    ContributorAuthor

    Step 5 of that last link seems important. I think the intended spec logic here is:

    * First dispatch a keydown event
    
    * If the event is handled, do nothing
    
    * Otherwise, if the focus is on an element with an activation behaviour, and the key is space or enter, fire an untrusted MouseEvent named click at the element
    

    Great! That sounds reasonable. Thanks for digging that out! 😀

  11. added a commit that references this issue on Mar 17, 2026
    8555464
  12. MichaelMcDonnell commented on Mar 25, 2026

    @MichaelMcDonnell
    ContributorAuthor

    Thank you for fixing this @mrobinson! It's great to see progress with the text issues 😀

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/editingC-has-manual-testcaseE-candidate-for-mentoring

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions