{-# LANGUAGE NoImplicitPrelude, QuasiQuotes, OverloadedStrings, UnicodeSyntax #-} module Data.Microformats2.ParserSpec (spec) where import Prelude.Compat import Test.Hspec hiding (shouldBe) import Test.Hspec.Expectations.Pretty (shouldBe) import TestCommon import Network.URI (parseURI) import Data.Microformats2.Parser spec ∷ Spec spec = do describe "parseItems" $ do let parseMf2'' c = parseMf2 c . documentRoot . parseLBS let parseMf2' = parseMf2'' def it "parses items" $ do parseMf2' [xml|

Name

other name
---
Some XSS
Card
org
haz name
|] `shouldBe` [json|{ "items": [ { "type": [ "h-something", "h-something-else" ], "properties": { "org": [ { "type": [ "h-card" ], "properties": { "name": [ "Card" ] }, "value": "Card" }, { "type": [ "h-card" ], "properties": { "name": [ "org" ], "url": [ "http:\/\/card.url" ] }, "value": "org" } ], "url": [ "http:\/\/main.url" ], "name": [ "Name", "other name" ], "content": [ { "html": "Some XSS", "value": "Some XSS" } ], "published": [ "17th of July 2015 at 21:05", "2015-07-17T21:05:13+00:00" ], "updated": [ "2015-07-17T21:05:13+00:00" ] }, "children": [ { "type": [ "h-nested-something" ], "properties": { "name": [ "haz name" ] }, "value": "haz name" }, { "type": [ "h-area" ], "properties": {}, "shape": "circle", "coords": "200,250,25" }, { "type": [ "h-area" ], "properties": {} } ] } ], "rels": {}, "rel-urls": {} }|] it "does not allow html in text nodes" $ do -- there was a bug where <script> would become <script></script>'|] `shouldBe` [json|{ "items": [ { "type": [ "h-entry" ], "properties": { "content": [ { "value": "hello '<script></script>'", "html": "hello '<script></script>'" } ] } } ], "rels": {}, "rel-urls": {} }|] it "inserts value and html into e-* h-* properties" $ do parseMf2' [xml|
some html and props|] `shouldBe` [json|{ "items": [ { "type": [ "h-parent" ], "properties": { "prop": [ { "type": [ "h-child" ], "properties": { "name": [ "props" ] }, "value": "some html and props", "html": "some html and props" } ] } } ], "rels": {}, "rel-urls": {} }|] it "parses nested properties but doesn't parse properties of nested microformats into the parent" $ do parseMf2' [xml| something
a
b
|] `shouldBe` [json|{ "items": [ { "type": [ "h-parent" ], "properties": { "outer": [ "something", "a" ], "inner": [ "some" ], "prop": [ { "type": [ "h-child" ], "properties": { "aaa": [ "a" ] }, "value": "a" } ] }, "children": [ { "type": [ "h-child" ], "properties": { "bbb": [ "b" ] } } ] } ], "rels": {}, "rel-urls": {} }|] it "parses rels" $ do parseMf2' [xml| test twitter

github

|] `shouldBe` [json|{ "items": [], "rels": { "feed": [ "/atom.xml" ], "me": [ "https:\/\/github.com\/myfreeweb", "https:\/\/twitter.com\/myfreeweb", "/atom.xml" ], "alternate": [ "/atom.xml" ], "prev": [ "/-1" ] }, "rel-urls": { "/atom.xml": { "type": "application/atom+xml", "rels": [ "alternate", "feed", "me" ] }, "https://twitter.com/myfreeweb": { "text": "twitter", "rels": [ "me" ] }, "https://github.com/myfreeweb": { "text": "github", "rels": [ "me" ], "media": "handheld", "hreflang": "en" }, "/-1": { "text": "-1", "rels": [ "prev" ] } } }|] it "resolves relative URIs" $ do parseMf2' [xml| feed |] `shouldBe` [json|{ "items": [], "rels": { "me": [ "http://example.com/atom.xml" ] }, "rel-urls": { "http://example.com/atom.xml": { "text": "feed", "rels": [ "me" ] } } }|] parseMf2'' (def { baseUri = parseURI "http://com.example" }) [xml| feed |] `shouldBe` [json|{ "items": [], "rels": { "me": [ "http://com.example/atom.xml" ] }, "rel-urls": { "http://com.example/atom.xml": { "text": "feed", "rels": [ "me" ] } } }|] parseMf2'' (def { baseUri = parseURI "http://com.example" }) [xml| feed |] `shouldBe` [json|{ "items": [], "rels": { "me": [ "http://example.com" ] }, "rel-urls": { "http://example.com": { "text": "feed", "rels": [ "me" ] } } }|] parseMf2'' (def { baseUri = parseURI "http://com.example" }) [xml| feed |] `shouldBe` [json|{ "items": [], "rels": { "me": [ "http://example.com/atom.xml" ] }, "rel-urls": { "http://example.com/atom.xml": { "text": "feed", "rels": [ "me" ] } } }|] parseMf2'' (def { baseUri = parseURI "http://com.example" }) [xml| feed |] `shouldBe` [json|{ "items": [], "rels": { "me": [ "http://com.example/base/atom.xml" ] }, "rel-urls": { "http://com.example/base/atom.xml": { "text": "feed", "rels": [ "me" ] } } }|] parseMf2' [xml| feed |] `shouldBe` [json|{ "items": [], "rels": { "me": [ "/base/atom.xml" ] }, "rel-urls": { "/base/atom.xml": { "text": "feed", "rels": [ "me" ] } } }|] parseMf2'' (def { baseUri = parseURI "http://com.example" }) [xml| feed

card

url /not!!!/resolved photo of me
|] `shouldBe` [json|{ "items": [ { "type": [ "h-card" ], "properties": { "photo": [ "http://com.example/base/photo.webp" ], "url": [ "http://com.example/base/url", "/not/resolved" ], "name": [ "card" ], "content": [ { "html": "

Hello!

", "value": "Hello!" } ] } } ], "rels": { "me": [ "http://com.example/base/atom.xml" ] }, "rel-urls": { "http://com.example/base/atom.xml": { "text": "feed", "rels": [ "me" ] } } }|]