git.iko.soy/typeable/pandoc
on master help sign in
HTML reader: only treat "a" element as link if it has href.

Otherwise treat as span. Closes #3226.

commit50f0cfcc1a96a418b5da9539f80499758ac207c7 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 13 Nov 2016, 22:41 (10 years ago) parent3de6b97
2 files changed+23−7
src/Text/Pandoc/Readers/HTML.hs+19−7 tests/Tests/Readers/HTML.hs+4
src/Text/Pandoc/Readers/HTML.hs+19−7
@@ -599,19 +599,31 @@ pLineBreak = do
599599 pSelfClosing (=="br") (const True)
600600 return B.linebreak
601601
602+-- Unlike fromAttrib from tagsoup, this distinguishes
603+-- between a missing attribute and an attribute with empty content.
604+maybeFromAttrib :: String -> Tag String -> Maybe String
605+maybeFromAttrib name (TagOpen _ attrs) = lookup name attrs
606+maybeFromAttrib _ _ = Nothing
607+
602608pLink :: TagParser Inlines
603609pLink = try $ do
604610 tag <- pSatisfy $ tagOpenLit "a" (const True)
605− mbBaseHref <- baseHref <$> getState
606− let url' = fromAttrib "href" tag
607− let url = case (parseURIReference url', mbBaseHref) of
608− (Just rel, Just bs) -> show (rel `nonStrictRelativeTo` bs)
609− _ -> url'
610611 let title = fromAttrib "title" tag
611− let uid = fromAttrib "id" tag
612+ -- take id from id attribute if present, otherwise name
613+ let uid = maybe (fromAttrib "name" tag) id $ maybeFromAttrib "id" tag
612614 let cls = words $ fromAttrib "class" tag
613615 lab <- trimInlines . mconcat <$> manyTill inline (pCloses "a")
614− return $ B.linkWith (uid, cls, []) (escapeURI url) title lab
616+ -- check for href; if href, then a link, otherwise a span
617+ case maybeFromAttrib "href" tag of
618+ Nothing ->
619+ return $ B.spanWith (uid, cls, []) lab
620+ Just url' -> do
621+ mbBaseHref <- baseHref <$> getState
622+ let url = case (parseURIReference url', mbBaseHref) of
623+ (Just rel, Just bs) ->
624+ show (rel `nonStrictRelativeTo` bs)
625+ _ -> url'
626+ return $ B.linkWith (uid, cls, []) (escapeURI url) title lab
615627
616628pImage :: TagParser Inlines
617629pImage = do
tests/Tests/Readers/HTML.hs+4
@@ -26,4 +26,8 @@ tests = [ testGroup "base tag"
2626 "<head><base href=\"http://www.w3schools.com/images/\" ></head><body><img src=\"http://example.com/stickman.gif\" alt=\"Stickman\"></head>" =?>
2727 plain (image "http://example.com/stickman.gif" "" (text "Stickman"))
2828 ]
29+ , testGroup "anchors"
30+ [ test html "anchor without href" $ "<a name=\"anchor\"/>" =?>
31+ plain (spanWith ("anchor",[],[]) mempty)
32+ ]
2933 ]