|
| 1 | +import { ElementAst } from '@angular/compiler'; |
| 2 | +import { IRuleMetadata, RuleFailure, Rules, Utils } from 'tslint/lib'; |
| 3 | +import { SourceFile } from 'typescript'; |
| 4 | +import { NgWalker } from './angular/ngWalker'; |
| 5 | +import { BasicTemplateAstVisitor } from './angular'; |
| 6 | + |
| 7 | +class TemplateAccessibilityTableScopeVisitor extends BasicTemplateAstVisitor { |
| 8 | + visitElement(ast: ElementAst, context: any) { |
| 9 | + this.validateElement(ast); |
| 10 | + super.visitElement(ast, context); |
| 11 | + } |
| 12 | + |
| 13 | + validateElement(element: ElementAst) { |
| 14 | + if (element.name === 'th') { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + const hasScopeInput = element.inputs.some(input => input.name === 'scope'); |
| 19 | + const hasScopeAttr = element.attrs.some(attr => attr.name === 'scope'); |
| 20 | + if (hasScopeInput || hasScopeAttr) { |
| 21 | + const { |
| 22 | + sourceSpan: { |
| 23 | + end: { offset: endOffset }, |
| 24 | + start: { offset: startOffset } |
| 25 | + } |
| 26 | + } = element; |
| 27 | + this.addFailureFromStartToEnd(startOffset, endOffset, Rule.FAILURE_MESSAGE); |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export class Rule extends Rules.AbstractRule { |
| 33 | + static readonly metadata: IRuleMetadata = { |
| 34 | + description: 'Ensures that scope is not used on any element except th', |
| 35 | + options: null, |
| 36 | + optionsDescription: 'Not configurable.', |
| 37 | + rationale: Utils.dedent` |
| 38 | + The scope attribute makes table navigation much easier for screen reader users, provided that it is used correctly. |
| 39 | + If used incorrectly, it can make table navigation much harder and less efficient. (aXe) |
| 40 | + `, |
| 41 | + ruleName: 'template-accessibility-table-scope', |
| 42 | + type: 'functionality', |
| 43 | + typescriptOnly: true |
| 44 | + }; |
| 45 | + |
| 46 | + static readonly FAILURE_MESSAGE = 'Scope attribute can only be on <th> element'; |
| 47 | + |
| 48 | + apply(sourceFile: SourceFile): RuleFailure[] { |
| 49 | + return this.applyWithWalker( |
| 50 | + new NgWalker(sourceFile, this.getOptions(), { |
| 51 | + templateVisitorCtrl: TemplateAccessibilityTableScopeVisitor |
| 52 | + }) |
| 53 | + ); |
| 54 | + } |
| 55 | +} |
0 commit comments