Fix the click target audit for a cookie banner
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, writtentag|role|onclick|tabindex|name. That is the HTML tag, its ARIA role,yeswhen it has a click handler, itstabindex, and its accessible name (visible text oraria-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,textareaandsummary. 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-1keeps 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
Input
audit_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
Input
audit_click_targets(["button||yes||Accept", "button||yes||Dismiss"])Output
[]Example 3
Input
audit_click_targets(["div|button|yes|0|"])Output
["0:no-accessible-name"]