HTML writer: avoid doubled style attribute...

when height and width are added to style because of an image,
but the image already has a style attribute.

Closes #8047.
This commit is contained in:
John MacFarlane 2022-04-28 15:49:20 -07:00
parent c05f95773d
commit c1105e6b06
2 changed files with 11 additions and 7 deletions

View file

@ -686,19 +686,13 @@ attrsToHtml opts (id',classes',keyvals) = do
imgAttrsToHtml :: PandocMonad m
=> WriterOptions -> Attr -> StateT WriterState m [Attribute]
imgAttrsToHtml opts attr = do
attrs <- attrsToHtml opts (ident,cls,kvs')
dimattrs <- toAttrs (dimensionsToAttrList attr)
return $ attrs ++ dimattrs
attrsToHtml opts (ident,cls, consolidateStyles (kvs' ++ dimensionsToAttrList attr))
where
(ident,cls,kvs) = attr
kvs' = filter isNotDim kvs
isNotDim ("width", _) = False
isNotDim ("height", _) = False
isNotDim _ = True
dimensionsToAttrList :: Attr -> [(Text, Text)]
dimensionsToAttrList attr = consolidateStyles $ go Width ++ go Height
where
consolidateStyles :: [(Text, Text)] -> [(Text, Text)]
consolidateStyles xs =
case partition isStyle xs of
@ -706,6 +700,10 @@ dimensionsToAttrList attr = consolidateStyles $ go Width ++ go Height
(ss, rest) -> ("style", T.intercalate ";" $ map snd ss) : rest
isStyle ("style", _) = True
isStyle _ = False
dimensionsToAttrList :: Attr -> [(Text, Text)]
dimensionsToAttrList attr = go Width ++ go Height
where
go dir = case dimension dir attr of
(Just (Pixel a)) -> [(tshow dir, tshow a)]
(Just x) -> [("style", tshow dir <> ":" <> tshow x)]

6
test/command/8047.md Normal file
View file

@ -0,0 +1,6 @@
```
% pandoc --wrap=none
![](file.jpg){height=10em width=10em style='border: 1px solid black'}
^D
<p><img src="file.jpg" style="border: 1px solid black;width:10em;height:10em" /></p>
```