Skip to content

Clicking nested element in disabled button invokes button's onclick handler #28788

Closed

Description

@nils4cosee

Current behavior

When "clicking" a nested element of a disabled button, the onclick-handler of this button is run (in Chrome or Firefox, not in Electron)

<body>
    <button disabled onclick="document.querySelector('div').innerText = 'Button clicked'">
        <span>Click me</span>
    </button>
    <div>

    </div>
</body>
</html>

Test:

it("does not invoke the onclick handler of a disabled parent button", () => {
    cy.visit("index.html")
    cy.contains("span", "Click me").click()
    cy.contains("Button clicked").should('not.exist')
})

image

Desired behavior

The behavior of

cy.contains("span", "Click me").click()

should be the same as

cy.contains("button", "Click me").click()

it only runs the onclick-handler if the button is not disabled and also waits for the button to be not disabled anymore.

Test code to reproduce

See my repo with the code https://github.com/nils4cosee/cypress-click-span-in-disabled-button

Cypress Version

13.6.1

Node version

18.15.0

Operating System

macOS 13.6

Debug Logs

Output of 


DEBUG=cypress:* npm run cypress:run -- --browser chrome --headless >debug.log 2>&1

is too long, according to GitHub. Please tell me if you really need it



### Other

_No response_

Activity

  1. jennifer-shehane commented on Jan 24, 2024

    @jennifer-shehane
    Member

    @nils4cosee Thank you for the clear reproducible example. This is surely a bug. We're open to a pull request to fix this. This bug is in the cypress/packages/driver package within this repo.

    I feel like the logic may be in the mouse.ts logic? https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cy/mouse.ts

    cy.click logic is defined here: https://github.com/cypress-io/cypress/blob/a11y-fixes-1/packages/driver/src/cy/commands/actions/click.ts#L259

    A test case would be added here to test this new behavior.: https://github.com/cypress-io/cypress/blob/a11y-fixes-1/packages/driver/cypress/e2e/commands/actions/click.cy.js

  2. TheoAnastasiadis commented on Jan 26, 2024

    @TheoAnastasiadis
    Contributor

    Apparently, disabled elements on Chrome do not prevent their children from firing "click" events. see this codepen.

    Maybe it is because according to the HTML spec, "actually disabled" elements can only ever be buttons, inputs, selects, textareas, options etc. ie. elements that must not have interactive content descendants.

    My idea would be to check if the mouse down or mouse up elements have an "actually disabled" parent, and skip the click command if they do. Will open a PR shortly.

  3. added 3 commits that reference this issue on Jan 26, 2024
    0bb361b
    622d4f3
    6c8943b
  4. added a commit that references this issue on Feb 27, 2024
    5d42406
  5. cypress-bot commented on Mar 13, 2024

    @cypress-bot
    Contributor

    Released in 13.7.0.

    This comment thread has been locked. If you are still experiencing this issue after upgrading to
    Cypress v13.7.0, please open a new issue.

  6. locked as resolved and limited conversation to collaborators on Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions