|
| 1 | +// ARIA attribute type definitions per WAI-ARIA spec |
| 2 | +const ARIA_ATTRIBUTE_TYPES = { |
| 3 | + 'aria-activedescendant': { type: 'id' }, |
| 4 | + 'aria-atomic': { type: 'boolean' }, |
| 5 | + 'aria-autocomplete': { type: 'token', values: ['inline', 'list', 'both', 'none'] }, |
| 6 | + 'aria-busy': { type: 'boolean' }, |
| 7 | + 'aria-checked': { type: 'tristate', allowundefined: true }, |
| 8 | + 'aria-colcount': { type: 'integer' }, |
| 9 | + 'aria-colindex': { type: 'integer' }, |
| 10 | + 'aria-colspan': { type: 'integer' }, |
| 11 | + 'aria-controls': { type: 'idlist' }, |
| 12 | + 'aria-current': { |
| 13 | + type: 'token', |
| 14 | + values: ['page', 'step', 'location', 'date', 'time', 'true', 'false'], |
| 15 | + }, |
| 16 | + 'aria-describedby': { type: 'idlist' }, |
| 17 | + 'aria-details': { type: 'id' }, |
| 18 | + 'aria-disabled': { type: 'boolean' }, |
| 19 | + 'aria-dropeffect': { |
| 20 | + type: 'tokenlist', |
| 21 | + values: ['copy', 'execute', 'link', 'move', 'none', 'popup'], |
| 22 | + }, |
| 23 | + 'aria-errormessage': { type: 'id' }, |
| 24 | + 'aria-expanded': { type: 'boolean', allowundefined: true }, |
| 25 | + 'aria-flowto': { type: 'idlist' }, |
| 26 | + 'aria-grabbed': { type: 'boolean', allowundefined: true }, |
| 27 | + 'aria-haspopup': { |
| 28 | + type: 'token', |
| 29 | + values: ['false', 'true', 'menu', 'listbox', 'tree', 'grid', 'dialog'], |
| 30 | + }, |
| 31 | + 'aria-hidden': { type: 'boolean', allowundefined: true }, |
| 32 | + 'aria-invalid': { type: 'token', values: ['grammar', 'false', 'spelling', 'true'] }, |
| 33 | + 'aria-keyshortcuts': { type: 'string' }, |
| 34 | + 'aria-label': { type: 'string' }, |
| 35 | + 'aria-labelledby': { type: 'idlist' }, |
| 36 | + 'aria-level': { type: 'integer' }, |
| 37 | + 'aria-live': { type: 'token', values: ['assertive', 'off', 'polite'] }, |
| 38 | + 'aria-modal': { type: 'boolean' }, |
| 39 | + 'aria-multiline': { type: 'boolean' }, |
| 40 | + 'aria-multiselectable': { type: 'boolean' }, |
| 41 | + 'aria-orientation': { type: 'token', values: ['horizontal', 'vertical', 'undefined'] }, |
| 42 | + 'aria-owns': { type: 'idlist' }, |
| 43 | + 'aria-placeholder': { type: 'string' }, |
| 44 | + 'aria-posinset': { type: 'integer' }, |
| 45 | + 'aria-pressed': { type: 'tristate', allowundefined: true }, |
| 46 | + 'aria-readonly': { type: 'boolean' }, |
| 47 | + 'aria-relevant': { type: 'tokenlist', values: ['additions', 'all', 'removals', 'text'] }, |
| 48 | + 'aria-required': { type: 'boolean' }, |
| 49 | + 'aria-roledescription': { type: 'string' }, |
| 50 | + 'aria-rowcount': { type: 'integer' }, |
| 51 | + 'aria-rowindex': { type: 'integer' }, |
| 52 | + 'aria-rowspan': { type: 'integer' }, |
| 53 | + 'aria-selected': { type: 'boolean', allowundefined: true }, |
| 54 | + 'aria-setsize': { type: 'integer' }, |
| 55 | + 'aria-sort': { type: 'token', values: ['ascending', 'descending', 'none', 'other'] }, |
| 56 | + 'aria-valuemax': { type: 'number' }, |
| 57 | + 'aria-valuemin': { type: 'number' }, |
| 58 | + 'aria-valuenow': { type: 'number' }, |
| 59 | + 'aria-valuetext': { type: 'string' }, |
| 60 | +}; |
| 61 | + |
| 62 | +const validAriaAttributes = new Set(Object.keys(ARIA_ATTRIBUTE_TYPES)); |
| 63 | + |
| 64 | +function isBoolean(value) { |
| 65 | + return value === 'true' || value === 'false'; |
| 66 | +} |
| 67 | + |
| 68 | +function isNumeric(value) { |
| 69 | + if (typeof value !== 'string' || value === '') { |
| 70 | + return false; |
| 71 | + } |
| 72 | + return !Number.isNaN(Number(value)); |
| 73 | +} |
| 74 | + |
| 75 | +function isValidAriaValue(attrName, value) { |
| 76 | + const attrDef = ARIA_ATTRIBUTE_TYPES[attrName]; |
| 77 | + if (!attrDef) { |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + if (value === 'undefined') { |
| 82 | + return Boolean(attrDef.allowundefined); |
| 83 | + } |
| 84 | + |
| 85 | + switch (attrDef.type) { |
| 86 | + case 'boolean': { |
| 87 | + return isBoolean(value); |
| 88 | + } |
| 89 | + case 'tristate': { |
| 90 | + return isBoolean(value) || value === 'mixed'; |
| 91 | + } |
| 92 | + case 'string': { |
| 93 | + return typeof value === 'string'; |
| 94 | + } |
| 95 | + case 'id': { |
| 96 | + return typeof value === 'string' && !isBoolean(value); |
| 97 | + } |
| 98 | + case 'idlist': { |
| 99 | + return ( |
| 100 | + typeof value === 'string' && |
| 101 | + value.split(' ').every((token) => token.length > 0 && !isBoolean(token)) |
| 102 | + ); |
| 103 | + } |
| 104 | + case 'integer': { |
| 105 | + return /^-?\d+$/.test(value); |
| 106 | + } |
| 107 | + case 'number': { |
| 108 | + return isNumeric(value) && !isBoolean(value); |
| 109 | + } |
| 110 | + case 'token': { |
| 111 | + return attrDef.values.includes(value); |
| 112 | + } |
| 113 | + case 'tokenlist': { |
| 114 | + return value.split(' ').every((token) => attrDef.values.includes(token.toLowerCase())); |
| 115 | + } |
| 116 | + default: { |
| 117 | + return true; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 123 | +module.exports = { |
| 124 | + meta: { |
| 125 | + type: 'problem', |
| 126 | + docs: { |
| 127 | + description: 'disallow invalid aria-* attributes', |
| 128 | + category: 'Accessibility', |
| 129 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-invalid-aria-attributes.md', |
| 130 | + }, |
| 131 | + schema: [], |
| 132 | + messages: { |
| 133 | + noInvalidAriaAttribute: 'Invalid ARIA attribute: {{attribute}}', |
| 134 | + invalidAriaAttributeValue: 'Invalid value for ARIA attribute {{attribute}}.', |
| 135 | + }, |
| 136 | + strictGjs: true, |
| 137 | + strictGts: true, |
| 138 | + }, |
| 139 | + |
| 140 | + create(context) { |
| 141 | + return { |
| 142 | + GlimmerAttrNode(node) { |
| 143 | + if (!node.name.startsWith('aria-')) { |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + // Check for unknown ARIA attribute |
| 148 | + if (!validAriaAttributes.has(node.name)) { |
| 149 | + context.report({ |
| 150 | + node, |
| 151 | + messageId: 'noInvalidAriaAttribute', |
| 152 | + data: { attribute: node.name }, |
| 153 | + }); |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + // Skip value validation for dynamic values (MustacheStatement, ConcatStatement) |
| 158 | + if ( |
| 159 | + !node.value || |
| 160 | + node.value.type === 'GlimmerMustacheStatement' || |
| 161 | + node.value.type === 'GlimmerConcatStatement' |
| 162 | + ) { |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + // Validate value for text node values |
| 167 | + if (node.value.type === 'GlimmerTextNode') { |
| 168 | + const value = node.value.chars; |
| 169 | + if (!isValidAriaValue(node.name, value)) { |
| 170 | + context.report({ |
| 171 | + node, |
| 172 | + messageId: 'invalidAriaAttributeValue', |
| 173 | + data: { attribute: node.name }, |
| 174 | + }); |
| 175 | + } |
| 176 | + } |
| 177 | + }, |
| 178 | + }; |
| 179 | + }, |
| 180 | +}; |
0 commit comments