UNPKG

7.61 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 }
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15Object.defineProperty(exports, "__esModule", { value: true });
16var ts = require("typescript");
17var Lint = require("tslint");
18var tsutils = require("tsutils");
19var JsxAttribute_1 = require("./utils/JsxAttribute");
20var TypeGuard_1 = require("./utils/TypeGuard");
21var ROLE_STRING = 'role';
22var ALT_STRING = 'alt';
23var TITLE_STRING = 'title';
24var IMAGE_FILENAME_REGEX = new RegExp('^.*\\.(jpg|bmp|jpeg|jfif|gif|png|tif|tiff)$', 'i');
25function getFailureStringNoAlt(tagName) {
26 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.";
27}
28exports.getFailureStringNoAlt = getFailureStringNoAlt;
29function getFailureStringEmptyAltAndNotPresentationRole(tagName) {
30 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.";
31}
32exports.getFailureStringEmptyAltAndNotPresentationRole = getFailureStringEmptyAltAndNotPresentationRole;
33function getFailureStringNonEmptyAltAndPresentationRole(tagName) {
34 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'.";
35}
36exports.getFailureStringNonEmptyAltAndPresentationRole = getFailureStringNonEmptyAltAndPresentationRole;
37function getFailureStringEmptyAltAndNotEmptyTitle(tagName) {
38 return "The value of alt attribute in <" + tagName + "> tag is empty and the role is presentation, but the value of its title attribute is not empty. Remove the title attribute.";
39}
40exports.getFailureStringEmptyAltAndNotEmptyTitle = getFailureStringEmptyAltAndNotEmptyTitle;
41function getFailureStringAltIsImageFileName(tagName) {
42 return "The value of alt attribute in <" + tagName + "> tag is an image file name. Give meaningful value to the alt attribute ";
43}
44exports.getFailureStringAltIsImageFileName = getFailureStringAltIsImageFileName;
45var Rule = (function (_super) {
46 __extends(Rule, _super);
47 function Rule() {
48 return _super !== null && _super.apply(this, arguments) || this;
49 }
50 Rule.prototype.apply = function (sourceFile) {
51 return sourceFile.languageVariant === ts.LanguageVariant.JSX
52 ? this.applyWithFunction(sourceFile, walk, this.parseOptions(this.getOptions()))
53 : [];
54 };
55 Rule.prototype.parseOptions = function (options) {
56 var args = options.ruleArguments;
57 return {
58 additionalTagNames: args.length > 0 ? args[0] : [],
59 allowNonEmptyAltWithRolePresentation: args.length > 1 ? args[1].allowNonEmptyAltWithRolePresentation : false
60 };
61 };
62 Rule.metadata = {
63 ruleName: 'react-a11y-img-has-alt',
64 type: 'maintainability',
65 description: 'Enforce that an img element contains the non-empty alt attribute. ' +
66 'For decorative images, using empty alt attribute and role="presentation".',
67 options: 'string[]',
68 rationale: "References:\n <ul>\n <li><a href=\"https://www.w3.org/TR/WCAG10/wai-pageauth.html#tech-text-equivalent\">Web Content Accessibility Guidelines 1.0</a></li>\n <li><a href=\"https://www.w3.org/TR/wai-aria/roles#presentation\">ARIA Presentation Role</a></li>\n <li><a href=\"http://oaa-accessibility.org/wcag20/rule/31\">WCAG Rule 31: If an image has an alt or title attribute, it should not have a presentation role</a></li>\n </ul>",
69 optionsDescription: '',
70 optionExamples: ['true', '[true, ["Image"]]'],
71 typescriptOnly: true,
72 issueClass: 'Non-SDL',
73 issueType: 'Warning',
74 severity: 'Important',
75 level: 'Opportunity for Excellence',
76 group: 'Accessibility'
77 };
78 return Rule;
79}(Lint.Rules.AbstractRule));
80exports.Rule = Rule;
81function walk(ctx) {
82 function checkJsxOpeningElement(node) {
83 var tagName = node.tagName.getText();
84 var targetTagNames = ['img'].concat(ctx.options.additionalTagNames);
85 if (!tagName || targetTagNames.indexOf(tagName) === -1) {
86 return;
87 }
88 var nodeAttributes = JsxAttribute_1.getAllAttributesFromJsxElement(node);
89 if (nodeAttributes !== undefined && nodeAttributes.some(TypeGuard_1.isJsxSpreadAttribute)) {
90 return;
91 }
92 var attributes = JsxAttribute_1.getJsxAttributesFromJsxElement(node);
93 var altAttribute = attributes[ALT_STRING];
94 if (!altAttribute) {
95 ctx.addFailureAt(node.getStart(), node.getWidth(), getFailureStringNoAlt(tagName));
96 }
97 else {
98 var roleAttribute = attributes[ROLE_STRING];
99 var roleAttributeValue = roleAttribute ? JsxAttribute_1.getStringLiteral(roleAttribute) : '';
100 var titleAttribute = attributes[TITLE_STRING];
101 var isPresentationRole = !!String(roleAttributeValue)
102 .toLowerCase()
103 .match(/\bpresentation\b/);
104 var isEmptyAlt = JsxAttribute_1.isEmpty(altAttribute) || JsxAttribute_1.getStringLiteral(altAttribute) === '';
105 var isEmptyTitle = JsxAttribute_1.isEmpty(titleAttribute) || JsxAttribute_1.getStringLiteral(titleAttribute) === '';
106 var isAltImageFileName = !isEmptyAlt && IMAGE_FILENAME_REGEX.test(JsxAttribute_1.getStringLiteral(altAttribute) || '');
107 if (!isEmptyAlt && isPresentationRole && !ctx.options.allowNonEmptyAltWithRolePresentation && !titleAttribute) {
108 ctx.addFailureAt(node.getStart(), node.getWidth(), getFailureStringNonEmptyAltAndPresentationRole(tagName));
109 }
110 else if (isEmptyAlt && !isPresentationRole && !titleAttribute) {
111 ctx.addFailureAt(node.getStart(), node.getWidth(), getFailureStringEmptyAltAndNotPresentationRole(tagName));
112 }
113 else if (isEmptyAlt && titleAttribute && !isEmptyTitle) {
114 ctx.addFailureAt(node.getStart(), node.getWidth(), getFailureStringEmptyAltAndNotEmptyTitle(tagName));
115 }
116 else if (isAltImageFileName) {
117 ctx.addFailureAt(node.getStart(), node.getWidth(), getFailureStringAltIsImageFileName(tagName));
118 }
119 }
120 }
121 function cb(node) {
122 if (tsutils.isJsxElement(node)) {
123 checkJsxOpeningElement(node.openingElement);
124 }
125 else if (tsutils.isJsxSelfClosingElement(node)) {
126 checkJsxOpeningElement(node);
127 }
128 return ts.forEachChild(node, cb);
129 }
130 return ts.forEachChild(ctx.sourceFile, cb);
131}
132//# sourceMappingURL=reactA11yImgHasAltRule.js.map
\No newline at end of file