1{-# LANGUAGE NoImplicitPrelude #-}
2{-# LANGUAGE OverloadedStrings #-}
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>
10 Portability : portable
12Tests for the HTML reader.
14module Tests.Readers.HTML (tests) where
17import Data.Text (Text)
18import qualified Data.Text as T
20import Test.Tasty.QuickCheck
21import Test.Tasty.Options (IsOption(defaultValue))
24import Text.Pandoc.Shared (isHeaderBlock)
25import Text.Pandoc.Arbitrary ()
26import Text.Pandoc.Builder
27import Text.Pandoc.Walk (walk)
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 ("",[],[]) []]]
44removeRawInlines :: Inline -> Inline
45removeRawInlines RawInline{} = Str "raw inline was here"
48roundTrip :: Blocks -> Bool
49roundTrip b = d'' == d'''
50 where d = walk removeRawInlines $
51 walk makeRoundTrip $ Pandoc nullMeta $ toList b
55 rewrite = html . T.pack . (++ "\n") . T.unpack .
56 purely (writeHtml5String def
57 { writerWrapText = WrapPreserve })
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"))
75 [ test html "anchor without href" $ "<a name=\"anchor\"/>" =?>
76 plain (spanWith ("anchor",[],[]) mempty)
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")))
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"))
98 test html "inline samp block" $
99 "<samp>Answer is 42</samp>" =?>
100 plain (codeWith ("",["sample"],[]) "Answer is 42")
104 test html "inline var block" $
105 "<var>result</var>" =?>
106 plain (codeWith ("",["variable"],[]) "result")
108 , askOption $ \(QuickCheckTests numtests) ->
109 testProperty "Round trip" $
110 withMaxSuccess (if QuickCheckTests numtests == defaultValue
112 else numtests) roundTrip
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list