on e06e524 help sign in
82 lines 3.4 kB history raw
1{-# LANGUAGE OverloadedStrings #-}
3module Main(main) where
5import Text.XML.Hexml as X
6import qualified Data.ByteString.Char8 as BS
7import Control.Monad
8import Data.Monoid
9import Data.Char
12examples :: [(Bool, BS.ByteString)]
13examples =
14 [(True, "<test id='bob'>here<extra/>there</test>")
15 ,(True, "<test /><close />")
16 ,(True, "<test /><!-- comment > --><close />")
17 ,(True, "<test id=\"bob value\" another-attr=\"test with <\">here </test> more text at the end<close />")
18 ,(False, "<test></more>")
19 ,(False, "<test")
20 ,(True, "<?xml version=\"1.1\"?>\n<greeting>Hello, world!</greeting>")
21 ]
23main :: IO ()
24main = do
25 forM_ examples $ \(parses, src) -> do
26 case parse src of
27 Left err -> when parses $ fail $ "Unexpected parse failure, " ++ show err
28 Right doc -> do
29 unless parses $ fail "Unexpected parse success"
30 checkFind doc
31 let r = render doc
32 r === rerender doc
33 let Right d = parse r
34 r === render d
36 let Right doc = parse "<test id=\"1\" extra=\"2\" />\n<test id=\"2\" /><b><test id=\"3\" /></b><test id=\"4\" /><test />"
37 map name (children doc) === ["test","test","b","test","test"]
38 location (children doc !! 2) === (2,16)
39 length (childrenBy doc "test") === 4
40 length (childrenBy doc "b") === 1
41 length (childrenBy doc "extra") === 0
42 attributes (head $ children doc) === [Attribute "id" "1", Attribute "extra" "2"]
43 map (`attributeBy` "id") (childrenBy doc "test") === map (fmap (Attribute "id")) [Just "1", Just "2", Just "4", Nothing]
45 Right _ <- return $ parse $ "<test " <> BS.unwords [BS.pack $ "x" ++ show i ++ "='value'" | i <- [1..10000]] <> " />"
46 Right _ <- return $ parse $ BS.unlines $ replicate 10000 "<test x='value' />"
48 let attrs = ["usd:jpy","test","extra","more","stuff","jpy:usd","xxx","xxxx"]
49 Right doc <- return $ parse $ "<test " <> BS.unwords [x <> "='" <> x <> "'" | x <- attrs] <> ">middle</test>"
50 [c] <- return $ childrenBy doc "test"
51 forM_ attrs $ \a -> attributeBy c a === Just (Attribute a a)
52 forM_ ["missing","gone","nothing"] $ \a -> attributeBy c a === Nothing
53 putStrLn "\nSuccess"
56checkFind :: Node -> IO ()
57checkFind n = do
58 forM_ (attributes n) $ \a -> attributeBy n (attributeName a) === Just a
59 attributeBy n "xxx" === Nothing
60 let cs = children n
61 forM_ ("xxx":map name cs) $ \c ->
62 map outer (filter ((==) c . name) cs) === map outer (childrenBy n c)
63 mapM_ checkFind $ children n
66a === b = if a == b then putChar '.' else fail $ "mismatch, " ++ show a ++ " /= " ++ show b
68rerender :: Node -> BS.ByteString
69rerender = inside
70 where
71 inside x = BS.concat $ map (either validStr node) $ contents x
72 node x = "<" <> BS.unwords (validName (name x) : map attr (attributes x)) <> ">" <>
73 inside x <>
74 "</" <> name x <> ">"
75 attr (Attribute a b) = validName a <> "=\"" <> validAttr b <> "\""
77 validName x | BS.all (\x -> isAlphaNum x || x `elem` ("-:_" :: String)) x = x
78 | otherwise = error "Invalid name"
79 validAttr x | BS.notElem '\"' x = x
80 | otherwise = error "Invalid attribute"
81 validStr x | BS.notElem '<' x || BS.isInfixOf "<!--" x = x
82 | otherwise = error $ show ("Invalid string", x)