git.iko.soy/typeable/pandoc
on 91ef25a help sign in
79 lines 4.1 kB history raw
1{-# LANGUAGE NoImplicitPrelude #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Tests.Readers.HTML (tests) where
5import Prelude
6import Data.Text (Text)
7import qualified Data.Text as T
8import Test.Tasty
9import Test.Tasty.QuickCheck
10import Tests.Helpers
11import Text.Pandoc
12import Text.Pandoc.Arbitrary ()
13import Text.Pandoc.Builder
14import Text.Pandoc.Walk (walk)
16html :: Text -> Pandoc
17html = purely $ readHtml def
19htmlNativeDivs :: Text -> Pandoc
20htmlNativeDivs = purely $ readHtml def { readerExtensions = enableExtension Ext_native_divs $ readerExtensions def }
22makeRoundTrip :: Block -> Block
23makeRoundTrip CodeBlock{} = Para [Str "code block was here"]
24makeRoundTrip LineBlock{} = Para [Str "line block was here"]
25makeRoundTrip RawBlock{} = Para [Str "raw block was here"]
26makeRoundTrip x = x
28removeRawInlines :: Inline -> Inline
29removeRawInlines RawInline{} = Str "raw inline was here"
30removeRawInlines x = x
32roundTrip :: Blocks -> Bool
33roundTrip b = d'' == d'''
34 where d = walk removeRawInlines $ walk makeRoundTrip $ Pandoc nullMeta $ toList b
35 d' = rewrite d
36 d'' = rewrite d'
37 d''' = rewrite d''
38 rewrite = html . T.pack . (++ "\n") . T.unpack .
39 purely (writeHtml5String def { writerWrapText = WrapPreserve })
41tests :: [TestTree]
42tests = [ testGroup "base tag"
43 [ test html "simple" $
44 "<head><base href=\"http://www.w3schools.com/images/foo\" ></head><body><img src=\"stickman.gif\" alt=\"Stickman\"></head>" =?>
45 plain (image "http://www.w3schools.com/images/stickman.gif" "" (text "Stickman"))
46 , test html "slash at end of base" $
47 "<head><base href=\"http://www.w3schools.com/images/\" ></head><body><img src=\"stickman.gif\" alt=\"Stickman\"></head>" =?>
48 plain (image "http://www.w3schools.com/images/stickman.gif" "" (text "Stickman"))
49 , test html "slash at beginning of href" $
50 "<head><base href=\"http://www.w3schools.com/images/\" ></head><body><img src=\"/stickman.gif\" alt=\"Stickman\"></head>" =?>
51 plain (image "http://www.w3schools.com/stickman.gif" "" (text "Stickman"))
52 , test html "absolute URL" $
53 "<head><base href=\"http://www.w3schools.com/images/\" ></head><body><img src=\"http://example.com/stickman.gif\" alt=\"Stickman\"></head>" =?>
54 plain (image "http://example.com/stickman.gif" "" (text "Stickman"))
55 ]
56 , testGroup "anchors"
57 [ test html "anchor without href" $ "<a name=\"anchor\"/>" =?>
58 plain (spanWith ("anchor",[],[]) mempty)
59 ]
60 , testGroup "lang"
61 [ test html "lang on <html>" $ "<html lang=\"es\">hola" =?>
62 setMeta "lang" (text "es") (doc (plain (text "hola")))
63 , test html "xml:lang on <html>" $ "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"es\"><head></head><body>hola</body></html>" =?>
64 setMeta "lang" (text "es") (doc (plain (text "hola")))
65 ]
66 , testGroup "main"
67 [ test htmlNativeDivs "<main> becomes <div role=main>" $ "<main>hello</main>" =?>
68 doc (divWith ("", [], [("role", "main")]) (plain (text "hello")))
69 , test htmlNativeDivs "<main role=X> becomes <div role=X>" $ "<main role=foobar>hello</main>" =?>
70 doc (divWith ("", [], [("role", "foobar")]) (plain (text "hello")))
71 , test htmlNativeDivs "<main> has attributes preserved" $ "<main id=foo class=bar data-baz=qux>hello</main>" =?>
72 doc (divWith ("foo", ["bar"], [("role", "main"), ("data-baz", "qux")]) (plain (text "hello")))
73 , test htmlNativeDivs "<main> closes <p>" $ "<p>hello<main>main content</main>" =?>
74 doc (para (text "hello") <> divWith ("", [], [("role", "main")]) (plain (text "main content")))
75 , test htmlNativeDivs "<main> followed by text" $ "<main>main content</main>non-main content" =?>
76 doc (divWith ("", [], [("role", "main")]) (plain (text "main content")) <> plain (text "non-main content"))
77 ]
78 , testProperty "Round trip" roundTrip
79 ]