-
-
Notifications
You must be signed in to change notification settings - Fork 78.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: make EventHandler better handle mouseenter/mouseleave events #33310
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ const customEvents = { | |
mouseenter: 'mouseover', | ||
mouseleave: 'mouseout' | ||
} | ||
const customEventsRegex = /^(mouseenter|mouseleave)/i | ||
const nativeEvents = new Set([ | ||
'click', | ||
'dblclick', | ||
|
@@ -113,7 +114,7 @@ function bootstrapDelegationHandler(element, selector, fn) { | |
|
||
if (handler.oneOff) { | ||
// eslint-disable-next-line unicorn/consistent-destructuring | ||
EventHandler.off(element, event.type, fn) | ||
EventHandler.off(element, event.type, selector, fn) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to reviewers, this fixes a bug with the one time delegated event handlers. Added a test to the specs too. |
||
} | ||
|
||
return fn.apply(target, [event]) | ||
|
@@ -144,14 +145,7 @@ function normalizeParams(originalTypeEvent, handler, delegationFn) { | |
const delegation = typeof handler === 'string' | ||
const originalHandler = delegation ? delegationFn : handler | ||
|
||
// allow to get the native events from namespaced events ('click.bs.button' --> 'click') | ||
let typeEvent = originalTypeEvent.replace(stripNameRegex, '') | ||
const custom = customEvents[typeEvent] | ||
|
||
if (custom) { | ||
typeEvent = custom | ||
} | ||
|
||
let typeEvent = getTypeEvent(originalTypeEvent) | ||
const isNative = nativeEvents.has(typeEvent) | ||
|
||
if (!isNative) { | ||
|
@@ -171,6 +165,24 @@ function addHandler(element, originalTypeEvent, handler, delegationFn, oneOff) { | |
delegationFn = null | ||
} | ||
|
||
// in case of mouseenter or mouseleave wrap the handler within a function that checks for its DOM position | ||
// this prevents the handler from being dispatched the same way as mouseover or mouseout does | ||
if (customEventsRegex.test(originalTypeEvent)) { | ||
const wrapFn = fn => { | ||
return function (event) { | ||
if (!event.relatedTarget || (event.relatedTarget !== event.delegateTarget && event.relatedTarget.contains(event.delegateTarget))) { | ||
return fn.call(this, event) | ||
} | ||
} | ||
} | ||
|
||
if (delegationFn) { | ||
delegationFn = wrapFn(delegationFn) | ||
} else { | ||
handler = wrapFn(handler) | ||
} | ||
} | ||
|
||
const [delegation, originalHandler, typeEvent] = normalizeParams(originalTypeEvent, handler, delegationFn) | ||
const events = getEvent(element) | ||
const handlers = events[typeEvent] || (events[typeEvent] = {}) | ||
|
@@ -219,6 +231,12 @@ function removeNamespacedHandlers(element, events, typeEvent, namespace) { | |
}) | ||
} | ||
|
||
function getTypeEvent(event) { | ||
// allow to get the native events from namespaced events ('click.bs.button' --> 'click') | ||
event = event.replace(stripNameRegex, '') | ||
return customEvents[event] || event | ||
} | ||
|
||
const EventHandler = { | ||
on(element, event, handler, delegationFn) { | ||
addHandler(element, event, handler, delegationFn, false) | ||
|
@@ -272,7 +290,7 @@ const EventHandler = { | |
} | ||
|
||
const $ = getjQuery() | ||
const typeEvent = event.replace(stripNameRegex, '') | ||
const typeEvent = getTypeEvent(event) | ||
const inNamespace = event !== typeEvent | ||
const isNative = nativeEvents.has(typeEvent) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
childish comment: better leave it as string inline, instead of use a var. Is very specific (either way is ok)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't matter, though, since the minifier should already inline it :)