Skip to content
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 this reference for functions #38725

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

nwalters512
Copy link
Contributor

Description

This PR fixes the bug described in #38720. Functions like content and title weren't being called with the correct this reference.

Motivation & Context

As far as I can tell, #36652 introduced this regression. It switched from calling the function with a specific this value to calling it with that value as the first argument.

While this appears to have been unintentional, folks may have started relying on that first argument as a workaround, and thus it could be considered part of the API. I didn't do anything about that yet, but it might be reasonable to retain both the old and new behavior (that is, for functions like content, call it with both a specific this value and an explicit argument). I can make that change and update the documentation if requested.

I repurposed the existing execute() function to support this. If a second argument is provided, it will be an array, and the first value will be treated as the this argument. This is done by just passing the entire arguments array to call. This required adding undefined as the first value in some places that don't expect an specific this value.

Type of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Refactoring (non-breaking change)
  • Breaking change (fix or feature that would change existing functionality)

Checklist

  • I have read the contributing guidelines
  • My code follows the code style of the project (using npm run lint)
  • My change introduces changes to the documentation
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed

Related issues

Fixes #38720.

@nwalters512 nwalters512 requested a review from a team as a code owner June 7, 2023 23:56
@XhmikosR
Copy link
Member

/CC @GeoSot

@@ -143,7 +143,7 @@ class TemplateFactory extends Config {
}

_resolvePossibleFunction(arg) {
return execute(arg, [this])
return execute(arg, [undefined, this])
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return execute(arg, [undefined, this])
return typeof arg === 'function' ? arg.call(this._element) : arg

I suppose it is better to change only this, as it is count as a regression, and add a test, instead of messing all the rest calls

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This callsite doesn't actually matter, AFAICT. There are two calls where the this argument is important:

  • _resolvePossibleFunction in js/src/tooltip.js
  • The call to execute(this._config.placement, ...) in _createPopper in js/src/tooltip.js

I can update those two call sites to use the typeof arg === 'function' ... pattern if you'd prefer that I avoid changing execute?

@mdo mdo requested a review from GeoSot July 17, 2023 03:43
@nwalters512
Copy link
Contributor Author

@mdo @GeoSot 👋 anything I can do to help this along?

@nwalters512

This comment was marked as resolved.

@julien-deramond

This comment was marked as resolved.

@mdo mdo force-pushed the fix-function-this-reference branch from ec77195 to 9148528 Compare November 16, 2023 02:10
@mdo
Copy link
Member

mdo commented Nov 16, 2023

Seems good to go @GeoSot?

@nwalters512
Copy link
Contributor Author

@mdo @GeoSot checking in to see if there's anything else I can do to help get this merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom popover no longer works after upgrading to 5.3.0
5 participants