HTML writer: improve alt-text/caption handling for HTML5

Screen readers read an image's `alt` attribute and the figure caption,
both of which come from the same source in pandoc. The figure caption is
hidden from screen readers with the `aria-hidden` attribute. This
improves accessibility.

For HTML4, where `aria-hidden` is not allowed, pandoc still uses an
empty `alt` attribute to avoid duplicate contents.

Closes: #6491
This commit is contained in:
Albert Krewinkel 2020-07-01 14:44:42 +02:00
parent ccf9889c2c
commit b894de6426
No known key found for this signature in database
GPG key ID: 388DC0B21F631124
6 changed files with 17 additions and 9 deletions

View file

@ -1,6 +1,7 @@
{-# LANGUAGE MultiWayIf #-} {-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-} {-# LANGUAGE ViewPatterns #-}
{- | {- |
Module : Text.Pandoc.Writers.HTML Module : Text.Pandoc.Writers.HTML
@ -587,11 +588,18 @@ figure :: PandocMonad m
=> WriterOptions -> Attr -> [Inline] -> (Text, Text) => WriterOptions -> Attr -> [Inline] -> (Text, Text)
-> StateT WriterState m Html -> StateT WriterState m Html
figure opts attr txt (s,tit) = do figure opts attr txt (s,tit) = do
img <- inlineToHtml opts (Image attr [Str ""] (s,tit))
html5 <- gets stHtml5 html5 <- gets stHtml5
-- Screen-readers will normally read the @alt@ text and the figure; we
-- want to avoid them reading the same text twice. With HTML5 we can
-- use aria-hidden for the caption; with HTML4, we use an empty
-- alt-text instead.
let alt = if html5 then txt else [Str ""]
let tocapt = if html5 let tocapt = if html5
then H5.figcaption then H5.figcaption !
H5.customAttribute (textTag "aria-hidden")
(toValue @Text "true")
else H.p ! A.class_ "caption" else H.p ! A.class_ "caption"
img <- inlineToHtml opts (Image attr alt (s,tit))
capt <- if null txt capt <- if null txt
then return mempty then return mempty
else tocapt `fmap` inlineListToHtml opts txt else tocapt `fmap` inlineListToHtml opts txt

View file

@ -16,10 +16,10 @@
\end{figure} \end{figure}
^D ^D
<figure> <figure>
<img src="img1.jpg" alt="" /><figcaption>Caption 1</figcaption> <img src="img1.jpg" alt="Caption 1" /><figcaption aria-hidden="true">Caption 1</figcaption>
</figure> </figure>
<figure> <figure>
<img src="img2.jpg" alt="" /><figcaption>Caption 2</figcaption> <img src="img2.jpg" alt="Caption 2" /><figcaption aria-hidden="true">Caption 2</figcaption>
</figure> </figure>
``` ```
``` ```
@ -30,6 +30,6 @@
\end{figure} \end{figure}
^D ^D
<figure> <figure>
<img src="img1.jpg" alt="" /><figcaption>Caption 3</figcaption> <img src="img1.jpg" alt="Caption 3" /><figcaption aria-hidden="true">Caption 3</figcaption>
</figure> </figure>
``` ```

View file

@ -3,6 +3,6 @@
![Caption](img.png){#img:1} ![Caption](img.png){#img:1}
^D ^D
<figure> <figure>
<img src="img.png" id="img:1" alt="" /><figcaption>Caption</figcaption> <img src="img.png" id="img:1" alt="Caption" /><figcaption aria-hidden="true">Caption</figcaption>
</figure> </figure>
``` ```

View file

@ -5,7 +5,7 @@
## Header 2 ## Header 2
^D ^D
<figure> <figure>
<img src="./my-figure.jpg" width="500" alt="" /><figcaption>My caption</figcaption> <img src="./my-figure.jpg" width="500" alt="My caption" /><figcaption aria-hidden="true">My caption</figcaption>
</figure> </figure>
Header 2 Header 2

View file

@ -3,6 +3,6 @@
![test](foo){aria-describedby="barbaz"} ![test](foo){aria-describedby="barbaz"}
^D ^D
<figure> <figure>
<img src="foo" aria-describedby="barbaz" alt="" /><figcaption>test</figcaption> <img src="foo" aria-describedby="barbaz" alt="test" /><figcaption aria-hidden="true">test</figcaption>
</figure> </figure>
``` ```

View file

@ -523,7 +523,7 @@ Blah
<h1 id="images">Images</h1> <h1 id="images">Images</h1>
<p>From “Voyage dans la Lune” by Georges Melies (1902):</p> <p>From “Voyage dans la Lune” by Georges Melies (1902):</p>
<figure> <figure>
<img src="lalune.jpg" title="Voyage dans la Lune" alt="" /><figcaption>lalune</figcaption> <img src="lalune.jpg" title="Voyage dans la Lune" alt="lalune" /><figcaption aria-hidden="true">lalune</figcaption>
</figure> </figure>
<p>Here is a movie <img src="movie.jpg" alt="movie" /> icon.</p> <p>Here is a movie <img src="movie.jpg" alt="movie" /> icon.</p>
<hr /> <hr />