Fix the click target audit for a cookie banner

Review the AI's codeEasy12 min

Your task

An AI generated a React cookie banner whose Accept and Dismiss controls are div elements with click handlers. It looks fine, but a keyboard or screen reader user cannot use it. Your team now audits rendered elements for the three things a real <button> gives for free: keyboard focus, a role a screen reader announces, and an accessible name.

An assistant wrote audit_click_targets(elements). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • elements: one descriptor per rendered element, written tag|role|onclick|tabindex|name. That is the HTML tag, its ARIA role, yes when it has a click handler, its tabindex, and its accessible name (visible text or aria-label). Split on |. A field can be empty, a missing field counts as empty, and fields after the fifth are ignored.
  • Return the findings as strings "<index>:<code>", where the index is the element's position in the list, from 0.

Rules the tests check:

  • Lowercase the tag, role and onclick fields before checking them. No field is trimmed.
  • Audit only elements whose onclick is yes. Other elements have no findings.
  • The native controls are the tags button, a, input, select, textarea and summary.
  • no-keyboard-focus: the element is not native, and its tabindex is empty or -1. An ARIA role does not make an element focusable, and -1 keeps it out of the Tab order.
  • no-semantic-role: the element is not native and its role is empty.
  • no-accessible-name: the name is empty. This applies to native controls too.
  • List findings in element order. Within one element, use the order of the three codes above.

Examples

audit_click_targets(elements: list[str]) -> list[str]
Example 1
Inputaudit_click_targets(["div||no||We use cookies to improve your experience.", "div||yes||Accept", "div||yes||"])
Output["1:no-keyboard-focus", "1:no-semantic-role", "2:no-keyboard-focus", "2:no-semantic-role", "2:no-accessible-name"]
Example 2
Inputaudit_click_targets(["button||yes||Accept", "button||yes||Dismiss"])
Output[]
Example 3
Inputaudit_click_targets(["div|button|yes|0|"])
Output["0:no-accessible-name"]