|
| 1 | +import { ElementAst, AttrAst, BoundElementPropertyAst, TextAst } from '@angular/compiler'; |
| 2 | +import { sprintf } from 'sprintf-js'; |
| 3 | +import { IRuleMetadata, RuleFailure, Rules } from 'tslint/lib'; |
| 4 | +import { SourceFile } from 'typescript/lib/typescript'; |
| 5 | +import { NgWalker } from './angular/ngWalker'; |
| 6 | +import { BasicTemplateAstVisitor } from './angular/templates/basicTemplateAstVisitor'; |
| 7 | + |
| 8 | +export class Rule extends Rules.AbstractRule { |
| 9 | + static readonly metadata: IRuleMetadata = { |
| 10 | + description: 'Enforces alternate text for elements which require the alt, aria-label, aria-labelledby attributes', |
| 11 | + options: null, |
| 12 | + optionsDescription: 'Not configurable.', |
| 13 | + rationale: 'Alternate text lets screen readers provide more information to end users.', |
| 14 | + ruleName: 'template-accessibility-alt-text', |
| 15 | + type: 'functionality', |
| 16 | + typescriptOnly: true |
| 17 | + }; |
| 18 | + |
| 19 | + static readonly FAILURE_STRING = '%s element must have a text alternative.'; |
| 20 | + static readonly DEFAULT_ELEMENTS = ['img', 'object', 'area', 'input[type="image"]']; |
| 21 | + |
| 22 | + apply(sourceFile: SourceFile): RuleFailure[] { |
| 23 | + return this.applyWithWalker( |
| 24 | + new NgWalker(sourceFile, this.getOptions(), { |
| 25 | + templateVisitorCtrl: TemplateAccessibilityAltTextVisitor |
| 26 | + }) |
| 27 | + ); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +export const getFailureMessage = (name: string): string => { |
| 32 | + return sprintf(Rule.FAILURE_STRING, name); |
| 33 | +}; |
| 34 | + |
| 35 | +class TemplateAccessibilityAltTextVisitor extends BasicTemplateAstVisitor { |
| 36 | + visitElement(ast: ElementAst, context: any) { |
| 37 | + this.validateElement(ast); |
| 38 | + super.visitElement(ast, context); |
| 39 | + } |
| 40 | + |
| 41 | + validateElement(element: ElementAst) { |
| 42 | + const typesToValidate = Rule.DEFAULT_ELEMENTS.map(type => { |
| 43 | + if (type === 'input[type="image"]') { |
| 44 | + return 'input'; |
| 45 | + } |
| 46 | + return type; |
| 47 | + }); |
| 48 | + if (typesToValidate.indexOf(element.name) === -1) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const isValid = this[element.name](element); |
| 53 | + if (isValid) { |
| 54 | + return; |
| 55 | + } |
| 56 | + const { |
| 57 | + sourceSpan: { |
| 58 | + end: { offset: endOffset }, |
| 59 | + start: { offset: startOffset } |
| 60 | + } |
| 61 | + } = element; |
| 62 | + this.addFailureFromStartToEnd(startOffset, endOffset, getFailureMessage(element.name)); |
| 63 | + } |
| 64 | + |
| 65 | + img(element: ElementAst) { |
| 66 | + const hasAltAttr = element.attrs.some(attr => attr.name === 'alt'); |
| 67 | + const hasAltInput = element.inputs.some(input => input.name === 'alt'); |
| 68 | + return hasAltAttr || hasAltInput; |
| 69 | + } |
| 70 | + |
| 71 | + object(element: ElementAst) { |
| 72 | + let elementHasText: string = ''; |
| 73 | + const hasLabelAttr = element.attrs.some(attr => attr.name === 'aria-label' || attr.name === 'aria-labelledby'); |
| 74 | + const hasLabelInput = element.inputs.some(input => input.name === 'aria-label' || input.name === 'aria-labelledby'); |
| 75 | + const hasTitleAttr = element.attrs.some(attr => attr.name === 'title'); |
| 76 | + const hasTitleInput = element.inputs.some(input => input.name === 'title'); |
| 77 | + if (element.children.length) { |
| 78 | + elementHasText = (<TextAst>element.children[0]).value; |
| 79 | + } |
| 80 | + return hasLabelAttr || hasLabelInput || hasTitleAttr || hasTitleInput || elementHasText; |
| 81 | + } |
| 82 | + |
| 83 | + area(element: ElementAst) { |
| 84 | + const hasLabelAttr = element.attrs.some(attr => attr.name === 'aria-label' || attr.name === 'aria-labelledby'); |
| 85 | + const hasLabelInput = element.inputs.some(input => input.name === 'aria-label' || input.name === 'aria-labelledby'); |
| 86 | + const hasAltAttr = element.attrs.some(attr => attr.name === 'alt'); |
| 87 | + const hasAltInput = element.inputs.some(input => input.name === 'alt'); |
| 88 | + console.log(element); |
| 89 | + return hasAltAttr || hasAltInput || hasLabelAttr || hasLabelInput; |
| 90 | + } |
| 91 | + |
| 92 | + input(element: ElementAst) { |
| 93 | + const attrType: AttrAst = element.attrs.find(attr => attr.name === 'type') || <AttrAst>{}; |
| 94 | + const inputType: BoundElementPropertyAst = element.inputs.find(input => input.name === 'type') || <BoundElementPropertyAst>{}; |
| 95 | + const type = attrType.value || inputType.value; |
| 96 | + if (type !== 'image') { |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + return this.area(element); |
| 101 | + } |
| 102 | +} |
0 commit comments