| 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 _arrayPrototype = _interopRequireDefault(require("array.prototype.flatmap"));
|
|---|
| 9 | var _schemas = require("../util/schemas");
|
|---|
| 10 | var _getElementType = _interopRequireDefault(require("../util/getElementType"));
|
|---|
| 11 | function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|---|
| 12 | /**
|
|---|
| 13 | * @fileoverview <audio> and <video> elements must have a <track> for captions.
|
|---|
| 14 | * @author Ethan Cohen
|
|---|
| 15 | *
|
|---|
| 16 | */
|
|---|
| 17 |
|
|---|
| 18 | // ----------------------------------------------------------------------------
|
|---|
| 19 | // Rule Definition
|
|---|
| 20 | // ----------------------------------------------------------------------------
|
|---|
| 21 |
|
|---|
| 22 | var errorMessage = 'Media elements such as <audio> and <video> must have a <track> for captions.';
|
|---|
| 23 | var MEDIA_TYPES = ['audio', 'video'];
|
|---|
| 24 | var schema = (0, _schemas.generateObjSchema)({
|
|---|
| 25 | audio: _schemas.arraySchema,
|
|---|
| 26 | video: _schemas.arraySchema,
|
|---|
| 27 | track: _schemas.arraySchema
|
|---|
| 28 | });
|
|---|
| 29 | var isMediaType = function isMediaType(context, type) {
|
|---|
| 30 | var options = context.options[0] || {};
|
|---|
| 31 | return MEDIA_TYPES.concat((0, _arrayPrototype["default"])(MEDIA_TYPES, function (mediaType) {
|
|---|
| 32 | return options[mediaType];
|
|---|
| 33 | })).some(function (typeToCheck) {
|
|---|
| 34 | return typeToCheck === type;
|
|---|
| 35 | });
|
|---|
| 36 | };
|
|---|
| 37 | var isTrackType = function isTrackType(context, type) {
|
|---|
| 38 | var options = context.options[0] || {};
|
|---|
| 39 | return ['track'].concat(options.track || []).some(function (typeToCheck) {
|
|---|
| 40 | return typeToCheck === type;
|
|---|
| 41 | });
|
|---|
| 42 | };
|
|---|
| 43 | var _default = exports["default"] = {
|
|---|
| 44 | meta: {
|
|---|
| 45 | docs: {
|
|---|
| 46 | url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/media-has-caption.md',
|
|---|
| 47 | description: 'Enforces that `<audio>` and `<video>` elements must have a `<track>` for captions.'
|
|---|
| 48 | },
|
|---|
| 49 | schema: [schema]
|
|---|
| 50 | },
|
|---|
| 51 | create: function create(context) {
|
|---|
| 52 | var elementType = (0, _getElementType["default"])(context);
|
|---|
| 53 | return {
|
|---|
| 54 | JSXElement: function JSXElement(node) {
|
|---|
| 55 | var element = node.openingElement;
|
|---|
| 56 | var type = elementType(element);
|
|---|
| 57 | if (!isMediaType(context, type)) {
|
|---|
| 58 | return;
|
|---|
| 59 | }
|
|---|
| 60 | var mutedProp = (0, _jsxAstUtils.getProp)(element.attributes, 'muted');
|
|---|
| 61 | var mutedPropVal = (0, _jsxAstUtils.getLiteralPropValue)(mutedProp);
|
|---|
| 62 | if (mutedPropVal === true) {
|
|---|
| 63 | return;
|
|---|
| 64 | }
|
|---|
| 65 | // $FlowFixMe https://github.com/facebook/flow/issues/1414
|
|---|
| 66 | var trackChildren = node.children.filter(function (child) {
|
|---|
| 67 | if (child.type !== 'JSXElement') {
|
|---|
| 68 | return false;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | // $FlowFixMe https://github.com/facebook/flow/issues/1414
|
|---|
| 72 | return isTrackType(context, elementType(child.openingElement));
|
|---|
| 73 | });
|
|---|
| 74 | if (trackChildren.length === 0) {
|
|---|
| 75 | context.report({
|
|---|
| 76 | node: element,
|
|---|
| 77 | message: errorMessage
|
|---|
| 78 | });
|
|---|
| 79 | return;
|
|---|
| 80 | }
|
|---|
| 81 | var hasCaption = trackChildren.some(function (track) {
|
|---|
| 82 | var kindProp = (0, _jsxAstUtils.getProp)(track.openingElement.attributes, 'kind');
|
|---|
| 83 | var kindPropValue = (0, _jsxAstUtils.getLiteralPropValue)(kindProp) || '';
|
|---|
| 84 | return kindPropValue.toLowerCase() === 'captions';
|
|---|
| 85 | });
|
|---|
| 86 | if (!hasCaption) {
|
|---|
| 87 | context.report({
|
|---|
| 88 | node: element,
|
|---|
| 89 | message: errorMessage
|
|---|
| 90 | });
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | };
|
|---|
| 94 | }
|
|---|
| 95 | };
|
|---|
| 96 | module.exports = exports.default; |
|---|