diff options
| author | John MacFarlane <jgm@berkeley.edu> | 2024-02-29 12:48:27 -0800 |
|---|---|---|
| committer | John MacFarlane <jgm@berkeley.edu> | 2024-02-29 12:48:27 -0800 |
| commit | 67fa6cda5c871093ef5e6711b1face0f66f3a8c1 (patch) | |
| tree | e407674f767a6ca6e134c50a89e6b672b3df9adf | |
| parent | 85b3ac3ee1a470c5c48ef8288865f115594090be (diff) | |
SelfContained: add `role="img"` to svgs.
This is needed in conjunction with `aria-label` for screen
readers. Completes the fix to #9525.
| -rw-r--r-- | src/Text/Pandoc/SelfContained.hs | 22 | ||||
| -rw-r--r-- | test/command/8948.md | 4 | ||||
| -rw-r--r-- | test/command/9420.md | 2 | ||||
| -rw-r--r-- | test/command/9467.md | 2 |
4 files changed, 19 insertions, 11 deletions
diff --git a/src/Text/Pandoc/SelfContained.hs b/src/Text/Pandoc/SelfContained.hs index e59971231..c20492d9e 100644 --- a/src/Text/Pandoc/SelfContained.hs +++ b/src/Text/Pandoc/SelfContained.hs @@ -146,13 +146,7 @@ convertTags (t@(TagOpen tagname as):ts) | any (isSourceAttribute tagname) as = do as' <- mapM processAttribute as - let rawattrs = rights as' - let attrs = case lookup "alt" rawattrs of - Nothing -> rawattrs - Just alt -> -- see #9525 - case lookup "aria-label" rawattrs of - Nothing -> ("aria-label", alt) : rawattrs - Just _ -> rawattrs + let attrs = addRole "img" $ addAriaLabel $ rights as' let svgContents = lefts as' rest <- convertTags ts case svgContents of @@ -225,6 +219,20 @@ convertTags (t@(TagOpen tagname as):ts) convertTags (t:ts) = (t:) <$> convertTags ts +addRole :: T.Text -> [(T.Text, T.Text)] -> [(T.Text, T.Text)] +addRole role attrs = + case lookup "role" attrs of + Nothing -> ("role", role) : attrs + Just _ -> attrs + +addAriaLabel :: [(T.Text, T.Text)] -> [(T.Text, T.Text)] +addAriaLabel attrs = + case lookup "aria-label" attrs of + Just _ -> attrs + Nothing -> case lookup "alt" attrs of + Just alt -> ("aria-label", alt) : attrs + Nothing -> attrs + -- we want to drop spaces, <?xml>, and comments before <svg> -- and anything after </svg>: getSvgTags :: T.Text -> [Tag T.Text] diff --git a/test/command/8948.md b/test/command/8948.md index b1852bcf8..dd3b439a0 100644 --- a/test/command/8948.md +++ b/test/command/8948.md @@ -3,7 +3,7 @@   ^D -<p><svg aria-label="minimal" alt="minimal" viewBox="-.333 -.333 480 150" style="background-color:#ffffff00" xml:space="preserve" width="480" height="150"><use href="#svg_7868854ffb8f30209cd0" width="100%" height="100%" /></svg> <svg id="svg_7868854ffb8f30209cd0" aria-label="minimal" alt="minimal" viewBox="-.333 -.333 480 150" style="background-color:#ffffff00" xml:space="preserve" width="480" height="150"> +<p><svg role="img" aria-label="minimal" alt="minimal" viewBox="-.333 -.333 480 150" style="background-color:#ffffff00" xml:space="preserve" width="480" height="150"><use href="#svg_7868854ffb8f30209cd0" width="100%" height="100%" /></svg> <svg id="svg_7868854ffb8f30209cd0" role="img" aria-label="minimal" alt="minimal" viewBox="-.333 -.333 480 150" style="background-color:#ffffff00" xml:space="preserve" width="480" height="150"> <path d="M 0 35.5 L 6.5 22.5 L 16 37 L 23 24 L 34.8 43.7 L 42.5 30 L 50.3 47 L 59.7 27.7 L 69 47 L 85 17.7 L 98.3 39 L 113 9.7 L 127.7 42.3 L 136.3 23.7 L 147 44.3 L 158.3 20.3 L 170.3 40.3 L 177.7 25.7 L 189.7 43 L 199.7 21 L 207.7 35 L 219 11 L 233 37 L 240.3 23.7 L 251 43 L 263 18.3 L 272.7 33.3 L 283 10 L 295 32.3 L 301.3 23 L 311.7 37 L 323.7 7.7 L 339.3 39 L 346.3 25.7 L 356.3 42.3 L 369.7 15 L 376.3 25.7 L 384 9 L 393 28.3 L 400.3 19 L 411.7 38.3 L 421 21 L 434.3 43 L 445 25 L 453 36.3 L 464.3 18.3 L 476.2 40.3 L 480 33.5 L 480 215 L 0 215 L 0 35.5 Z" fill="#175720" /> </svg></p> ``` @@ -13,7 +13,7 @@  {#foo} ^D -<p><svg aria-label="minimal" alt="minimal" viewBox="-.333 -.333 480 150" style="background-color:#ffffff00" xml:space="preserve" width="480" height="150"><use href="#foo" width="100%" height="100%" /></svg> <svg id="foo" aria-label="minimal" alt="minimal" viewBox="-.333 -.333 480 150" style="background-color:#ffffff00" xml:space="preserve" width="480" height="150"> +<p><svg role="img" aria-label="minimal" alt="minimal" viewBox="-.333 -.333 480 150" style="background-color:#ffffff00" xml:space="preserve" width="480" height="150"><use href="#foo" width="100%" height="100%" /></svg> <svg id="foo" role="img" aria-label="minimal" alt="minimal" viewBox="-.333 -.333 480 150" style="background-color:#ffffff00" xml:space="preserve" width="480" height="150"> <path d="M 0 35.5 L 6.5 22.5 L 16 37 L 23 24 L 34.8 43.7 L 42.5 30 L 50.3 47 L 59.7 27.7 L 69 47 L 85 17.7 L 98.3 39 L 113 9.7 L 127.7 42.3 L 136.3 23.7 L 147 44.3 L 158.3 20.3 L 170.3 40.3 L 177.7 25.7 L 189.7 43 L 199.7 21 L 207.7 35 L 219 11 L 233 37 L 240.3 23.7 L 251 43 L 263 18.3 L 272.7 33.3 L 283 10 L 295 32.3 L 301.3 23 L 311.7 37 L 323.7 7.7 L 339.3 39 L 346.3 25.7 L 356.3 42.3 L 369.7 15 L 376.3 25.7 L 384 9 L 393 28.3 L 400.3 19 L 411.7 38.3 L 421 21 L 434.3 43 L 445 25 L 453 36.3 L 464.3 18.3 L 476.2 40.3 L 480 33.5 L 480 215 L 0 215 L 0 35.5 Z" fill="#175720" /> </svg></p> ``` diff --git a/test/command/9420.md b/test/command/9420.md index 89f666964..ce92c4c58 100644 --- a/test/command/9420.md +++ b/test/command/9420.md @@ -2,7 +2,7 @@ % pandoc --embed-resources  ^D -<p><svg id="svg_e1815ef374a63cf552e4" width="504pt" height="360pt" viewBox="0 0 504 360"> +<p><svg id="svg_e1815ef374a63cf552e4" role="img" width="504pt" height="360pt" viewBox="0 0 504 360"> <defs> <clipPath id="svg_e1815ef374a63cf552e4_clip1-c3ce354c"> <path /> diff --git a/test/command/9467.md b/test/command/9467.md index 3b6a411e8..6b37d195d 100644 --- a/test/command/9467.md +++ b/test/command/9467.md @@ -2,7 +2,7 @@ % pandoc --embed-resources  ^D -<p><svg id="svg2" width="191.56267" height="151.71201" viewBox="0 0 191.56267 151.71201" xmlns:svg="http://www.w3.org/2000/svg"> +<p><svg id="svg2" role="img" width="191.56267" height="151.71201" viewBox="0 0 191.56267 151.71201" xmlns:svg="http://www.w3.org/2000/svg"> <defs id="svg2_defs6"> <clipPath clipPathUnits="userSpaceOnUse" id="svg2_clipPath24"> <path d="M 56.69362,0 113.38724,113.38724 170.08086,0 Z" id="svg2_path22" /> |
