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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

- The global option `knitr.progress.simple` can be used to decide whether to output the bar in the progress. When set to `FALSE`, only the step numbers and chunk labels will be printed, and the progress bar is omitted. This can be more useful for logging purposes since the bar itself is not useful (thanks, @hadley, #2221). By default, the simple progress output is used when the progress is not written to a connection such as `stdout` or `stderr` (e.g., written to a file instead), or the output connection is not a "terminal".

- HTML Widgets can now support alt text by specifying an attribute `aria-labelledby="<label>"` in their first HTML tag. The text will be obtained from the `fig.alt` or `fig.cap` chunk option in the usual way (thanks, @dmurdoch, #2243).

## BUG FIXES

- The chunk option `collapse = TRUE` works with HTML widgets now (thanks, @dmurdoch, #2212).
Expand Down
30 changes: 25 additions & 5 deletions R/output.R
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,12 @@ sew.knit_asis = function(x, options, inline = FALSE, ...) {
if (inherits(x, 'knit_asis_htmlwidget')) {
options$fig.cur = plot_counter()
options = reduce_plot_opts(options)
return(add_html_caption(options, wrap_asis(x, options)))
# look for attribute 'aria-labelledby="label"' in the first HTML tag and
# use the label to provide alt text if found
return(add_html_caption(
options, wrap_asis(x, options),
xfun::grep_sub('^[^<]*<[^>]+aria-labelledby[ ]*=[ ]*"([^"]+)".*$', '\\1', x)
))
}
}
x = wrap_asis(x, options)
Expand Down Expand Up @@ -642,12 +647,27 @@ sew.knit_embed_url = function(x, options = opts_chunk$get(), inline = FALSE, ...
))
}

add_html_caption = function(options, code) {
add_html_caption = function(options, code, id = NULL) {
cap = .img.cap(options)
if (cap == '') return(code)
if (cap == '' && !length(id)) return(code)

if (length(id)) {
alt = .img.cap(options, alt = TRUE)
if (cap == alt && cap != '') {
# both are the same, so insert cap with id
alttext = sprintf('<p class="caption" id="%s">%s</p>\n', id, cap)
# prevent a second insertion
cap = ''
} else {
alttext = sprintf('<p id="%s" hidden>%s</p>\n', id, alt)
}
} else alttext = ''

captext = if (cap == '') '' else sprintf('<p class="caption">%s</p>\n', cap)

sprintf(
'<div class="figure"%s>\n%s\n<p class="caption">%s</p>\n</div>',
css_text_align(options$fig.align), code, cap
'<div class="figure"%s>\n%s\n%s%s</div>',
css_text_align(options$fig.align), code, captext, alttext
)
}

Expand Down