-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(biome_html_analyze): add useMediaCaption rule for HTML #8742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b8af3bb
feat(biome_html_analyze): add useMediaCaption rule for HTML
rahuld109 fbd6019
refactor: organize imports and add muted audio test case
rahuld109 ae9ce01
fix: muted exemption only applies to video, not audio
rahuld109 ade0283
test: add edge cases for missing and empty kind attribute
rahuld109 c7291f8
fix: skip analysis on malformed HTML to avoid false positives
rahuld109 9571ede
refactor: address review feedback
rahuld109 1d6edb4
Merge branch 'next' into use-media-caption-html
rahuld109 f261a16
test(useMediaCaption): add Vue, Svelte, and Astro test files
rahuld109 155d31b
test(useMediaCaption): add source element test cases
rahuld109 520312a
fix(useMediaCaption): use find_map instead of filter_map().next()
rahuld109 0f23f78
docs: add note about case-sensitivity behavior for element names
rahuld109 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Added the HTML-specific lint rule [`useMediaCaption`](https://biomejs.dev/linter/rules/use-media-caption/). Enforces that `audio` and `video` elements have a `track` element with `kind="captions"` for accessibility. Muted videos are allowed without captions. | ||
|
rahuld109 marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
crates/biome_html_analyze/src/lint/a11y/use_media_caption.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| use biome_analyze::{ | ||
| Ast, Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule, | ||
| }; | ||
| use biome_console::markup; | ||
| use biome_diagnostics::Severity; | ||
| use biome_html_syntax::AnyHtmlElement; | ||
| use biome_rowan::AstNode; | ||
|
|
||
| declare_lint_rule! { | ||
| /// Enforces that `audio` and `video` elements must have a `track` for captions. | ||
| /// | ||
| /// Captions support users with hearing-impairments. They should be a transcription | ||
| /// or translation of the dialogue, sound effects, musical cues, and other relevant | ||
| /// audio information. | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ### Invalid | ||
| /// | ||
| /// ```html,expect_diagnostic | ||
| /// <video src="video.mp4"></video> | ||
| /// ``` | ||
| /// | ||
| /// ```html,expect_diagnostic | ||
| /// <audio src="audio.mp3"> | ||
| /// <source src="audio.ogg" type="audio/ogg" /> | ||
| /// </audio> | ||
| /// ``` | ||
| /// | ||
| /// ### Valid | ||
| /// | ||
| /// ```html | ||
| /// <video src="video.mp4"> | ||
| /// <track kind="captions" src="captions.vtt" /> | ||
| /// </video> | ||
| /// ``` | ||
| /// | ||
| /// ```html | ||
| /// <audio src="audio.mp3"> | ||
| /// <track kind="captions" src="captions.vtt" /> | ||
| /// </audio> | ||
| /// ``` | ||
| /// | ||
| /// ```html | ||
| /// <video muted src="video.mp4"></video> | ||
| /// ``` | ||
| /// | ||
| /// ## Accessibility guidelines | ||
| /// | ||
| /// - [WCAG 1.2.2](https://www.w3.org/WAI/WCAG21/Understanding/captions-prerecorded) | ||
| /// - [WCAG 1.2.3](https://www.w3.org/WAI/WCAG21/Understanding/audio-description-or-media-alternative-prerecorded) | ||
| /// | ||
| pub UseMediaCaption { | ||
| version: "next", | ||
| name: "useMediaCaption", | ||
| language: "html", | ||
| sources: &[RuleSource::EslintJsxA11y("media-has-caption").same()], | ||
| recommended: true, | ||
| severity: Severity::Error, | ||
| } | ||
| } | ||
|
|
||
| impl Rule for UseMediaCaption { | ||
| type Query = Ast<AnyHtmlElement>; | ||
| type State = (); | ||
| type Signals = Option<Self::State>; | ||
| type Options = (); | ||
|
|
||
| fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
| let node = ctx.query(); | ||
|
|
||
| // Check if element is audio or video | ||
| let element_name = node.name()?; | ||
| if element_name != "audio" && element_name != "video" { | ||
| return None; | ||
| } | ||
|
rahuld109 marked this conversation as resolved.
Outdated
|
||
|
|
||
| // If element has muted attribute, it's valid (no caption needed) | ||
| if node.find_attribute_by_name("muted").is_some() { | ||
| return None; | ||
| } | ||
|
rahuld109 marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Check for track element with kind="captions" in children | ||
| let html_element = node.as_html_element()?; | ||
| if html_element.opening_element().is_ok() && has_caption_track(&html_element.children()) { | ||
| return None; | ||
| } | ||
|
|
||
| // No muted attribute and no caption track found - emit diagnostic | ||
| Some(()) | ||
| } | ||
|
|
||
| fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
| let node = ctx.query(); | ||
| let diagnostic = RuleDiagnostic::new( | ||
| rule_category!(), | ||
| node.syntax().text_trimmed_range(), | ||
| markup! { | ||
| "Provide a "<Emphasis>"track"</Emphasis>" for captions when using "<Emphasis>"audio"</Emphasis>" or "<Emphasis>"video"</Emphasis>" elements." | ||
| }, | ||
| ) | ||
| .note(markup! { | ||
| "Captions support users with hearing-impairments. They should be a transcription or translation of the dialogue, sound effects, musical cues, and other relevant audio information." | ||
| }); | ||
|
|
||
| Some(diagnostic) | ||
| } | ||
| } | ||
|
|
||
| use biome_html_syntax::HtmlElementList; | ||
|
|
||
| /// Checks if the given `HtmlElementList` has a `track` element with `kind="captions"`. | ||
| fn has_caption_track(html_child_list: &HtmlElementList) -> bool { | ||
| html_child_list.into_iter().any(|child| { | ||
|
rahuld109 marked this conversation as resolved.
Outdated
|
||
| // Check if element is a track element (works for both HtmlElement and HtmlSelfClosingElement) | ||
| let Some(name) = child.name() else { | ||
| return false; | ||
| }; | ||
|
|
||
| if name.text() != "track" { | ||
|
rahuld109 marked this conversation as resolved.
Outdated
|
||
| return false; | ||
| } | ||
|
|
||
| // Check if track has kind="captions" | ||
| let Some(kind_attr) = child.find_attribute_by_name("kind") else { | ||
| return false; | ||
| }; | ||
| let Some(initializer) = kind_attr.initializer() else { | ||
| return false; | ||
| }; | ||
| let Ok(value) = initializer.value() else { | ||
| return false; | ||
| }; | ||
| let Some(string_value) = value.string_value() else { | ||
| return false; | ||
| }; | ||
| string_value.eq_ignore_ascii_case("captions") | ||
| }) | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
crates/biome_html_analyze/tests/specs/a11y/useMediaCaption/invalid.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <!-- should generate diagnostics --> | ||
| <video src="video.mp4"></video> | ||
| <audio src="audio.mp3"></audio> | ||
| <video> | ||
| <source src="video.webm" type="video/webm" /> | ||
| </video> | ||
| <audio> | ||
| <source src="audio.ogg" type="audio/ogg" /> | ||
| </audio> | ||
| <!-- track without kind="captions" --> | ||
| <video> | ||
| <track kind="subtitles" src="subtitles.vtt" /> | ||
| </video> |
114 changes: 114 additions & 0 deletions
114
crates/biome_html_analyze/tests/specs/a11y/useMediaCaption/invalid.html.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| --- | ||
| source: crates/biome_html_analyze/tests/spec_tests.rs | ||
| expression: invalid.html | ||
| --- | ||
| # Input | ||
| ```html | ||
| <!-- should generate diagnostics --> | ||
| <video src="video.mp4"></video> | ||
| <audio src="audio.mp3"></audio> | ||
| <video> | ||
| <source src="video.webm" type="video/webm" /> | ||
| </video> | ||
| <audio> | ||
| <source src="audio.ogg" type="audio/ogg" /> | ||
| </audio> | ||
| <!-- track without kind="captions" --> | ||
| <video> | ||
| <track kind="subtitles" src="subtitles.vtt" /> | ||
| </video> | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| invalid.html:2:1 lint/a11y/useMediaCaption ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| × Provide a track for captions when using audio or video elements. | ||
|
|
||
| 1 │ <!-- should generate diagnostics --> | ||
| > 2 │ <video src="video.mp4"></video> | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 3 │ <audio src="audio.mp3"></audio> | ||
| 4 │ <video> | ||
|
|
||
| i Captions support users with hearing-impairments. They should be a transcription or translation of the dialogue, sound effects, musical cues, and other relevant audio information. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.html:3:1 lint/a11y/useMediaCaption ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| × Provide a track for captions when using audio or video elements. | ||
|
|
||
| 1 │ <!-- should generate diagnostics --> | ||
| 2 │ <video src="video.mp4"></video> | ||
| > 3 │ <audio src="audio.mp3"></audio> | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 4 │ <video> | ||
| 5 │ <source src="video.webm" type="video/webm" /> | ||
|
|
||
| i Captions support users with hearing-impairments. They should be a transcription or translation of the dialogue, sound effects, musical cues, and other relevant audio information. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.html:4:1 lint/a11y/useMediaCaption ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| × Provide a track for captions when using audio or video elements. | ||
|
|
||
| 2 │ <video src="video.mp4"></video> | ||
| 3 │ <audio src="audio.mp3"></audio> | ||
| > 4 │ <video> | ||
| │ ^^^^^^^ | ||
| > 5 │ <source src="video.webm" type="video/webm" /> | ||
| > 6 │ </video> | ||
| │ ^^^^^^^^ | ||
| 7 │ <audio> | ||
| 8 │ <source src="audio.ogg" type="audio/ogg" /> | ||
|
|
||
| i Captions support users with hearing-impairments. They should be a transcription or translation of the dialogue, sound effects, musical cues, and other relevant audio information. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.html:7:1 lint/a11y/useMediaCaption ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| × Provide a track for captions when using audio or video elements. | ||
|
|
||
| 5 │ <source src="video.webm" type="video/webm" /> | ||
| 6 │ </video> | ||
| > 7 │ <audio> | ||
| │ ^^^^^^^ | ||
| > 8 │ <source src="audio.ogg" type="audio/ogg" /> | ||
| > 9 │ </audio> | ||
| │ ^^^^^^^^ | ||
| 10 │ <!-- track without kind="captions" --> | ||
| 11 │ <video> | ||
|
|
||
| i Captions support users with hearing-impairments. They should be a transcription or translation of the dialogue, sound effects, musical cues, and other relevant audio information. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.html:11:1 lint/a11y/useMediaCaption ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| × Provide a track for captions when using audio or video elements. | ||
|
|
||
| 9 │ </audio> | ||
| 10 │ <!-- track without kind="captions" --> | ||
| > 11 │ <video> | ||
| │ ^^^^^^^ | ||
| > 12 │ <track kind="subtitles" src="subtitles.vtt" /> | ||
| > 13 │ </video> | ||
| │ ^^^^^^^^ | ||
| 14 │ | ||
|
|
||
| i Captions support users with hearing-impairments. They should be a transcription or translation of the dialogue, sound effects, musical cues, and other relevant audio information. | ||
|
|
||
|
|
||
| ``` |
16 changes: 16 additions & 0 deletions
16
crates/biome_html_analyze/tests/specs/a11y/useMediaCaption/valid.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <!-- should not generate diagnostics --> | ||
| <video src="video.mp4"> | ||
| <track kind="captions" src="captions.vtt" /> | ||
| </video> | ||
| <audio src="audio.mp3"> | ||
| <track kind="captions" src="captions.vtt" /> | ||
| </audio> | ||
| <!-- muted videos don't need captions --> | ||
| <video muted src="video.mp4"></video> | ||
| <video muted> | ||
| <source src="video.webm" type="video/webm" /> | ||
| </video> | ||
| <!-- case insensitive kind check --> | ||
| <video> | ||
| <track kind="Captions" src="captions.vtt" /> | ||
| </video> |
24 changes: 24 additions & 0 deletions
24
crates/biome_html_analyze/tests/specs/a11y/useMediaCaption/valid.html.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| --- | ||
| source: crates/biome_html_analyze/tests/spec_tests.rs | ||
| expression: valid.html | ||
| --- | ||
| # Input | ||
| ```html | ||
| <!-- should not generate diagnostics --> | ||
| <video src="video.mp4"> | ||
| <track kind="captions" src="captions.vtt" /> | ||
| </video> | ||
| <audio src="audio.mp3"> | ||
| <track kind="captions" src="captions.vtt" /> | ||
| </audio> | ||
| <!-- muted videos don't need captions --> | ||
| <video muted src="video.mp4"></video> | ||
| <video muted> | ||
| <source src="video.webm" type="video/webm" /> | ||
| </video> | ||
| <!-- case insensitive kind check --> | ||
| <video> | ||
| <track kind="Captions" src="captions.vtt" /> | ||
| </video> | ||
|
|
||
| ``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.