2018-03-18 18:46:28 +01:00
|
|
|
{-# LANGUAGE NoImplicitPrelude #-}
|
2015-05-14 05:39:01 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module Tests.Readers.HTML (tests) where
|
|
|
|
|
2018-03-18 18:46:28 +01:00
|
|
|
import Prelude
|
2017-10-28 05:28:29 +02:00
|
|
|
import Data.Text (Text)
|
2018-09-10 11:15:27 +02:00
|
|
|
import qualified Data.Text as T
|
2017-03-14 17:05:36 +01:00
|
|
|
import Test.Tasty
|
2018-09-10 11:15:27 +02:00
|
|
|
import Test.Tasty.QuickCheck
|
2015-05-14 05:39:01 +02:00
|
|
|
import Tests.Helpers
|
|
|
|
import Text.Pandoc
|
2017-03-04 13:03:41 +01:00
|
|
|
import Text.Pandoc.Arbitrary ()
|
|
|
|
import Text.Pandoc.Builder
|
2018-09-10 11:15:27 +02:00
|
|
|
import Text.Pandoc.Walk (walk)
|
2015-05-14 05:39:01 +02:00
|
|
|
|
2017-06-10 18:26:44 +02:00
|
|
|
html :: Text -> Pandoc
|
2016-12-01 18:47:05 +01:00
|
|
|
html = purely $ readHtml def
|
2015-05-14 05:39:01 +02:00
|
|
|
|
2017-08-09 18:10:12 +02:00
|
|
|
htmlNativeDivs :: Text -> Pandoc
|
|
|
|
htmlNativeDivs = purely $ readHtml def { readerExtensions = enableExtension Ext_native_divs $ readerExtensions def }
|
|
|
|
|
2018-09-10 11:15:27 +02:00
|
|
|
makeRoundTrip :: Block -> Block
|
|
|
|
makeRoundTrip Table{} = Para [Str "table was here"]
|
|
|
|
makeRoundTrip CodeBlock{} = Para [Str "code block was here"]
|
|
|
|
makeRoundTrip LineBlock{} = Para [Str "line block was here"]
|
2018-09-10 13:28:28 +02:00
|
|
|
makeRoundTrip RawBlock{} = Para [Str "raw block was here"]
|
2018-09-10 11:15:27 +02:00
|
|
|
makeRoundTrip x = x
|
|
|
|
|
|
|
|
removeRawInlines :: Inline -> Inline
|
|
|
|
removeRawInlines RawInline{} = Str "raw inline was here"
|
|
|
|
removeRawInlines x = x
|
|
|
|
|
|
|
|
roundTrip :: Block -> Bool
|
|
|
|
roundTrip b = d'' == d'''
|
|
|
|
where d = walk removeRawInlines $ walk makeRoundTrip $ Pandoc nullMeta [b]
|
|
|
|
d' = rewrite d
|
|
|
|
d'' = rewrite d'
|
|
|
|
d''' = rewrite d''
|
|
|
|
rewrite = html . T.pack . (++ "\n") . T.unpack .
|
|
|
|
purely (writeHtml5String def { writerWrapText = WrapPreserve })
|
|
|
|
|
2017-03-14 17:05:36 +01:00
|
|
|
tests :: [TestTree]
|
2015-05-14 05:39:01 +02:00
|
|
|
tests = [ testGroup "base tag"
|
|
|
|
[ test html "simple" $
|
2016-03-11 04:59:55 +01:00
|
|
|
"<head><base href=\"http://www.w3schools.com/images/foo\" ></head><body><img src=\"stickman.gif\" alt=\"Stickman\"></head>" =?>
|
2015-05-14 05:39:01 +02:00
|
|
|
plain (image "http://www.w3schools.com/images/stickman.gif" "" (text "Stickman"))
|
|
|
|
, test html "slash at end of base" $
|
|
|
|
"<head><base href=\"http://www.w3schools.com/images/\" ></head><body><img src=\"stickman.gif\" alt=\"Stickman\"></head>" =?>
|
|
|
|
plain (image "http://www.w3schools.com/images/stickman.gif" "" (text "Stickman"))
|
2016-03-11 04:59:55 +01:00
|
|
|
, test html "slash at beginning of href" $
|
|
|
|
"<head><base href=\"http://www.w3schools.com/images/\" ></head><body><img src=\"/stickman.gif\" alt=\"Stickman\"></head>" =?>
|
|
|
|
plain (image "http://www.w3schools.com/stickman.gif" "" (text "Stickman"))
|
2015-05-14 05:39:01 +02:00
|
|
|
, test html "absolute URL" $
|
|
|
|
"<head><base href=\"http://www.w3schools.com/images/\" ></head><body><img src=\"http://example.com/stickman.gif\" alt=\"Stickman\"></head>" =?>
|
|
|
|
plain (image "http://example.com/stickman.gif" "" (text "Stickman"))
|
|
|
|
]
|
2016-11-13 22:41:11 +01:00
|
|
|
, testGroup "anchors"
|
|
|
|
[ test html "anchor without href" $ "<a name=\"anchor\"/>" =?>
|
|
|
|
plain (spanWith ("anchor",[],[]) mempty)
|
|
|
|
]
|
2017-06-27 10:19:37 +02:00
|
|
|
, testGroup "lang"
|
|
|
|
[ test html "lang on <html>" $ "<html lang=\"es\">hola" =?>
|
|
|
|
setMeta "lang" (text "es") (doc (plain (text "hola")))
|
|
|
|
, test html "xml:lang on <html>" $ "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"es\"><head></head><body>hola</body></html>" =?>
|
|
|
|
setMeta "lang" (text "es") (doc (plain (text "hola")))
|
|
|
|
]
|
2017-08-09 18:10:12 +02:00
|
|
|
, testGroup "main"
|
|
|
|
[ test htmlNativeDivs "<main> becomes <div role=main>" $ "<main>hello</main>" =?>
|
|
|
|
doc (divWith ("", [], [("role", "main")]) (plain (text "hello")))
|
|
|
|
, test htmlNativeDivs "<main role=X> becomes <div role=X>" $ "<main role=foobar>hello</main>" =?>
|
|
|
|
doc (divWith ("", [], [("role", "foobar")]) (plain (text "hello")))
|
|
|
|
, test htmlNativeDivs "<main> has attributes preserved" $ "<main id=foo class=bar data-baz=qux>hello</main>" =?>
|
|
|
|
doc (divWith ("foo", ["bar"], [("role", "main"), ("data-baz", "qux")]) (plain (text "hello")))
|
|
|
|
, test htmlNativeDivs "<main> closes <p>" $ "<p>hello<main>main content</main>" =?>
|
|
|
|
doc (para (text "hello") <> divWith ("", [], [("role", "main")]) (plain (text "main content")))
|
|
|
|
, test htmlNativeDivs "<main> followed by text" $ "<main>main content</main>non-main content" =?>
|
|
|
|
doc (divWith ("", [], [("role", "main")]) (plain (text "main content")) <> plain (text "non-main content"))
|
|
|
|
]
|
2018-09-10 11:15:27 +02:00
|
|
|
, testProperty "Round trip" roundTrip
|
2015-05-14 05:39:01 +02:00
|
|
|
]
|