Skip to content

Generate a FocusEvent (not Event) for focus-related events - #272

Merged
route merged 3 commits into
rubycdp:mainfrom
davidrunger:focus-events-as-focus-events
Aug 24, 2026
Merged

Generate a FocusEvent (not Event) for focus-related events#272
route merged 3 commits into
rubycdp:mainfrom
davidrunger:focus-events-as-focus-events

Conversation

@davidrunger

@davidrunger davidrunger commented Aug 25, 2024

Copy link
Copy Markdown
Contributor

Currently on main, focus-related events (blur, focus, focusin, and focusout) create a JavaScript Event object.

However, as can be seen by clicking into and out of the input in this JSFiddle (tested in Firefox 130 and Chrome 128), those events should actually be JavaScript FocusEvent objects (not Event objects).

This change makes it so that Cuprite will generate FocusEvent objects (not Event objects) for focus-related events.

@davidrunger

davidrunger commented Aug 25, 2024

Copy link
Copy Markdown
Contributor Author

Context

The reason that this initially came to my attention (and my motivation for this change) is that a JavaScript library that I use (element-plus) actually validates that focus and blur events are indeed instances of FocusEvent.

Because currently on main Cuprite generates Event objects (not FocusEvent objects), warnings are printed when I execute in my application's test suite a feature spec that uses the Capybara fill_in method (which triggers focus and blur events) to fill in one of the inputs managed by that element-plus library:

image

This change makes it so that those warnings are no longer printed when I use the Capybara fill_in method to fill in an input for which the focus and blur event types are validated by element-plus.

@davidrunger

davidrunger commented Aug 25, 2024

Copy link
Copy Markdown
Contributor Author

Looking at the Ruby 3.3 test run, there are five failing specs (link).

I believe that none of those failures are related to my change, and that those failures are already present on main. All five of those tests also fail when I run the test suite locally on main.

Edit: I submitted a separate PR to fix these spec failures.

})
.focus(function(event) {
$("#changes_on_focus").text("Focus")
$("#changes_on_focus").text(`Focus (${event.originalEvent.constructor.name})`)

@davidrunger davidrunger Aug 26, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like tests to cover the change in this PR, so I added the event constructor name in parentheses here. On main, this text would be Focus (Event). On this branch, it's Focus (FocusEvent).

@davidrunger

Copy link
Copy Markdown
Contributor Author

Looking at the Ruby 3.3 test run, there are five failing specs (link).

I believe that none of those failures are related to my change, and that those failures are already present on main. All five of those tests also fail when I run the test suite locally on main.

I have submitted a separate PR to fix these spec failures that currently exist on the main branch: #273.

@route
route force-pushed the focus-events-as-focus-events branch from ad4a53d to 3047b60 Compare August 24, 2026 16:51
@route
route merged commit 171c42c into rubycdp:main Aug 24, 2026
7 checks passed
@route

route commented Aug 24, 2026

Copy link
Copy Markdown
Member

Thanks for this — confirmed the underlying issue is real and still present on main (focus/blur events dispatch as plain Event instead of FocusEvent, which trips up libraries doing instanceof FocusEvent checks, like the element-plus case above).

I rebased this onto current main and also swapped the deprecated document.createEvent/initEvent pair for the modern new FocusEvent(name, { bubbles: true, cancelable: true }) constructor, consistent with how input/keydown/keyup events are already constructed elsewhere in this file. Ran the full session_spec.rb suite locally (1670 examples, 0 failures). Added a changelog entry. Merging — thanks again!

@davidrunger

Copy link
Copy Markdown
Contributor Author

@route Thank you so much for taking a look, improving this branch, and merging! This has been continuing to be a small annoyance for me, so I'm grateful that it has been resolved on main, and I look forward to the next release!

@davidrunger
davidrunger deleted the focus-events-as-focus-events branch August 24, 2026 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants