From c1105e6b06d7436f43236191e32156a863de0e13 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Thu, 28 Apr 2022 15:49:20 -0700
Subject: [PATCH] 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.
---
 src/Text/Pandoc/Writers/HTML.hs | 12 +++++-------
 test/command/8047.md            |  6 ++++++
 2 files changed, 11 insertions(+), 7 deletions(-)
 create mode 100644 test/command/8047.md

diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs
index 418155f77..59196a27a 100644
--- a/src/Text/Pandoc/Writers/HTML.hs
+++ b/src/Text/Pandoc/Writers/HTML.hs
@@ -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)]
diff --git a/test/command/8047.md b/test/command/8047.md
new file mode 100644
index 000000000..5b5efb7bf
--- /dev/null
+++ b/test/command/8047.md
@@ -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>
+```