git.iko.soy/typeable/pandoc
on eceb8ea help sign in
113 lines 5.3 kB history raw
1{-# LANGUAGE NoImplicitPrelude #-}
2{-# LANGUAGE OverloadedStrings #-}
3{- |
4 Module : Tests.Readers.HTML
5 Copyright : © 2006-2020 John MacFarlane
6 License : GNU GPL, version 2 or above
8 Maintainer : John MacFarlane <jgm@berkeley.edu>
9 Stability : alpha
10 Portability : portable
12Tests for the HTML reader.
13-}
14module Tests.Readers.HTML (tests) where
16import Prelude
17import Data.Text (Text)
18import qualified Data.Text as T
19import Test.Tasty
20import Test.Tasty.QuickCheck
21import Test.Tasty.Options (IsOption(defaultValue))
22import Tests.Helpers
23import Text.Pandoc
24import Text.Pandoc.Shared (isHeaderBlock)
25import Text.Pandoc.Arbitrary ()
26import Text.Pandoc.Builder
27import Text.Pandoc.Walk (walk)
29html :: Text -> Pandoc
30html = purely $ readHtml def
32htmlNativeDivs :: Text -> Pandoc
33htmlNativeDivs = purely $ readHtml def { readerExtensions = enableExtension Ext_native_divs $ readerExtensions def }
35makeRoundTrip :: Block -> Block
36makeRoundTrip CodeBlock{} = Para [Str "code block was here"]
37makeRoundTrip LineBlock{} = Para [Str "line block was here"]
38makeRoundTrip RawBlock{} = Para [Str "raw block was here"]
39makeRoundTrip (Div attr bs) = Div attr $ filter (not . isHeaderBlock) bs
40-- avoids round-trip failures related to makeSections
41-- e.g. with [Div ("loc",[],[("a","11"),("b_2","a b c")]) [Header 3 ("",[],[]) []]]
42makeRoundTrip x = x
44removeRawInlines :: Inline -> Inline
45removeRawInlines RawInline{} = Str "raw inline was here"
46removeRawInlines x = x
48roundTrip :: Blocks -> Bool
49roundTrip b = d'' == d'''
50 where d = walk removeRawInlines $
51 walk makeRoundTrip $ Pandoc nullMeta $ toList b
52 d' = rewrite d
53 d'' = rewrite d'
54 d''' = rewrite d''
55 rewrite = html . T.pack . (++ "\n") . T.unpack .
56 purely (writeHtml5String def
57 { writerWrapText = WrapPreserve })
59tests :: [TestTree]
60tests = [ testGroup "base tag"
61 [ test html "simple" $
62 "<head><base href=\"http://www.w3schools.com/images/foo\" ></head><body><img src=\"stickman.gif\" alt=\"Stickman\"></head>" =?>
63 plain (image "http://www.w3schools.com/images/stickman.gif" "" (text "Stickman"))
64 , test html "slash at end of base" $
65 "<head><base href=\"http://www.w3schools.com/images/\" ></head><body><img src=\"stickman.gif\" alt=\"Stickman\"></head>" =?>
66 plain (image "http://www.w3schools.com/images/stickman.gif" "" (text "Stickman"))
67 , test html "slash at beginning of href" $
68 "<head><base href=\"http://www.w3schools.com/images/\" ></head><body><img src=\"/stickman.gif\" alt=\"Stickman\"></head>" =?>
69 plain (image "http://www.w3schools.com/stickman.gif" "" (text "Stickman"))
70 , test html "absolute URL" $
71 "<head><base href=\"http://www.w3schools.com/images/\" ></head><body><img src=\"http://example.com/stickman.gif\" alt=\"Stickman\"></head>" =?>
72 plain (image "http://example.com/stickman.gif" "" (text "Stickman"))
73 ]
74 , testGroup "anchors"
75 [ test html "anchor without href" $ "<a name=\"anchor\"/>" =?>
76 plain (spanWith ("anchor",[],[]) mempty)
77 ]
78 , testGroup "lang"
79 [ test html "lang on <html>" $ "<html lang=\"es\">hola" =?>
80 setMeta "lang" (text "es") (doc (plain (text "hola")))
81 , test html "xml:lang on <html>" $ "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"es\"><head></head><body>hola</body></html>" =?>
82 setMeta "lang" (text "es") (doc (plain (text "hola")))
83 ]
84 , testGroup "main"
85 [ test htmlNativeDivs "<main> becomes <div role=main>" $ "<main>hello</main>" =?>
86 doc (divWith ("", [], [("role", "main")]) (plain (text "hello")))
87 , test htmlNativeDivs "<main role=X> becomes <div role=X>" $ "<main role=foobar>hello</main>" =?>
88 doc (divWith ("", [], [("role", "foobar")]) (plain (text "hello")))
89 , test htmlNativeDivs "<main> has attributes preserved" $ "<main id=foo class=bar data-baz=qux>hello</main>" =?>
90 doc (divWith ("foo", ["bar"], [("role", "main"), ("baz", "qux")]) (plain (text "hello")))
91 , test htmlNativeDivs "<main> closes <p>" $ "<p>hello<main>main content</main>" =?>
92 doc (para (text "hello") <> divWith ("", [], [("role", "main")]) (plain (text "main content")))
93 , test htmlNativeDivs "<main> followed by text" $ "<main>main content</main>non-main content" =?>
94 doc (divWith ("", [], [("role", "main")]) (plain (text "main content")) <> plain (text "non-main content"))
95 ]
96 , testGroup "samp"
97 [
98 test html "inline samp block" $
99 "<samp>Answer is 42</samp>" =?>
100 plain (codeWith ("",["sample"],[]) "Answer is 42")
101 ]
102 , testGroup "var"
103 [
104 test html "inline var block" $
105 "<var>result</var>" =?>
106 plain (codeWith ("",["variable"],[]) "result")
107 ]
108 , askOption $ \(QuickCheckTests numtests) ->
109 testProperty "Round trip" $
110 withMaxSuccess (if QuickCheckTests numtests == defaultValue
111 then 25
112 else numtests) roundTrip
113 ]