1{-# LANGUAGE OverloadedStrings #-}
5import Text.XML.Hexml as X
6import qualified Data.ByteString.Char8 as BS
12examples :: [(Bool, BS.ByteString)]
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>")
20 ,(True, "<?xml version=\"1.1\"?>\n<greeting>Hello, world!</greeting>")
25 forM_ examples $ \(parses, src) -> do
27 Left err -> when parses $ fail $ "Unexpected parse failure, " ++ show err
29 unless parses $ fail "Unexpected parse success"
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
56checkFind :: Node -> IO ()
58 forM_ (attributes n) $ \a -> attributeBy n (attributeName a) === Just a
59 attributeBy n "xxx" === Nothing
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
71 inside x = BS.concat $ map (either validStr node) $ contents x
72 node x = "<" <> BS.unwords (validName (name x) : map attr (attributes 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)
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