Skip to content

Ensure unique HTML IDs for dropdown items #642

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

Open
peterserfozo opened this issue Feb 10, 2025 · 2 comments · May be fixed by #666
Open

Ensure unique HTML IDs for dropdown items #642

peterserfozo opened this issue Feb 10, 2025 · 2 comments · May be fixed by #666
Labels
bug Something isn't working

Comments

@peterserfozo
Copy link

peterserfozo commented Feb 10, 2025

Currently, dropdown IDs in Twig templates are generated using:

{% set _id = id|default('dropdown-' ~ random(1000)) %}

in https://github.com./openeuropa/bootstrap-component-library/blob/development/src/components/bcl-dropdown/dropdown.html.twig#L38

random(1000) in Twig does not ensure unique IDs, which can lead to duplicate IDs in the rendered HTML. There are multiple ways to fix it, for example, using crypto.randomUUID() as a suffix or using a 3rd party solution, e.g. https://www.npmjs.com/package/shortid. Note that, in Drupal there is a new Twig filter available since D10 for this: https://www.drupal.org/node/3363551
Another option is increasing the the argument for the random() function or concatenate more:

{% set _id = id|default('dropdown-' ~ random(1000000)) %}
{% set _id = id|default('dropdown-' ~ random(1000) ~ '-' ~ random(1000)) %}

Image

There are some other templates where this approach is used to generate unique IDs:

Image

This issue was detected during an accessibility audit, here is the explanation:

  • Document has multiple elements referenced with ARIA with the same id attribute: dropdown-988
  • Labels and ARIA relationship attributes, like aria-controls, aria-labelledby, and aria-owns, rely on unique id values to identify specific UI components. If multiple elements on a web page have the same id value, assistive technologies are likely to only recognize the first element and ignore the rest.
@escuriola escuriola added the bug Something isn't working label Feb 17, 2025
@escuriola
Copy link

Hello,

Thanks for your comments and recommendations.

This will be checked and answered shortly.

@tibi2303
Copy link
Collaborator

tibi2303 commented Apr 16, 2025

I increased the number generation from 1000 to 10000.... the probability from my point of view would be to low to happen so i think the solution would be okay... Right now bcl is not linked to drupal in any way and we cant access filters from there...
The probability of selecting exactly two specific numbers from a set of 7 random numbers (example menu in header), each chosen from the range 0 to 9999, is approximately 4.2 × 10⁻⁷. This is a very small probability, reflecting the low likelihood of this event occurring.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants