import * as namespaces from '../../utils/namespaces'; import getStaticAttributeValue from '../../utils/getStaticAttributeValue'; import fuzzymatch from '../utils/fuzzymatch'; import validateEventHandler from './validateEventHandler'; import { Validator } from '../index'; import { Node } from '../../interfaces'; const ariaAttributes = 'activedescendant atomic autocomplete busy checked controls current describedby disabled dropeffect expanded flowto grabbed haspopup hidden invalid label labelledby level live multiline multiselectable orientation owns posinset pressed readonly relevant required selected setsize sort valuemax valuemin valuenow valuetext'.split(' '); const ariaAttributeSet = new Set(ariaAttributes); const ariaRoles = 'alert alertdialog application article banner button checkbox columnheader combobox command complementary composite contentinfo definition dialog directory document form grid gridcell group heading img input landmark link list listbox listitem log main marquee math menu menubar menuitem menuitemcheckbox menuitemradio navigation note option presentation progressbar radio radiogroup range region roletype row rowgroup rowheader scrollbar search section sectionhead select separator slider spinbutton status structure tab tablist tabpanel textbox timer toolbar tooltip tree treegrid treeitem widget window'.split(' '); const ariaRoleSet = new Set(ariaRoles); const invisibleElements = new Set(['meta', 'html', 'script', 'style']); export default function a11y( validator: Validator, node: Node, elementStack: Node[] ) { if (node.type === 'Text') { // accessible-emoji return; } if (node.type !== 'Element') return; const attributeMap = new Map(); node.attributes.forEach((attribute: Node) => { const name = attribute.name.toLowerCase(); // aria-props if (name.startsWith('aria-')) { if (invisibleElements.has(node.name)) { // aria-unsupported-elements validator.warn(`A11y: <${node.name}> should not have aria-* attributes`, attribute.start); } const type = name.slice(5); if (!ariaAttributeSet.has(type)) { const match = fuzzymatch(type, ariaAttributes); let message = `A11y: Unknown aria attribute 'aria-${type}'`; if (match) message += ` (did you mean '${match}'?)`; validator.warn(message, attribute.start); } } // aria-role if (name === 'role') { if (invisibleElements.has(node.name)) { // aria-unsupported-elements validator.warn(`A11y: <${node.name}> should not have role attribute`, attribute.start); } const value = getStaticAttributeValue(node, 'role'); if (value && !ariaRoleSet.has(value)) { const match = fuzzymatch(value, ariaRoles); let message = `A11y: Unknown role '${value}'`; if (match) message += ` (did you mean '${match}'?)`; validator.warn(message, attribute.start); } } // no-access-key if (name === 'accesskey') { validator.warn(`A11y: Avoid using accesskey`, attribute.start); } // no-autofocus if (name === 'autofocus') { validator.warn(`A11y: Avoid using autofocus`, attribute.start); } // scope if (name === 'scope' && node.name !== 'th') { validator.warn(`A11y: The scope attribute should only be used with