Skip to content

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

@GeoSot

Description

@GeoSot

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmedjsp3Medium priority, and does not prevent the core functionalityv5

    Type

    No type

    Projects

    Status

    Inbox

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions