|
| 1 | +const REDUNDANT_WORDS = ['image', 'photo', 'picture', 'logo', 'spacer']; |
| 2 | + |
| 3 | +function findAttr(node, name) { |
| 4 | + return node.attributes?.find((a) => a.name === name); |
| 5 | +} |
| 6 | + |
| 7 | +function hasAttr(node, name) { |
| 8 | + return node.attributes?.some((a) => a.name === name); |
| 9 | +} |
| 10 | + |
| 11 | +function hasAnyAttr(node, names) { |
| 12 | + return names.some((name) => hasAttr(node, name)); |
| 13 | +} |
| 14 | + |
| 15 | +function getTextValue(attr) { |
| 16 | + if (!attr?.value) { |
| 17 | + return undefined; |
| 18 | + } |
| 19 | + if (attr.value.type === 'GlimmerTextNode') { |
| 20 | + return attr.value.chars; |
| 21 | + } |
| 22 | + return undefined; |
| 23 | +} |
| 24 | + |
| 25 | +function getNormalizedAltText(altAttr) { |
| 26 | + if (!altAttr?.value) { |
| 27 | + return null; |
| 28 | + } |
| 29 | + if (altAttr.value.type === 'GlimmerTextNode') { |
| 30 | + return altAttr.value.chars.trim().toLowerCase(); |
| 31 | + } |
| 32 | + if (altAttr.value.type === 'GlimmerConcatStatement') { |
| 33 | + const parts = (altAttr.value.parts || []) |
| 34 | + .filter((p) => p.type === 'GlimmerTextNode') |
| 35 | + .map((p) => p.chars) |
| 36 | + .join(' ') |
| 37 | + .trim() |
| 38 | + .toLowerCase(); |
| 39 | + return parts === '' ? null : parts; |
| 40 | + } |
| 41 | + return null; |
| 42 | +} |
| 43 | + |
| 44 | +function hasChildren(node) { |
| 45 | + return ( |
| 46 | + node.children && |
| 47 | + node.children.some((child) => { |
| 48 | + if (child.type === 'GlimmerTextNode') { |
| 49 | + return child.chars.trim().length > 0; |
| 50 | + } |
| 51 | + return true; |
| 52 | + }) |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 57 | +module.exports = { |
| 58 | + meta: { |
| 59 | + type: 'problem', |
| 60 | + docs: { |
| 61 | + description: 'require valid alt text for images and other elements', |
| 62 | + category: 'Accessibility', |
| 63 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-require-valid-alt-text.md', |
| 64 | + templateMode: 'both', |
| 65 | + }, |
| 66 | + schema: [], |
| 67 | + messages: { |
| 68 | + imgMissing: 'All `<img>` tags must have an alt attribute', |
| 69 | + imgRedundant: |
| 70 | + 'Invalid alt attribute. Words such as `image`, `photo,` or `picture` are already announced by screen readers.', |
| 71 | + imgAltEqualsSrc: 'The alt text must not be the same as the image source', |
| 72 | + imgNumericAlt: 'A number is not valid alt text', |
| 73 | + imgRolePresentation: |
| 74 | + 'The `alt` attribute should be empty if `<img>` has `role` of `none` or `presentation`', |
| 75 | + inputImage: |
| 76 | + 'All <input> elements with type="image" must have a text alternative through the `alt`, `aria-label`, or `aria-labelledby` attribute.', |
| 77 | + objectMissing: |
| 78 | + 'Embedded <object> elements must have alternative text by providing inner text, aria-label or aria-labelledby attributes.', |
| 79 | + areaMissing: |
| 80 | + 'Each area of an image map must have a text alternative through the `alt`, `aria-label`, or `aria-labelledby` attribute.', |
| 81 | + }, |
| 82 | + originallyFrom: { |
| 83 | + name: 'ember-template-lint', |
| 84 | + rule: 'lib/rules/require-valid-alt-text.js', |
| 85 | + docs: 'docs/rule/require-valid-alt-text.md', |
| 86 | + tests: 'test/unit/rules/require-valid-alt-text-test.js', |
| 87 | + }, |
| 88 | + }, |
| 89 | + create(context) { |
| 90 | + return { |
| 91 | + // eslint-disable-next-line complexity |
| 92 | + GlimmerElementNode(node) { |
| 93 | + // Skip hidden elements |
| 94 | + if (hasAttr(node, 'hidden')) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + const ariaHidden = findAttr(node, 'aria-hidden'); |
| 99 | + if (ariaHidden) { |
| 100 | + const val = getTextValue(ariaHidden); |
| 101 | + if (val === 'true') { |
| 102 | + return; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Skip elements with ...attributes (splattributes) |
| 107 | + if (hasAttr(node, '...attributes')) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + const tag = node.tag; |
| 112 | + |
| 113 | + switch (tag) { |
| 114 | + case 'img': { |
| 115 | + const altAttr = findAttr(node, 'alt'); |
| 116 | + const roleAttr = findAttr(node, 'role'); |
| 117 | + const srcAttr = findAttr(node, 'src'); |
| 118 | + |
| 119 | + // Check role=none/presentation with non-empty alt |
| 120 | + if (altAttr && roleAttr) { |
| 121 | + const roleValue = getTextValue(roleAttr); |
| 122 | + const altValue = getTextValue(altAttr); |
| 123 | + if ( |
| 124 | + roleValue && |
| 125 | + ['none', 'presentation'].includes(roleValue.trim().toLowerCase()) && |
| 126 | + altValue !== '' |
| 127 | + ) { |
| 128 | + context.report({ node, messageId: 'imgRolePresentation' }); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if (!altAttr) { |
| 133 | + context.report({ node, messageId: 'imgMissing' }); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + // Check alt === src |
| 138 | + const altValue = getTextValue(altAttr); |
| 139 | + const srcValue = getTextValue(srcAttr); |
| 140 | + if (altValue !== undefined && srcValue !== undefined && altValue === srcValue) { |
| 141 | + context.report({ node, messageId: 'imgAltEqualsSrc' }); |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + // Check numeric-only alt and redundant words |
| 146 | + const normalizedAlt = getNormalizedAltText(altAttr); |
| 147 | + if (normalizedAlt !== null) { |
| 148 | + if (/^\d+$/.test(normalizedAlt)) { |
| 149 | + context.report({ node, messageId: 'imgNumericAlt' }); |
| 150 | + } else { |
| 151 | + const words = normalizedAlt.split(' '); |
| 152 | + const hasRedundant = REDUNDANT_WORDS.some((w) => words.includes(w)); |
| 153 | + if (hasRedundant) { |
| 154 | + context.report({ node, messageId: 'imgRedundant' }); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + break; |
| 160 | + } |
| 161 | + case 'input': { |
| 162 | + // Only check input type="image" |
| 163 | + const typeAttr = findAttr(node, 'type'); |
| 164 | + const typeVal = getTextValue(typeAttr); |
| 165 | + if (typeVal !== 'image') { |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + if (!hasAnyAttr(node, ['aria-label', 'aria-labelledby', 'alt'])) { |
| 170 | + context.report({ node, messageId: 'inputImage' }); |
| 171 | + } |
| 172 | + |
| 173 | + break; |
| 174 | + } |
| 175 | + case 'object': { |
| 176 | + const roleAttr = findAttr(node, 'role'); |
| 177 | + const roleValue = getTextValue(roleAttr); |
| 178 | + |
| 179 | + if ( |
| 180 | + hasAnyAttr(node, ['aria-label', 'aria-labelledby', 'title']) || |
| 181 | + hasChildren(node) || |
| 182 | + (roleValue && ['presentation', 'none'].includes(roleValue)) |
| 183 | + ) { |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + context.report({ node, messageId: 'objectMissing' }); |
| 188 | + |
| 189 | + break; |
| 190 | + } |
| 191 | + case 'area': { |
| 192 | + if (!hasAnyAttr(node, ['aria-label', 'aria-labelledby', 'alt'])) { |
| 193 | + context.report({ node, messageId: 'areaMissing' }); |
| 194 | + } |
| 195 | + |
| 196 | + break; |
| 197 | + } |
| 198 | + // No default |
| 199 | + } |
| 200 | + }, |
| 201 | + }; |
| 202 | + }, |
| 203 | +}; |
0 commit comments