From 757bbeba0fc6fed73f64821f4473c2adf1b398bb Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Thu, 29 Feb 2024 09:20:43 -0800 Subject: SelfContained: Add title element to embedded inline svgs... from the alt attribute. This works around the fact that present-day screen readers ignore the alt attribute on an svg element. Suggestion is from https://stackoverflow.com/questions/4697100/accessibility-recommended-alt-text-convention-for-svg-and-mathml Addresses #9525. Potential drawbacks: - Should we use the title attribute instead if there is one on the image? Otherwise the results may be unexpected. - Is it a drawback that this alt text will display as popup text when you hover over the image? Can this be suppressed? What about using desc instead of title? --- src/Text/Pandoc/SelfContained.hs | 33 +++++++++++++++++++++++++-------- test/command/8948.md | 4 ++-- test/command/9420.md | 2 +- test/command/9467.md | 2 +- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/Text/Pandoc/SelfContained.hs b/src/Text/Pandoc/SelfContained.hs index fdec874ff..a05ff15eb 100644 --- a/src/Text/Pandoc/SelfContained.hs +++ b/src/Text/Pandoc/SelfContained.hs @@ -146,7 +146,10 @@ convertTags (t@(TagOpen tagname as):ts) | any (isSourceAttribute tagname) as = do as' <- mapM processAttribute as - let attrs = rights as' + let rawattrs = rights as' + let attrs = case lookup "role" rawattrs of + Nothing -> ("role", "img") : rawattrs -- see #9525 + Just _ -> rawattrs let svgContents = lefts as' rest <- convertTags ts case svgContents of @@ -162,12 +165,13 @@ convertTags (t@(TagOpen tagname as):ts) let attrs' = [(k,v) | (k,v) <- combineSvgAttrs svgattrs attrs , k /= "id"] return $ TagOpen "svg" attrs' : - TagOpen "use" [("href", "#" <> svgid), - ("width", "100%"), - ("height", "100%")] : - TagClose "use" : - TagClose "svg" : - rest' + addTitle attrs' + [ TagOpen "use" [("href", "#" <> svgid), + ("width", "100%"), + ("height", "100%")] + , TagClose "use" + , TagClose "svg" + ] ++ rest' Nothing -> case dropWhile (not . isTagOpenName "svg") tags of TagOpen "svg" svgattrs : tags' -> do @@ -198,7 +202,8 @@ convertTags (t@(TagOpen tagname as):ts) TagOpen tname (map addIdPrefix ats) ensureUniqueId x = x return $ TagOpen "svg" attrs'' : - map ensureUniqueId tags' ++ rest' + addTitle attrs'' (map ensureUniqueId tags') ++ + rest' _ -> return $ TagOpen tagname attrs : rest where processAttribute (x,y) = if isSourceAttribute tagname (x,y) @@ -219,6 +224,18 @@ convertTags (t@(TagOpen tagname as):ts) convertTags (t:ts) = (t:) <$> convertTags ts +-- add a title element to the svg if attributes include 'alt' and +-- the svg doesn't already have a title element. Motivation: see #9525, +-- as of 2024 screen readers don't notice the alt text on the svg element. +addTitle :: [(T.Text, T.Text)] -> [Tag T.Text] -> [Tag T.Text] +addTitle attrs tags = + case lookup "alt" attrs of + Nothing -> tags + Just alt + | any (~== (TagOpen "title" [] :: Tag T.Text)) tags -> tags + | otherwise -> + TagOpen "title" [] : TagText alt : TagClose "title" : tags + -- we want to drop spaces, , and comments before -- and anything after : getSvgTags :: T.Text -> [Tag T.Text] diff --git a/test/command/8948.md b/test/command/8948.md index ecb0f2a43..cb1b502b4 100644 --- a/test/command/8948.md +++ b/test/command/8948.md @@ -3,7 +3,7 @@ ![minimal](command/minimal.svg) ![minimal](command/minimal.svg) ^D -

+

minimal minimal

``` @@ -13,7 +13,7 @@ ![minimal](command/minimal.svg) ![minimal](command/minimal.svg){#foo} ^D -

+

minimal minimal

``` 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 ![](command/9420.svg) ^D -

+

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 ![](command/9467.svg) ^D -

+

-- cgit v1.2.3