-
-
Notifications
You must be signed in to change notification settings - Fork 640
/
media-has-caption.js
97 lines (83 loc) · 3.16 KB
/
media-has-caption.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* @fileoverview <audio> and <video> elements must have a <track> for captions.
* @author Ethan Cohen
* @flow
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
import type { JSXElement, JSXOpeningElement, Node } from 'ast-types-flow';
import { getProp, getLiteralPropValue } from 'jsx-ast-utils';
import flatMap from 'array.prototype.flatmap';
import type { ESLintConfig, ESLintContext, ESLintVisitorSelectorConfig } from '../../flow/eslint';
import { generateObjSchema, arraySchema } from '../util/schemas';
import getElementType from '../util/getElementType';
const errorMessage = 'Media elements such as <audio> and <video> must have a <track> for captions.';
const MEDIA_TYPES = ['audio', 'video'];
const schema = generateObjSchema({
audio: arraySchema,
video: arraySchema,
track: arraySchema,
});
const isMediaType = (context, type) => {
const options = context.options[0] || {};
return MEDIA_TYPES
.concat(flatMap(MEDIA_TYPES, (mediaType) => options[mediaType]))
.some((typeToCheck) => typeToCheck === type);
};
const isTrackType = (context, type) => {
const options = context.options[0] || {};
return ['track'].concat(options.track || []).some((typeToCheck) => typeToCheck === type);
};
export default ({
meta: {
docs: {
url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/media-has-caption.md',
description: 'Enforces that `<audio>` and `<video>` elements must have a `<track>` for captions.',
},
schema: [schema],
},
create: (context: ESLintContext): ESLintVisitorSelectorConfig => {
const elementType = getElementType(context);
return {
JSXElement: (node: JSXElement) => {
const element: JSXOpeningElement = node.openingElement;
const type = elementType(element);
if (!isMediaType(context, type)) {
return;
}
const mutedProp = getProp(element.attributes, 'muted');
const mutedPropVal: boolean = getLiteralPropValue(mutedProp);
if (mutedPropVal === true) {
return;
}
// $FlowFixMe https://github.com/facebook/flow/issues/1414
const trackChildren: Array<JSXElement> = node.children.filter((child: Node) => {
if (child.type !== 'JSXElement') {
return false;
}
// $FlowFixMe https://github.com/facebook/flow/issues/1414
return isTrackType(context, elementType(child.openingElement));
});
if (trackChildren.length === 0) {
context.report({
node: element,
message: errorMessage,
});
return;
}
const hasCaption: boolean = trackChildren.some((track) => {
const kindProp = getProp(track.openingElement.attributes, 'kind');
const kindPropValue = getLiteralPropValue(kindProp) || '';
return kindPropValue.toLowerCase() === 'captions';
});
if (!hasCaption) {
context.report({
node: element,
message: errorMessage,
});
}
},
};
},
}: ESLintConfig);