Skip to content
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

Support aria-labelledby in htmlwidgets. #2243

Merged
merged 6 commits into from
May 22, 2023
Merged

Conversation

dmurdoch
Copy link
Contributor

This allows htmlwidgets to be labelled with alt text for screen readers.

@dmurdoch
Copy link
Contributor Author

Fixes #2241

Blank alt should be inserted to
satisfy the search for the id.
@dmurdoch
Copy link
Contributor Author

dmurdoch commented May 4, 2023

No comments on this yet. Now that I look back at it after 6 weeks, I can see that the explanation is a little lacking.

The issue here is that htmlwidgets don't have any way to handle alt-text for screen readers if it is specified using fig.alt or fig.cur in the chunk options, because those options may be vectors of values to be spread across different figures in the chunk, and the assignment to particular figures happens after the figure is already produced. (It has to be done this way to suppress minor updates to plots.)

This PR handles this by allowing the htmlwidget to put an aria-labelled-by="label" field in the first HTML tag that it emits. The label is generated by the htmlwidget, and found by knitr using the new get_alt_id() function. If there's no id, knitr can assume there's
no alt-text needed. If an id is found, then knitr will emit the alt text in the add_html_caption() function, with the id set to the given label. When a screen reader or browser reads the final HTML file, it will know the alt-text for the htmlwidget.

I chose to look only in the first tag of htmlwidgets output because some htmlwidgets (e.g. ones produced by rgl) can be really huge, but I think (almost?) all widgets would have an HTML tag pretty early in their output.

Currently the htmlwidgets package doesn't have specific support for producing these; I've also submitted ramnathv/htmlwidgets#465 to add it there. However, rgl support for this mechanism is already written.

@yihui
Copy link
Owner

yihui commented May 4, 2023

Thanks for the explanation! This has been on my TODO list for a while. I haven't reviewed the PR carefully yet but I think I'll merge it in a few days.

@yihui yihui linked an issue May 19, 2023 that may be closed by this pull request
@yihui yihui merged commit 3bb35d5 into yihui:master May 22, 2023
@giabaio giabaio mentioned this pull request May 26, 2023
3 tasks
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using fig.cur in rgl (or another htmlwidget)?
2 participants