From b894de64264fe0386b5cb3e1680955a8e879c78e Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Wed, 1 Jul 2020 14:44:42 +0200 Subject: [PATCH] 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 --- src/Text/Pandoc/Writers/HTML.hs | 12 ++++++++++-- test/command/3577.md | 6 +++--- test/command/4677.md | 2 +- test/command/5121.md | 2 +- test/command/5642.md | 2 +- test/writer.html5 | 2 +- 6 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs index 11daaf06b..4bfd95674 100644 --- a/src/Text/Pandoc/Writers/HTML.hs +++ b/src/Text/Pandoc/Writers/HTML.hs @@ -1,6 +1,7 @@ {-# LANGUAGE MultiWayIf #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TypeApplications #-} {-# LANGUAGE ViewPatterns #-} {- | Module : Text.Pandoc.Writers.HTML @@ -587,11 +588,18 @@ figure :: PandocMonad m => WriterOptions -> Attr -> [Inline] -> (Text, Text) -> StateT WriterState m Html figure opts attr txt (s,tit) = do - img <- inlineToHtml opts (Image attr [Str ""] (s,tit)) 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 - then H5.figcaption + then H5.figcaption ! + H5.customAttribute (textTag "aria-hidden") + (toValue @Text "true") else H.p ! A.class_ "caption" + img <- inlineToHtml opts (Image attr alt (s,tit)) capt <- if null txt then return mempty else tocapt `fmap` inlineListToHtml opts txt diff --git a/test/command/3577.md b/test/command/3577.md index d1df1610d..2f415146d 100644 --- a/test/command/3577.md +++ b/test/command/3577.md @@ -16,10 +16,10 @@ \end{figure} ^D
-
Caption 1
+Caption 1
-
Caption 2
+Caption 2
``` ``` @@ -30,6 +30,6 @@ \end{figure} ^D
-
Caption 3
+Caption 3
``` diff --git a/test/command/4677.md b/test/command/4677.md index 482db4c02..2694624b9 100644 --- a/test/command/4677.md +++ b/test/command/4677.md @@ -3,6 +3,6 @@ ![Caption](img.png){#img:1} ^D
-
Caption
+Caption
``` diff --git a/test/command/5121.md b/test/command/5121.md index 42f2fdea2..419b8b9f4 100644 --- a/test/command/5121.md +++ b/test/command/5121.md @@ -5,7 +5,7 @@ ## Header 2 ^D
-
My caption
+My caption
Header 2 diff --git a/test/command/5642.md b/test/command/5642.md index 7fe8f5a5f..cd60df812 100644 --- a/test/command/5642.md +++ b/test/command/5642.md @@ -3,6 +3,6 @@ ![test](foo){aria-describedby="barbaz"} ^D
-
test
+test
``` diff --git a/test/writer.html5 b/test/writer.html5 index 321b65afd..0f0db799e 100644 --- a/test/writer.html5 +++ b/test/writer.html5 @@ -523,7 +523,7 @@ Blah

Images

From “Voyage dans la Lune” by Georges Melies (1902):

-
lalune
+lalune

Here is a movie movie icon.