Skip to content

Conversation

kevin-brown
Copy link
Member

This pull request includes a

  • Bug fix
  • New feature
  • Translation

The following changes were made

  • Change internal data ID generation to be more unique

If this is related to an existing ticket, include a link to it as well.
Fixes #5810

This changes the internal generation of the ID used to uniquely
identify an element to be more resistant to collisions. Previously
it was an incremented integer or the exact ID used on the element,
which for most cases was unique enough. Unfortunately if any elements
used by Select2 also had an ID that was a low number, it was almost
guaranteed to collide with an internal ID that Select2 generated.

The internal generation now relies on a prefix when the ID already
exists, similar to what we do with core IDs. It also uses a set of
4 random characters along with the incrementing ID to reduce the
chance of collisions even further.

It is definitely still possible for a collision to occur, especially
if two elements exist but one has a prefix of `data-` already, but
the chances have been greatly reduced. We'll accept it for now.
@kevin-brown kevin-brown merged commit 00aa2a5 into develop Apr 26, 2020
@kevin-brown kevin-brown deleted the GH-5810 branch April 26, 2020 00:28
anttikuuskoski pushed a commit to anttikuuskoski/select2 that referenced this pull request Mar 29, 2022
This changes the internal generation of the ID used to uniquely
identify an element to be more resistant to collisions. Previously
it was an incremented integer or the exact ID used on the element,
which for most cases was unique enough. Unfortunately if any elements
used by Select2 also had an ID that was a low number, it was almost
guaranteed to collide with an internal ID that Select2 generated.

The internal generation now relies on a prefix when the ID already
exists, similar to what we do with core IDs. It also uses a set of
4 random characters along with the incrementing ID to reduce the
chance of collisions even further.

It is definitely still possible for a collision to occur, especially
if two elements exist but one has a prefix of `data-` already, but
the chances have been greatly reduced. We'll accept it for now.
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.

Error caused by numeric ID's
1 participant