Skip to content

Commit 954a34c

Browse files
committed
a11y: implement media-has-caption rule
1 parent 4c135b0 commit 954a34c

File tree

3 files changed

+61
-34
lines changed

3 files changed

+61
-34
lines changed

src/compiler/compile/nodes/Element.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,29 @@ export default class Element extends Node {
287287
}
288288
}
289289

290+
if (this.is_media_node()) {
291+
const has_muted_attribute = this.attributes.some(attr => attr.name === 'muted');
292+
293+
if (has_muted_attribute) {
294+
return;
295+
}
296+
297+
const is_track = (child: INode) => child.type === 'Element' && child.name === 'track';
298+
const is_caption = (attr: Attribute) =>
299+
attr.name === 'kind' &&
300+
attr.get_static_value().toString().toLowerCase() === 'captions';
301+
const has_captions = this.children.some(
302+
child => !is_track(child) ? false : child.attributes.some(is_caption)
303+
);
304+
305+
if (!has_captions) {
306+
this.component.warn(this, {
307+
code: `a11y-media-has-caption`,
308+
message: `A11y: <${this.name}> should have a <track> for captions`
309+
});
310+
}
311+
}
312+
290313
this.validate_attributes();
291314
this.validate_special_cases();
292315
this.validate_bindings();
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
<video><track kind="captions"/></video>
2-
<video></video>
3-
<video><track /></video>
4-
<audio muted></audio>
1+
<video>
2+
<track kind="captions" />
3+
</video>
4+
<video />
5+
<video>
6+
<track />
7+
</video>
8+
<audio muted />
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
[
2-
{
3-
"code": "a11y-media-has-caption",
4-
"end": {
5-
"character": 55,
6-
"column": 15,
7-
"line": 2
8-
},
9-
"message": "A11y: Media elements must have a <track kind=\"captions\">",
10-
"pos": 40,
11-
"start": {
12-
"character": 40,
13-
"column": 0,
14-
"line": 2
15-
}
16-
},
17-
{
18-
"code": "a11y-media-has-caption",
19-
"end": {
20-
"character": 80,
21-
"column": 24,
22-
"line": 3
23-
},
24-
"message": "A11y: Media elements must have a <track kind=\"captions\">",
25-
"pos": 56,
26-
"start": {
27-
"character": 56,
28-
"column": 0,
29-
"line": 3
30-
}
31-
}
2+
{
3+
"code": "a11y-media-has-caption",
4+
"end": {
5+
"character": 55,
6+
"column": 15,
7+
"line": 2
8+
},
9+
"message": "A11y: Media elements must have a <track kind=\"captions\">",
10+
"pos": 40,
11+
"start": {
12+
"character": 40,
13+
"column": 0,
14+
"line": 2
15+
}
16+
},
17+
{
18+
"code": "a11y-media-has-caption",
19+
"end": {
20+
"character": 80,
21+
"column": 24,
22+
"line": 3
23+
},
24+
"message": "A11y: Media elements must have a <track kind=\"captions\">",
25+
"pos": 56,
26+
"start": {
27+
"character": 56,
28+
"column": 0,
29+
"line": 3
30+
}
31+
}
3232
]

0 commit comments

Comments
 (0)