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

JavaScript Instances registry, faulty re-set new instance #37245

Open
3 tasks done
GeoSot opened this issue Oct 3, 2022 · 5 comments · May be fixed by #37442
Open
3 tasks done

JavaScript Instances registry, faulty re-set new instance #37245

GeoSot opened this issue Oct 3, 2022 · 5 comments · May be fixed by #37442
Labels
confirmed js p3 Medium priority, and does not prevent the core functionality v5

Comments

@GeoSot
Copy link
Member

GeoSot commented Oct 3, 2022

Prerequisites

Describe the issue

Bootstrap's Instances registry, by design, doesn't support multiple instances per element. However, in the rare case we are trying to re-initiate the same component for second time, it wrongly overrides the already existing instance

I found this flaw on #37195, where two tooltips were created using the same element

and is caused on this method

set(element, key, instance) {
if (!elementMap.has(element)) {
elementMap.set(element, new Map())
}
const instanceMap = elementMap.get(element)
// make it clear we only want one instance per element
// can be removed later when multiple key/instances are fine to be used
if (!instanceMap.has(key) && instanceMap.size !== 0) {
// eslint-disable-next-line no-console
console.error(`Bootstrap doesn't allow more than one instance per element. Bound instance: ${Array.from(instanceMap.keys())[0]}.`)
return
}
instanceMap.set(key, instance)
},


Steps to reproduce

  • open debugger with given breakpoint on
    instanceMap.set(key, instance)
  • initialize an instance of any available js component, using constructor ex: new Toast(myelement)
  • Debugger with stop on the breakpoint
  • repeat the first step again -> new Toast(myelement)
  • Debugger should not stop in the same point as the instance already exists

Reduced test cases

https://codepen.io/GeosSV/pen/gOzzWYV?editors=1011

What operating system(s) are you seeing the problem on?

Windows, macOS, Android, iOS, Linux

What browser(s) are you seeing the problem on?

Chrome, Safari, Firefox, Microsoft Edge, Opera

What version of Bootstrap are you using?

v5.2.2

@GeoSot GeoSot added js confirmed v5 p3 Medium priority, and does not prevent the core functionality labels Oct 3, 2022
@GeoSot GeoSot added this to v5.3.0 Oct 3, 2022
@GeoSot GeoSot moved this to To do in v5.3.0 Oct 3, 2022
@GeoSot
Copy link
Member Author

GeoSot commented Oct 5, 2022

@XhmikosR can you remember something on the following test, or explain it?
I am curious for what reason, we allow overwriting an element instance (as it is not disposed first)

it('should overwrite data if something is already stored', () => {
const data = { ...TEST_DATA }
const copy = { ...data }
Data.set(div, TEST_KEY, data)
Data.set(div, TEST_KEY, copy)
// Using `toBe` since spread creates a shallow copy
expect(Data.get(div, TEST_KEY)).not.toBe(data)
expect(Data.get(div, TEST_KEY)).toBe(copy)
})

@tobiasberge
Copy link

Hi everyone, 👋

is this by chance also related to this topic? #35710

If yes I would postpone a workaround re-initializing Popover and Tooltip every time manually after dynamic content changes and use the "selector" option instead.

@WinterSilence
Copy link
Contributor

also, old versions using jQuery, not throws error if selector not exists - need add "silent mode" to keep BC

@GeoSot
Copy link
Member Author

GeoSot commented Oct 11, 2022

Hey @tobiasberge, it is a bit different, and the tooltip after 5.1.3 (I am not sure of the version) re-initializes its self every time

@tobiasberge
Copy link

@GeoSot Thanks for your answer, good to know. We wanted to update to 5.2.x anyway. 👍

@GeoSot GeoSot linked a pull request Nov 7, 2022 that will close this issue
10 tasks
@github-project-automation github-project-automation bot moved this to To do in v5.3.3 Nov 12, 2023
@julien-deramond julien-deramond moved this from To do to Needs review in v5.3.3 Nov 12, 2023
@louismaximepiton louismaximepiton removed this from v5.3.3 Dec 6, 2023
@julien-deramond julien-deramond moved this to Inbox in v6.0.0 Aug 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed js p3 Medium priority, and does not prevent the core functionality v5
Projects
Status: Inbox
Status: To do
Development

Successfully merging a pull request may close this issue.

3 participants