| 1 |
-------------------------------------------------------------------------------- |
| 2 |
module Hakyll.Web.Html.Tests |
| 3 |
( tests |
| 4 |
) where |
| 5 |
|
| 6 |
|
| 7 |
-------------------------------------------------------------------------------- |
| 8 |
import Data.Char (toUpper) |
| 9 |
import Test.Tasty (TestTree, testGroup) |
| 10 |
import Test.Tasty.HUnit ((@=?)) |
| 11 |
import qualified Text.HTML.TagSoup as TS |
| 12 |
|
| 13 |
|
| 14 |
-------------------------------------------------------------------------------- |
| 15 |
import Hakyll.Web.Html |
| 16 |
import TestSuite.Util |
| 17 |
|
| 18 |
|
| 19 |
-------------------------------------------------------------------------------- |
| 20 |
tests :: TestTree |
| 21 |
tests = testGroup "Hakyll.Web.Html.Tests" $ concat |
| 22 |
[ fromAssertions "demoteHeaders" |
| 23 |
[ "<h2>A h1 title</h2>" @=? |
| 24 |
demoteHeaders "<h1>A h1 title</h1>" -- Assert single-step demotion |
| 25 |
, "<h6>A h6 title</h6>" @=? |
| 26 |
demoteHeaders "<h6>A h6 title</h6>" -- Assert maximum demotion is h6 |
| 27 |
] |
| 28 |
|
| 29 |
, fromAssertions "demoteHeadersBy" |
| 30 |
[ "<h3>A h1 title</h3>" @=? |
| 31 |
demoteHeadersBy 2 "<h1>A h1 title</h1>" |
| 32 |
, "<h6>A h5 title</h6>" @=? |
| 33 |
demoteHeadersBy 2 "<h5>A h5 title</h5>" -- Assert that h6 is the lowest possible demoted header. |
| 34 |
, "<h4>A h4 title</h4>" @=? |
| 35 |
demoteHeadersBy 0 "<h4>A h4 title</h4>" -- Assert that a demotion of @N < 1@ is a no-op. |
| 36 |
] |
| 37 |
|
| 38 |
, fromAssertions "getUrls" |
| 39 |
[ ["/image1.png", "/image2.jpeg", "https://example.com", "/game.swf", "/poster.jpeg"] @=? |
| 40 |
getUrls [ |
| 41 |
TS.TagOpen "img" [("src", "/image1.png")] |
| 42 |
, TS.TagOpen "img" [("src", "/image2.jpeg")] |
| 43 |
, TS.TagOpen "a" [("href", "https://example.com")] |
| 44 |
, TS.TagOpen "object" [("data", "/game.swf")] |
| 45 |
, TS.TagOpen "video" [("poster", "/poster.jpeg")] |
| 46 |
] |
| 47 |
, ["/image1.png", "/image2.jpeg", "/image3.bmp"] @=? |
| 48 |
getUrls [ TS.TagOpen "img" [("srcset", "/image1.png 10w, /image2.jpeg, /image3.bmp 1.3x")] ] |
| 49 |
|
| 50 |
-- Invalid srcset specification means no URLs are extracted |
| 51 |
, [] @=? |
| 52 |
getUrls [ TS.TagOpen "img" [("srcset", "/image1.png 10wide, /image2.jpeg, /image3.bmp 1.3px")] ] |
| 53 |
] |
| 54 |
|
| 55 |
, fromAssertions "withUrls" |
| 56 |
[ "<a href=\"FOO\">bar</a>" @=? |
| 57 |
withUrls (map toUpper) "<a href=\"foo\">bar</a>" |
| 58 |
, "<img src=\"OH BAR\" />" @=? |
| 59 |
withUrls (map toUpper) "<img src=\"oh bar\" />" |
| 60 |
|
| 61 |
-- Test escaping |
| 62 |
, "<script>\"sup\"</script>" @=? |
| 63 |
withUrls id "<script>\"sup\"</script>" |
| 64 |
, "<code><stdio></code>" @=? |
| 65 |
withUrls id "<code><stdio></code>" |
| 66 |
, "<style>body > p { line-height: 1.3 }</style>" @=? |
| 67 |
withUrls id "<style>body > p { line-height: 1.3 }</style>" |
| 68 |
|
| 69 |
-- Test minimizing elements |
| 70 |
, "<meta bar=\"foo\" />" @=? |
| 71 |
withUrls id "<meta bar=\"foo\" />" |
| 72 |
|
| 73 |
-- Test that URLs are extracted from img's srcset |
| 74 |
, "<img srcset=\"foo 200w\" />" @=? |
| 75 |
withUrls (const "foo") "<img srcset=\"/path/to/image.png 200w\" />" |
| 76 |
, "<img srcset=\"bar 200w, bar 400w\" />" @=? |
| 77 |
withUrls (const "bar") "<img srcset=\"/small.jpeg 200w, /img/large.jpeg 400w\" />" |
| 78 |
|
| 79 |
-- Invalid srcsets are left unchanged |
| 80 |
, "<img srcset=\"/image1.png 200px\" />" @=? |
| 81 |
withUrls (const "bar") "<img srcset=\"/image1.png 200px\" />" |
| 82 |
] |
| 83 |
|
| 84 |
, fromAssertions "toUrl" |
| 85 |
[ "/foo/bar.html" @=? toUrl "foo/bar.html" |
| 86 |
, "/foo/bar.html" @=? toUrl "foo\\bar.html" -- Windows-specific |
| 87 |
, "/" @=? toUrl "/" |
| 88 |
, "/funny-pics.html" @=? toUrl "/funny-pics.html" |
| 89 |
, "/funny%20pics.html" @=? toUrl "funny pics.html" |
| 90 |
-- Test various reserved characters (RFC 3986, section 2.2) |
| 91 |
, "/%21%2A%27%28%29%3B%3A%40%26.html" @=? toUrl "/!*'();:@&.html" |
| 92 |
, "/%3D%2B%24%2C/%3F%23%5B%5D.html" @=? toUrl "=+$,/?#[].html" |
| 93 |
-- Test various characters that are nor reserved, nor unreserved. |
| 94 |
, "/%E3%81%82%F0%9D%90%87%E2%88%80" @=? toUrl "\12354\119815\8704" |
| 95 |
] |
| 96 |
|
| 97 |
, fromAssertions "toSiteRoot" |
| 98 |
[ ".." @=? toSiteRoot "/foo/bar.html" |
| 99 |
, "." @=? toSiteRoot "index.html" |
| 100 |
, "." @=? toSiteRoot "/index.html" |
| 101 |
, "../.." @=? toSiteRoot "foo/bar/qux" |
| 102 |
, ".." @=? toSiteRoot "./foo/bar.html" |
| 103 |
, ".." @=? toSiteRoot "/foo/./bar.html" |
| 104 |
] |
| 105 |
|
| 106 |
, fromAssertions "isExternal" |
| 107 |
[ True @=? isExternal "http://reddit.com" |
| 108 |
, True @=? isExternal "https://mail.google.com" |
| 109 |
, True @=? isExternal "//ajax.googleapis.com" |
| 110 |
, False @=? isExternal "../header.png" |
| 111 |
, False @=? isExternal "/foo/index.html" |
| 112 |
] |
| 113 |
|
| 114 |
, fromAssertions "stripTags" |
| 115 |
[ "foo" @=? stripTags "<p>foo</p>" |
| 116 |
, "foo bar" @=? stripTags "<p>foo</p> bar" |
| 117 |
, "foo" @=? stripTags "<p>foo</p" |
| 118 |
] |
| 119 |
|
| 120 |
, fromAssertions "escapeHtml" |
| 121 |
[ "Me & Dean" @=? escapeHtml "Me & Dean" |
| 122 |
, "<img>" @=? escapeHtml "<img>" |
| 123 |
] |
| 124 |
] |