| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports["default"] = void 0;
|
|---|
| 7 | var _jsxAstUtils = require("jsx-ast-utils");
|
|---|
| 8 | var _arrayIncludes = _interopRequireDefault(require("array-includes"));
|
|---|
| 9 | var _stringPrototype = _interopRequireDefault(require("string.prototype.includes"));
|
|---|
| 10 | var _safeRegexTest = _interopRequireDefault(require("safe-regex-test"));
|
|---|
| 11 | var _schemas = require("../util/schemas");
|
|---|
| 12 | var _getElementType = _interopRequireDefault(require("../util/getElementType"));
|
|---|
| 13 | var _isHiddenFromScreenReader = _interopRequireDefault(require("../util/isHiddenFromScreenReader"));
|
|---|
| 14 | function _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 |
|
|---|
| 24 | var REDUNDANT_WORDS = ['image', 'photo', 'picture'];
|
|---|
| 25 | var 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.';
|
|---|
| 26 | var schema = (0, _schemas.generateObjSchema)({
|
|---|
| 27 | components: _schemas.arraySchema,
|
|---|
| 28 | words: _schemas.arraySchema
|
|---|
| 29 | });
|
|---|
| 30 | var isASCII = (0, _safeRegexTest["default"])(/[\x20-\x7F]+/);
|
|---|
| 31 | function 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 | }
|
|---|
| 44 | var _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 | };
|
|---|
| 88 | module.exports = exports.default; |
|---|