Skip to content

Commit

Permalink
do not escape alt text by default; escaping is only necessary for HTM…
Browse files Browse the repository at this point in the history
…L output
  • Loading branch information
yihui committed Aug 17, 2023
1 parent d56d3ba commit 4a7512b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
9 changes: 6 additions & 3 deletions R/hooks-html.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ hook_animation = function(options) {
paste0(res, if (tag == 'object') '></object>' else ' />')
}

.img.cap = function(options, alt = FALSE) {
.img.cap = function(options, alt = FALSE, escape = FALSE) {
cap = options$fig.cap %n% {
if (is.null(pandoc_to())) sprintf('plot of chunk %s', options$label) else ''
}
if (length(cap) == 0) cap = ''
if (alt) return(escape_html(options$fig.alt %n% cap))
if (alt) {
alt = options$fig.alt %n% cap
return(if (escape) escape_html(alt) else alt)
}
if (is_blank(cap)) return(cap)
paste0(create_label(
options$fig.lp, options$label,
Expand Down Expand Up @@ -140,7 +143,7 @@ hook_ffmpeg = function(x, options, format = 'webm') {
sprintf('width="%s"', options$out.width),
sprintf('height="%s"', options$out.height), opts
)
cap = .img.cap(options, alt = TRUE)
cap = .img.cap(options, alt = TRUE, escape = TRUE)
if (cap != '') cap = sprintf('<p>%s</p>', cap)
sprintf(
'<video %s><source src="%s" />%s</video>', trimws(opts),
Expand Down
2 changes: 1 addition & 1 deletion R/output.R
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ add_html_caption = function(options, code, id = NULL) {
if (cap == '' && !length(id)) return(code)

if (length(id)) {
alt = .img.cap(options, alt = TRUE)
alt = .img.cap(options, alt = TRUE, escape = 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)
Expand Down
4 changes: 2 additions & 2 deletions tests/testit/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ opts = list(
assert('.img.cap() generates the figure caption and alt attribute', {
(.img.cap(list(fig.cap = NULL), FALSE) %==% "")
(.img.cap(opts, FALSE) %==% opts$fig.cap)
(.img.cap(opts, TRUE) %==% 'Figure &quot;caption&quot; &lt;&gt;.')
(.img.cap(opts, TRUE, TRUE) %==% 'Figure &quot;caption&quot; &lt;&gt;.')

opts$fig.alt = 'Figure "alternative text" <>.'

(.img.cap(opts, TRUE) %==% 'Figure &quot;alternative text&quot; &lt;&gt;.')
(.img.cap(opts, TRUE, TRUE) %==% 'Figure &quot;alternative text&quot; &lt;&gt;.')
(.img.cap(opts, FALSE) %==% opts$fig.cap)

(.img.cap(list(fig.cap = '', fig.alt = "alt"), FALSE) %==% "")
Expand Down

0 comments on commit 4a7512b

Please sign in to comment.