Skip to content

Commit c0d7171

Browse files
committed
fix #2415: respect empty fig.alt, i.e., still generate the alt attribute even if fig.alt = ''; discard the attribute only when fig.alt = NA
1 parent a4cb643 commit c0d7171

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: knitr
22
Type: Package
33
Title: A General-Purpose Package for Dynamic Report Generation in R
4-
Version: 1.50.6
4+
Version: 1.50.7
55
Authors@R: c(
66
person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666", URL = "https://yihui.org")),
77
person("Abhraneel", "Sarma", role = "ctb"),

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222

2323
- HTML screenshots are taken inside the current working directory by default if it is writable, instead of `tempdir()`, which may not be accessible to certain browsers (thanks, @markschl, #2416). The directory can be customized via the global R option `knitr.html_screenshot.tmpdir`, e.g., if you want the old behavior, you may set `options(knitr.html_screenshot.tmpdir = tempdir())`.
2424

25+
## MINOR CHANGES
26+
27+
- Empty alt text for images is allowed now (thanks, @Olivia-Box-Power, #2415). With the chunk option `fig.alt = ''`, the HTML output will be `<img alt = "" ... />`. Previously, the `alt` attribute was omitted in this case. If you still want to omit the `alt` attribute, you may use `fig.alt = NA`.
28+
2529
# CHANGES IN knitr VERSION 1.50
2630

2731
## NEW FEATURES

R/hooks-html.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,18 @@ hook_animation = function(options) {
3939

4040
.img.tag = function(src, w, h, caption, extra) {
4141
ext = tolower(file_ext(src))
42-
if (length(caption) != 1 || caption == '') caption = NULL
4342
tag = 'img'; extra2 = NULL; att = 'src'
4443
if (ext == 'pdf') {
4544
extra2 = 'type="application/pdf"'; tag = 'embed'
4645
} else if (ext == 'svg' && getOption('knitr.svg.object', FALSE)) {
4746
extra2 = 'type="image/svg+xml"'; tag = 'object'; att = 'data'
4847
}
48+
if (length(caption) != 1 || is.na(caption) || (tag != 'img' && caption == ''))
49+
caption = NULL
4950
res = paste0(c(
5051
paste0('<', tag),
5152
sprintf('%s="%s%s"', att, opts_knit$get('base.url') %n% '', .upload.url(src)),
52-
sprintf('%s="%s"', if (tag %in% c('embed', 'object')) 'title' else 'alt', caption),
53+
sprintf('%s="%s"', if (tag == 'img') 'alt' else 'title', caption),
5354
.img.attr(w, h, c(extra, extra2))
5455
), collapse = ' ')
5556
paste0(res, if (tag == 'object') '></object>' else ' />')
@@ -59,7 +60,7 @@ hook_animation = function(options) {
5960
cap = options$fig.cap %n% {
6061
if (is.null(pandoc_to())) sprintf('plot of chunk %s', options$label) else ''
6162
}
62-
if (length(cap) == 0) cap = ''
63+
if (length(cap) == 0 || is.na(cap)) cap = ''
6364
if (alt) {
6465
alt = options$fig.alt %n% cap
6566
return(if (escape) html_escape(alt) else alt)

tests/testit/test-hooks-md.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ assert('include_graphics() includes custom images correctly', {
2222
(img_output('a.png') %==% '![](a.png)')
2323
(img_output(c('a.png', 'b.png'), list(fig.show = 'hold')) %==% '![](a.png)![](b.png)')
2424
(img_output('a.png', list(fig.cap = 'foo bar')) %==% '![foo bar](a.png)')
25-
(img_output('a.png', list(out.width = '50%')) %==% '<img src="a.png" width="50%" />')
25+
(img_output('a.png', list(out.width = '50%')) %==% '<img src="a.png" alt="" width="50%" />')
2626
(img_output('a.pdf', list(out.width = '300px')) %==% '<embed src="a.pdf" width="300px" type="application/pdf" />')
2727
})
2828

@@ -101,6 +101,11 @@ assert("Include a plot by pandoc md", {
101101
sprintf("![%s](1.png){width=%s %s}", cap, w, ex))
102102
})
103103

104+
assert('empty alt text is preserved and NA alt is discarded', {
105+
(hook_plot_md(x, opts_chunk$merge(list(fig.alt = ''))) %==% '<img src="1.png" alt="" />')
106+
(hook_plot_md(x, opts_chunk$merge(list(fig.alt = NA, out.width = '100'))) %==% '<img src="1.png" width="100" />')
107+
})
108+
104109
assert("fig.alt does not break office document", {
105110
old = opts_knit$get('rmarkdown.pandoc.to')
106111
opts_knit$set(rmarkdown.pandoc.to = "docx")

0 commit comments

Comments
 (0)