1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
6 | return function (d, b) {
|
7 | extendStatics(d, b);
|
8 | function __() { this.constructor = d; }
|
9 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
10 | };
|
11 | })();
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | var ts = require("typescript");
|
14 | var Lint = require("tslint");
|
15 | var JsxAttribute_1 = require("./utils/JsxAttribute");
|
16 | var TypeGuard_1 = require("./utils/TypeGuard");
|
17 | var ROLE_STRING = 'role';
|
18 | var ALT_STRING = 'alt';
|
19 | function getFailureStringNoAlt(tagName) {
|
20 | return "<" + tagName + "> elements must have an non-empty alt attribute or use empty alt attribute as well as role='presentation' for decorative/presentational images. A reference for the presentation role can be found at https://www.w3.org/TR/wai-aria/roles#presentation.";
|
21 | }
|
22 | exports.getFailureStringNoAlt = getFailureStringNoAlt;
|
23 | function getFailureStringEmptyAltAndNotPresentationRole(tagName) {
|
24 | return "The value of alt attribute in <" + tagName + "> tag is empty and role value is not presentation. Add more details in alt attribute or specify role attribute to equal 'presentation' when 'alt' attribute is empty.";
|
25 | }
|
26 | exports.getFailureStringEmptyAltAndNotPresentationRole = getFailureStringEmptyAltAndNotPresentationRole;
|
27 | function getFailureStringNonEmptyAltAndPresentationRole(tagName) {
|
28 | return "The value of alt attribute in <" + tagName + "> tag is non-empty and role value is presentation. Remove role='presentation' or specify 'alt' attribute to be empty when role attributes equals 'presentation'.";
|
29 | }
|
30 | exports.getFailureStringNonEmptyAltAndPresentationRole = getFailureStringNonEmptyAltAndPresentationRole;
|
31 | var Rule = (function (_super) {
|
32 | __extends(Rule, _super);
|
33 | function Rule() {
|
34 | return _super !== null && _super.apply(this, arguments) || this;
|
35 | }
|
36 | Rule.prototype.apply = function (sourceFile) {
|
37 | return sourceFile.languageVariant === ts.LanguageVariant.JSX
|
38 | ? this.applyWithWalker(new ImgHasAltWalker(sourceFile, this.getOptions()))
|
39 | : [];
|
40 | };
|
41 | Rule.metadata = {
|
42 | ruleName: 'react-a11y-img-has-alt',
|
43 | type: 'maintainability',
|
44 | description: 'Enforce that an img element contains the non-empty alt attribute. ' +
|
45 | 'For decorative images, using empty alt attribute and role="presentation".',
|
46 | options: 'string[]',
|
47 | optionsDescription: '',
|
48 | optionExamples: ['true', '[true, ["Image"]]'],
|
49 | typescriptOnly: true,
|
50 | issueClass: 'Non-SDL',
|
51 | issueType: 'Warning',
|
52 | severity: 'Important',
|
53 | level: 'Opportunity for Excellence',
|
54 | group: 'Accessibility'
|
55 | };
|
56 | return Rule;
|
57 | }(Lint.Rules.AbstractRule));
|
58 | exports.Rule = Rule;
|
59 | var ImgHasAltWalker = (function (_super) {
|
60 | __extends(ImgHasAltWalker, _super);
|
61 | function ImgHasAltWalker() {
|
62 | return _super !== null && _super.apply(this, arguments) || this;
|
63 | }
|
64 | ImgHasAltWalker.prototype.visitJsxElement = function (node) {
|
65 | this.checkJsxOpeningElement(node.openingElement);
|
66 | _super.prototype.visitJsxElement.call(this, node);
|
67 | };
|
68 | ImgHasAltWalker.prototype.visitJsxSelfClosingElement = function (node) {
|
69 | this.checkJsxOpeningElement(node);
|
70 | _super.prototype.visitJsxSelfClosingElement.call(this, node);
|
71 | };
|
72 | ImgHasAltWalker.prototype.checkJsxOpeningElement = function (node) {
|
73 | var tagName = node.tagName.getText();
|
74 | var options = this.getOptions();
|
75 | var additionalTagNames = options.length > 0 ? options[0] : [];
|
76 | var targetTagNames = ['img'].concat(additionalTagNames);
|
77 | if (!tagName || targetTagNames.indexOf(tagName) === -1) {
|
78 | return;
|
79 | }
|
80 | if (JsxAttribute_1.getAllAttributesFromJsxElement(node).some(TypeGuard_1.isJsxSpreadAttribute)) {
|
81 | return;
|
82 | }
|
83 | var attributes = JsxAttribute_1.getJsxAttributesFromJsxElement(node);
|
84 | var altAttribute = attributes[ALT_STRING];
|
85 | if (!altAttribute) {
|
86 | this.addFailureAt(node.getStart(), node.getWidth(), getFailureStringNoAlt(tagName));
|
87 | }
|
88 | else {
|
89 | var roleAttribute = attributes[ROLE_STRING];
|
90 | var roleAttributeValue = roleAttribute ? JsxAttribute_1.getStringLiteral(roleAttribute) : '';
|
91 | var isPresentationRole = !!roleAttributeValue.toLowerCase().match(/\bpresentation\b/);
|
92 | var isEmptyAlt = JsxAttribute_1.isEmpty(altAttribute) || JsxAttribute_1.getStringLiteral(altAttribute) === '';
|
93 | var allowNonEmptyAltWithRolePresentation = options.length > 1
|
94 | ? options[1].allowNonEmptyAltWithRolePresentation
|
95 | : false;
|
96 | if (!isEmptyAlt && isPresentationRole && !allowNonEmptyAltWithRolePresentation) {
|
97 | this.addFailureAt(node.getStart(), node.getWidth(), getFailureStringNonEmptyAltAndPresentationRole(tagName));
|
98 | }
|
99 | else if (isEmptyAlt && !isPresentationRole) {
|
100 | this.addFailureAt(node.getStart(), node.getWidth(), getFailureStringEmptyAltAndNotPresentationRole(tagName));
|
101 | }
|
102 | }
|
103 | };
|
104 | return ImgHasAltWalker;
|
105 | }(Lint.RuleWalker));
|
106 |
|
\ | No newline at end of file |