source: frontend/node_modules/eslint-plugin-jsx-a11y/lib/rules/img-redundant-alt.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.6 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = void 0;
7var _jsxAstUtils = require("jsx-ast-utils");
8var _arrayIncludes = _interopRequireDefault(require("array-includes"));
9var _stringPrototype = _interopRequireDefault(require("string.prototype.includes"));
10var _safeRegexTest = _interopRequireDefault(require("safe-regex-test"));
11var _schemas = require("../util/schemas");
12var _getElementType = _interopRequireDefault(require("../util/getElementType"));
13var _isHiddenFromScreenReader = _interopRequireDefault(require("../util/isHiddenFromScreenReader"));
14function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
15/**
16 * @fileoverview Enforce img alt attribute does not have the word image, picture, or photo.
17 * @author Ethan Cohen
18 */
19
20// ----------------------------------------------------------------------------
21// Rule Definition
22// ----------------------------------------------------------------------------
23
24var REDUNDANT_WORDS = ['image', 'photo', 'picture'];
25var errorMessage = 'Redundant alt attribute. Screen-readers already announce `img` tags as an image. You don’t need to use the words `image`, `photo,` or `picture` (or any specified custom words) in the alt prop.';
26var schema = (0, _schemas.generateObjSchema)({
27 components: _schemas.arraySchema,
28 words: _schemas.arraySchema
29});
30var isASCII = (0, _safeRegexTest["default"])(/[\x20-\x7F]+/);
31function containsRedundantWord(value, redundantWords) {
32 var lowercaseRedundantWords = redundantWords.map(function (redundantWord) {
33 return redundantWord.toLowerCase();
34 });
35 if (isASCII(value)) {
36 return value.split(/\s+/).some(function (valueWord) {
37 return (0, _arrayIncludes["default"])(lowercaseRedundantWords, valueWord.toLowerCase());
38 });
39 }
40 return lowercaseRedundantWords.some(function (redundantWord) {
41 return (0, _stringPrototype["default"])(value.toLowerCase(), redundantWord);
42 });
43}
44var _default = exports["default"] = {
45 meta: {
46 docs: {
47 url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/img-redundant-alt.md',
48 description: 'Enforce `<img>` alt prop does not contain the word "image", "picture", or "photo".'
49 },
50 schema: [schema]
51 },
52 create: function create(context) {
53 var elementType = (0, _getElementType["default"])(context);
54 return {
55 JSXOpeningElement: function JSXOpeningElement(node) {
56 var options = context.options[0] || {};
57 var componentOptions = options.components || [];
58 var typesToValidate = ['img'].concat(componentOptions);
59 var nodeType = elementType(node);
60
61 // Only check 'label' elements and custom types.
62 if (typesToValidate.indexOf(nodeType) === -1) {
63 return;
64 }
65 var altProp = (0, _jsxAstUtils.getProp)(node.attributes, 'alt');
66 // Return if alt prop is not present.
67 if (altProp === undefined) {
68 return;
69 }
70 var value = (0, _jsxAstUtils.getLiteralPropValue)(altProp);
71 var isVisible = (0, _isHiddenFromScreenReader["default"])(nodeType, node.attributes) === false;
72 var _options$words = options.words,
73 words = _options$words === void 0 ? [] : _options$words;
74 var redundantWords = REDUNDANT_WORDS.concat(words);
75 if (typeof value === 'string' && isVisible) {
76 var hasRedundancy = containsRedundantWord(value, redundantWords);
77 if (hasRedundancy === true) {
78 context.report({
79 node,
80 message: errorMessage
81 });
82 }
83 }
84 }
85 };
86 }
87};
88module.exports = exports.default;
Note: See TracBrowser for help on using the repository browser.