@@ -32,7 +32,7 @@ import Control.Monad.State.Strict
3232import Data.Char (ord)
3333import Data.List (intercalate, intersperse, partition, delete, (\\))
3434import Data.List.NonEmpty (NonEmpty((:|)))
35−import Data.Maybe (fromMaybe, isJust, isNothing, mapMaybe)
35+import Data.Maybe (fromMaybe, isJust, isNothing)
3636import qualified Data.Set as Set
3737import Data.Text (Text)
3838import qualified Data.Text as T
@@ -551,23 +551,28 @@ toAttrs :: PandocMonad m
551551toAttrs kvs = do
552552 html5 <- gets stHtml5
553553 mbEpubVersion <- gets stEPUBVersion
554− return $ mapMaybe (\(x,y) ->
555− if html5
556− then
557− if x `Set.member` (html5Attributes <> rdfaAttributes)
558− || T.any (== ':') x -- e.g. epub: namespace
559− || "data-" `T.isPrefixOf` x
560− || "aria-" `T.isPrefixOf` x
561− then Just $ customAttribute (textTag x) (toValue y)
562− else Just $ customAttribute (textTag ("data-" <> x))
563− (toValue y)
564− else
565− if mbEpubVersion == Just EPUB2 &&
566− not (x `Set.member` (html4Attributes <> rdfaAttributes) ||
567− "xml:" `T.isPrefixOf` x)
568− then Nothing
569− else Just $ customAttribute (textTag x) (toValue y))
570− kvs
554+ reverse . snd <$> foldM (go html5 mbEpubVersion) (Set.empty, []) kvs
555+ where
556+ go html5 mbEpubVersion (keys, attrs) (k,v) = do
557+ if k `Set.member` keys
558+ then do
559+ report $ DuplicateAttribute k v
560+ return (keys, attrs)
561+ else return (Set.insert k keys, addAttr html5 mbEpubVersion k v attrs)
562+ addAttr html5 mbEpubVersion x y
563+ | html5
564+ = if x `Set.member` (html5Attributes <> rdfaAttributes)
565+ || T.any (== ':') x -- e.g. epub: namespace
566+ || "data-" `T.isPrefixOf` x
567+ || "aria-" `T.isPrefixOf` x
568+ then (customAttribute (textTag x) (toValue y) :)
569+ else (customAttribute (textTag ("data-" <> x)) (toValue y) :)
570+ | mbEpubVersion == Just EPUB2
571+ , not (x `Set.member` (html4Attributes <> rdfaAttributes) ||
572+ "xml:" `T.isPrefixOf` x)
573+ = id
574+ | otherwise
575+ = (customAttribute (textTag x) (toValue y) :)
571576
572577attrsToHtml :: PandocMonad m
573578 => WriterOptions -> Attr -> StateT WriterState m [Attribute]