|
| 1 | +import { BoundDirectivePropertyAst, ElementAst } from '@angular/compiler'; |
| 2 | +import { sprintf } from 'sprintf-js'; |
| 3 | +import { IRuleMetadata, RuleFailure, Rules, Utils } from 'tslint/lib'; |
| 4 | +import { SourceFile } from 'typescript/lib/typescript'; |
| 5 | +import { NgWalker } from './angular/ngWalker'; |
| 6 | +import { BasicTemplateAstVisitor } from './angular/templates/basicTemplateAstVisitor'; |
| 7 | +import { mayContainChildComponent } from './util/mayContainChildComponent'; |
| 8 | + |
| 9 | +interface ILabelForOptions { |
| 10 | + labelComponents: string[]; |
| 11 | + labelAttributes: string[]; |
| 12 | + controlComponents: string[]; |
| 13 | +} |
| 14 | +export class Rule extends Rules.AbstractRule { |
| 15 | + static readonly metadata: IRuleMetadata = { |
| 16 | + description: 'Checks if the label has associated for attribute or a form element', |
| 17 | + optionExamples: [[true, { labelComponents: ['app-label'], labelAttributes: ['id'], controlComponents: ['app-input', 'app-select'] }]], |
| 18 | + options: { |
| 19 | + items: { |
| 20 | + type: 'object', |
| 21 | + properties: { |
| 22 | + labelComponents: { |
| 23 | + type: 'array', |
| 24 | + items: { |
| 25 | + type: 'string' |
| 26 | + } |
| 27 | + }, |
| 28 | + labelAttributes: { |
| 29 | + type: 'array', |
| 30 | + items: { |
| 31 | + type: 'string' |
| 32 | + } |
| 33 | + }, |
| 34 | + controlComponents: { |
| 35 | + type: 'array', |
| 36 | + items: { |
| 37 | + type: 'string' |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + }, |
| 42 | + type: 'array' |
| 43 | + }, |
| 44 | + optionsDescription: 'Add custom label, label attribute and controls', |
| 45 | + rationale: Utils.dedent` |
| 46 | + The label tag should either have a for attribute or should have associated control. |
| 47 | + This rule supports two ways, either the label component should explicitly have a for attribute or a control nested inside the label component |
| 48 | + It also supports adding custom control component and custom label component support.`, |
| 49 | + ruleName: 'template-accessibility-label-for', |
| 50 | + type: 'functionality', |
| 51 | + typescriptOnly: true |
| 52 | + }; |
| 53 | + |
| 54 | + static readonly FAILURE_STRING = 'A form label must be associated with a control'; |
| 55 | + static readonly FORM_ELEMENTS = ['input', 'select', 'textarea']; |
| 56 | + |
| 57 | + apply(sourceFile: SourceFile): RuleFailure[] { |
| 58 | + return this.applyWithWalker( |
| 59 | + new NgWalker(sourceFile, this.getOptions(), { |
| 60 | + templateVisitorCtrl: TemplateAccessibilityLabelForVisitor |
| 61 | + }) |
| 62 | + ); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +class TemplateAccessibilityLabelForVisitor extends BasicTemplateAstVisitor { |
| 67 | + visitElement(element: ElementAst, context: any) { |
| 68 | + this.validateElement(element); |
| 69 | + super.visitElement(element, context); |
| 70 | + } |
| 71 | + |
| 72 | + private validateElement(element: ElementAst) { |
| 73 | + let { labelAttributes, labelComponents, controlComponents }: ILabelForOptions = this.getOptions() || {}; |
| 74 | + controlComponents = Rule.FORM_ELEMENTS.concat(controlComponents || []); |
| 75 | + labelComponents = ['label'].concat(labelComponents || []); |
| 76 | + labelAttributes = ['for'].concat(labelAttributes || []); |
| 77 | + |
| 78 | + if (labelComponents.indexOf(element.name) === -1) { |
| 79 | + return; |
| 80 | + } |
| 81 | + const hasForAttr = element.attrs.some(attr => labelAttributes.indexOf(attr.name) !== -1); |
| 82 | + const hasForInput = element.inputs.some(input => { |
| 83 | + return labelAttributes.indexOf(input.name) !== -1; |
| 84 | + }); |
| 85 | + |
| 86 | + const hasImplicitFormElement = controlComponents.some(component => mayContainChildComponent(element, component)); |
| 87 | + |
| 88 | + if (hasForAttr || hasForInput || hasImplicitFormElement) { |
| 89 | + return; |
| 90 | + } |
| 91 | + const { |
| 92 | + sourceSpan: { |
| 93 | + end: { offset: endOffset }, |
| 94 | + start: { offset: startOffset } |
| 95 | + } |
| 96 | + } = element; |
| 97 | + |
| 98 | + this.addFailureFromStartToEnd(startOffset, endOffset, Rule.FAILURE_STRING); |
| 99 | + } |
| 100 | +} |
0 commit comments